diff --git a/agent/app/agent.go b/agent/app/agent.go index 477ade44b63..f8760b648cc 100644 --- a/agent/app/agent.go +++ b/agent/app/agent.go @@ -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, diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client/ecs_client_factory.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client/ecs_client_factory.go new file mode 100644 index 00000000000..9eacf74964d --- /dev/null +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client/ecs_client_factory.go @@ -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 +} diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/generate_mocks.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/generate_mocks.go index 012f038b3bc..903d52a3fdd 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/generate_mocks.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/generate_mocks.go @@ -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 diff --git a/ecs-agent/api/ecs/client/ecs_client_factory.go b/ecs-agent/api/ecs/client/ecs_client_factory.go new file mode 100644 index 00000000000..9eacf74964d --- /dev/null +++ b/ecs-agent/api/ecs/client/ecs_client_factory.go @@ -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 +} diff --git a/ecs-agent/api/ecs/client/ecs_client_factory_test.go b/ecs-agent/api/ecs/client/ecs_client_factory_test.go new file mode 100644 index 00000000000..d33f5a26a7d --- /dev/null +++ b/ecs-agent/api/ecs/client/ecs_client_factory_test.go @@ -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) +} diff --git a/ecs-agent/api/ecs/generate_mocks.go b/ecs-agent/api/ecs/generate_mocks.go index 012f038b3bc..903d52a3fdd 100644 --- a/ecs-agent/api/ecs/generate_mocks.go +++ b/ecs-agent/api/ecs/generate_mocks.go @@ -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 diff --git a/ecs-agent/api/ecs/mocks/client/client_mocks.go b/ecs-agent/api/ecs/mocks/client/client_mocks.go new file mode 100644 index 00000000000..4033b91f9e7 --- /dev/null +++ b/ecs-agent/api/ecs/mocks/client/client_mocks.go @@ -0,0 +1,79 @@ +// 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. +// + +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client (interfaces: ECSClientFactory) + +// Package mock_client is a generated GoMock package. +package mock_client + +import ( + reflect "reflect" + + ecs "github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs" + credentials "github.com/aws/aws-sdk-go/aws/credentials" + gomock "github.com/golang/mock/gomock" +) + +// MockECSClientFactory is a mock of ECSClientFactory interface. +type MockECSClientFactory struct { + ctrl *gomock.Controller + recorder *MockECSClientFactoryMockRecorder +} + +// MockECSClientFactoryMockRecorder is the mock recorder for MockECSClientFactory. +type MockECSClientFactoryMockRecorder struct { + mock *MockECSClientFactory +} + +// NewMockECSClientFactory creates a new mock instance. +func NewMockECSClientFactory(ctrl *gomock.Controller) *MockECSClientFactory { + mock := &MockECSClientFactory{ctrl: ctrl} + mock.recorder = &MockECSClientFactoryMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockECSClientFactory) EXPECT() *MockECSClientFactoryMockRecorder { + return m.recorder +} + +// Credentials mocks base method. +func (m *MockECSClientFactory) Credentials() *credentials.Credentials { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Credentials") + ret0, _ := ret[0].(*credentials.Credentials) + return ret0 +} + +// Credentials indicates an expected call of Credentials. +func (mr *MockECSClientFactoryMockRecorder) Credentials() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Credentials", reflect.TypeOf((*MockECSClientFactory)(nil).Credentials)) +} + +// NewClient mocks base method. +func (m *MockECSClientFactory) NewClient() (ecs.ECSClient, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "NewClient") + ret0, _ := ret[0].(ecs.ECSClient) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// NewClient indicates an expected call of NewClient. +func (mr *MockECSClientFactoryMockRecorder) NewClient() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewClient", reflect.TypeOf((*MockECSClientFactory)(nil).NewClient)) +}