-
Notifications
You must be signed in to change notification settings - Fork 618
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
AvailabilityZone show on taskmetadata endpoint #1674
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my understanding, why do we update both v2 and v3 metadata handlers?
agent/api/ecsclient/client.go
Outdated
if err == nil { | ||
return containerInstanceArn, nil | ||
return containerInstanceArn,availabilityzone, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add space
agent/api/ecsclient/client.go
Outdated
} | ||
// If trying to register fails, try to create the cluster before calling | ||
// register again | ||
clusterRef, err = client.CreateCluster(clusterRef) | ||
if err != nil { | ||
return "", err | ||
return "","", err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add space
Same for below
agent/api/ecsclient/client.go
Outdated
var availabilityzone_name = "ecs.availability-zone" | ||
var availabilityzone = "" | ||
attrs := resp.ContainerInstance.Attributes | ||
if(resp != nil){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parentheses around if conditional not needed in Golang
agent/api/ecsclient/client.go
Outdated
return "","", err | ||
} | ||
|
||
var availabilityzone_name = "ecs.availability-zone" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mixed caps can be used: availabilityZoneName
https://golang.org/doc/effective_go.html#mixed-caps
Same for other variables below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, make this a const and put it at the top of the file
agent/api/ecsclient/client_test.go
Outdated
if err != nil { | ||
t.Errorf("Should not be an error: %v", err) | ||
} | ||
if arn != "registerArn" { | ||
t.Errorf("Wrong arn: %v", arn) | ||
} | ||
if availabilityzone != "us-west-2b"{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use assert.Equal instead of using an if statement to report the error like you do in TestRegisterContainerInstance +
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 This test uses a different style of test assertions -- you can migrate it if you want :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, will refactor the test style.
agent/app/agent_test.go
Outdated
@@ -628,7 +647,7 @@ func TestReregisterContainerInstanceInstanceTypeChanged(t *testing.T) { | |||
mockDockerClient.EXPECT().ListPluginsWithFilters(gomock.Any(), gomock.Any(), | |||
gomock.Any(), gomock.Any()).AnyTimes().Return([]string{}, nil), | |||
client.EXPECT().RegisterContainerInstance(containerInstanceARN, gomock.Any(), gomock.Any()).Return( | |||
"", awserr.New("", apierrors.InstanceTypeChangedErrorMessage, errors.New(""))), | |||
"", "",awserr.New("", apierrors.InstanceTypeChangedErrorMessage, errors.New(""))), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add space
@@ -184,7 +185,7 @@ func verifyTaskMetadataResponse(taskMetadataRawMsg json.RawMessage) error { | |||
"KnownStatus": "RUNNING", | |||
} | |||
|
|||
taskExpectedFieldNotEmptyArray := []string{"TaskARN", "Family", "Revision", "PullStartedAt", "PullStoppedAt", "Containers"} | |||
taskExpectedFieldNotEmptyArray := []string{"TaskARN", "Family", "Revision", "PullStartedAt", "PullStoppedAt", "Containers","AvailabilityZone"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add space
@linkar-ec2 v3 handler shares the same task metadata response format with v2 handler, so to keep consistent, we add availabilityZone to both places. |
@@ -41,6 +41,7 @@ type TaskResponse struct { | |||
PullStartedAt *time.Time `json:"PullStartedAt,omitempty"` | |||
PullStoppedAt *time.Time `json:"PullStoppedAt,omitempty"` | |||
ExecutionStoppedAt *time.Time `json:"ExecutionStoppedAt,omitempty"` | |||
//AvailabilityZone string `json:"AvailabilityZone"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this commented out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will update it, haven't add it to windows yet.
agent/api/ecsclient/client.go
Outdated
return "","", err | ||
} | ||
|
||
var availabilityzone_name = "ecs.availability-zone" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, make this a const and put it at the top of the file
agent/app/agent_test.go
Outdated
@@ -103,10 +104,11 @@ func TestDoStartMinimumSupportedDockerVersionTerminal(t *testing.T) { | |||
stateManagerFactory: stateManagerFactory, | |||
saveableOptionFactory: saveableOptionFactory, | |||
} | |||
|
|||
assert.Equal(t, "", agent.availabilityzone) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this here. The test isn't doing anything AZ-specific
agent/app/agent_test.go
Outdated
exitCode := agent.doStart(eventstream.NewEventStream("events", ctx), | ||
credentialsManager, state, imageManager, client) | ||
assert.Equal(t, exitcodes.ExitTerminal, exitCode) | ||
assert.Equal(t,"", agent.availabilityzone) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this here either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will remove it.
@@ -41,6 +41,7 @@ type TaskResponse struct { | |||
PullStartedAt *time.Time `json:"PullStartedAt,omitempty"` | |||
PullStoppedAt *time.Time `json:"PullStoppedAt,omitempty"` | |||
ExecutionStoppedAt *time.Time `json:"ExecutionStoppedAt,omitempty"` | |||
//AvailabilityZone string `json:"AvailabilityZone"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this commented out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will update it, haven't add it to windows yet.
agent/app/agent_test.go
Outdated
@@ -763,10 +785,11 @@ func TestRegisterContainerInstanceWhenContainerInstanceARNIsNotSetHappyPath(t *t | |||
credentialProvider: aws_credentials.NewCredentials(mockCredentialsProvider), | |||
mobyPlugins: mockMobyPlugins, | |||
} | |||
|
|||
assert.Equal(t, "", agent.availabilityzone) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to check for emptiness before running the test.
agent/api/ecsclient/client.go
Outdated
seelog.Info("Registered container instance with cluster!") | ||
err = validateRegisteredAttributes(registerRequest.Attributes, resp.ContainerInstance.Attributes) | ||
return aws.StringValue(resp.ContainerInstance.ContainerInstanceArn), err | ||
return aws.StringValue(resp.ContainerInstance.ContainerInstanceArn), availabilityzone,err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: space, this happens a lot, have you configured gofmt
on your editor? It will automatically adjust spaces.
if(resp != nil){ | ||
for _, attr := range attrs { | ||
if aws.StringValue(attr.Name) == availabilityzone_name { | ||
availabilityzone = aws.StringValue(attr.Value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add a break here
@@ -88,6 +90,7 @@ func NewTaskResponse(taskARN string, | |||
Revision: task.Version, | |||
DesiredStatus: task.GetDesiredStatus().String(), | |||
KnownStatus: task.GetKnownStatus().String(), | |||
AvailabilityZone: az, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please gofmt
this
previousTaskEngine := engine.NewTaskEngine(agent.cfg, agent.dockerClient, | ||
credentialsManager, containerChangeEventStream, imageManager, state, | ||
agent.metadataManager, agent.resourceFields) | ||
|
||
// previousStateManager is used to verify that our current runtime configuration is | ||
// compatible with our past configuration as reflected by our state-file | ||
previousStateManager, err := agent.newStateManager(previousTaskEngine, &previousCluster, | ||
&previousContainerInstanceArn, &previousEC2InstanceID) | ||
&previousContainerInstanceArn, &previousEC2InstanceID, &previousAZ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need to bump ECSDataVersion
?
agent/api/ecsclient/client.go
Outdated
var availabilityzone = "" | ||
attrs := resp.ContainerInstance.Attributes | ||
if resp != nil { | ||
for _, attr := range attrs { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
this can be for _, attr := range resp.ContainerInstance.Attributes {
agent/app/agent.go
Outdated
@@ -427,6 +429,7 @@ func (agent *ecsAgent) newStateManager( | |||
agent.saveableOptionFactory.AddSaveable("Cluster", cluster), | |||
// This is for making testing easier as we can mock this | |||
agent.saveableOptionFactory.AddSaveable("EC2InstanceID", savedInstanceID), | |||
agent.saveableOptionFactory.AddSaveable("availabilityzone", availabilityzone), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: AvailabilityZone
should be the key
agent/app/agent.go
Outdated
@@ -107,6 +107,7 @@ type ecsAgent struct { | |||
terminationHandler sighandlers.TerminationHandler | |||
mobyPlugins mobypkgwrapper.Plugins | |||
resourceFields *taskresource.ResourceFields | |||
availabilityzone string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: availabilityZone
@@ -36,6 +36,7 @@ type TaskResponse struct { | |||
Revision string `json:"Revision"` | |||
DesiredStatus string `json:"DesiredStatus,omitempty"` | |||
KnownStatus string `json:"KnownStatus"` | |||
AvailabilityZone string `json:"AvailabilityZone"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe similar changes are needed in this file.
agent/api/ecsclient/client.go
Outdated
@@ -41,6 +41,7 @@ const ( | |||
pollEndpointCacheSize = 1 | |||
pollEndpointCacheTTL = 20 * time.Minute | |||
roundtripTimeout = 5 * time.Second | |||
availabilityZoneName = "ecs.availability-zone" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about naming this azAttrName
?
agent/api/task/task.go
Outdated
@@ -94,6 +94,8 @@ type TaskOverrides struct{} | |||
type Task struct { | |||
// Arn is the unique identifier for the task | |||
Arn string | |||
//AvailabilityZone is the zone for task locates in | |||
AvailabilityZone string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this needed? as I understand, we use agent's availabilityZone field for v2/v3 responses, right?
@@ -70,7 +70,8 @@ const ( | |||
// 17) | |||
// a) Add 'secrets' field to 'apicontainer.Container' | |||
// b) Add 'ssmsecret' field to 'resources' | |||
ECSDataVersion = 17 | |||
// 18) Add 'AvailabilityZone' field to the TaskResponse struct |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason that we bump ECSDataVersion
is we want to maintain compatibility when we serialize/deserizlize state manager, TaskResponse struct
is not maintained by state manager. I suggested bumping the version because I saw you added az in state manager.
Summary
Expose availabilityZone to task metadata endpoint
Implementation details
Pass the availabilityZone from container instance level to agent metadata and expose availabilityZone from agent metadata to task response struct.
Testing
make release
)go build -out amazon-ecs-agent.exe ./agent
)make test
) passgo test -timeout=25s ./agent/...
) passmake run-integ-tests
) pass.\scripts\run-integ-tests.ps1
) passmake run-functional-tests
) pass.\scripts\run-functional-tests.ps1
) passNew tests cover the changes:
Description for the changelog
Licensing
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.