Skip to content

add ImageId to the set of additional cloud provider labels #1119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/providers/v1/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,10 @@ func (c *Cloud) InstanceType(ctx context.Context, nodeName types.NodeName) (stri
return aws.StringValue(inst.InstanceType), nil
}

func (c *Cloud) getImageID(instance *ec2.Instance) string {
return aws.StringValue(instance.ImageId)
}

// GetZone implements Zones.GetZone
func (c *Cloud) GetZone(ctx context.Context) (cloudprovider.Zone, error) {
return cloudprovider.Zone{
Expand Down
9 changes: 5 additions & 4 deletions pkg/providers/v1/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,10 @@ func testHasNodeAddress(t *testing.T, addrs []v1.NodeAddress, addressType v1.Nod
}

func makeMinimalInstance(instanceID string) ec2.Instance {
return makeInstance(instanceID, "", "", "", "", nil, false)
return makeInstance(instanceID, "", "", "", "", nil, false, "")
}

func makeInstance(instanceID string, privateIP, publicIP, privateDNSName, publicDNSName string, ipv6s []string, setNetInterface bool) ec2.Instance {
func makeInstance(instanceID string, privateIP, publicIP, privateDNSName, publicDNSName string, ipv6s []string, setNetInterface bool, imageID string) ec2.Instance {
var tag ec2.Tag
tag.Key = aws.String(TagNameKubernetesClusterLegacy)
tag.Value = aws.String(TestClusterID)
Expand All @@ -570,6 +570,7 @@ func makeInstance(instanceID string, privateIP, publicIP, privateDNSName, public
PublicDnsName: aws.String(publicDNSName),
PublicIpAddress: aws.String(publicIP),
InstanceType: aws.String("c3.large"),
ImageId: aws.String(imageID),
Tags: tags,
Placement: &ec2.Placement{AvailabilityZone: aws.String("us-west-2a")},
State: &ec2.InstanceState{
Expand Down Expand Up @@ -651,7 +652,7 @@ func TestNodeAddressesByProviderID(t *testing.T) {
},
} {
t.Run(tc.Name, func(t *testing.T) {
instance := makeInstance(tc.InstanceID, tc.PrivateIP, tc.PublicIP, tc.PrivateDNSName, tc.PublicDNSName, tc.Ipv6s, tc.SetNetInterface)
instance := makeInstance(tc.InstanceID, tc.PrivateIP, tc.PublicIP, tc.PrivateDNSName, tc.PublicDNSName, tc.Ipv6s, tc.SetNetInterface, "")
aws1, _ := mockInstancesResp(&instance, []*ec2.Instance{&instance})
_, err := aws1.NodeAddressesByProviderID(context.TODO(), "i-xxx")
if err == nil {
Expand Down Expand Up @@ -757,7 +758,7 @@ func TestNodeAddresses(t *testing.T) {
},
} {
t.Run(tc.Name, func(t *testing.T) {
instance := makeInstance(tc.InstanceID, tc.PrivateIP, tc.PublicIP, tc.PrivateDNSName, tc.PublicDNSName, tc.Ipv6s, tc.SetNetInterface)
instance := makeInstance(tc.InstanceID, tc.PrivateIP, tc.PublicIP, tc.PrivateDNSName, tc.PublicDNSName, tc.Ipv6s, tc.SetNetInterface, "")
aws1, _ := mockInstancesResp(&instance, []*ec2.Instance{&instance})
_, err := aws1.NodeAddresses(context.TODO(), "instance-mismatch.ec2.internal")
if err == nil {
Expand Down
11 changes: 9 additions & 2 deletions pkg/providers/v1/instances_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *Cloud) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, erro
}

func (c *Cloud) getAdditionalLabels(ctx context.Context, zoneName string, instanceID string, instanceType string,
region string, existingLabels map[string]string) (map[string]string, error) {
region string, imageID string, existingLabels map[string]string) (map[string]string, error) {
additionalLabels := map[string]string{}

// If zone ID label is already set, skip.
Expand All @@ -82,6 +82,11 @@ func (c *Cloud) getAdditionalLabels(ctx context.Context, zoneName string, instan
additionalLabels[LabelZoneID] = zoneID
}

// If image id label is already set, skip.
if _, ok := existingLabels[LabelImageID]; !ok {
additionalLabels[LabelImageID] = imageID
}

// If topology labels are already set, skip.
if _, ok := existingLabels[LabelNetworkNodePrefix+"1"]; !ok {
nodeTopology, err := c.instanceTopologyManager.GetNodeTopology(ctx, instanceType, region, instanceID)
Expand Down Expand Up @@ -127,6 +132,7 @@ func (c *Cloud) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprov

var (
instanceType string
imageID string
zone cloudprovider.Zone
nodeAddresses []v1.NodeAddress
)
Expand All @@ -151,6 +157,7 @@ func (c *Cloud) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprov
return nil, fmt.Errorf("failed to get instance by ID %s: %w", instanceID, err)
}

imageID = c.getImageID(instance)
instanceType = c.getInstanceType(instance)
zone = c.getInstanceZone(instance)
nodeAddresses, err = c.getInstanceNodeAddress(instance)
Expand All @@ -159,7 +166,7 @@ func (c *Cloud) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprov
}
}

additionalLabels, err := c.getAdditionalLabels(ctx, zone.FailureDomain, string(instanceID), instanceType, zone.Region, node.Labels)
additionalLabels, err := c.getAdditionalLabels(ctx, zone.FailureDomain, string(instanceID), instanceType, zone.Region, imageID, node.Labels)
if err != nil {
return nil, err
}
Expand Down
15 changes: 9 additions & 6 deletions pkg/providers/v1/instances_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestInstanceShutdown(t *testing.T) {

func TestInstanceMetadata(t *testing.T) {
t.Run("Should return populated InstanceMetadata", func(t *testing.T) {
instance := makeInstance("i-00000000000000000", "192.168.0.1", "1.2.3.4", "instance-same.ec2.internal", "instance-same.ec2.external", nil, true)
instance := makeInstance("i-00000000000000000", "192.168.0.1", "1.2.3.4", "instance-same.ec2.internal", "instance-same.ec2.external", nil, true, "ami-0000")
c, _ := mockInstancesResp(&instance, []*ec2.Instance{&instance})
var mockedTopologyManager resourcemanagers.MockedInstanceTopologyManager
c.instanceTopologyManager = &mockedTopologyManager
Expand Down Expand Up @@ -207,11 +207,12 @@ func TestInstanceMetadata(t *testing.T) {
LabelNetworkNodePrefix + "1": "nn-123456789",
LabelNetworkNodePrefix + "2": "nn-234567890",
LabelNetworkNodePrefix + "3": "nn-345678901",
LabelImageID: "ami-0000",
}, result.AdditionalLabels)
})

t.Run("Should skip additional labels if already set", func(t *testing.T) {
instance := makeInstance("i-00000000000000000", "192.168.0.1", "1.2.3.4", "instance-same.ec2.internal", "instance-same.ec2.external", nil, true)
instance := makeInstance("i-00000000000000000", "192.168.0.1", "1.2.3.4", "instance-same.ec2.internal", "instance-same.ec2.external", nil, true, "ami-0000")
c, _ := mockInstancesResp(&instance, []*ec2.Instance{&instance})
var mockedTopologyManager resourcemanagers.MockedInstanceTopologyManager
c.instanceTopologyManager = &mockedTopologyManager
Expand All @@ -226,6 +227,7 @@ func TestInstanceMetadata(t *testing.T) {
LabelNetworkNodePrefix + "1": "nn-123456789",
LabelNetworkNodePrefix + "2": "nn-234567890",
LabelNetworkNodePrefix + "3": "nn-345678901",
LabelImageID: "ami-0000",
}

result, err := c.InstanceMetadata(context.TODO(), node)
Expand All @@ -239,7 +241,7 @@ func TestInstanceMetadata(t *testing.T) {
})

t.Run("Should swallow errors if getting node topology fails if instance type not expected to be supported", func(t *testing.T) {
instance := makeInstance("i-00000000000000000", "192.168.0.1", "1.2.3.4", "instance-same.ec2.internal", "instance-same.ec2.external", nil, true)
instance := makeInstance("i-00000000000000000", "192.168.0.1", "1.2.3.4", "instance-same.ec2.internal", "instance-same.ec2.external", nil, true, "ami-0000")
c, _ := mockInstancesResp(&instance, []*ec2.Instance{&instance})
var mockedTopologyManager resourcemanagers.MockedInstanceTopologyManager
c.instanceTopologyManager = &mockedTopologyManager
Expand All @@ -259,12 +261,13 @@ func TestInstanceMetadata(t *testing.T) {

mockedTopologyManager.AssertNumberOfCalls(t, "GetNodeTopology", 1)
assert.Equal(t, map[string]string{
LabelZoneID: "az1",
LabelZoneID: "az1",
LabelImageID: "ami-0000",
}, result.AdditionalLabels)
})

t.Run("Should not swallow errors if getting node topology fails if instance type is expected to be supported", func(t *testing.T) {
instance := makeInstance("i-00000000000000000", "192.168.0.1", "1.2.3.4", "instance-same.ec2.internal", "instance-same.ec2.external", nil, true)
instance := makeInstance("i-00000000000000000", "192.168.0.1", "1.2.3.4", "instance-same.ec2.internal", "instance-same.ec2.external", nil, true, "ami-0000")
c, _ := mockInstancesResp(&instance, []*ec2.Instance{&instance})
var mockedTopologyManager resourcemanagers.MockedInstanceTopologyManager
c.instanceTopologyManager = &mockedTopologyManager
Expand All @@ -286,7 +289,7 @@ func TestInstanceMetadata(t *testing.T) {
})

t.Run("Should limit ec2:DescribeInstances calls to a single request per instance", func(t *testing.T) {
instance := makeInstance("i-00000000000001234", "192.168.0.1", "1.2.3.4", "instance-same.ec2.internal", "instance-same.ec2.external", nil, true)
instance := makeInstance("i-00000000000001234", "192.168.0.1", "1.2.3.4", "instance-same.ec2.internal", "instance-same.ec2.external", nil, true, "ami-0000")
c, awsServices := mockInstancesResp(&instance, []*ec2.Instance{&instance})
node := &v1.Node{
Spec: v1.NodeSpec{
Expand Down
2 changes: 2 additions & 0 deletions pkg/providers/v1/well_known_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ const (
// but will be initially applied to nodes. The suffix should be an incremented
// integer starting at 1.
LabelNetworkNodePrefix = "topology.k8s.aws/network-node-layer-"
// LabelImageID is a machine image label that can be applied to node resources.
LabelImageID = "k8s.aws/image-id"
)