Skip to content

Commit c4ae168

Browse files
committed
improve tagging
1 parent 9ad4c4b commit c4ae168

File tree

3 files changed

+61
-30
lines changed

3 files changed

+61
-30
lines changed

pkg/cloudprovider/providers/oci/instances.go

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3339
var _ 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 nil, err
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

pkg/cloudprovider/providers/oci/node_info_controller.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ import (
4141

4242
// metadata labeling for placement info
4343
const (
44-
FaultDomainLabel = "oci.oraclecloud.com/fault-domain"
45-
CompartmentIDAnnotation = "oci.oraclecloud.com/compartment-id"
46-
OpenShiftNodeIdentifierLabel = "node.openshift.io/os_id"
47-
timeout = 10 * time.Second
44+
FaultDomainLabel = "oci.oraclecloud.com/fault-domain"
45+
CompartmentIDAnnotation = "oci.oraclecloud.com/compartment-id"
46+
timeout = 10 * time.Second
4847
)
4948

5049
// NodeInfoController helps compute workers in the cluster

pkg/oci/client/compute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func (c *client) GetSecondaryVNICForInstance(ctx context.Context, compartmentID,
231231
}
232232
}
233233

234-
return nil, errors.WithStack(errNotFound)
234+
return nil, nil
235235
}
236236

237237
func (c *client) GetInstanceByNodeName(ctx context.Context, compartmentID, vcnID, nodeName string) (*core.Instance, error) {

0 commit comments

Comments
 (0)