@@ -18,6 +18,7 @@ import (
18
18
"context"
19
19
"fmt"
20
20
"net"
21
+ "strings"
21
22
22
23
"github.com/oracle/oci-go-sdk/v65/core"
23
24
"k8s.io/apimachinery/pkg/labels"
@@ -29,6 +30,12 @@ import (
29
30
cloudprovider "k8s.io/cloud-provider"
30
31
)
31
32
33
+ const (
34
+ OpenShiftTagNamesapcePrefix = "openshift-"
35
+ OpenShiftBootVolumeType = "boot-volume-type"
36
+ OpenShiftBootVolumeISCSI = "ISCSI"
37
+ )
38
+
32
39
var _ cloudprovider.Instances = & CloudProvider {}
33
40
34
41
// mapNodeNameToInstanceName maps a kube NodeName to a OCI instance display
@@ -97,33 +104,38 @@ func (cp *CloudProvider) extractNodeAddresses(ctx context.Context, instanceID st
97
104
addresses = append (addresses , api.NodeAddress {Type : api .NodeExternalIP , Address : ip .String ()})
98
105
}
99
106
100
- useSecondaryVnic , err := cp .checkOpenShiftNodesSecondaryVnicByInstance (instanceID )
101
- if useSecondaryVnic {
102
- secondaryVnic , err := cp .client .Compute ().GetSecondaryVNICForInstance (ctx , compartmentID , instanceID )
107
+ OpenShiftTagNamesapce := cp .getOpenShiftTagNamespaceByInstance (ctx , instanceID )
108
+
109
+ if OpenShiftTagNamesapce != "" {
110
+ secondaryVnics , err := cp .client .Compute ().GetSecondaryVNICsForInstance (ctx , compartmentID , instanceID )
103
111
if err != nil {
104
- return nil , errors . Wrap ( err , "GetSecondaryVNICForInstance" )
112
+ return nil , err
105
113
}
106
114
107
- if secondaryVnic == nil {
115
+ if secondaryVnics == nil || len ( secondaryVnics ) == 0 {
108
116
return addresses , nil
109
117
}
118
+ for _ , secondaryVnic := range secondaryVnics {
119
+ if cp .checkOpenShiftISCSIBootVolumeTagByVnic (ctx , secondaryVnic , OpenShiftTagNamesapce ) {
120
+ if (secondaryVnic .IsPrimary == nil || ! * secondaryVnic .IsPrimary ) && secondaryVnic .PrivateIp != nil && * secondaryVnic .PrivateIp != "" {
121
+ ip := net .ParseIP (* secondaryVnic .PrivateIp )
122
+ if ip == nil {
123
+ return nil , fmt .Errorf ("instance has invalid private address: %q" , * secondaryVnic .PrivateIp )
124
+ }
125
+ addresses = append (addresses , api.NodeAddress {Type : api .NodeInternalIP , Address : ip .String ()})
126
+ }
110
127
111
- if (secondaryVnic .IsPrimary == nil || ! * secondaryVnic .IsPrimary ) && secondaryVnic .PrivateIp != nil && * secondaryVnic .PrivateIp != "" {
112
- ip := net .ParseIP (* secondaryVnic .PrivateIp )
113
- if ip == nil {
114
- return nil , fmt .Errorf ("instance has invalid private address: %q" , * secondaryVnic .PrivateIp )
115
- }
116
- addresses = append (addresses , api.NodeAddress {Type : api .NodeInternalIP , Address : ip .String ()})
117
- }
118
-
119
- if (secondaryVnic .IsPrimary == nil || ! * secondaryVnic .IsPrimary ) && secondaryVnic .PublicIp != nil && * secondaryVnic .PublicIp != "" {
120
- ip := net .ParseIP (* secondaryVnic .PublicIp )
121
- if ip == nil {
122
- return nil , errors .Errorf ("instance has invalid public address: %q" , * secondaryVnic .PublicIp )
128
+ if (secondaryVnic .IsPrimary == nil || ! * secondaryVnic .IsPrimary ) && secondaryVnic .PublicIp != nil && * secondaryVnic .PublicIp != "" {
129
+ ip := net .ParseIP (* secondaryVnic .PublicIp )
130
+ if ip == nil {
131
+ return nil , errors .Errorf ("instance has invalid public address: %q" , * secondaryVnic .PublicIp )
132
+ }
133
+ addresses = append (addresses , api.NodeAddress {Type : api .NodeExternalIP , Address : ip .String ()})
134
+ }
123
135
}
124
- addresses = append (addresses , api.NodeAddress {Type : api .NodeExternalIP , Address : ip .String ()})
125
136
}
126
137
}
138
+
127
139
// Changing this can have wide reaching impact.
128
140
//
129
141
// if vnic.HostnameLabel != nil && *vnic.HostnameLabel != "" {
@@ -340,32 +352,32 @@ func (cp *CloudProvider) getCompartmentIDByNodeName(nodeName string) (string, er
340
352
return "" , errors .New ("compartmentID annotation missing in the node. Would retry" )
341
353
}
342
354
343
- func (cp * CloudProvider ) checkOpenShiftNodesSecondaryVnicByInstance (instanceID string ) (bool , error ) {
344
- var SecondaryVnicUsageInstances = []string {"BM.Standard3.64" }
345
- nodeList , err := cp .NodeLister .List (labels .Everything ())
355
+ func (cp * CloudProvider ) getOpenShiftTagNamespaceByInstance (ctx context.Context , instanceID string ) string {
356
+ instance , err := cp .client .Compute ().GetInstance (ctx , instanceID )
346
357
if err != nil {
347
- return false , errors . Wrap ( err , "error listing all the nodes using node informer" )
358
+ return ""
348
359
}
349
- for _ , node := range nodeList {
350
- providerID , err := MapProviderIDToInstanceID (node .Spec .ProviderID )
351
- if err != nil {
352
- return false , errors .New ("Failed to map providerID to instanceID." )
353
- }
354
- if providerID == instanceID {
355
- if _ , ok := node .Labels [OpenShiftNodeIdentifierLabel ]; ok {
356
- if instanceType , ok := node .Labels [api .LabelInstanceTypeStable ]; ok && contains (SecondaryVnicUsageInstances , instanceType ) {
357
- return true , nil
358
- }
359
- }
360
+
361
+ if instance .DefinedTags == nil {
362
+ return ""
363
+ }
364
+
365
+ for namespace := range instance .DefinedTags {
366
+ if strings .HasPrefix (namespace , OpenShiftTagNamesapcePrefix ) {
367
+ return namespace
360
368
}
361
369
}
362
- return false , errors . New ( "Failed to check OpenShift node using node lables. Returning false" )
370
+ return ""
363
371
}
364
372
365
- // contains is a utility method to check if a string is part of a slice
366
- func contains (s []string , e string ) bool {
367
- for _ , a := range s {
368
- if a == e {
373
+ func (cp * CloudProvider ) checkOpenShiftISCSIBootVolumeTagByVnic (ctx context.Context , vnic * core.Vnic , namespace string ) bool {
374
+ if vnic .DefinedTags == nil {
375
+ return false
376
+ }
377
+
378
+ if tags , namespaceExists := vnic .DefinedTags [namespace ]; namespaceExists {
379
+ // Check if the boot volume type key exists and its value is ISCSI
380
+ if bootVolume , keyExists := tags [OpenShiftBootVolumeType ]; keyExists && bootVolume == OpenShiftBootVolumeISCSI {
369
381
return true
370
382
}
371
383
}
0 commit comments