Skip to content

Commit

Permalink
adds hardcoded nameserver for testing dns config
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Villalta committed Oct 29, 2024
1 parent ea301d0 commit e477dfb
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 2 deletions.
41 changes: 39 additions & 2 deletions ecs-agent/netlib/platform/managed_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
goErr "errors"
"fmt"
"path/filepath"

"github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs"
"github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/model/ecs"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/serviceconnect"
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/status"
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/tasknetworkconfig"

"github.com/aws/aws-sdk-go/aws"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -65,7 +65,44 @@ func (m *managedLinux) BuildTaskNetworkConfiguration(

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

func (m *managedLinux) createDNSConfig(taskID string,
netNS *tasknetworkconfig.NetworkNamespace) error {
netNSName := netNS.Name
primaryIF := netNS.GetPrimaryInterface()

// Hard-code names servers (Temp fix)
data := m.common.nsUtil.BuildResolvConfig([]string{"8.8.8.8"},
[]string{"us-west-2.compute.internal"})

m.common.ioutil.WriteFile(
filepath.Join(networkConfigFileDirectory, netNSName, ResolveConfFileName),
[]byte(data),
networkConfigFileMode)

err := m.common.createHostnameFileForNetNS(netNSName, primaryIF)
if err != nil {
return errors.Wrap(err, "unable to create hostname file for netns")
}

err = m.common.createHostnameFileForDefaultNetNS()
if err != nil {
return errors.Wrap(err, "unable to verify the existence of /etc/hostname on the host")
}

err = m.common.createHostsFile(netNSName, primaryIF)
if err != nil {
return errors.Wrap(err, "unable to create hosts file for netns")
}

// Next, copy these files into a task volume, which can be used by containers as well, to
// configure their network.
if err := m.common.copyNetworkConfigFilesToTask(taskID, netNSName); err != nil {
return err
}
return nil
}

func (m *managedLinux) ConfigureInterface(
Expand Down
99 changes: 99 additions & 0 deletions ecs-agent/netlib/platform/managed_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//go:build !windows && unit
// +build !windows,unit

// 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 platform

import (
"fmt"
"io/fs"
"os"
"testing"

mock_ecscni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/ecscni/mocks_nsutil"
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/tasknetworkconfig"
mock_ioutilwrapper "github.com/aws/amazon-ecs-agent/ecs-agent/utils/ioutilwrapper/mocks"
mock_oswrapper "github.com/aws/amazon-ecs-agent/ecs-agent/utils/oswrapper/mocks"
mock_volume "github.com/aws/amazon-ecs-agent/ecs-agent/volume/mocks"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)

func TestManagedLinux_CreateDNSConfig(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

netNSName := "netns-name"
netNSPath := "/etc/netns/" + netNSName
iface := getTestInterface()

netns := &tasknetworkconfig.NetworkNamespace{
Name: netNSName,
Path: netNSPath,
NetworkInterfaces: []*networkinterface.NetworkInterface{iface},
}
ioutil := mock_ioutilwrapper.NewMockIOUtil(ctrl)
nsUtil := mock_ecscni.NewMockNetNSUtil(ctrl)
osWrapper := mock_oswrapper.NewMockOS(ctrl)
mockFile := mock_oswrapper.NewMockFile(ctrl)
volumeAccessor := mock_volume.NewMockTaskVolumeAccessor(ctrl)
commonPlatform := &common{
ioutil: ioutil,
nsUtil: nsUtil,
os: osWrapper,
dnsVolumeAccessor: volumeAccessor,
}

managedPlatform := &managedLinux{
common: *commonPlatform,
}

resolvData := fmt.Sprintf("nameserver %s\nsearch %s\n",
"8.8.8.8",
"us-west-2.compute.internal",
)
hostnameData := fmt.Sprintf("%s\n", iface.GetHostname())

// Test creation of hosts file.
hostsData := fmt.Sprintf("%s\n%s %s\n%s %s\n%s %s\n",
HostsLocalhostEntry,
ipv4Addr, dnsName,
addr, hostName,
addr2, hostName2,
)

taskID := "taskID"

// Creation of resolv.conf file.
nsUtil.EXPECT().BuildResolvConfig([]string{"8.8.8.8"}, []string{"us-west-2.compute.internal"}).Return(resolvData).Times(1)
ioutil.EXPECT().WriteFile(netNSPath+"/resolv.conf", []byte(resolvData), fs.FileMode(0644))

// Creation of hostname file.
ioutil.EXPECT().WriteFile(netNSPath+"/hostname", []byte(hostnameData), fs.FileMode(0644))
osWrapper.EXPECT().OpenFile("/etc/hostname", os.O_RDONLY|os.O_CREATE, fs.FileMode(0644)).Return(mockFile, nil).Times(1)
mockFile.EXPECT().Close().Times(1)
ioutil.EXPECT().WriteFile(netNSPath+"/hosts", []byte(hostsData), fs.FileMode(0644))

// CopyToVolume created files into task volume.
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 := managedPlatform.CreateDNSConfig(taskID, netns)
require.NoError(t, err)

}

0 comments on commit e477dfb

Please sign in to comment.