Skip to content

Commit

Permalink
Add debug mode for containerd agent and minor patches (#4059)
Browse files Browse the repository at this point in the history
* Add GetInterfaceNamesToNetNSMapping method in netlib

* Add GetInterfaceByIndex method in netlib

* Add debug mode for containerd platform

* Fix DNS config for debug mode

* Add firecracker debug platform in netlib
  • Loading branch information
samjkon authored Dec 16, 2023
1 parent b7e8df1 commit 10a3127
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 1 deletion.
11 changes: 11 additions & 0 deletions ecs-agent/netlib/model/tasknetworkconfig/network_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,14 @@ func (ns *NetworkNamespace) GetPrimaryInterface() *networkinterface.NetworkInter
func (ns *NetworkNamespace) IsPrimary() bool {
return ns.Index == 0
}

// GetInterfaceByIndex returns the interface in the netns that has the specified index.
func (ns *NetworkNamespace) GetInterfaceByIndex(idx int64) *networkinterface.NetworkInterface {
for _, iface := range ns.NetworkInterfaces {
if iface.Index == idx {
return iface
}
}

return nil
}
14 changes: 14 additions & 0 deletions ecs-agent/netlib/model/tasknetworkconfig/task_network_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,17 @@ func (tnc *TaskNetworkConfig) GetEniNamesToAssociationProtocolMapping() map[stri
}
return eniNameToAssociationProtocol
}

// GetInterfaceNamesToNetNSMapping returns a map where key is interface name and value is the netns
// in which the interface exists.
func (tnc *TaskNetworkConfig) GetInterfaceNamesToNetNSMapping() map[string]*NetworkNamespace {
name2NetNS := make(map[string]*NetworkNamespace)
for _, netNS := range tnc.NetworkNamespaces {
for _, iface := range netNS.NetworkInterfaces {
if iface.Name != "" {
name2NetNS[iface.Name] = netNS
}
}
}
return name2NetNS
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ package tasknetworkconfig

import (
"github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/model/ecs"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"fmt"
"testing"
)

Expand Down Expand Up @@ -79,3 +83,29 @@ func TestNewTaskNetConfig(t *testing.T) {
assert.Equal(t, *netNSs[0], *taskNetConfig.NetworkNamespaces[0])
assert.Equal(t, *netNSs[1], *taskNetConfig.NetworkNamespaces[1])
}

// TestTaskNetworkConfig_GetInterfaceNamesToNetNSMapping verifies the map created
// between interface name and netNS is accurate.
func TestTaskNetworkConfig_GetInterfaceNamesToNetNSMapping(t *testing.T) {
var netNSs []*NetworkNamespace
for i := 0; i < 3; i++ {
netNSs = append(netNSs, &NetworkNamespace{
Name: fmt.Sprintf("ns%d", i),
NetworkInterfaces: []*ni.NetworkInterface{
{
Name: fmt.Sprintf("ni%d", i),
},
},
})
}

netConfig := &TaskNetworkConfig{
NetworkNamespaces: netNSs,
}

name2NetNS := netConfig.GetInterfaceNamesToNetNSMapping()
require.Equal(t, 3, len(name2NetNS))
for i := 0; i < 3; i++ {
require.Equal(t, name2NetNS[fmt.Sprintf("ni%d", i)].Name, fmt.Sprintf("ns%d", i))
}
}
14 changes: 13 additions & 1 deletion ecs-agent/netlib/platform/common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ func NewPlatform(
return &firecraker{
common: commonPlatform,
}, nil
case WarmpoolDebugPlatform:
return &containerdDebug{
containerd: containerd{
common: commonPlatform,
},
}, nil
case FirecrackerDebugPlatform:
return &firecrackerDebug{
firecraker: firecraker{
common: commonPlatform,
},
}, nil
}
return nil, errors.New("invalid platform: " + platformString)
}
Expand Down Expand Up @@ -494,7 +506,7 @@ func (c *common) generateNetworkConfigFilesForDebugPlatforms(
return nil
}

func (c *common) copyFile(src, dst string, fileMode os.FileMode) error {
func (c *common) copyFile(dst, src string, fileMode os.FileMode) error {
contents, err := c.ioutil.ReadFile(src)
if err != nil {
return errors.Wrapf(err, "unable to read %s", src)
Expand Down
14 changes: 14 additions & 0 deletions ecs-agent/netlib/platform/containerd_debug_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package platform

import (
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/tasknetworkconfig"
)

// containerdDebug implements platform API methods for non-firecrakcer infrastructure.
type containerdDebug struct {
containerd
}

func (c *containerdDebug) CreateDNSConfig(taskID string, netNS *tasknetworkconfig.NetworkNamespace) error {
return c.common.createDNSConfig(taskID, true, netNS)
}
11 changes: 11 additions & 0 deletions ecs-agent/netlib/platform/firecracker_debug_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package platform

import "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/tasknetworkconfig"

type firecrackerDebug struct {
firecraker
}

func (fc *firecrackerDebug) CreateDNSConfig(taskID string, netNS *tasknetworkconfig.NetworkNamespace) error {
return fc.common.createDNSConfig(taskID, true, netNS)
}

0 comments on commit 10a3127

Please sign in to comment.