Skip to content

Commit

Permalink
Minor patches in shared library (aws#4054)
Browse files Browse the repository at this point in the history
* Minor patches in netlib package

Added DeleteDNSConfig to platform APIs list. This enables deletion of
DNS config files from task netns. Renamed VolumeAccessor to
TaskVolumeAccessor.

* Add MockNetworkBuilder

* Add GetEniNamesToAssociationProtocolMapping
  • Loading branch information
samjkon authored Dec 12, 2023
1 parent 4e688f6 commit 711cec2
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 55 deletions.
16 changes: 16 additions & 0 deletions ecs-agent/netlib/generate_mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package netlib

//go:generate mockgen -destination=mocks/netbuilder_mocks.go -copyright_file=../../scripts/copyright_file github.com/aws/amazon-ecs-agent/ecs-agent/netlib NetworkBuilder
94 changes: 94 additions & 0 deletions ecs-agent/netlib/mocks/netbuilder_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -59,3 +59,17 @@ func (tnc *TaskNetworkConfig) GetPrimaryNetNS() *NetworkNamespace {

return nil
}

// GetEniNamesToAssociationProtocolMapping returns a map of ENI names to
// interface association protocols (like tunnel/veth).
func (tnc *TaskNetworkConfig) GetEniNamesToAssociationProtocolMapping() map[string]string {
eniNameToAssociationProtocol := make(map[string]string)
for _, netNS := range tnc.NetworkNamespaces {
for _, iface := range netNS.NetworkInterfaces {
if iface.Name != "" {
eniNameToAssociationProtocol[iface.Name] = iface.InterfaceAssociationProtocol
}
}
}
return eniNameToAssociationProtocol
}
8 changes: 7 additions & 1 deletion ecs-agent/netlib/network_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type networkBuilder struct {
func NewNetworkBuilder(
platformString string,
metricsFactory metrics.EntryFactory,
volumeAccessor volume.VolumeAccessor,
volumeAccessor volume.TaskVolumeAccessor,
networkDao data.NetworkDataClient,
stateDBDir string) (NetworkBuilder, error) {
pAPI, err := platform.NewPlatform(
Expand Down Expand Up @@ -261,6 +261,12 @@ func (nb *networkBuilder) stopAWSVPC(ctx context.Context, netNS *tasknetworkconf
errs = multierror.Append(err, errs)
}

err = nb.platformAPI.DeleteDNSConfig(netNS.Name)
if err != nil {
logger.Error(fmt.Sprintf("Failed to cleanup DNS config files: %v", err))
errs = multierror.Append(err, errs)
}

err = nb.platformAPI.DeleteNetNS(netNS.Path)
if err != nil {
logger.Error(fmt.Sprintf("Failed to delete network namespace: %v", err), logFields)
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 @@ -373,5 +373,6 @@ func getExpectedCalls_StopAWSVPC(
}

return append(calls,
platformAPI.EXPECT().DeleteDNSConfig(netNS.Name).Return(nil).Times(1),
platformAPI.EXPECT().DeleteNetNS(netNS.Path).Return(nil).Times(1))
}
3 changes: 3 additions & 0 deletions ecs-agent/netlib/platform/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type API interface {
// have access to the accurate DNS configuration information.
CreateDNSConfig(taskID string, netNS *tasknetworkconfig.NetworkNamespace) error

// DeleteDNSConfig deletes the directory at /etc/netns/<netns-name> and all its files.
DeleteDNSConfig(netNSName string) error

// GetNetNSPath returns the path of a network namespace.
GetNetNSPath(netNSName string) string

Expand Down
56 changes: 36 additions & 20 deletions ecs-agent/netlib/platform/common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,33 +74,33 @@ const (
// It contains all fields and methods that can be commonly used by all
// platforms.
type common struct {
nsUtil ecscni.NetNSUtil
taskVolumeAccessor volume.VolumeAccessor
os oswrapper.OS
ioutil ioutilwrapper.IOUtil
netlink netlinkwrapper.NetLink
stateDBDir string
cniClient ecscni.CNI
net netwrapper.Net
nsUtil ecscni.NetNSUtil
dnsVolumeAccessor volume.TaskVolumeAccessor
os oswrapper.OS
ioutil ioutilwrapper.IOUtil
netlink netlinkwrapper.NetLink
stateDBDir string
cniClient ecscni.CNI
net netwrapper.Net
}

// NewPlatform creates an implementation of the platform API depending on the
// platform type where the agent is executing.
func NewPlatform(
platformString string,
volumeAccessor volume.VolumeAccessor,
volumeAccessor volume.TaskVolumeAccessor,
stateDBDirectory string,
netWrapper netwrapper.Net,
) (API, error) {
commonPlatform := common{
nsUtil: ecscni.NewNetNSUtil(),
taskVolumeAccessor: volumeAccessor,
os: oswrapper.NewOS(),
ioutil: ioutilwrapper.NewIOUtil(),
netlink: netlinkwrapper.New(),
stateDBDir: stateDBDirectory,
cniClient: ecscni.NewCNIClient([]string{CNIPluginPathDefault}),
net: netWrapper,
nsUtil: ecscni.NewNetNSUtil(),
dnsVolumeAccessor: volumeAccessor,
os: oswrapper.NewOS(),
ioutil: ioutilwrapper.NewIOUtil(),
netlink: netlinkwrapper.New(),
stateDBDir: stateDBDirectory,
cniClient: ecscni.NewCNIClient([]string{CNIPluginPathDefault}),
net: netWrapper,
}

// TODO: implement remaining platforms - windows.
Expand Down Expand Up @@ -334,6 +334,22 @@ func (c *common) DeleteNetNS(netNSPath string) error {
return nil
}

// DeleteDNSConfig deletes the directory at /etc/netns/<netns-name> and all its files.
func (c *common) DeleteDNSConfig(netNSName string) error {
if netNSName == "" {
return errors.New("netns name cannot be empty")
}
netNSDir := filepath.Join(networkConfigFileDirectory, netNSName)
_, err := c.os.Stat(netNSDir)
if c.os.IsNotExist(err) {
return errors.Wrap(err, "network config directory not found")
} else if err != nil {
return err
}

return c.os.RemoveAll(netNSDir)
}

// setUpLoFunc returns a method that sets the loop back interface inside a
// particular network namespace to the state "UP". This function is used to
// set up the loop back interface inside a task network namespace soon after
Expand Down Expand Up @@ -385,7 +401,7 @@ func (c *common) createDNSConfig(
// Next, copy these files into a task volume, which can be used by containers as well, to
// configure their network.
configFiles := []string{HostsFileName, ResolveConfFileName, HostnameFileName}
if err := c.copyNetworkConfigFilesToTask(netNS.Name, configFiles); err != nil {
if err := c.copyNetworkConfigFilesToTask(taskID, netNS.Name, configFiles); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -431,10 +447,10 @@ func (c *common) createNetworkConfigFiles(netNSName string, primaryIF *networkin

// copyNetworkConfigFilesToTask copies the contents of the DNS config files for a
// task into the task volume.
func (c *common) copyNetworkConfigFilesToTask(netNSName string, configFiles []string) error {
func (c *common) copyNetworkConfigFilesToTask(taskID, netNSName string, configFiles []string) error {
for _, file := range configFiles {
source := filepath.Join(networkConfigFileDirectory, netNSName, file)
err := c.taskVolumeAccessor.CopyToVolume(source, file, networkConfigFileMode)
err := c.dnsVolumeAccessor.CopyToVolume(taskID, source, file, networkConfigFileMode)
if err != nil {
return errors.Wrapf(err, "unable to populate %s for task", file)
}
Expand Down
19 changes: 10 additions & 9 deletions ecs-agent/netlib/platform/common_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ func TestCommon_CreateDNSFiles(t *testing.T) {
nsUtil := mock_ecscni.NewMockNetNSUtil(ctrl)
osWrapper := mock_oswrapper.NewMockOS(ctrl)
mockFile := mock_oswrapper.NewMockFile(ctrl)
volumeAccessor := mock_volume.NewMockVolumeAccessor(ctrl)
volumeAccessor := mock_volume.NewMockTaskVolumeAccessor(ctrl)
commonPlatform := &common{
ioutil: ioutil,
nsUtil: nsUtil,
os: osWrapper,
taskVolumeAccessor: volumeAccessor,
ioutil: ioutil,
nsUtil: nsUtil,
os: osWrapper,
dnsVolumeAccessor: volumeAccessor,
}

// Test creation of hosts file.
Expand All @@ -183,6 +183,7 @@ func TestCommon_CreateDNSFiles(t *testing.T) {
)
hostnameData := fmt.Sprintf("%s\n", iface.GetHostname())

taskID := "taskID"
gomock.InOrder(
// Creation of netns path.
osWrapper.EXPECT().Stat(netNSPath).Return(nil, os.ErrNotExist).Times(1),
Expand All @@ -202,11 +203,11 @@ func TestCommon_CreateDNSFiles(t *testing.T) {
ioutil.EXPECT().WriteFile(netNSPath+"/hosts", []byte(hostsData), fs.FileMode(0644)),

// CopyToVolume created files into task volume.
volumeAccessor.EXPECT().CopyToVolume(netNSPath+"/hosts", "hosts", fs.FileMode(0644)).Return(nil).Times(1),
volumeAccessor.EXPECT().CopyToVolume(netNSPath+"/resolv.conf", "resolv.conf", fs.FileMode(0644)).Return(nil).Times(1),
volumeAccessor.EXPECT().CopyToVolume(netNSPath+"/hostname", "hostname", fs.FileMode(0644)).Return(nil).Times(1),
volumeAccessor.EXPECT().CopyToVolume(taskID, netNSPath+"/hosts", "hosts", fs.FileMode(0644)).Return(nil).Times(1),
volumeAccessor.EXPECT().CopyToVolume(taskID, netNSPath+"/resolv.conf", "resolv.conf", fs.FileMode(0644)).Return(nil).Times(1),
volumeAccessor.EXPECT().CopyToVolume(taskID, netNSPath+"/hostname", "hostname", fs.FileMode(0644)).Return(nil).Times(1),
)
err := commonPlatform.createDNSConfig("taskID", false, netns)
err := commonPlatform.createDNSConfig(taskID, false, netns)
require.NoError(t, err)
}

Expand Down
6 changes: 5 additions & 1 deletion ecs-agent/netlib/platform/containerd_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type common struct {

func NewPlatform(
platformString string,
volumeAccessor volume.VolumeAccessor,
volumeAccessor volume.TaskVolumeAccessor,
stateDBDirectory string,
netWrapper netwrapper.Net) (API, error) {
return nil, nil
Expand All @@ -61,6 +61,10 @@ func (c *common) CreateDNSConfig(taskNetConfig *tasknetworkconfig.TaskNetworkCon
return nil
}

func (c *common) DeleteDNSConfig(netNSName string) error {
return nil
}

func (c *common) GetNetNSPath(netNSName string) string {
return ""
}
Expand Down
14 changes: 14 additions & 0 deletions ecs-agent/netlib/platform/mocks/platform_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecs-agent/volume/generate_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@

package volume

//go:generate mockgen -destination=mocks/volume_accessor_mocks.go -copyright_file=../../scripts/copyright_file github.com/aws/amazon-ecs-agent/ecs-agent/volume VolumeAccessor
//go:generate mockgen -destination=mocks/volume_accessor_mocks.go -copyright_file=../../scripts/copyright_file github.com/aws/amazon-ecs-agent/ecs-agent/volume TaskVolumeAccessor
Loading

0 comments on commit 711cec2

Please sign in to comment.