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

Migrate ECS client to aws-sdk-go-v2 #4447

Open
wants to merge 18 commits into
base: dev
Choose a base branch
from

Conversation

tinnywang
Copy link
Contributor

@tinnywang tinnywang commented Dec 5, 2024

Summary

Migrate ECS client to aws-sdk-go-v2.

#4406 added our bespoke codegen'd aws-sdk-go-v2 ECS client. This PR integrates the v2 ECS client with Agent and removes all uses of our v1 ECS client.

Implementation details

In go.mod for the agent and ecs-agent modules,

  1. add a dependency ongithub.com/aws/aws-sdk-go-v2/service/ecs
  2. add a replace directive so that we use our bespoke ECS package instead of the official SDK package
replace github.com/aws/aws-sdk-go-v2/service/ecs => ../aws-sdk-go-v2/service/ecs

Update ECS client construction per the docs.

Replace all uses of our v1 ECS client with the v2 ECS client.

Update error handling to handle SDKv1 and SDKv2 errors. v1 error handling can be removed after all clients have been migrated to aws-sdk-go-v2. See the docs for more info on error handling in v2.

Rewrite the oneDayRetrier to be compatible with aws-sdk-go-v2 per the docs.

Testing

GitHub checks pass.

New tests cover the changes: no

Description for the changelog

Migrate ECS client toaws-sdk-go-v2.

Licensing

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@tinnywang tinnywang force-pushed the aws-sdk-go-v2/ecs branch 2 times, most recently from 6b18859 to a1a9480 Compare December 6, 2024 08:34
@tinnywang tinnywang marked this pull request as ready for review December 6, 2024 18:06
@tinnywang tinnywang requested a review from a team as a code owner December 6, 2024 18:06
@tinnywang tinnywang force-pushed the aws-sdk-go-v2/ecs branch 2 times, most recently from 1c2d4b8 to 7d2a689 Compare December 10, 2024 11:19
Yiyuanzzz
Yiyuanzzz previously approved these changes Dec 13, 2024
@@ -64,12 +53,20 @@ func Uint16SliceToStringSlice(slice []uint16) []string {
return stringSlice
}

// Int64PtrToIntPtr converts a *int64 to *int.
func Int64PtrToIntPtr(int64ptr *int64) *int {
// Int32PtrToIntPtr converts a *int64 to *int.
Copy link
Contributor

Choose a reason for hiding this comment

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

// Int32PtrToIntPtr converts a *int32 to *int.

@@ -83,36 +83,28 @@ func TestUint16SliceToStringSlice(t *testing.T) {
stringSlice := Uint16SliceToStringSlice(tc.param)
assert.Equal(t, tc.expected, len(stringSlice), tc.name)

stringPtrSlice := Uint16SliceToStringPtrSlice(tc.param)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this unit test entirely, since the function Uint16SliceToStringPtrSlice is removed? And add a new unit test for the new Int64PtrToInt32Ptr method?

Copy link
Contributor

Choose a reason for hiding this comment

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

sure, will add it

"fmt"
"strings"

"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/aws/aws-sdk-go/aws/awserr"
Copy link
Contributor

Choose a reason for hiding this comment

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

we need to move to using this awserr stuff from aws-sdk-v2 too?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, v1 error handling will be removed after all clients have been migrated to aws-sdk-go-v2.

Copy link
Contributor

@singholt singholt Jan 28, 2025

Choose a reason for hiding this comment

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

I see, thanks. Can we add a comment to both the modified methods below about this? It isnt immediately clear why we do error handling both v1 and v2 way right now.

Copy link
Contributor

Choose a reason for hiding this comment

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

sure, will add

ecs-agent/api/errors/errors.go Show resolved Hide resolved
return client
}

// oneDayRetrier is a retrier for the AWS SDK that retries up to one day.
// Each retry will have an exponential backoff from 30ms to 5 minutes. Once the
// backoff has reached 5 minutes, it will not increase further.
// Conforms to the request.Retryer interface https://github.com/aws/aws-sdk-go/blob/v1.0.0/aws/request/retryer.go#L13
// Confirms to the aws.Retryer interface https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/aws#Retryer
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think "Conforms" should be the correct word choice here

}

return &tasknetworkconfig.TaskNetworkConfig{
NetworkNamespaces: netNSs,
NetworkMode: mode,
NetworkMode: types.NetworkMode(mode),
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't mode already type types.NetworkMode here? Type conversion seems unnecessary here, let's remove it.

@@ -481,7 +481,7 @@ func (AttachmentStateChange) GetEventType() statechange.EventType {
return statechange.AttachmentEvent
}

func buildManagedAgentStateChangePayload(change ManagedAgentStateChange) *ecsmodel.ManagedAgentStateChange {
func buildManagedAgentStateChangePayload(change ManagedAgentStateChange) *types.ManagedAgentStateChange {
Copy link
Contributor

@danehlim danehlim Feb 3, 2025

Choose a reason for hiding this comment

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

Is there any specific reason why we don't just return type types.ManagedAgentStateChange here? Returning pointer type *types.ManagedAgentStateChange here seems unnecessary to me (i.e., we just end up dereferencing on L427 anyway), but I may be missing something. It seems like in most other places in this pull request we replace *ecsmodel.<INSERT TYPE HERE> with types.<INSERT TYPE HERE> as opposed to *types.<INSERT TYPE HERE>.

ContainerName: aws.String(change.Container.Name),
Status: aws.String(change.Status.String()),
Reason: aws.String(change.Reason),
}
}

func buildContainerStateChangePayload(change ContainerStateChange) (*ecsmodel.ContainerStateChange, error) {
func buildContainerStateChangePayload(change ContainerStateChange) (*types.ContainerStateChange, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as this previous comment applies here too.

res: 400,
},
{
name: "TestGetRequestFailureStatusCodeSuccess SDKv2",
Copy link
Contributor

Choose a reason for hiding this comment

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

Test case names on L152 and L165 should be swapped (or alternatively, L154-L162 and L166-L167 should be swapped).

Comment on lines +17 to +21
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/smithy-go"
"github.com/cihub/seelog"
"github.com/pkg/errors"
Copy link
Contributor

Choose a reason for hiding this comment

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

this import block should go below with other external imports. import order -

  1. standard imports, 2. ecs agent imports, 3. external imports


logger.Error(fmt.Sprintf(
"got a %T exception that does not implement ResponseError. This should not happen, return statusCode 500 for whatever errorCode. Original err: %v.",
err, err))
Copy link
Contributor

Choose a reason for hiding this comment

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

logging the err twice in the same log statement doesn't help much. I would suggest removing the first one and just logging the error under "Original err:"

logger.Error(fmt.Sprintf("Got an exception that does not implement ResponseError. This should not happen, return statusCode 500 for whatever errorCode. Original err: %v.",err))

Copy link
Contributor

Choose a reason for hiding this comment

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

also, can you update all logger.Error statements in this file to start with a capitalized letter?

Comment on lines 34 to 36
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
"github.com/aws/aws-sdk-go-v2/service/ecs"
ecstypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
Copy link
Contributor

Choose a reason for hiding this comment

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

these sdk imports should go along with the other external import block below.

Copy link
Contributor

Choose a reason for hiding this comment

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

applicable to multiple files throughout this PR, please take a look

}

Copy link
Contributor

Choose a reason for hiding this comment

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

please add a comment here that this is the aws-sdk-go-v2 error handling and the above error handling (L407-422) will be removed once we complete our migration.

@@ -59,6 +61,10 @@ type TaskProtectionRequest struct {
ExpiresInMinutes *int64
}

type CanceledError interface {
Copy link
Contributor

Choose a reason for hiding this comment

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

please add a comment explaining what this is useful for, and a link to https://pkg.go.dev/github.com/aws/smithy-go#CanceledError.CanceledError

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.

5 participants