Skip to content

Commit

Permalink
Add windows support in netlib (#4060)
Browse files Browse the repository at this point in the history
* Add windows support in netlib

Added a windows specific implementation of the platform interface.
  • Loading branch information
samjkon authored Dec 19, 2023
1 parent 13e1852 commit 63b8d9b
Show file tree
Hide file tree
Showing 12 changed files with 891 additions and 96 deletions.
119 changes: 119 additions & 0 deletions ecs-agent/netlib/model/ecscni/mocks_nsutil/nsutil_mocks_windows.go

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

95 changes: 95 additions & 0 deletions ecs-agent/netlib/network_builder_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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.

//go:build windows && unit
// +build windows,unit

package netlib

import (
"encoding/json"
"fmt"
"net"
"testing"

"github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs"
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/tasknetworkconfig"
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/platform"
mock_netwrapper "github.com/aws/amazon-ecs-agent/ecs-agent/utils/netwrapper/mocks"

"github.com/aws/aws-sdk-go/aws"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)

func TestNetworkBuilder_BuildTaskNetworkConfiguration(t *testing.T) {
t.Run("containerd-default", getTestFunc(getSingleNetNSAWSVPCTestData, platform.WarmpoolPlatform))
}

// getTestFunc returns a test function that verifies the capability of the networkBuilder
// to translate a given input task payload into desired network data models.
func getTestFunc(
dataGenF func(string) (input *ecsacs.Task, expected tasknetworkconfig.TaskNetworkConfig),
plt string,
) func(*testing.T) {

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

// Create a networkBuilder for the warmpool platform.
mockNet := mock_netwrapper.NewMockNet(ctrl)
platformAPI, err := platform.NewPlatform(plt, nil, "", mockNet)
require.NoError(t, err)
netBuilder := &networkBuilder{
platformAPI: platformAPI,
}

// Generate input task payload and a reference to verify the output with.
taskPayload, expectedConfig := dataGenF(taskID)

// The agent expects the regular ENI to be present on the host.
// We should mock the net.Interfaces() method to return a list of interfaces
// on the host accordingly.
var ifaces []net.Interface
idx := 1
for _, eni := range taskPayload.ElasticNetworkInterfaces {
mac := aws.StringValue(eni.MacAddress)
hw, err := net.ParseMAC(mac)
require.NoError(t, err)
ifaces = append(ifaces, net.Interface{
HardwareAddr: hw,
Name: fmt.Sprintf("eth%d", idx),
})
idx += 1
}
mockNet.EXPECT().Interfaces().Return(ifaces, nil).Times(1)

// Invoke networkBuilder function for building the task network config.
actualConfig, err := netBuilder.BuildTaskNetworkConfiguration(taskID, taskPayload)
require.NoError(t, err)

// NetNS path on windows is auto generated in place. Hence, we exclude it from verification.
for i := 0; i < len(actualConfig.NetworkNamespaces); i++ {
expectedConfig.NetworkNamespaces[i].Path = actualConfig.NetworkNamespaces[i].Path
}
// Convert the obtained output and the reference data into json data to make it
// easier to compare.
expected, err := json.Marshal(expectedConfig)
require.NoError(t, err)
actual, err := json.Marshal(actualConfig)
require.NoError(t, err)

require.Equal(t, string(expected), string(actual))
}
}
6 changes: 0 additions & 6 deletions ecs-agent/netlib/platform/cniconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

package platform

import "time"

const (
cniSpecVersion = "0.3.0"
blockInstanceMetadataDefault = true
Expand All @@ -26,8 +24,4 @@ const (
CNIPluginLogFileEnv = "ECS_CNI_LOG_FILE"
VPCCNIPluginLogFileEnv = "VPC_CNI_LOG_FILE"
IPAMDataPathEnv = "IPAM_DB_PATH"

// Timeout duration for each network setup and cleanup operation before it is cancelled.
nsSetupTimeoutDuration = 1 * time.Minute
nsCleanupTimeoutDuration = 30 * time.Second
)
4 changes: 4 additions & 0 deletions ecs-agent/netlib/platform/cniconf_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ const (
BridgeInterfaceName = "fargate-bridge"

IPAMDataFileName = "eni-ipam.db"

// Timeout duration for each network setup and cleanup operation before it is cancelled.
nsSetupTimeoutDuration = 1 * time.Minute
nsCleanupTimeoutDuration = 30 * time.Second
)

// createENIPluginConfigs constructs the configuration object for eni plugin
Expand Down
15 changes: 6 additions & 9 deletions ecs-agent/netlib/platform/cniconf_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ import (
)

const (
netNSPath = "ns1"
ipAddress = "169.254.0.1"
eniID = "eni-abe4d"
eniMAC = "f0:5c:89:a3:ab:01"
subnetGatewayCIDR = "10.1.0.1/24"
deviceName = "eth1"
vni = "ABC123"
destinationIP = "10.0.3.1"
destinationPort = 6081
netNSPath = "ns1"
ipAddress = "169.254.0.1"
eniID = "eni-abe4d"
vni = "ABC123"
destinationIP = "10.0.3.1"
destinationPort = 6081
)

func TestCreateBridgeConfig(t *testing.T) {
Expand Down
Loading

0 comments on commit 63b8d9b

Please sign in to comment.