Skip to content

Commit

Permalink
Ensure that addresses are added in network device index order
Browse files Browse the repository at this point in the history
  • Loading branch information
javanthropus committed May 17, 2024
1 parent 68cbc26 commit e637708
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/providers/v1/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,19 @@ func extractIPv4NodeAddresses(instance *ec2.Instance) ([]v1.NodeAddress, error)

addresses := []v1.NodeAddress{}

// sort by device index so that the first address added to the addresses list is from the first (primary) device
sort.Slice(instance.NetworkInterfaces, func(i, j int) bool {
// These nil checks should cause interfaces with non-nil attachments to sort before those with nil attachments
if instance.NetworkInterfaces[i].Attachment == nil {
return false
}
if instance.NetworkInterfaces[j].Attachment == nil {
return true
}

return aws.Int64Value(instance.NetworkInterfaces[i].Attachment.DeviceIndex) < aws.Int64Value(instance.NetworkInterfaces[j].Attachment.DeviceIndex)
})

// handle internal network interfaces
for _, networkInterface := range instance.NetworkInterfaces {
// skip network interfaces that are not currently in use
Expand Down
39 changes: 39 additions & 0 deletions pkg/providers/v1/aws_fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,45 @@ func NewFakeAWSServices(clusterID string) *FakeAWSServices {
s.selfInstance = selfInstance
s.instances = []*ec2.Instance{selfInstance}

selfInstance.NetworkInterfaces = []*ec2.InstanceNetworkInterface{
{
Attachment: &ec2.InstanceNetworkInterfaceAttachment{
DeviceIndex: aws.Int64(1),
},
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
{
Primary: aws.Bool(true),
PrivateDnsName: aws.String("ip-172-20-1-100.ec2.internal"),
PrivateIpAddress: aws.String("172.20.1.1"),
},
{
Primary: aws.Bool(false),
PrivateDnsName: aws.String("ip-172-20-1-101.ec2.internal"),
PrivateIpAddress: aws.String("172.20.1.2"),
},
},
Status: aws.String(ec2.NetworkInterfaceStatusInUse),
},
{
Attachment: &ec2.InstanceNetworkInterfaceAttachment{
DeviceIndex: aws.Int64(0),
},
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
{
Primary: aws.Bool(true),
PrivateDnsName: aws.String("ip-172-20-0-100.ec2.internal"),
PrivateIpAddress: aws.String("172.20.0.100"),
},
{
Primary: aws.Bool(false),
PrivateDnsName: aws.String("ip-172-20-0-101.ec2.internal"),
PrivateIpAddress: aws.String("172.20.0.101"),
},
},
Status: aws.String(ec2.NetworkInterfaceStatusInUse),
},
}

var tag ec2.Tag
tag.Key = aws.String(TagNameKubernetesClusterLegacy)
tag.Value = aws.String(clusterID)
Expand Down
17 changes: 17 additions & 0 deletions pkg/providers/v1/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3559,6 +3559,23 @@ func verifyNodeAddressesForFargate(t *testing.T, ipFamily string, verifyPublicIP
assert.Equal(t, v1.NodeInternalIP, nodeAddresses[0].Type)
}

func TestNodeAddressesOrderedByDeviceIndex(t *testing.T) {
awsServices := newMockedFakeAWSServices(TestClusterID)
c, _ := newAWSCloud(CloudConfig{}, awsServices)

nodeAddresses, _ := c.NodeAddressesByProviderID(context.TODO(), "aws:///us-west-2a/i-self")
expectedAddresses := []v1.NodeAddress{
{Type: v1.NodeInternalIP, Address: "172.20.0.100"},
{Type: v1.NodeInternalIP, Address: "172.20.0.101"},
{Type: v1.NodeInternalIP, Address: "172.20.1.1"},
{Type: v1.NodeInternalIP, Address: "172.20.1.2"},
{Type: v1.NodeExternalIP, Address: "1.2.3.4"},
{Type: v1.NodeInternalDNS, Address: "ip-172-20-0-100.ec2.internal"},
{Type: v1.NodeHostName, Address: "ip-172-20-0-100.ec2.internal"},
}
assert.Equal(t, expectedAddresses, nodeAddresses)
}

func TestInstanceExistsByProviderIDForFargate(t *testing.T) {
awsServices := newMockedFakeAWSServices(TestClusterID)
c, _ := newAWSCloud(CloudConfig{}, awsServices)
Expand Down

0 comments on commit e637708

Please sign in to comment.