Skip to content

Commit

Permalink
Incorporating getting default network interface on host mode in task …
Browse files Browse the repository at this point in the history
…metadata
  • Loading branch information
mye956 committed Sep 24, 2024
1 parent 1c09ac7 commit 6998817
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 7 deletions.
29 changes: 22 additions & 7 deletions agent/handlers/v4/tmdsstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/field"
tmdsv4 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state"
"github.com/aws/amazon-ecs-agent/ecs-agent/tmds/utils/netconfig"
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/netlinkwrapper"
)

const (
Expand Down Expand Up @@ -91,21 +93,21 @@ func (s *TMDSAgentState) GetContainerMetadata(v3EndpointID string) (tmdsv4.Conta

// Returns task metadata in v4 format for the task identified by the provided endpointContainerID.
func (s *TMDSAgentState) GetTaskMetadata(v3EndpointID string) (tmdsv4.TaskResponse, error) {
return s.getTaskMetadata(v3EndpointID, false, false)
return s.getTaskMetadata(v3EndpointID, false, false, nil)
}

// Returns task metadata including task and container instance tags in v4 format for the
// task identified by the provided endpointContainerID.
func (s *TMDSAgentState) GetTaskMetadataWithTags(v3EndpointID string) (tmdsv4.TaskResponse, error) {
return s.getTaskMetadata(v3EndpointID, true, false)
return s.getTaskMetadata(v3EndpointID, true, false, nil)
}

func (s *TMDSAgentState) GetTaskMetadataWithTaskNetworkConfig(v3EndpointID string) (tmdsv4.TaskResponse, error) {
return s.getTaskMetadata(v3EndpointID, false, true)
func (s *TMDSAgentState) GetTaskMetadataWithTaskNetworkConfig(v3EndpointID string, netlinkClient netlinkwrapper.NetLink) (tmdsv4.TaskResponse, error) {
return s.getTaskMetadata(v3EndpointID, false, true, netlinkClient)
}

// Returns task metadata in v4 format for the task identified by the provided endpointContainerID.
func (s *TMDSAgentState) getTaskMetadata(v3EndpointID string, includeTags, includeTaskNetworkConfig bool) (tmdsv4.TaskResponse, error) {
func (s *TMDSAgentState) getTaskMetadata(v3EndpointID string, includeTags, includeTaskNetworkConfig bool, netlinkClient netlinkwrapper.NetLink) (tmdsv4.TaskResponse, error) {
taskARN, ok := s.state.TaskARNByV3EndpointID(v3EndpointID)
if !ok {
return tmdsv4.TaskResponse{}, tmdsv4.NewErrorLookupFailure(fmt.Sprintf(
Expand Down Expand Up @@ -163,10 +165,23 @@ func (s *TMDSAgentState) getTaskMetadata(v3EndpointID string, includeTags, inclu
taskResponse.FaultInjectionEnabled = task.IsFaultInjectionEnabled()
var taskNetworkConfig *tmdsv4.TaskNetworkConfig
if task.IsNetworkModeHost() {
deviceName := ""
if netlinkClient != nil {
deviceName, err = netconfig.DefaultNetInterfaceName(netlinkClient)
if err != nil {
logger.Warn("Unable to obtain default network interface name on host for task.", logger.Fields{
field.TaskARN: taskARN,
field.Error: err,
})
}
logger.Info("Obtained default network interface name on host for task", logger.Fields{
field.TaskARN: taskARN,
"defaultInterfaceName": deviceName,
})
}
// For host most, we don't really need the network namespace in order to do anything within the host instance network namespace
// and so we will set this to an arbitrary value such as "host".
// TODO: Will need to find/obtain the interface name of the default network interface on the host instance
taskNetworkConfig = tmdsv4.NewTaskNetworkConfig(task.GetNetworkMode(), defaultHostNetworkNamespace, task.GetDefaultIfname())
taskNetworkConfig = tmdsv4.NewTaskNetworkConfig(task.GetNetworkMode(), defaultHostNetworkNamespace, deviceName)
} else {
taskNetworkConfig = tmdsv4.NewTaskNetworkConfig(task.GetNetworkMode(), task.GetNetworkNamespace(), task.GetDefaultIfname())
}
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.

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.

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

2 changes: 2 additions & 0 deletions agent/vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4
github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state
github.com/aws/amazon-ecs-agent/ecs-agent/tmds/logging
github.com/aws/amazon-ecs-agent/ecs-agent/tmds/utils/mux
github.com/aws/amazon-ecs-agent/ecs-agent/tmds/utils/netconfig
github.com/aws/amazon-ecs-agent/ecs-agent/utils
github.com/aws/amazon-ecs-agent/ecs-agent/utils/arn
github.com/aws/amazon-ecs-agent/ecs-agent/utils/cipher
github.com/aws/amazon-ecs-agent/ecs-agent/utils/execwrapper
github.com/aws/amazon-ecs-agent/ecs-agent/utils/execwrapper/mocks
github.com/aws/amazon-ecs-agent/ecs-agent/utils/httpproxy
github.com/aws/amazon-ecs-agent/ecs-agent/utils/netlinkwrapper
github.com/aws/amazon-ecs-agent/ecs-agent/utils/retry
github.com/aws/amazon-ecs-agent/ecs-agent/utils/retry/mock
github.com/aws/amazon-ecs-agent/ecs-agent/utils/ttime
Expand Down
3 changes: 3 additions & 0 deletions ecs-agent/tmds/handlers/fault/v1/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
v4 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4"
state "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state"
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/execwrapper"
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/netlinkwrapper"
"github.com/aws/aws-sdk-go/aws"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -68,6 +69,7 @@ type FaultHandler struct {
AgentState state.AgentState
MetricsFactory metrics.EntryFactory
osExecWrapper execwrapper.Exec
netlinkClient netlinkwrapper.NetLink
}

func New(agentState state.AgentState, mf metrics.EntryFactory, execWrapper execwrapper.Exec) *FaultHandler {
Expand All @@ -76,6 +78,7 @@ func New(agentState state.AgentState, mf metrics.EntryFactory, execWrapper execw
MetricsFactory: mf,
mutexMap: sync.Map{},
osExecWrapper: execWrapper,
netlinkClient: netlinkwrapper.New(),
}
}

Expand Down

0 comments on commit 6998817

Please sign in to comment.