Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VpcId to TMDE Task Responses #3288

Merged
merged 7 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions agent/app/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,24 @@ func (agent *ecsAgent) doStart(containerChangeEventStream *eventstream.EventStre
}
return exitcodes.ExitError
}
} else if !agent.cfg.External.Enabled() {
// Set VPC and Subnet IDs for the EC2 instance
err, terminal := agent.setVPCSubnet()
switch err {
case nil:
// No error so do nothing
case instanceNotLaunchedInVPCError:
// We have ascertained that the EC2 Instance is not running in a VPC
// No need to stop the ECS Agent in this case
logger.Info("Unable to detect VPC ID for the instance as it was not launched in VPC mode.")
default:
// Encountered an error initializing VPC ID and Subnet
seelog.Criticalf("Unable to detect VPC ID and Subnet: %v", err)
if terminal {
return exitcodes.ExitTerminal
}
return exitcodes.ExitError
}
}

// Register the container instance
Expand Down Expand Up @@ -813,9 +831,9 @@ func (agent *ecsAgent) startAsyncRoutines(
// Start serving the endpoint to fetch IAM Role credentials and other task metadata
if agent.cfg.TaskMetadataAZDisabled {
// send empty availability zone
go handlers.ServeTaskHTTPEndpoint(agent.ctx, credentialsManager, state, client, agent.containerInstanceARN, agent.cfg, statsEngine, "")
go handlers.ServeTaskHTTPEndpoint(agent.ctx, credentialsManager, state, client, agent.containerInstanceARN, agent.cfg, statsEngine, "", agent.vpc)
} else {
go handlers.ServeTaskHTTPEndpoint(agent.ctx, credentialsManager, state, client, agent.containerInstanceARN, agent.cfg, statsEngine, agent.availabilityZone)
go handlers.ServeTaskHTTPEndpoint(agent.ctx, credentialsManager, state, client, agent.containerInstanceARN, agent.cfg, statsEngine, agent.availabilityZone, agent.vpc)
}

// Start sending events to the backend
Expand Down
31 changes: 27 additions & 4 deletions agent/app/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ func TestDoStartRegisterContainerInstanceErrorTerminal(t *testing.T) {

mockPauseLoader.EXPECT().IsLoaded(gomock.Any()).Return(false, nil).AnyTimes()
mockPauseLoader.EXPECT().LoadImage(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockEC2Metadata.EXPECT().PrimaryENIMAC().Return("mac", nil)
mockEC2Metadata.EXPECT().VPCID(gomock.Eq("mac")).Return("vpc-id", nil)
mockEC2Metadata.EXPECT().SubnetID(gomock.Eq("mac")).Return("subnet-id", nil)
gomock.InOrder(
dockerClient.EXPECT().SupportedVersions().Return(apiVersions),
mockCredentialsProvider.EXPECT().Retrieve().Return(aws_credentials.Value{}, nil),
Expand Down Expand Up @@ -261,6 +264,9 @@ func TestDoStartRegisterContainerInstanceErrorNonTerminal(t *testing.T) {

mockPauseLoader.EXPECT().IsLoaded(gomock.Any()).Return(false, nil).AnyTimes()
mockPauseLoader.EXPECT().LoadImage(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockEC2Metadata.EXPECT().PrimaryENIMAC().Return("mac", nil)
mockEC2Metadata.EXPECT().VPCID(gomock.Eq("mac")).Return("vpc-id", nil)
mockEC2Metadata.EXPECT().SubnetID(gomock.Eq("mac")).Return("subnet-id", nil)
gomock.InOrder(
dockerClient.EXPECT().SupportedVersions().Return(apiVersions),
mockCredentialsProvider.EXPECT().Retrieve().Return(aws_credentials.Value{}, nil),
Expand Down Expand Up @@ -338,18 +344,22 @@ func TestDoStartWarmPoolsError(t *testing.T) {
}

func TestDoStartHappyPath(t *testing.T) {
testDoStartHappyPathWithConditions(t, false, false)
testDoStartHappyPathWithConditions(t, false, false, false)
}

func TestDoStartWarmPoolsEnabled(t *testing.T) {
testDoStartHappyPathWithConditions(t, false, true)
testDoStartHappyPathWithConditions(t, false, true, false)
}

func TestDoStartWarmPoolsBlackholed(t *testing.T) {
testDoStartHappyPathWithConditions(t, true, true)
testDoStartHappyPathWithConditions(t, true, true, false)
}

func testDoStartHappyPathWithConditions(t *testing.T, blackholed bool, warmPoolsEnv bool) {
func TestDoStartHappyPathExternal(t *testing.T) {
testDoStartHappyPathWithConditions(t, false, false, true)
}

func testDoStartHappyPathWithConditions(t *testing.T, blackholed bool, warmPoolsEnv bool, isExternalLaunchType bool) {
ctrl, credentialsManager, _, imageManager, client,
dockerClient, stateManagerFactory, saveableOptionFactory, execCmdMgr := setup(t)
defer ctrl.Finish()
Expand All @@ -363,6 +373,13 @@ func testDoStartHappyPathWithConditions(t *testing.T, blackholed bool, warmPools
ec2MetadataClient.EXPECT().PublicIPv4Address().Return(hostPublicIPv4Address, nil)
ec2MetadataClient.EXPECT().OutpostARN().Return("", nil)

if !isExternalLaunchType {
singholt marked this conversation as resolved.
Show resolved Hide resolved
// VPC and Subnet should not be initizalied for external launch type
ec2MetadataClient.EXPECT().PrimaryENIMAC().Return("mac", nil)
ec2MetadataClient.EXPECT().VPCID(gomock.Eq("mac")).Return("vpc-id", nil)
ec2MetadataClient.EXPECT().SubnetID(gomock.Eq("mac")).Return("subnet-id", nil)
}

if blackholed {
if warmPoolsEnv {
ec2MetadataClient.EXPECT().TargetLifecycleState().Return("", errors.New("blackholed")).Times(targetLifecycleMaxRetryCount)
Expand Down Expand Up @@ -425,6 +442,9 @@ func testDoStartHappyPathWithConditions(t *testing.T, blackholed bool, warmPools
if warmPoolsEnv {
cfg.WarmPoolsSupport = config.BooleanDefaultFalse{Value: config.ExplicitlyEnabled}
}
if isExternalLaunchType {
cfg.External = config.BooleanDefaultFalse{Value: config.ExplicitlyEnabled}
}
cfg.Cluster = clusterName
ctx, cancel := context.WithCancel(context.TODO())

Expand Down Expand Up @@ -1250,6 +1270,9 @@ func TestRegisterContainerInstanceInvalidParameterTerminalError(t *testing.T) {

mockPauseLoader.EXPECT().IsLoaded(gomock.Any()).Return(false, nil).AnyTimes()
mockPauseLoader.EXPECT().LoadImage(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockEC2Metadata.EXPECT().PrimaryENIMAC().Return("mac", nil)
mockEC2Metadata.EXPECT().VPCID(gomock.Eq("mac")).Return("vpc-id", nil)
mockEC2Metadata.EXPECT().SubnetID(gomock.Eq("mac")).Return("subnet-id", nil)
gomock.InOrder(
dockerClient.EXPECT().SupportedVersions().Return(apiVersions),
mockCredentialsProvider.EXPECT().Retrieve().Return(aws_credentials.Value{}, nil),
Expand Down
6 changes: 6 additions & 0 deletions agent/app/agent_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ func TestDoStartCgroupInitHappyPath(t *testing.T) {
dockerClient.EXPECT().SupportedVersions().Return(apiVersions)
imageManager.EXPECT().StartImageCleanupProcess(gomock.Any()).MaxTimes(1)
mockCredentialsProvider.EXPECT().IsExpired().Return(false).AnyTimes()
ec2MetadataClient.EXPECT().PrimaryENIMAC().Return("mac", nil)
ec2MetadataClient.EXPECT().VPCID(gomock.Eq("mac")).Return("vpc-id", nil)
ec2MetadataClient.EXPECT().SubnetID(gomock.Eq("mac")).Return("subnet-id", nil)
ec2MetadataClient.EXPECT().OutpostARN().Return("", nil)
mockPauseLoader.EXPECT().LoadImage(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockPauseLoader.EXPECT().IsLoaded(gomock.Any()).Return(true, nil).AnyTimes()
Expand Down Expand Up @@ -582,6 +585,9 @@ func TestDoStartGPUManagerHappyPath(t *testing.T) {
dockerClient.EXPECT().SupportedVersions().Return(apiVersions)
imageManager.EXPECT().StartImageCleanupProcess(gomock.Any()).MaxTimes(1)
mockCredentialsProvider.EXPECT().IsExpired().Return(false).AnyTimes()
ec2MetadataClient.EXPECT().PrimaryENIMAC().Return("mac", nil)
ec2MetadataClient.EXPECT().VPCID(gomock.Eq("mac")).Return("vpc-id", nil)
ec2MetadataClient.EXPECT().SubnetID(gomock.Eq("mac")).Return("subnet-id", nil)
ec2MetadataClient.EXPECT().OutpostARN().Return("", nil)
mockPauseLoader.EXPECT().LoadImage(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockPauseLoader.EXPECT().IsLoaded(gomock.Any()).Return(true, nil).AnyTimes()
Expand Down
13 changes: 8 additions & 5 deletions agent/handlers/task_server_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func taskServerSetup(credentialsManager credentials.Manager,
steadyStateRate int,
burstRate int,
availabilityZone string,
vpcID string,
containerInstanceArn string) *http.Server {
muxRouter := mux.NewRouter()

Expand All @@ -69,7 +70,7 @@ func taskServerSetup(credentialsManager credentials.Manager,

v3HandlersSetup(muxRouter, state, ecsClient, statsEngine, cluster, availabilityZone, containerInstanceArn)

v4HandlersSetup(muxRouter, state, ecsClient, statsEngine, cluster, availabilityZone, containerInstanceArn)
v4HandlersSetup(muxRouter, state, ecsClient, statsEngine, cluster, availabilityZone, vpcID, containerInstanceArn)

limiter := tollbooth.NewLimiter(int64(steadyStateRate), nil)
limiter.SetOnLimitReached(handlersutils.LimitReachedHandler(auditLogger))
Expand Down Expand Up @@ -141,10 +142,11 @@ func v4HandlersSetup(muxRouter *mux.Router,
statsEngine stats.Engine,
cluster string,
availabilityZone string,
vpcID string,
containerInstanceArn string) {
muxRouter.HandleFunc(v4.ContainerMetadataPath, v4.ContainerMetadataHandler(state))
muxRouter.HandleFunc(v4.TaskMetadataPath, v4.TaskMetadataHandler(state, ecsClient, cluster, availabilityZone, containerInstanceArn, false))
muxRouter.HandleFunc(v4.TaskWithTagsMetadataPath, v4.TaskMetadataHandler(state, ecsClient, cluster, availabilityZone, containerInstanceArn, true))
muxRouter.HandleFunc(v4.TaskMetadataPath, v4.TaskMetadataHandler(state, ecsClient, cluster, availabilityZone, vpcID, containerInstanceArn, false))
muxRouter.HandleFunc(v4.TaskWithTagsMetadataPath, v4.TaskMetadataHandler(state, ecsClient, cluster, availabilityZone, vpcID, containerInstanceArn, true))
muxRouter.HandleFunc(v4.ContainerStatsPath, v4.ContainerStatsHandler(state, statsEngine))
muxRouter.HandleFunc(v4.TaskStatsPath, v4.TaskStatsHandler(state, statsEngine))
muxRouter.HandleFunc(v4.ContainerAssociationsPath, v4.ContainerAssociationsHandler(state))
Expand All @@ -162,7 +164,8 @@ func ServeTaskHTTPEndpoint(
containerInstanceArn string,
cfg *config.Config,
statsEngine stats.Engine,
availabilityZone string) {
availabilityZone string,
vpcID string) {
// Create and initialize the audit log
logger, err := seelog.LoggerFromConfigAsString(audit.AuditLoggerConfig(cfg))
if err != nil {
Expand All @@ -174,7 +177,7 @@ func ServeTaskHTTPEndpoint(
auditLogger := audit.NewAuditLog(containerInstanceArn, cfg, logger)

server := taskServerSetup(credentialsManager, auditLogger, state, ecsClient, cfg.Cluster, statsEngine,
cfg.TaskMetadataSteadyStateRate, cfg.TaskMetadataBurstRate, availabilityZone, containerInstanceArn)
cfg.TaskMetadataSteadyStateRate, cfg.TaskMetadataBurstRate, availabilityZone, vpcID, containerInstanceArn)

go func() {
<-ctx.Done()
Expand Down
Loading