diff --git a/pkg/cloudprovider/providers/oci/instances.go b/pkg/cloudprovider/providers/oci/instances.go index 51ef20b42a..33a2b0ec4a 100644 --- a/pkg/cloudprovider/providers/oci/instances.go +++ b/pkg/cloudprovider/providers/oci/instances.go @@ -30,6 +30,12 @@ import ( cloudprovider "k8s.io/cloud-provider" ) +const ( + OpenShiftTagNamesapcePrefix = "openshift-" + OpenShiftBootVolumeType = "boot-volume-type" + OpenShiftBootVolumeISCSI = "ISCSI" +) + var _ cloudprovider.Instances = &CloudProvider{} // mapNodeNameToInstanceName maps a kube NodeName to a OCI instance display @@ -98,32 +104,37 @@ func (cp *CloudProvider) extractNodeAddresses(ctx context.Context, instanceID st addresses = append(addresses, api.NodeAddress{Type: api.NodeExternalIP, Address: ip.String()}) } - secondaryVnic, err := cp.client.Compute().GetSecondaryVNICForInstance(ctx, compartmentID, instanceID) - if err != nil { - return nil, errors.Wrap(err, "GetSecondaryVNICForInstance") - } + OpenShiftTagNamesapce := cp.getOpenShiftTagNamespaceByInstance(ctx, instanceID) - if secondaryVnic == nil { - return addresses, nil - } + if OpenShiftTagNamesapce != "" { + secondaryVnic, err := cp.client.Compute().GetSecondaryVNICForInstance(ctx, compartmentID, instanceID) + if err != nil { + return addresses, nil + } - if cp.checkOpenShiftISCSIBootVolumeByVnic(ctx, secondaryVnic) { - if (secondaryVnic.IsPrimary == nil || !*secondaryVnic.IsPrimary) && secondaryVnic.PrivateIp != nil && *secondaryVnic.PrivateIp != "" { - ip := net.ParseIP(*secondaryVnic.PrivateIp) - if ip == nil { - return nil, fmt.Errorf("instance has invalid private address: %q", *secondaryVnic.PrivateIp) - } - addresses = append(addresses, api.NodeAddress{Type: api.NodeInternalIP, Address: ip.String()}) + if secondaryVnic == nil { + return addresses, nil } - if (secondaryVnic.IsPrimary == nil || !*secondaryVnic.IsPrimary) && secondaryVnic.PublicIp != nil && *secondaryVnic.PublicIp != "" { - ip := net.ParseIP(*secondaryVnic.PublicIp) - if ip == nil { - return nil, errors.Errorf("instance has invalid public address: %q", *secondaryVnic.PublicIp) + if cp.checkOpenShiftISCSIBootVolumeTagByVnic(ctx, secondaryVnic, OpenShiftTagNamesapce) { + if (secondaryVnic.IsPrimary == nil || !*secondaryVnic.IsPrimary) && secondaryVnic.PrivateIp != nil && *secondaryVnic.PrivateIp != "" { + ip := net.ParseIP(*secondaryVnic.PrivateIp) + if ip == nil { + return nil, fmt.Errorf("instance has invalid private address: %q", *secondaryVnic.PrivateIp) + } + addresses = append(addresses, api.NodeAddress{Type: api.NodeInternalIP, Address: ip.String()}) + } + + if (secondaryVnic.IsPrimary == nil || !*secondaryVnic.IsPrimary) && secondaryVnic.PublicIp != nil && *secondaryVnic.PublicIp != "" { + ip := net.ParseIP(*secondaryVnic.PublicIp) + if ip == nil { + return nil, errors.Errorf("instance has invalid public address: %q", *secondaryVnic.PublicIp) + } + addresses = append(addresses, api.NodeAddress{Type: api.NodeExternalIP, Address: ip.String()}) } - addresses = append(addresses, api.NodeAddress{Type: api.NodeExternalIP, Address: ip.String()}) } } + // Changing this can have wide reaching impact. // // if vnic.HostnameLabel != nil && *vnic.HostnameLabel != "" { @@ -340,12 +351,33 @@ func (cp *CloudProvider) getCompartmentIDByNodeName(nodeName string) (string, er return "", errors.New("compartmentID annotation missing in the node. Would retry") } -func (cp *CloudProvider) checkOpenShiftISCSIBootVolumeByVnic(ctx context.Context, vnic *core.Vnic) bool { - for namespace := range vnic.DefinedTags { - if strings.HasPrefix(namespace, "openshift") { - if bootVolume, exist := vnic.DefinedTags[namespace]["boot-volume-type"]; exist && bootVolume == "ISCSI" { - return true - } +func (cp *CloudProvider) getOpenShiftTagNamespaceByInstance(ctx context.Context, instanceID string) string { + instance, err := cp.client.Compute().GetInstance(ctx, instanceID) + if err != nil { + return "" + } + + if instance.DefinedTags == nil { + return "" + } + + for namespace := range instance.DefinedTags { + if strings.HasPrefix(namespace, OpenShiftTagNamesapcePrefix) { + return namespace + } + } + return "" +} + +func (cp *CloudProvider) checkOpenShiftISCSIBootVolumeTagByVnic(ctx context.Context, vnic *core.Vnic, namespace string) bool { + if vnic.DefinedTags == nil { + return false + } + + if tags, namespaceExists := vnic.DefinedTags[namespace]; namespaceExists { + // Check if the boot volume type key exists and its value is ISCSI + if bootVolume, keyExists := tags[OpenShiftBootVolumeType]; keyExists && bootVolume == OpenShiftBootVolumeISCSI { + return true } } return false diff --git a/pkg/cloudprovider/providers/oci/node_info_controller.go b/pkg/cloudprovider/providers/oci/node_info_controller.go index 5505569c23..69c9b00f14 100644 --- a/pkg/cloudprovider/providers/oci/node_info_controller.go +++ b/pkg/cloudprovider/providers/oci/node_info_controller.go @@ -41,10 +41,9 @@ import ( // metadata labeling for placement info const ( - FaultDomainLabel = "oci.oraclecloud.com/fault-domain" - CompartmentIDAnnotation = "oci.oraclecloud.com/compartment-id" - OpenShiftNodeIdentifierLabel = "node.openshift.io/os_id" - timeout = 10 * time.Second + FaultDomainLabel = "oci.oraclecloud.com/fault-domain" + CompartmentIDAnnotation = "oci.oraclecloud.com/compartment-id" + timeout = 10 * time.Second ) // NodeInfoController helps compute workers in the cluster