Skip to content

Commit

Permalink
Add ECSClientFactory to create new ECS clients
Browse files Browse the repository at this point in the history
  • Loading branch information
danehlim committed Dec 21, 2023
1 parent fef8e10 commit b2c148f
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 1 deletion.
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)
// Credentials returns the credentials chain used by the ECS client.
Credentials() *credentials.Credentials
}

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

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

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

func (f *ecsClientFactory) Credentials() *credentials.Credentials {
return f.credentialsProvider
}
45 changes: 45 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,45 @@
//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"

mock_config "github.com/aws/amazon-ecs-agent/ecs-agent/config/mocks"
"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 TestNewECSClientFactory(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

credentialsProvider := credentials.AnonymousCredentials
cfgAccessor := mock_config.NewMockAgentConfigAccessor(ctrl)
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.agentVer)
}
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.

0 comments on commit b2c148f

Please sign in to comment.