Skip to content

Commit e6fa3e8

Browse files
authored
Merge pull request #4421 from aws/platform-config
Adds config parameter to shared netlib platform
2 parents 40ac239 + a77d3d1 commit e6fa3e8

File tree

7 files changed

+104
-17
lines changed

7 files changed

+104
-17
lines changed

ecs-agent/netlib/network_builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ type networkBuilder struct {
4848
}
4949

5050
func NewNetworkBuilder(
51-
platformString string,
51+
platformConfig platform.Config,
5252
metricsFactory metrics.EntryFactory,
5353
volumeAccessor volume.TaskVolumeAccessor,
5454
networkDao data.NetworkDataClient,
5555
stateDBDir string) (NetworkBuilder, error) {
5656
pAPI, err := platform.NewPlatform(
57-
platformString,
57+
platformConfig,
5858
volumeAccessor,
5959
stateDBDir,
6060
netwrapper.NewNet(),

ecs-agent/netlib/network_builder_linux_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/serviceconnect"
3333
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/status"
3434
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/tasknetworkconfig"
35-
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/platform"
35+
platform "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/platform"
3636
mock_platform "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/platform/mocks"
3737
mock_netwrapper "github.com/aws/amazon-ecs-agent/ecs-agent/utils/netwrapper/mocks"
3838

@@ -42,12 +42,12 @@ import (
4242
)
4343

4444
func TestNewNetworkBuilder(t *testing.T) {
45-
nbi, err := NewNetworkBuilder(platform.WarmpoolPlatform, nil, nil, nil, "")
45+
nbi, err := NewNetworkBuilder(platform.Config{Name: platform.WarmpoolPlatform}, nil, nil, nil, "")
4646
nb := nbi.(*networkBuilder)
4747
require.NoError(t, err)
4848
require.NotNil(t, nb.platformAPI)
4949

50-
nbi, err = NewNetworkBuilder("invalid-platform", nil, nil, nil, "")
50+
nbi, err = NewNetworkBuilder(platform.Config{Name: "invalid-platform"}, nil, nil, nil, "")
5151
require.Error(t, err)
5252
require.Nil(t, nbi)
5353
}
@@ -88,7 +88,8 @@ func getTestFunc(
8888

8989
// Create a networkBuilder for the warmpool platform.
9090
mockNet := mock_netwrapper.NewMockNet(ctrl)
91-
platformAPI, err := platform.NewPlatform(plt, nil, "", mockNet)
91+
platformConfig := platform.Config{Name: plt}
92+
platformAPI, err := platform.NewPlatform(platformConfig, nil, "", mockNet)
9293
require.NoError(t, err)
9394
netBuilder := &networkBuilder{
9495
platformAPI: platformAPI,

ecs-agent/netlib/network_builder_windows_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func getTestFunc(
4949

5050
// Create a networkBuilder for the warmpool platform.
5151
mockNet := mock_netwrapper.NewMockNet(ctrl)
52-
platformAPI, err := platform.NewPlatform(plt, nil, "", mockNet)
52+
config := platform.Config{Name: plt}
53+
platformAPI, err := platform.NewPlatform(config, nil, "", mockNet)
5354
require.NoError(t, err)
5455
netBuilder := &networkBuilder{
5556
platformAPI: platformAPI,

ecs-agent/netlib/platform/api.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,12 @@ type API interface {
7979
scConfig *serviceconnect.ServiceConnectConfig,
8080
) error
8181
}
82+
83+
// Config contains platform-specific data.
84+
type Config struct {
85+
// Name specifies which platform to use (Linux, Windows, ec2-debug, etc).
86+
Name string
87+
// ResolvConfPath specifies path to resolv.conf file for DNS config.
88+
// Different platforms may have different paths for this file.
89+
ResolvConfPath string
90+
}

ecs-agent/netlib/platform/common_linux.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ type common struct {
8282
stateDBDir string
8383
cniClient ecscni.CNI
8484
net netwrapper.Net
85+
resolvConfPath string
8586
}
8687

8788
// NewPlatform creates an implementation of the platform API depending on the
8889
// platform type where the agent is executing.
8990
func NewPlatform(
90-
platformString string,
91+
platformConfig Config,
9192
volumeAccessor volume.TaskVolumeAccessor,
9293
stateDBDirectory string,
9394
netWrapper netwrapper.Net,
@@ -101,10 +102,10 @@ func NewPlatform(
101102
stateDBDir: stateDBDirectory,
102103
cniClient: ecscni.NewCNIClient([]string{CNIPluginPathDefault}),
103104
net: netWrapper,
105+
resolvConfPath: platformConfig.ResolvConfPath,
104106
}
105107

106-
// TODO: implement remaining platforms - windows.
107-
switch platformString {
108+
switch platformConfig.Name {
108109
case WarmpoolPlatform:
109110
return &containerd{
110111
common: commonPlatform,
@@ -134,7 +135,7 @@ func NewPlatform(
134135
client: ec2Client,
135136
}, nil
136137
}
137-
return nil, errors.New("invalid platform: " + platformString)
138+
return nil, errors.New("invalid platform: " + platformConfig.Name)
138139
}
139140

140141
// BuildTaskNetworkConfiguration translates network data in task payload sent by ACS
@@ -533,10 +534,14 @@ func (c *common) generateNetworkConfigFilesForDebugPlatforms(
533534
return errors.Wrap(err, "unable to create hostname file for netns")
534535
}
535536

536-
err = c.copyFile(filepath.Join(netNSDir, ResolveConfFileName), "/etc/resolv.conf", taskDNSConfigFileMode)
537+
// Copy Host's resolv.conf file.
538+
err = c.copyFile(filepath.Join(netNSDir, ResolveConfFileName),
539+
filepath.Join(c.resolvConfPath, ResolveConfFileName),
540+
taskDNSConfigFileMode)
537541
if err != nil {
538542
return err
539543
}
544+
540545
err = c.copyFile(filepath.Join(netNSDir, HostsFileName), "/etc/hosts", taskDNSConfigFileMode)
541546
if err != nil {
542547
return err

ecs-agent/netlib/platform/common_linux_test.go

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ import (
4747
)
4848

4949
func TestNewPlatform(t *testing.T) {
50-
_, err := NewPlatform(WarmpoolPlatform, nil, "", nil)
50+
_, err := NewPlatform(Config{Name: WarmpoolPlatform}, nil, "", nil)
5151
assert.NoError(t, err)
5252

53-
_, err = NewPlatform("invalid-platform", nil, "", nil)
53+
_, err = NewPlatform(Config{Name: "invalid-platform"}, nil, "", nil)
5454
assert.Error(t, err)
5555
}
5656

@@ -170,6 +170,77 @@ func TestCommon_CreateDNSFiles(t *testing.T) {
170170
require.NoError(t, err)
171171
}
172172

173+
func TestCommon_CreateDNSFilesForDebug(t *testing.T) {
174+
ctrl := gomock.NewController(t)
175+
defer ctrl.Finish()
176+
177+
for _, testCase := range []struct {
178+
name string
179+
resolvConfPath string
180+
}{
181+
{name: "al", resolvConfPath: "/etc"},
182+
{name: "br", resolvConfPath: "/run/netdog"},
183+
} {
184+
t.Run(testCase.name, func(t *testing.T) {
185+
netNSName := "netns-name"
186+
netNSPath := "/etc/netns/" + netNSName
187+
iface := getTestInterface()
188+
189+
netns := &tasknetworkconfig.NetworkNamespace{
190+
Name: netNSName,
191+
Path: netNSPath,
192+
NetworkInterfaces: []*networkinterface.NetworkInterface{iface},
193+
}
194+
195+
ioutil := mock_ioutilwrapper.NewMockIOUtil(ctrl)
196+
nsUtil := mock_ecscni.NewMockNetNSUtil(ctrl)
197+
osWrapper := mock_oswrapper.NewMockOS(ctrl)
198+
mockFile := mock_oswrapper.NewMockFile(ctrl)
199+
volumeAccessor := mock_volume.NewMockTaskVolumeAccessor(ctrl)
200+
commonPlatform := &common{
201+
ioutil: ioutil,
202+
nsUtil: nsUtil,
203+
os: osWrapper,
204+
dnsVolumeAccessor: volumeAccessor,
205+
resolvConfPath: testCase.resolvConfPath,
206+
}
207+
208+
hostnameData := fmt.Sprintf("%s\n", iface.GetHostname())
209+
210+
taskID := "taskID"
211+
gomock.InOrder(
212+
// Read hostname file.
213+
osWrapper.EXPECT().OpenFile("/etc/hostname", os.O_RDONLY|os.O_CREATE, fs.FileMode(0644)).Return(mockFile, nil).Times(1),
214+
mockFile.EXPECT().Close().Times(1),
215+
216+
// Creation of netns path.
217+
osWrapper.EXPECT().Stat(netNSPath).Return(nil, os.ErrNotExist).Times(1),
218+
osWrapper.EXPECT().IsNotExist(os.ErrNotExist).Return(true).Times(1),
219+
osWrapper.EXPECT().MkdirAll(netNSPath, fs.FileMode(0644)),
220+
221+
// Write hostname file.
222+
ioutil.EXPECT().WriteFile(netNSPath+"/hostname", []byte(hostnameData), fs.FileMode(0644)),
223+
224+
// Copy resolv.conf file.
225+
ioutil.EXPECT().ReadFile(testCase.resolvConfPath+"/resolv.conf"),
226+
ioutil.EXPECT().WriteFile(netNSPath+"/resolv.conf", gomock.Any(), gomock.Any()),
227+
228+
// Creation of hosts file.
229+
ioutil.EXPECT().ReadFile("/etc/hosts"),
230+
ioutil.EXPECT().WriteFile(netNSPath+"/hosts", gomock.Any(), gomock.Any()),
231+
232+
// CopyToVolume created files into task volume.
233+
volumeAccessor.EXPECT().CopyToVolume(taskID, netNSPath+"/hosts", "hosts", fs.FileMode(0644)).Return(nil).Times(1),
234+
volumeAccessor.EXPECT().CopyToVolume(taskID, netNSPath+"/resolv.conf", "resolv.conf", fs.FileMode(0644)).Return(nil).Times(1),
235+
volumeAccessor.EXPECT().CopyToVolume(taskID, netNSPath+"/hostname", "hostname", fs.FileMode(0644)).Return(nil).Times(1),
236+
)
237+
err := commonPlatform.createDNSConfig(taskID, true, netns)
238+
require.NoError(t, err)
239+
})
240+
}
241+
242+
}
243+
173244
func TestCommon_ConfigureInterface(t *testing.T) {
174245
t.Run("regular-eni", testRegularENIConfiguration)
175246
t.Run("branch-eni", testBranchENIConfiguration)

ecs-agent/netlib/platform/containerd_windows.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type containerdDebug struct {
5656

5757
// NewPlatform returns a platform instance with windows specific implementations of the API interface.
5858
func NewPlatform(
59-
platformString string,
59+
config Config,
6060
_ volume.TaskVolumeAccessor,
6161
_ string,
6262
net netwrapper.Net) (API, error) {
@@ -68,15 +68,15 @@ func NewPlatform(
6868
net: net,
6969
},
7070
}
71-
switch platformString {
71+
switch config.Name {
7272
case WarmpoolPlatform:
7373
return &c, nil
7474
case WarmpoolDebugPlatform:
7575
return &containerdDebug{
7676
containerd: c,
7777
}, nil
7878
default:
79-
return nil, errors.New("invalid platform string: " + platformString)
79+
return nil, errors.New("invalid platform string: " + config.Name)
8080
}
8181
return nil, nil
8282
}

0 commit comments

Comments
 (0)