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 18, 2023
1 parent 10a3127 commit 72d622e
Show file tree
Hide file tree
Showing 3 changed files with 124 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.

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
}

0 comments on commit 72d622e

Please sign in to comment.