From d82188ab18b3507bcebd66aaebb964b3a300de1f Mon Sep 17 00:00:00 2001 From: Samuel Konat Date: Wed, 13 Sep 2023 19:40:51 +0000 Subject: [PATCH 01/10] Add new network data structures This change adds a new task level network data structure and a network namespace level data structure into the shared network library. This enables the path to having a unified network library for ECS data plane. --- .../netlib/model/status/network_status.go | 42 ++++++++++++++++ .../model/status/network_status_test.go | 48 +++++++++++++++++++ .../model/tasknetworkconfig/common_test.go | 44 +++++++++++++++++ .../tasknetworkconfig/network_namespace.go | 35 ++++++++++++++ .../network_namespace_test.go | 38 +++++++++++++++ .../tasknetworkconfig/task_network_config.go | 22 +++++++++ .../task_network_config_test.go | 25 ++++++++++ 7 files changed, 254 insertions(+) create mode 100644 ecs-agent/netlib/model/status/network_status.go create mode 100644 ecs-agent/netlib/model/status/network_status_test.go create mode 100644 ecs-agent/netlib/model/tasknetworkconfig/common_test.go create mode 100644 ecs-agent/netlib/model/tasknetworkconfig/network_namespace.go create mode 100644 ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go create mode 100644 ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go create mode 100644 ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go diff --git a/ecs-agent/netlib/model/status/network_status.go b/ecs-agent/netlib/model/status/network_status.go new file mode 100644 index 00000000000..b6fdb9d2d84 --- /dev/null +++ b/ecs-agent/netlib/model/status/network_status.go @@ -0,0 +1,42 @@ +package status + +// NetworkStatus represents the status of a network resource. +type NetworkStatus string + +const ( + // NetworkNone is the initial status of the ENI. + NetworkNone NetworkStatus = "NONE" + // NetworkReadyPull indicates that the ENI is ready for downloading resources associated with + // the execution role. This includes container images, task secrets and configs. + NetworkReadyPull NetworkStatus = "READY_PULL" + // NetworkReady indicates that the ENI is ready for use by containers in the task. + NetworkReady NetworkStatus = "READY" + // NetworkDeleted indicates that the ENI is deleted. + NetworkDeleted NetworkStatus = "DELETED" +) + +var ( + eniStatusOrder = map[NetworkStatus]int{ + NetworkNone: 0, + NetworkReadyPull: 1, + NetworkReady: 2, + NetworkDeleted: 3, + } +) + +func (es NetworkStatus) String() string { + return string(es) +} + +func (es NetworkStatus) ENIStatusBackwards(es2 NetworkStatus) bool { + return eniStatusOrder[es] < eniStatusOrder[es2] +} + +func GetAllENIStatuses() []NetworkStatus { + return []NetworkStatus{ + NetworkNone, + NetworkReadyPull, + NetworkReady, + NetworkDeleted, + } +} diff --git a/ecs-agent/netlib/model/status/network_status_test.go b/ecs-agent/netlib/model/status/network_status_test.go new file mode 100644 index 00000000000..09f3324aa84 --- /dev/null +++ b/ecs-agent/netlib/model/status/network_status_test.go @@ -0,0 +1,48 @@ +package status + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +// TestNetworkStatus verifies the corresponding string values of each status +// is appropriate. +func TestNetworkStatus(t *testing.T) { + testCases := []struct { + status NetworkStatus + str string + }{ + { + status: NetworkNone, + str: "NONE", + }, + { + status: NetworkReadyPull, + str: "NONE", + }, + { + status: NetworkReady, + str: "NONE", + }, + { + status: NetworkDeleted, + str: "NONE", + }, + } + for _, tc := range testCases { + t.Run(tc.str, func(t *testing.T) { + assert.Equal(t, tc.str, (&tc.status).String()) + }) + } +} + +// TestNetworkStatusOrder verifies that order of statuses are as required. +func TestNetworkStatusOrder(t *testing.T) { + assert.True(t, NetworkNone.ENIStatusBackwards(NetworkReadyPull)) + assert.True(t, NetworkReadyPull.ENIStatusBackwards(NetworkReady)) + assert.True(t, NetworkReady.ENIStatusBackwards(NetworkDeleted)) + + assert.False(t, NetworkReadyPull.ENIStatusBackwards(NetworkNone)) + assert.False(t, NetworkReady.ENIStatusBackwards(NetworkReadyPull)) + assert.False(t, NetworkDeleted.ENIStatusBackwards(NetworkReady)) +} diff --git a/ecs-agent/netlib/model/tasknetworkconfig/common_test.go b/ecs-agent/netlib/model/tasknetworkconfig/common_test.go new file mode 100644 index 00000000000..35da4a995fc --- /dev/null +++ b/ecs-agent/netlib/model/tasknetworkconfig/common_test.go @@ -0,0 +1,44 @@ +package tasknetworkconfig + +import ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" + +const ( + primaryNetNSName = "primary-netns" + secondaryNetNSName = "secondary-netns" + primaryInterfaceName = "primary-interface" + secondaryInterfaceName = "secondary-interface" +) + +func getTestTaskNetworkConfig() *TaskNetworkConfig { + return &TaskNetworkConfig{ + NetworkNamespaces: getTestNetworkNamespaces(), + } +} + +func getTestNetworkNamespaces() []*NetworkNamespace { + return []*NetworkNamespace{ + { + Name: secondaryNetNSName, + Index: 1, + NetworkInterfaces: getTestNetworkInterfaces(), + }, + { + Name: primaryNetNSName, + Index: 0, + NetworkInterfaces: getTestNetworkInterfaces(), + }, + } +} + +func getTestNetworkInterfaces() []*ni.NetworkInterface { + return []*ni.NetworkInterface{ + { + Name: secondaryInterfaceName, + Index: 1, + }, + { + Name: primaryInterfaceName, + Index: 0, + }, + } +} diff --git a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace.go b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace.go new file mode 100644 index 00000000000..ae6d9103e43 --- /dev/null +++ b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace.go @@ -0,0 +1,35 @@ +package tasknetworkconfig + +import ( + "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/appmesh" + "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" +) + +// NetworkNamespace is model representing each network namespace. +type NetworkNamespace struct { + Name string + Path string + Index int + + // NetworkInterfaces represents ENIs or any kind of network interface associated the particular netns. + NetworkInterfaces []*networkinterface.NetworkInterface + + // AppMeshConfig holds AppMesh related parameters for the particular netns. + AppMeshConfig *appmesh.AppMesh + + // TODO: Add Service Connect model here once it is moved under the netlib package. + + KnownState string + DesiredState string +} + +// GetPrimaryInterface returns the network interface that has the index value of 0 within +// the network namespace. +func (ns NetworkNamespace) GetPrimaryInterface() *networkinterface.NetworkInterface { + for _, ni := range ns.NetworkInterfaces { + if ni.Index == 0 { + return ni + } + } + return nil +} diff --git a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go new file mode 100644 index 00000000000..36b2dad9d24 --- /dev/null +++ b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go @@ -0,0 +1,38 @@ +package tasknetworkconfig + +import ( + "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNetworkNamespace_GetPrimaryInterface(t *testing.T) { + testCases := []struct { + netns *NetworkNamespace + primaryNI string + }{ + { + netns: &NetworkNamespace{ + NetworkInterfaces: []*networkinterface.NetworkInterface{ + { + Index: 1, + Name: secondaryInterfaceName, + }, + { + Index: 0, + Name: primaryInterfaceName, + }, + }, + }, + primaryNI: primaryInterfaceName, + }, + { + netns: &NetworkNamespace{}, + primaryNI: "", + }, + } + + for _, tc := range testCases { + assert.Equal(t, tc.primaryNI, tc.netns.GetPrimaryInterface()) + } +} diff --git a/ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go new file mode 100644 index 00000000000..98f8ab38c54 --- /dev/null +++ b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go @@ -0,0 +1,22 @@ +package tasknetworkconfig + +import ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" + +type TaskNetworkConfig struct { + NetworkNamespaces []*NetworkNamespace + NetworkMode string +} + +func (tnc *TaskNetworkConfig) GetPrimaryInterface() *ni.NetworkInterface { + return tnc.GetPrimaryNetNS().GetPrimaryInterface() +} + +func (tnc *TaskNetworkConfig) GetPrimaryNetNS() *NetworkNamespace { + for _, netns := range tnc.NetworkNamespaces { + if netns.Index == 0 { + return netns + } + } + + return nil +} diff --git a/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go new file mode 100644 index 00000000000..bc8e832a8e4 --- /dev/null +++ b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go @@ -0,0 +1,25 @@ +package tasknetworkconfig + +import ( + "github.com/stretchr/testify/assert" + + "testing" +) + +func TestTaskNetworkConfig_GetPrimaryInterface(t *testing.T) { + testNetConfig := getTestTaskNetworkConfig() + assert.Equal(t, primaryInterfaceName, testNetConfig.GetPrimaryInterface().Name) + + testNetConfig = &TaskNetworkConfig{ + NetworkNamespaces: []*NetworkNamespace{}, + } + assert.Nil(t, testNetConfig.GetPrimaryInterface()) +} + +func TestTaskNetworkConfig_GetPrimaryNetNS(t *testing.T) { + testNetConfig := getTestTaskNetworkConfig() + assert.Equal(t, primaryNetNSName, testNetConfig.GetPrimaryNetNS().Name) + + testNetConfig = &TaskNetworkConfig{} + assert.Nil(t, testNetConfig.GetPrimaryNetNS()) +} From c12a13e2ad95bdec2908a3f88e1cd8a8a6720af2 Mon Sep 17 00:00:00 2001 From: Samuel Konat Date: Wed, 13 Sep 2023 20:35:56 +0000 Subject: [PATCH 02/10] Lint fixes --- ecs-agent/netlib/model/status/network_status_test.go | 1 + .../netlib/model/tasknetworkconfig/network_namespace_test.go | 1 + .../netlib/model/tasknetworkconfig/task_network_config_test.go | 1 + 3 files changed, 3 insertions(+) diff --git a/ecs-agent/netlib/model/status/network_status_test.go b/ecs-agent/netlib/model/status/network_status_test.go index 09f3324aa84..a2eea8abf87 100644 --- a/ecs-agent/netlib/model/status/network_status_test.go +++ b/ecs-agent/netlib/model/status/network_status_test.go @@ -46,3 +46,4 @@ func TestNetworkStatusOrder(t *testing.T) { assert.False(t, NetworkReady.ENIStatusBackwards(NetworkReadyPull)) assert.False(t, NetworkDeleted.ENIStatusBackwards(NetworkReady)) } + diff --git a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go index 36b2dad9d24..73db3a833c0 100644 --- a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go +++ b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go @@ -36,3 +36,4 @@ func TestNetworkNamespace_GetPrimaryInterface(t *testing.T) { assert.Equal(t, tc.primaryNI, tc.netns.GetPrimaryInterface()) } } + diff --git a/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go index bc8e832a8e4..a0908891d35 100644 --- a/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go +++ b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go @@ -23,3 +23,4 @@ func TestTaskNetworkConfig_GetPrimaryNetNS(t *testing.T) { testNetConfig = &TaskNetworkConfig{} assert.Nil(t, testNetConfig.GetPrimaryNetNS()) } + From d1bd8897041a3f1da566988627700a7f5a569428 Mon Sep 17 00:00:00 2001 From: Samuel Konat Date: Wed, 13 Sep 2023 20:57:13 +0000 Subject: [PATCH 03/10] Lint fixes --- ecs-agent/netlib/model/status/network_status_test.go | 1 - .../netlib/model/tasknetworkconfig/network_namespace_test.go | 1 - .../netlib/model/tasknetworkconfig/task_network_config_test.go | 3 +-- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/ecs-agent/netlib/model/status/network_status_test.go b/ecs-agent/netlib/model/status/network_status_test.go index a2eea8abf87..09f3324aa84 100644 --- a/ecs-agent/netlib/model/status/network_status_test.go +++ b/ecs-agent/netlib/model/status/network_status_test.go @@ -46,4 +46,3 @@ func TestNetworkStatusOrder(t *testing.T) { assert.False(t, NetworkReady.ENIStatusBackwards(NetworkReadyPull)) assert.False(t, NetworkDeleted.ENIStatusBackwards(NetworkReady)) } - diff --git a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go index 73db3a833c0..36b2dad9d24 100644 --- a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go +++ b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go @@ -36,4 +36,3 @@ func TestNetworkNamespace_GetPrimaryInterface(t *testing.T) { assert.Equal(t, tc.primaryNI, tc.netns.GetPrimaryInterface()) } } - diff --git a/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go index a0908891d35..beed6efa249 100644 --- a/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go +++ b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go @@ -2,7 +2,7 @@ package tasknetworkconfig import ( "github.com/stretchr/testify/assert" - + "testing" ) @@ -23,4 +23,3 @@ func TestTaskNetworkConfig_GetPrimaryNetNS(t *testing.T) { testNetConfig = &TaskNetworkConfig{} assert.Nil(t, testNetConfig.GetPrimaryNetNS()) } - From 2d1bdf19b98def4d3752106a7a4f1aca45921a5a Mon Sep 17 00:00:00 2001 From: Samuel Konat Date: Wed, 13 Sep 2023 22:39:03 +0000 Subject: [PATCH 04/10] Fix lint issues --- ecs-agent/netlib/model/status/network_status_test.go | 3 ++- .../netlib/model/tasknetworkconfig/network_namespace_test.go | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ecs-agent/netlib/model/status/network_status_test.go b/ecs-agent/netlib/model/status/network_status_test.go index 09f3324aa84..2b43f3e401e 100644 --- a/ecs-agent/netlib/model/status/network_status_test.go +++ b/ecs-agent/netlib/model/status/network_status_test.go @@ -1,8 +1,9 @@ package status import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) // TestNetworkStatus verifies the corresponding string values of each status diff --git a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go index 36b2dad9d24..18e2ae4d4b8 100644 --- a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go +++ b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go @@ -2,8 +2,10 @@ package tasknetworkconfig import ( "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" - "github.com/stretchr/testify/assert" + "testing" + + "github.com/stretchr/testify/assert" ) func TestNetworkNamespace_GetPrimaryInterface(t *testing.T) { From 1ba2e890ca960ab27a45364b5fa25db9ccd68307 Mon Sep 17 00:00:00 2001 From: Samuel Konat Date: Wed, 13 Sep 2023 22:45:47 +0000 Subject: [PATCH 05/10] Add comments --- .../netlib/model/tasknetworkconfig/task_network_config.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go index 98f8ab38c54..74990255958 100644 --- a/ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go +++ b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go @@ -2,15 +2,19 @@ package tasknetworkconfig import ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" +// TaskNetworkConfig is the top level network data structure associated with a task. type TaskNetworkConfig struct { NetworkNamespaces []*NetworkNamespace NetworkMode string } +// GetPrimaryInterface returns the interface with index 0 inside the network namespace +// with index 0 associated with the task's network config. func (tnc *TaskNetworkConfig) GetPrimaryInterface() *ni.NetworkInterface { return tnc.GetPrimaryNetNS().GetPrimaryInterface() } +// GetPrimaryNetNS returns the netns with index 0 associated with the task's network config. func (tnc *TaskNetworkConfig) GetPrimaryNetNS() *NetworkNamespace { for _, netns := range tnc.NetworkNamespaces { if netns.Index == 0 { From 4c5549aeb1fd09bcfcad76d127a4e3eefcfe2c60 Mon Sep 17 00:00:00 2001 From: Samuel Konat Date: Thu, 14 Sep 2023 23:06:02 +0000 Subject: [PATCH 06/10] Add unit test tags --- ecs-agent/netlib/model/status/network_status_test.go | 9 ++++++--- .../model/tasknetworkconfig/network_namespace_test.go | 5 ++++- .../model/tasknetworkconfig/task_network_config_test.go | 3 +++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/ecs-agent/netlib/model/status/network_status_test.go b/ecs-agent/netlib/model/status/network_status_test.go index 2b43f3e401e..27131e0b46b 100644 --- a/ecs-agent/netlib/model/status/network_status_test.go +++ b/ecs-agent/netlib/model/status/network_status_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + package status import ( @@ -19,15 +22,15 @@ func TestNetworkStatus(t *testing.T) { }, { status: NetworkReadyPull, - str: "NONE", + str: "READY_PULL", }, { status: NetworkReady, - str: "NONE", + str: "READY", }, { status: NetworkDeleted, - str: "NONE", + str: "DELETED", }, } for _, tc := range testCases { diff --git a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go index 18e2ae4d4b8..c6da5e4282f 100644 --- a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go +++ b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + package tasknetworkconfig import ( @@ -35,6 +38,6 @@ func TestNetworkNamespace_GetPrimaryInterface(t *testing.T) { } for _, tc := range testCases { - assert.Equal(t, tc.primaryNI, tc.netns.GetPrimaryInterface()) + assert.Equal(t, tc.primaryNI, tc.netns.GetPrimaryInterface().Name) } } diff --git a/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go index beed6efa249..1e2716df339 100644 --- a/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go +++ b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config_test.go @@ -1,3 +1,6 @@ +//go:build unit +// +build unit + package tasknetworkconfig import ( From 887cb2ccaa9ae191faa4bcb2403171f26cee7e9e Mon Sep 17 00:00:00 2001 From: Samuel Konat Date: Fri, 15 Sep 2023 00:06:37 +0000 Subject: [PATCH 07/10] Test fix --- .../netlib/model/tasknetworkconfig/network_namespace_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go index c6da5e4282f..c13285c17cd 100644 --- a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go +++ b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go @@ -37,7 +37,6 @@ func TestNetworkNamespace_GetPrimaryInterface(t *testing.T) { }, } - for _, tc := range testCases { - assert.Equal(t, tc.primaryNI, tc.netns.GetPrimaryInterface().Name) - } + assert.Equal(t, tc.primaryNI, testCases[0].netns.GetPrimaryInterface().Name) + assert.Nil(t, testCases[1].netns.GetPrimaryInterface()) } From 39229dca1bd456746316c118b039d324b9acb382 Mon Sep 17 00:00:00 2001 From: Samuel Konat Date: Fri, 15 Sep 2023 00:18:31 +0000 Subject: [PATCH 08/10] Test fix --- .../network_namespace_test.go | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go index c13285c17cd..84ab949f659 100644 --- a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go +++ b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go @@ -12,31 +12,20 @@ import ( ) func TestNetworkNamespace_GetPrimaryInterface(t *testing.T) { - testCases := []struct { - netns *NetworkNamespace - primaryNI string - }{ - { - netns: &NetworkNamespace{ - NetworkInterfaces: []*networkinterface.NetworkInterface{ - { - Index: 1, - Name: secondaryInterfaceName, - }, - { - Index: 0, - Name: primaryInterfaceName, - }, - }, + netns := &NetworkNamespace{ + NetworkInterfaces: []*networkinterface.NetworkInterface{ + { + Index: 1, + Name: secondaryInterfaceName, + }, + { + Index: 0, + Name: primaryInterfaceName, }, - primaryNI: primaryInterfaceName, - }, - { - netns: &NetworkNamespace{}, - primaryNI: "", }, } + assert.Equal(t, primaryInterfaceName, netns.GetPrimaryInterface().Name) - assert.Equal(t, tc.primaryNI, testCases[0].netns.GetPrimaryInterface().Name) - assert.Nil(t, testCases[1].netns.GetPrimaryInterface()) + netns = &NetworkNamespace{} + assert.Equal(t, netns) } From cdbc82bf12bad08bf681dbf421773c85f18ec47f Mon Sep 17 00:00:00 2001 From: Samuel Konat Date: Mon, 18 Sep 2023 22:19:29 +0000 Subject: [PATCH 09/10] Fix tests --- .../netlib/model/tasknetworkconfig/network_namespace_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go index 84ab949f659..6af1b838658 100644 --- a/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go +++ b/ecs-agent/netlib/model/tasknetworkconfig/network_namespace_test.go @@ -27,5 +27,5 @@ func TestNetworkNamespace_GetPrimaryInterface(t *testing.T) { assert.Equal(t, primaryInterfaceName, netns.GetPrimaryInterface().Name) netns = &NetworkNamespace{} - assert.Equal(t, netns) + assert.Empty(t, netns.GetPrimaryInterface()) } From dd027aad7127ab6ece81ff4259091c1453574496 Mon Sep 17 00:00:00 2001 From: Samuel Konat Date: Mon, 18 Sep 2023 22:29:17 +0000 Subject: [PATCH 10/10] Fix tests --- .../netlib/model/tasknetworkconfig/task_network_config.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go index 74990255958..61ce6c0ddfb 100644 --- a/ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go +++ b/ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go @@ -11,7 +11,10 @@ type TaskNetworkConfig struct { // GetPrimaryInterface returns the interface with index 0 inside the network namespace // with index 0 associated with the task's network config. func (tnc *TaskNetworkConfig) GetPrimaryInterface() *ni.NetworkInterface { - return tnc.GetPrimaryNetNS().GetPrimaryInterface() + if tnc != nil && tnc.GetPrimaryNetNS() != nil { + return tnc.GetPrimaryNetNS().GetPrimaryInterface() + } + return nil } // GetPrimaryNetNS returns the netns with index 0 associated with the task's network config.