Skip to content

Determine instance IP compatibility using all ENIs #4674

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

Merged
merged 1 commit into from
Jun 10, 2025
Merged
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
17 changes: 1 addition & 16 deletions agent/config/config_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,8 @@ func getConfigFileName() (string, error) {
//
//lint:ignore U1000 Function will be used in the future
func (c *Config) determineIPCompatibility(ec2client ec2.EC2MetadataClient) {
// Load primary ENI's MAC address on EC2 Launch Type
var primaryENIMAC string
if !c.External.Enabled() {
logger.Info("Calling IMDS to fetch mac address of the primary ENI")
var eniMACFetchErr error
primaryENIMAC, eniMACFetchErr = ec2client.PrimaryENIMAC()
if eniMACFetchErr != nil {
logger.Warn("Failed to fetch primary ENI's mac address from IMDS."+
" Failing back instance IP compatibility to IPv4-only.",
logger.Fields{field.Error: eniMACFetchErr})
c.InstanceIPCompatibility = ipcompatibility.NewIPv4OnlyCompatibility()
return
}
}

var err error
c.InstanceIPCompatibility, err = netutils.DetermineIPCompatibility(nlWrapper, primaryENIMAC)
c.InstanceIPCompatibility, err = netutils.DetermineIPCompatibility(nlWrapper, "")
if err != nil {
logger.Warn("Failed to determine instance IP compatibility."+
" Failing back instance IP compatibility to IPv4-only.",
Expand Down
37 changes: 3 additions & 34 deletions agent/config/config_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,56 +298,25 @@ func TestCPUPeriodSettings(t *testing.T) {
}

func TestDetermineIPCompatibility(t *testing.T) {
mac, err := net.ParseMAC("02:34:80:c5:c0:e1")
require.NoError(t, err)
macLink := &netlink.Dummy{LinkAttrs: netlink.LinkAttrs{HardwareAddr: mac}}
ipv6Gw := net.ParseIP("1:2:3:4::")
require.NotNil(t, ipv6Gw)
ipv6DefaultRoute := netlink.Route{Gw: ipv6Gw, Dst: nil}

testCases := []struct {
name string
externalMode BooleanDefaultFalse
setExpectations func(*mock_ec2.MockEC2MetadataClient, *mock_netlinkwrapper.MockNetLink)
expected ipcompatibility.IPCompatibility
}{
{
name: "external disabled, IP compatibility determined successfully",
setExpectations: func(ec2c *mock_ec2.MockEC2MetadataClient, nl *mock_netlinkwrapper.MockNetLink) {
ec2c.EXPECT().PrimaryENIMAC().Return(mac.String(), nil)
nl.EXPECT().LinkList().Return([]netlink.Link{macLink}, nil)
nl.EXPECT().RouteList(macLink, netlink.FAMILY_V4).Return([]netlink.Route{}, nil)
nl.EXPECT().RouteList(macLink, netlink.FAMILY_V6).Return([]netlink.Route{ipv6DefaultRoute}, nil)
},
expected: ipcompatibility.NewIPCompatibility(false, true),
},
{
name: "external disabled, IP compatibility could not be determined",
setExpectations: func(ec2c *mock_ec2.MockEC2MetadataClient, nl *mock_netlinkwrapper.MockNetLink) {
ec2c.EXPECT().PrimaryENIMAC().Return(mac.String(), nil)
nl.EXPECT().LinkList().Return(nil, errors.New("some error"))
},
expected: ipcompatibility.NewIPv4OnlyCompatibility(),
},
{
name: "external disabled, primary ENI's mac could not be fetched",
setExpectations: func(ec2c *mock_ec2.MockEC2MetadataClient, nl *mock_netlinkwrapper.MockNetLink) {
ec2c.EXPECT().PrimaryENIMAC().Return("", errors.New("some error"))
},
expected: ipcompatibility.NewIPv4OnlyCompatibility(),
},
{
name: "external enabled, IP compatibility determined successfully",
externalMode: BooleanDefaultFalse{Value: ExplicitlyEnabled},
name: "IP compatibility determined successfully",
setExpectations: func(ec2c *mock_ec2.MockEC2MetadataClient, nl *mock_netlinkwrapper.MockNetLink) {
nl.EXPECT().RouteList(nil, netlink.FAMILY_V4).Return([]netlink.Route{}, nil)
nl.EXPECT().RouteList(nil, netlink.FAMILY_V6).Return([]netlink.Route{ipv6DefaultRoute}, nil)
},
expected: ipcompatibility.NewIPCompatibility(false, true),
},
{
name: "external enabled, IP compatibility could not be determined",
externalMode: BooleanDefaultFalse{Value: ExplicitlyEnabled},
name: "IP compatibility could not be determined",
setExpectations: func(ec2c *mock_ec2.MockEC2MetadataClient, nl *mock_netlinkwrapper.MockNetLink) {
nl.EXPECT().RouteList(nil, netlink.FAMILY_V4).Return(nil, errors.New("some error"))
},
Expand All @@ -374,7 +343,7 @@ func TestDetermineIPCompatibility(t *testing.T) {
tc.setExpectations(imdsClient, mockNLWrapper)

// Run the test
cfg := Config{External: tc.externalMode}
cfg := Config{}
cfg.determineIPCompatibility(imdsClient)
assert.Equal(t, tc.expected, cfg.InstanceIPCompatibility)
})
Expand Down
Loading