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

AvailabilityZone show on taskmetadata endpoint #1674

Merged
merged 1 commit into from
Nov 9, 2018
Merged

AvailabilityZone show on taskmetadata endpoint #1674

merged 1 commit into from
Nov 9, 2018

Conversation

cyastella
Copy link
Contributor

@cyastella cyastella commented Nov 6, 2018

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

  • Builds on Linux (make release)
  • Builds on Windows (go build -out amazon-ecs-agent.exe ./agent)
  • Unit tests on Linux (make test) pass
  • Unit tests on Windows (go test -timeout=25s ./agent/...) pass
  • Integration tests on Linux (make run-integ-tests) pass
  • Integration tests on Windows (.\scripts\run-integ-tests.ps1) pass
  • Functional tests on Linux (make run-functional-tests) pass
  • Functional tests on Windows (.\scripts\run-functional-tests.ps1) pass

New 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.

@cyastella cyastella requested review from petderek and a team November 6, 2018 18:47
Copy link
Contributor

@linkar-ec2 linkar-ec2 left a 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?

if err == nil {
return containerInstanceArn, nil
return containerInstanceArn,availabilityzone, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add space

}
// 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
Copy link
Contributor

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

var availabilityzone_name = "ecs.availability-zone"
var availabilityzone = ""
attrs := resp.ContainerInstance.Attributes
if(resp != nil){
Copy link
Contributor

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

return "","", err
}

var availabilityzone_name = "ecs.availability-zone"
Copy link
Contributor

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.

Copy link
Contributor

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

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"{
Copy link
Contributor

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 +

Copy link
Contributor

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 :)

Copy link
Contributor Author

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.

@@ -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(""))),
Copy link
Contributor

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"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add space

@cyastella
Copy link
Contributor Author

@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"`
Copy link
Contributor

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?

Copy link
Contributor Author

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.

return "","", err
}

var availabilityzone_name = "ecs.availability-zone"
Copy link
Contributor

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

@@ -103,10 +104,11 @@ func TestDoStartMinimumSupportedDockerVersionTerminal(t *testing.T) {
stateManagerFactory: stateManagerFactory,
saveableOptionFactory: saveableOptionFactory,
}

assert.Equal(t, "", agent.availabilityzone)
Copy link
Contributor

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

exitCode := agent.doStart(eventstream.NewEventStream("events", ctx),
credentialsManager, state, imageManager, client)
assert.Equal(t, exitcodes.ExitTerminal, exitCode)
assert.Equal(t,"", agent.availabilityzone)
Copy link
Contributor

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.

Copy link
Contributor Author

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"`
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@@ -763,10 +785,11 @@ func TestRegisterContainerInstanceWhenContainerInstanceARNIsNotSetHappyPath(t *t
credentialProvider: aws_credentials.NewCredentials(mockCredentialsProvider),
mobyPlugins: mockMobyPlugins,
}

assert.Equal(t, "", agent.availabilityzone)
Copy link
Contributor

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.

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
Copy link
Contributor

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)
Copy link
Contributor

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,
Copy link
Contributor

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)
Copy link
Contributor

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?

var availabilityzone = ""
attrs := resp.ContainerInstance.Attributes
if resp != nil {
for _, attr := range attrs {
Copy link
Contributor

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 {

@@ -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),
Copy link
Contributor

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

@@ -107,6 +107,7 @@ type ecsAgent struct {
terminationHandler sighandlers.TerminationHandler
mobyPlugins mobypkgwrapper.Plugins
resourceFields *taskresource.ResourceFields
availabilityzone string
Copy link
Contributor

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"`
Copy link
Contributor

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.

@@ -41,6 +41,7 @@ const (
pollEndpointCacheSize = 1
pollEndpointCacheTTL = 20 * time.Minute
roundtripTimeout = 5 * time.Second
availabilityZoneName = "ecs.availability-zone"
Copy link
Contributor

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?

@@ -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
Copy link
Contributor

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
Copy link
Contributor

@haikuoliu haikuoliu Nov 9, 2018

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.

@cyastella cyastella merged commit 44c6b76 into aws:dev Nov 9, 2018
@yumex93 yumex93 added this to the 1.23.0 milestone Dec 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants