Skip to content

Commit

Permalink
Fix netns model creation issue (#4076)
Browse files Browse the repository at this point in the history
Fixes a bug in the creation of network namespace models for AWSVPC mode tasks which causes the agent to crash in an edge case situation.
  • Loading branch information
samjkon authored Jan 22, 2024
1 parent 473aa9a commit f3208a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
18 changes: 18 additions & 0 deletions ecs-agent/netlib/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func getSingleNetNSAWSVPCTestData(testTaskID string) (*ecsacs.Task, tasknetworkc
taskPayload := &ecsacs.Task{
NetworkMode: aws.String(ecs.NetworkModeAwsvpc),
ElasticNetworkInterfaces: []*ecsacs.ElasticNetworkInterface{enis[0]},
Containers: []*ecsacs.Container{{}},
}

netNSName := fmt.Sprintf(netNSNamePattern, testTaskID, eniName)
Expand All @@ -85,6 +86,23 @@ func getSingleNetNSAWSVPCTestData(testTaskID string) (*ecsacs.Task, tasknetworkc
return taskPayload, taskNetConfig
}

// getSingleNetNSMultiIfaceWithNameTestData returns the test data for EKS like use cases but with names specified for interfaces.
func getSingleNetNSMultiIfaceWithNameTestData(testTaskID string) (*ecsacs.Task, tasknetworkconfig.TaskNetworkConfig) {
taskPayload, taskNetConfig := getSingleNetNSMultiIfaceAWSVPCTestData(testTaskID)
for i, iface := range taskPayload.ElasticNetworkInterfaces {
eniName := fmt.Sprintf("eni-%d", i)
iface.Name = aws.String(eniName)
taskNetConfig.NetworkNamespaces[0].NetworkInterfaces[i].Name = eniName
}

netNSName := fmt.Sprintf(netNSNamePattern, testTaskID, "eni-0")
netNSPath := netNSPathDir + netNSName
taskNetConfig.NetworkNamespaces[0].Name = netNSName
taskNetConfig.NetworkNamespaces[0].Path = netNSPath

return taskPayload, taskNetConfig
}

// getSingleNetNSMultiIfaceAWSVPCTestData returns test data for EKS like use cases.
func getSingleNetNSMultiIfaceAWSVPCTestData(testTaskID string) (*ecsacs.Task, tasknetworkconfig.TaskNetworkConfig) {
taskPayload, taskNetConfig := getSingleNetNSAWSVPCTestData(testTaskID)
Expand Down
1 change: 1 addition & 0 deletions ecs-agent/netlib/network_builder_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestNetworkBuilder_BuildTaskNetworkConfiguration(t *testing.T) {
// Warmpool test cases.
t.Run("containerd-default", getTestFunc(getSingleNetNSAWSVPCTestData, platform.WarmpoolPlatform))
t.Run("containerd-multi-interface", getTestFunc(getSingleNetNSMultiIfaceAWSVPCTestData, platform.WarmpoolPlatform))
t.Run("containerd-multi-interface-with-names", getTestFunc(getSingleNetNSMultiIfaceWithNameTestData, platform.WarmpoolPlatform))
t.Run("containerd-multi-netns", getTestFunc(getMultiNetNSMultiIfaceAWSVPCTestData, platform.WarmpoolPlatform))

// Firecracker test cases.
Expand Down
3 changes: 2 additions & 1 deletion ecs-agent/netlib/platform/common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ func (c *common) buildAWSVPCNetworkNamespaces(
// This case is identified if the singleNetNS flag is set, or if the ENIs have an empty 'Name' field,
// or if there is only on ENI in the payload.
if singleNetNS || len(taskPayload.ElasticNetworkInterfaces) == 1 ||
aws.StringValue(taskPayload.ElasticNetworkInterfaces[0].Name) == "" {
aws.StringValue(taskPayload.ElasticNetworkInterfaces[0].Name) == "" ||
len(taskPayload.Containers[0].NetworkInterfaceNames) == 0 {
primaryNetNS, err := c.buildNetNS(taskID,
0,
taskPayload.ElasticNetworkInterfaces,
Expand Down

0 comments on commit f3208a9

Please sign in to comment.