Skip to content

Commit

Permalink
Add AdditionalLabels to cloudprovider.InstanceMetadata
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Merkes <merkes@amazon.com>
  • Loading branch information
mmerkes committed Feb 9, 2024
1 parent ad19bea commit f776ea2
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 6 deletions.
5 changes: 5 additions & 0 deletions staging/src/k8s.io/cloud-provider/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,9 @@ type InstanceMetadata struct {
// * topology.kubernetes.io/region=<region>
// * failure-domain.beta.kubernetes.io/region=<region> (DEPRECATED)
Region string

// AdditionalLabels is a map of additional labels provided by the cloud provider.
// When provided, they will be applied to the node and enable cloud providers
// to labels nodes with information that may be valuable to that provider.
AdditionalLabels map[string]string
}
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,19 @@ func (cnc *CloudNodeController) getNodeModifiersFromCloudProvider(
})
}

if instanceMeta.AdditionalLabels != nil {
klog.V(2).Infof("Adding additional node label(s) from cloud provider: %v", instanceMeta.AdditionalLabels)
nodeModifiers = append(nodeModifiers, func(n *v1.Node) {
if n.Labels == nil {
n.Labels = map[string]string{}
}

for k, v := range instanceMeta.AdditionalLabels {
n.Labels[k] = v
}
})
}

return nodeModifiers, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,115 @@ func Test_syncNode(t *testing.T) {
},
},
},
{
name: "[instanceV2] provided additional labels",
fakeCloud: &fakecloud.Cloud{
EnableInstancesV2: true,
Addresses: []v1.NodeAddress{
{
Type: v1.NodeInternalIP,
Address: "10.0.0.1",
},
{
Type: v1.NodeExternalIP,
Address: "132.143.154.163",
},
},
ExistsByProviderID: true,
Err: nil,
AdditionalLabels: map[string]string{
"topology.k8s.aws/zone-id": "az1",
"my.custom.label/foo": "bar",
},
},
existingNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node0",
CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
Annotations: map[string]string{
cloudproviderapi.AnnotationAlphaProvidedIPAddr: "10.0.0.1",
},
},
Spec: v1.NodeSpec{
Taints: []v1.Taint{
{
Key: "ImproveCoverageTaint",
Value: "true",
Effect: v1.TaintEffectNoSchedule,
},
{
Key: cloudproviderapi.TaintExternalCloudProvider,
Value: "true",
Effect: v1.TaintEffectNoSchedule,
},
},
ProviderID: "node0.aws.12345",
},
Status: v1.NodeStatus{
Conditions: []v1.NodeCondition{
{
Type: v1.NodeReady,
Status: v1.ConditionUnknown,
LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
},
},
Addresses: []v1.NodeAddress{
{
Type: v1.NodeHostName,
Address: "node0.cloud.internal",
},
},
},
},
updatedNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node0",
CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
Annotations: map[string]string{
cloudproviderapi.AnnotationAlphaProvidedIPAddr: "10.0.0.1",
},
Labels: map[string]string{
"topology.k8s.aws/zone-id": "az1",
"my.custom.label/foo": "bar",
},
},
Spec: v1.NodeSpec{
Taints: []v1.Taint{
{
Key: "ImproveCoverageTaint",
Value: "true",
Effect: v1.TaintEffectNoSchedule,
},
},
ProviderID: "node0.aws.12345",
},
Status: v1.NodeStatus{
Conditions: []v1.NodeCondition{
{
Type: v1.NodeReady,
Status: v1.ConditionUnknown,
LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
},
},
Addresses: []v1.NodeAddress{
{
Type: v1.NodeInternalIP,
Address: "10.0.0.1",
},
{
Type: v1.NodeExternalIP,
Address: "132.143.154.163",
},
{
Type: v1.NodeHostName,
Address: "node0.cloud.internal",
},
},
},
},
},
{
name: "[instanceV2] provider ID already set",
fakeCloud: &fakecloud.Cloud{
Expand Down
14 changes: 8 additions & 6 deletions staging/src/k8s.io/cloud-provider/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ type Cloud struct {
ProviderID map[types.NodeName]string
addCallLock sync.Mutex
cloudprovider.Zone
VolumeLabelMap map[string]map[string]string
VolumeLabelMap map[string]map[string]string
AdditionalLabels map[string]string

OverrideInstanceMetadata func(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error)

Expand Down Expand Up @@ -373,11 +374,12 @@ func (f *Cloud) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprov
}

return &cloudprovider.InstanceMetadata{
ProviderID: providerID,
InstanceType: f.InstanceTypes[types.NodeName(node.Spec.ProviderID)],
NodeAddresses: f.Addresses,
Zone: f.Zone.FailureDomain,
Region: f.Zone.Region,
ProviderID: providerID,
InstanceType: f.InstanceTypes[types.NodeName(node.Spec.ProviderID)],
NodeAddresses: f.Addresses,
Zone: f.Zone.FailureDomain,
Region: f.Zone.Region,
AdditionalLabels: f.AdditionalLabels,
}, f.MetadataErr
}

Expand Down

0 comments on commit f776ea2

Please sign in to comment.