@@ -30,6 +30,12 @@ import (
3030 cloudprovider "k8s.io/cloud-provider"
3131)
3232
33+ const (
34+ OpenShiftTagNamesapcePrefix = "openshift-"
35+ OpenShiftBootVolumeType = "boot-volume-type"
36+ OpenShiftBootVolumeISCSI = "ISCSI"
37+ )
38+
3339var _ cloudprovider.Instances = & CloudProvider {}
3440
3541// mapNodeNameToInstanceName maps a kube NodeName to a OCI instance display
@@ -98,32 +104,37 @@ func (cp *CloudProvider) extractNodeAddresses(ctx context.Context, instanceID st
98104 addresses = append (addresses , api.NodeAddress {Type : api .NodeExternalIP , Address : ip .String ()})
99105 }
100106
101- secondaryVnic , err := cp .client .Compute ().GetSecondaryVNICForInstance (ctx , compartmentID , instanceID )
102- if err != nil {
103- return nil , errors .Wrap (err , "GetSecondaryVNICForInstance" )
104- }
107+ OpenShiftTagNamesapce := cp .getOpenShiftTagNamespaceByInstance (ctx , instanceID )
105108
106- if secondaryVnic == nil {
107- return addresses , nil
108- }
109+ if OpenShiftTagNamesapce != "" {
110+ secondaryVnic , err := cp .client .Compute ().GetSecondaryVNICForInstance (ctx , compartmentID , instanceID )
111+ if err != nil {
112+ return addresses , nil
113+ }
109114
110- if cp .checkOpenShiftISCSIBootVolumeByVnic (ctx , secondaryVnic ) {
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 ()})
115+ if secondaryVnic == nil {
116+ return addresses , nil
117117 }
118118
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 )
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+ }
127+
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 ()})
123134 }
124- addresses = append (addresses , api.NodeAddress {Type : api .NodeExternalIP , Address : ip .String ()})
125135 }
126136 }
137+
127138 // Changing this can have wide reaching impact.
128139 //
129140 // if vnic.HostnameLabel != nil && *vnic.HostnameLabel != "" {
@@ -340,12 +351,33 @@ func (cp *CloudProvider) getCompartmentIDByNodeName(nodeName string) (string, er
340351 return "" , errors .New ("compartmentID annotation missing in the node. Would retry" )
341352}
342353
343- func (cp * CloudProvider ) checkOpenShiftISCSIBootVolumeByVnic (ctx context.Context , vnic * core.Vnic ) bool {
344- for namespace := range vnic .DefinedTags {
345- if strings .HasPrefix (namespace , "openshift" ) {
346- if bootVolume , exist := vnic .DefinedTags [namespace ]["boot-volume-type" ]; exist && bootVolume == "ISCSI" {
347- return true
348- }
354+ func (cp * CloudProvider ) getOpenShiftTagNamespaceByInstance (ctx context.Context , instanceID string ) string {
355+ instance , err := cp .client .Compute ().GetInstance (ctx , instanceID )
356+ if err != nil {
357+ return ""
358+ }
359+
360+ if instance .DefinedTags == nil {
361+ return ""
362+ }
363+
364+ for namespace := range instance .DefinedTags {
365+ if strings .HasPrefix (namespace , OpenShiftTagNamesapcePrefix ) {
366+ return namespace
367+ }
368+ }
369+ return ""
370+ }
371+
372+ func (cp * CloudProvider ) checkOpenShiftISCSIBootVolumeTagByVnic (ctx context.Context , vnic * core.Vnic , namespace string ) bool {
373+ if vnic .DefinedTags == nil {
374+ return false
375+ }
376+
377+ if tags , namespaceExists := vnic .DefinedTags [namespace ]; namespaceExists {
378+ // Check if the boot volume type key exists and its value is ISCSI
379+ if bootVolume , keyExists := tags [OpenShiftBootVolumeType ]; keyExists && bootVolume == OpenShiftBootVolumeISCSI {
380+ return true
349381 }
350382 }
351383 return false
0 commit comments