Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -1013,6 +1013,10 @@ func (c *Cloud) InstanceType(ctx context.Context, nodeName types.NodeName) (stri
return string(inst.InstanceType), nil
}

func (c *Cloud) getImageID(instance *ec2types.Instance) string {
return aws.ToString(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 @@ -511,10 +511,10 @@ func testHasNodeAddress(t *testing.T, addrs []v1.NodeAddress, addressType v1.Nod
}

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

func makeInstance(instanceID string, privateIP, publicIP, privateDNSName, publicDNSName string, ipv6s []string, setNetInterface bool) ec2types.Instance {
func makeInstance(instanceID string, privateIP, publicIP, privateDNSName, publicDNSName string, ipv6s []string, setNetInterface bool, imageID string) ec2types.Instance {
var tag ec2types.Tag
tag.Key = aws.String(TagNameKubernetesClusterLegacy)
tag.Value = aws.String(TestClusterID)
Expand All @@ -527,6 +527,7 @@ func makeInstance(instanceID string, privateIP, publicIP, privateDNSName, public
PublicDnsName: aws.String(publicDNSName),
PublicIpAddress: aws.String(publicIP),
InstanceType: ec2types.InstanceTypeC3Large,
ImageId: aws.String(imageID),
Tags: tags,
Placement: &ec2types.Placement{AvailabilityZone: aws.String("us-west-2a")},
State: &ec2types.InstanceState{
Expand Down Expand Up @@ -608,7 +609,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, []*ec2types.Instance{&instance})
_, err := aws1.NodeAddressesByProviderID(context.TODO(), "i-xxx")
if err == nil {
Expand Down Expand Up @@ -714,7 +715,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, []*ec2types.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, []*ec2types.Instance{&instance})
var mockedTopologyManager 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, []*ec2types.Instance{&instance})
var mockedTopologyManager 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, []*ec2types.Instance{&instance})
var mockedTopologyManager 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, []*ec2types.Instance{&instance})
var mockedTopologyManager 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, []*ec2types.Instance{&instance})

// Add mock for DescribeInstanceTopology on the EC2 mock
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think node.k8s.aws/image-id would be better wdyt?

)