Skip to content

Commit

Permalink
improve tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
yutpeng committed Aug 4, 2024
1 parent 9ad4c4b commit ea8396a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 29 deletions.
82 changes: 57 additions & 25 deletions pkg/cloudprovider/providers/oci/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions pkg/cloudprovider/providers/oci/node_info_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ea8396a

Please sign in to comment.