@@ -18,6 +18,7 @@ import (
1818 "context"
1919 "fmt"
2020 "net"
21+ "strings"
2122
2223 "github.com/oracle/oci-go-sdk/v65/core"
2324 "k8s.io/apimachinery/pkg/labels"
@@ -29,6 +30,12 @@ import (
2930 cloudprovider "k8s.io/cloud-provider"
3031)
3132
33+ const (
34+ OpenShiftTagNamesapcePrefix = "openshift-"
35+ OpenShiftBootVolumeType = "boot-volume-type"
36+ OpenShiftBootVolumeISCSI = "ISCSI"
37+ )
38+
3239var _ cloudprovider.Instances = & CloudProvider {}
3340
3441// mapNodeNameToInstanceName maps a kube NodeName to a OCI instance display
@@ -97,33 +104,38 @@ func (cp *CloudProvider) extractNodeAddresses(ctx context.Context, instanceID st
97104 addresses = append (addresses , api.NodeAddress {Type : api .NodeExternalIP , Address : ip .String ()})
98105 }
99106
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 )
103111 if err != nil {
104- return nil , errors . Wrap ( err , "GetSecondaryVNICForInstance" )
112+ return nil , err
105113 }
106114
107- if secondaryVnic == nil {
115+ if secondaryVnics == nil || len ( secondaryVnics ) == 0 {
108116 return addresses , nil
109117 }
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+ }
110127
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+ }
123135 }
124- addresses = append (addresses , api.NodeAddress {Type : api .NodeExternalIP , Address : ip .String ()})
125136 }
126137 }
138+
127139 // Changing this can have wide reaching impact.
128140 //
129141 // if vnic.HostnameLabel != nil && *vnic.HostnameLabel != "" {
@@ -340,32 +352,32 @@ func (cp *CloudProvider) getCompartmentIDByNodeName(nodeName string) (string, er
340352 return "" , errors .New ("compartmentID annotation missing in the node. Would retry" )
341353}
342354
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 )
346357 if err != nil {
347- return false , errors . Wrap ( err , "error listing all the nodes using node informer" )
358+ return ""
348359 }
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
360368 }
361369 }
362- return false , errors . New ( "Failed to check OpenShift node using node lables. Returning false" )
370+ return ""
363371}
364372
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 {
369381 return true
370382 }
371383 }
0 commit comments