Skip to content

Commit

Permalink
Adding new error type for default network interface not found
Browse files Browse the repository at this point in the history
  • Loading branch information
mye956 committed Sep 30, 2024
1 parent 38e02f0 commit 29f91e1
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 3 deletions.
50 changes: 49 additions & 1 deletion agent/handlers/task_server_setup_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package handlers

import (
"errors"
"fmt"
"net"
"testing"

Expand All @@ -34,13 +36,20 @@ import (
"github.com/vishvananda/netlink"
)

const (
internalError = "internal error"
defaultNetworkInterfaceNameErrorMessage = "unable to obtain default network interface name: %s"
)

func TestV4GetTaskMetadataWithTaskNetworkConfig(t *testing.T) {

tcs := []struct {
name string
setStateExpectations func(state *mock_dockerstate.MockTaskEngineState)
setNetLinkExpectations func(netLink *mock_netlinkwrapper.MockNetLink)
expectedTaskNetworkConfig *v4.TaskNetworkConfig
shouldError bool
errorMessage string
}{
{
name: "happy case with awsvpc mode",
Expand Down Expand Up @@ -112,6 +121,38 @@ func TestV4GetTaskMetadataWithTaskNetworkConfig(t *testing.T) {
},
expectedTaskNetworkConfig: expectedV4TaskNetworkConfig(true, bridgeMode, "", ""),
},
{
name: "unhappy case with host mode",
setStateExpectations: func(state *mock_dockerstate.MockTaskEngineState) {
hostTask := standardHostTask()
hostTask.EnableFaultInjection = true
hostTask.NetworkNamespace = networkNamespace
hostTask.DefaultIfname = defaultIfname
gomock.InOrder(
state.EXPECT().TaskARNByV3EndpointID(v3EndpointID).Return(taskARN, true),
state.EXPECT().TaskByArn(taskARN).Return(hostTask, true).Times(2),
state.EXPECT().ContainerMapByArn(taskARN).Return(containerNameToDockerContainer, true),
state.EXPECT().ContainerByID(containerID).Return(nil, false).AnyTimes(),
state.EXPECT().PulledContainerMapByArn(taskARN).Return(nil, true),
state.EXPECT().ContainerByID(containerID).Return(nil, false).AnyTimes(),
)
},
setNetLinkExpectations: func(netLink *mock_netlinkwrapper.MockNetLink) {
routes := []netlink.Route{
netlink.Route{
Gw: net.ParseIP("10.194.20.1"),
Dst: nil,
LinkIndex: 0,
},
}
gomock.InOrder(
netLink.EXPECT().RouteList(nil, netlink.FAMILY_ALL).Return(routes, errors.New(internalError)).Times(1),
)
},
expectedTaskNetworkConfig: expectedV4TaskNetworkConfig(true, apitask.HostNetworkMode, hostNetworkNamespace, ""),
shouldError: true,
errorMessage: fmt.Sprintf(defaultNetworkInterfaceNameErrorMessage, internalError),
},
}

for _, tc := range tcs {
Expand All @@ -138,7 +179,14 @@ func TestV4GetTaskMetadataWithTaskNetworkConfig(t *testing.T) {

actualTaskResponse, err := tmdsAgentState.GetTaskMetadataWithTaskNetworkConfig(v3EndpointID, netConfigClient)

assert.NoError(t, err)
if tc.shouldError {
var errDefaultNetworkInterfaceName *v4.ErrorDefaultNetworkInterfaceName
assert.Error(t, err)
assert.ErrorAs(t, err, &errDefaultNetworkInterfaceName)
assert.Equal(t, err.Error(), tc.errorMessage)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tc.expectedTaskNetworkConfig, actualTaskResponse.TaskNetworkConfig)
})
}
Expand Down
5 changes: 3 additions & 2 deletions agent/handlers/v4/tmdsstate_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ func (s *TMDSAgentState) GetTaskMetadataWithTaskNetworkConfig(v3EndpointID strin
taskResponse, err := s.getTaskMetadata(v3EndpointID, false, true)
if err == nil {
if taskResponse.TaskNetworkConfig != nil && taskResponse.TaskNetworkConfig.NetworkMode == "host" {
hostDeviceName, err := netconfig.DefaultNetInterfaceName(networkConfigClient.NetlinkClient)
if err != nil {
hostDeviceName, netErr := netconfig.DefaultNetInterfaceName(networkConfigClient.NetlinkClient)
if netErr != nil {
err = tmdsv4.NewErrorDefaultNetworkInterfaceName(netErr.Error())
logger.Warn("Unable to obtain default network interface on host", logger.Fields{
field.TaskARN: taskResponse.TaskARN,
field.Error: err,
Expand Down

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

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

5 changes: 5 additions & 0 deletions ecs-agent/tmds/handlers/fault/v1/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,11 @@ func getTaskMetadataErrorResponse(endpointContainerID, requestType string, err e
return http.StatusInternalServerError, fmt.Errorf("unable to obtain container metadata for container: %s", endpointContainerID)
}

var errDefaultNetworkInterfaceName *state.ErrorDefaultNetworkInterfaceName
if errors.As(err, &errDefaultNetworkInterfaceName) {
return http.StatusInternalServerError, fmt.Errorf("unable to obtain default network interface name on host for container: %s", endpointContainerID)
}

logger.Error("Unknown error encountered when handling task metadata fetch failure", logger.Fields{
field.Error: err,
field.RequestType: requestType,
Expand Down
17 changes: 17 additions & 0 deletions ecs-agent/tmds/handlers/v4/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ func (e *ErrorStatsFetchFailure) Unwrap() error {
return e.cause
}

// Error to be returned when we're unable to obtain the default network interface name on the host namespace for a task
type ErrorDefaultNetworkInterfaceName struct {
externalReason string // Reason to be returned in TMDS response
}

func NewErrorDefaultNetworkInterfaceName(externalReason string) *ErrorDefaultNetworkInterfaceName {
return &ErrorDefaultNetworkInterfaceName{externalReason: externalReason}
}

func (e *ErrorDefaultNetworkInterfaceName) ExternalReason() string {
return e.externalReason
}

func (e *ErrorDefaultNetworkInterfaceName) Error() string {
return fmt.Sprintf("unable to obtain default network interface name: %s", e.externalReason)
}

// Interface for interacting with Agent State relevant to TMDS
type AgentState interface {
// Returns container metadata in v4 format for the container identified by the
Expand Down

0 comments on commit 29f91e1

Please sign in to comment.