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 ECSClientFactory to create new ECS clients #4061

Merged
merged 1 commit into from
Dec 21, 2023
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
3 changes: 2 additions & 1 deletion agent/app/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,9 @@ func (agent *ecsAgent) start() int {
})
return exitcodes.ExitError
}
client, err := ecsclient.NewECSClient(agent.credentialProvider, cfgAccessor, agent.ec2MetadataClient,
clientFactory := ecsclient.NewECSClientFactory(agent.credentialProvider, cfgAccessor, agent.ec2MetadataClient,
version.String(), ecsclient.WithIPv6PortBindingExcluded(true))
client, err := clientFactory.NewClient()
if err != nil {
logger.Critical("Unable to create new ECS client", logger.Fields{
field.Error: err,
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.

61 changes: 61 additions & 0 deletions ecs-agent/api/ecs/client/ecs_client_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package ecsclient

import (
"github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs"
"github.com/aws/amazon-ecs-agent/ecs-agent/config"
"github.com/aws/amazon-ecs-agent/ecs-agent/ec2"
"github.com/aws/aws-sdk-go/aws/credentials"
)

// ECSClientFactory interface can be used to create new ECS clients.
// It wraps the internal ecsClientFactory and can help ease writing unit test code for consumers.
type ECSClientFactory interface {
// NewClient creates a new ECS client.
NewClient() (ecs.ECSClient, error)
// GetCredentials returns the credentials chain used by the ECS client.
GetCredentials() *credentials.Credentials
}

type ecsClientFactory struct {
credentialsProvider *credentials.Credentials
configAccessor config.AgentConfigAccessor
ec2MetadataClient ec2.EC2MetadataClient
agentVersion string
options []ECSClientOption
}

func NewECSClientFactory(
credentialsProvider *credentials.Credentials,
configAccessor config.AgentConfigAccessor,
ec2MetadataClient ec2.EC2MetadataClient,
agentVersion string,
options ...ECSClientOption) ECSClientFactory {
return &ecsClientFactory{
credentialsProvider: credentialsProvider,
configAccessor: configAccessor,
ec2MetadataClient: ec2MetadataClient,
agentVersion: agentVersion,
options: options,
}
}

func (f *ecsClientFactory) NewClient() (ecs.ECSClient, error) {
return NewECSClient(f.credentialsProvider, f.configAccessor, f.ec2MetadataClient, f.agentVersion, f.options...)
}

func (f *ecsClientFactory) GetCredentials() *credentials.Credentials {
return f.credentialsProvider
}
54 changes: 54 additions & 0 deletions ecs-agent/api/ecs/client/ecs_client_factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//go:build unit
// +build unit

// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package ecsclient

import (
"testing"

"github.com/aws/amazon-ecs-agent/ecs-agent/ec2"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestECSClientFactory(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

credentialsProvider := credentials.AnonymousCredentials
cfgAccessor := newMockConfigAccessor(ctrl, nil)
ec2MetadataClient := ec2.NewBlackholeEC2MetadataClient()
agentVersion := "0.0.0"

clientFactory, ok := NewECSClientFactory(credentialsProvider, cfgAccessor, ec2MetadataClient,
agentVersion).(*ecsClientFactory)
assert.True(t, ok)
assert.Equal(t, credentialsProvider, clientFactory.credentialsProvider)
assert.Equal(t, cfgAccessor, clientFactory.configAccessor)
assert.Equal(t, ec2MetadataClient, clientFactory.ec2MetadataClient)
assert.Equal(t, agentVersion, clientFactory.agentVersion)

client, err := clientFactory.NewClient()
assert.NoError(t, err)
c, ok := client.(*ecsClient)
assert.True(t, ok)
assert.Equal(t, credentialsProvider, c.credentialsProvider)
assert.Equal(t, cfgAccessor, c.configAccessor)
assert.Equal(t, ec2MetadataClient, c.ec2metadata)

assert.Equal(t, credentialsProvider, clientFactory.GetCredentials())
}
danehlim marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions ecs-agent/api/ecs/generate_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ package ecs

//go:generate mockgen -destination=mocks/api_mocks.go -copyright_file=../../../scripts/copyright_file github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs ECSStandardSDK,ECSSubmitStateSDK,ECSClient,ECSTaskProtectionSDK
//go:generate mockgen -destination=mocks/statechange/statechange_mocks.go -package=mock_statechange -copyright_file=../../../scripts/copyright_file github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs ContainerMetadataGetter,TaskMetadataGetter
//go:generate mockgen -destination=mocks/client/client_mocks.go -package=mock_client -copyright_file=../../../scripts/copyright_file github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client ECSClientFactory
79 changes: 79 additions & 0 deletions ecs-agent/api/ecs/mocks/client/client_mocks.go

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