From 52a699150fe582db06ae680579e9499078571187 Mon Sep 17 00:00:00 2001 From: Richa Gangwar Date: Mon, 31 Jul 2023 10:32:00 -0700 Subject: [PATCH] Move appnet client interface to ecs-agent (#3827) * Move appnet client lib to ecs-agent * Removed updated version on dependencies in go.mod for agent and ecs-agent * appnet client: changes for shared lib * appnet client: shared lib * appnet client: shared lib * appnet client: new mocks * appnet client: new mocks * appnet client: add generate_mocks to agent vendor * appnet client: remove arguments passed to createclient * appnet client: updated api mock * appnet client: Fix name on service_connect_linux * Remove ECSTaskProtectionSDK from interface.go * Update mocks * AppnetClient: Updated interface to remove appnet * Updated api mocks * Fix go imports * AppnetClient: Update files in vendor dir * AppnetClient: Update import for mock appnet client * Test fix --------- Co-authored-by: Richa Gangwar --- agent/api/generate_mocks.go | 2 +- agent/api/interface.go | 8 --- agent/api/mocks/api_mocks.go | 55 +--------------- agent/engine/docker_task_engine.go | 4 +- agent/engine/docker_task_engine_linux_test.go | 4 +- agent/stats/service_connect_linux.go | 5 +- agent/stats/service_connect_linux_test.go | 2 +- .../ecs-agent/api/appnet/client.go | 23 +++++-- .../ecs-agent/api/appnet/client_linux.go | 6 +- .../ecs-agent/api/appnet/client_other.go | 4 +- .../ecs-agent/api/appnet/generate_mocks.go | 3 + .../api/appnet/mocks/client_mocks.go | 64 +++++++++++++++++++ agent/vendor/modules.txt | 1 + ecs-agent/api/appnet/client.go | 23 +++++-- ecs-agent/api/appnet/client_linux.go | 6 +- ecs-agent/api/appnet/client_linux_test.go | 4 +- ecs-agent/api/appnet/client_other.go | 4 +- ecs-agent/api/appnet/generate_mocks.go | 3 + ecs-agent/api/appnet/mocks/client_mocks.go | 64 +++++++++++++++++++ 19 files changed, 190 insertions(+), 95 deletions(-) create mode 100644 agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/generate_mocks.go create mode 100644 agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/mocks/client_mocks.go create mode 100644 ecs-agent/api/appnet/generate_mocks.go create mode 100644 ecs-agent/api/appnet/mocks/client_mocks.go diff --git a/agent/api/generate_mocks.go b/agent/api/generate_mocks.go index 8e1cf824b8c..c19794a9d82 100644 --- a/agent/api/generate_mocks.go +++ b/agent/api/generate_mocks.go @@ -13,4 +13,4 @@ package api -//go:generate mockgen -destination=mocks/api_mocks.go -copyright_file=../../scripts/copyright_file github.com/aws/amazon-ecs-agent/agent/api ECSSDK,ECSSubmitStateSDK,ECSClient,AppnetClient +//go:generate mockgen -destination=mocks/api_mocks.go -copyright_file=../../scripts/copyright_file github.com/aws/amazon-ecs-agent/agent/api ECSSDK,ECSSubmitStateSDK,ECSClient diff --git a/agent/api/interface.go b/agent/api/interface.go index bc5d61f08d9..48a0478d80d 100644 --- a/agent/api/interface.go +++ b/agent/api/interface.go @@ -15,7 +15,6 @@ package api import ( "github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs" - prometheus "github.com/prometheus/client_model/go" ) // ECSClient is an interface over the ECSSDK interface which abstracts away some @@ -77,10 +76,3 @@ type ECSSubmitStateSDK interface { SubmitTaskStateChange(*ecs.SubmitTaskStateChangeInput) (*ecs.SubmitTaskStateChangeOutput, error) SubmitAttachmentStateChanges(*ecs.SubmitAttachmentStateChangesInput) (*ecs.SubmitAttachmentStateChangesOutput, error) } - -// AppnetClient is an interface with customized Appnet client that -// implements the GetStats and DrainInboundConnections -type AppnetClient interface { - GetStats(adminSocketPath string, statsRequest string) (map[string]*prometheus.MetricFamily, error) - DrainInboundConnections(adminSocketPath string, drainRequest string) error -} diff --git a/agent/api/mocks/api_mocks.go b/agent/api/mocks/api_mocks.go index 5eedc9be661..72cc74f2c17 100644 --- a/agent/api/mocks/api_mocks.go +++ b/agent/api/mocks/api_mocks.go @@ -13,7 +13,7 @@ // // Code generated by MockGen. DO NOT EDIT. -// Source: github.com/aws/amazon-ecs-agent/agent/api (interfaces: ECSSDK,ECSSubmitStateSDK,ECSClient,AppnetClient) +// Source: github.com/aws/amazon-ecs-agent/agent/api (interfaces: ECSSDK,ECSSubmitStateSDK,ECSClient) // Package mock_api is a generated GoMock package. package mock_api @@ -24,7 +24,6 @@ import ( api "github.com/aws/amazon-ecs-agent/agent/api" ecs "github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs" gomock "github.com/golang/mock/gomock" - io_prometheus_client "github.com/prometheus/client_model/go" ) // MockECSSDK is a mock of ECSSDK interface. @@ -362,55 +361,3 @@ func (mr *MockECSClientMockRecorder) UpdateContainerInstancesState(arg0, arg1 in mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateContainerInstancesState", reflect.TypeOf((*MockECSClient)(nil).UpdateContainerInstancesState), arg0, arg1) } - -// MockAppnetClient is a mock of AppnetClient interface. -type MockAppnetClient struct { - ctrl *gomock.Controller - recorder *MockAppnetClientMockRecorder -} - -// MockAppnetClientMockRecorder is the mock recorder for MockAppnetClient. -type MockAppnetClientMockRecorder struct { - mock *MockAppnetClient -} - -// NewMockAppnetClient creates a new mock instance. -func NewMockAppnetClient(ctrl *gomock.Controller) *MockAppnetClient { - mock := &MockAppnetClient{ctrl: ctrl} - mock.recorder = &MockAppnetClientMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockAppnetClient) EXPECT() *MockAppnetClientMockRecorder { - return m.recorder -} - -// DrainInboundConnections mocks base method. -func (m *MockAppnetClient) DrainInboundConnections(arg0, arg1 string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DrainInboundConnections", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// DrainInboundConnections indicates an expected call of DrainInboundConnections. -func (mr *MockAppnetClientMockRecorder) DrainInboundConnections(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DrainInboundConnections", reflect.TypeOf((*MockAppnetClient)(nil).DrainInboundConnections), arg0, arg1) -} - -// GetStats mocks base method. -func (m *MockAppnetClient) GetStats(arg0, arg1 string) (map[string]*io_prometheus_client.MetricFamily, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetStats", arg0, arg1) - ret0, _ := ret[0].(map[string]*io_prometheus_client.MetricFamily) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetStats indicates an expected call of GetStats. -func (mr *MockAppnetClientMockRecorder) GetStats(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetStats", reflect.TypeOf((*MockAppnetClient)(nil).GetStats), arg0, arg1) -} diff --git a/agent/engine/docker_task_engine.go b/agent/engine/docker_task_engine.go index 51ac7074289..271feccc919 100644 --- a/agent/engine/docker_task_engine.go +++ b/agent/engine/docker_task_engine.go @@ -144,7 +144,7 @@ type DockerTaskEngine struct { client dockerapi.DockerClient dataClient data.Client cniClient ecscni.CNIClient - appnetClient api.AppnetClient + appnetClient appnet.AppNetClient containerChangeEventStream *eventstream.EventStream @@ -226,7 +226,7 @@ func NewDockerTaskEngine(cfg *config.Config, imageManager: imageManager, hostResourceManager: hostResourceManager, cniClient: ecscni.NewClient(cfg.CNIPluginsPath), - appnetClient: appnet.Client(), + appnetClient: appnet.CreateClient(), metadataManager: metadataManager, serviceconnectManager: serviceConnectManager, diff --git a/agent/engine/docker_task_engine_linux_test.go b/agent/engine/docker_task_engine_linux_test.go index 46f38db203c..098545c5b88 100644 --- a/agent/engine/docker_task_engine_linux_test.go +++ b/agent/engine/docker_task_engine_linux_test.go @@ -30,7 +30,6 @@ import ( "github.com/aws/amazon-ecs-agent/agent/api/appmesh" apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container" apicontainerstatus "github.com/aws/amazon-ecs-agent/agent/api/container/status" - mock_api "github.com/aws/amazon-ecs-agent/agent/api/mocks" "github.com/aws/amazon-ecs-agent/agent/api/serviceconnect" apitask "github.com/aws/amazon-ecs-agent/agent/api/task" apitaskstatus "github.com/aws/amazon-ecs-agent/agent/api/task/status" @@ -54,6 +53,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/taskresource/ssmsecret" resourcestatus "github.com/aws/amazon-ecs-agent/agent/taskresource/status" mock_ioutilwrapper "github.com/aws/amazon-ecs-agent/agent/utils/ioutilwrapper/mocks" + mock_appnet "github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/mocks" "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni" "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" @@ -989,7 +989,7 @@ func TestContainersWithServiceConnect(t *testing.T) { defer ctrl.Finish() cniClient := mock_ecscni.NewMockCNIClient(ctrl) - appnetClient := mock_api.NewMockAppnetClient(ctrl) + appnetClient := mock_appnet.NewMockAppNetClient(ctrl) taskEngine.(*DockerTaskEngine).cniClient = cniClient taskEngine.(*DockerTaskEngine).appnetClient = appnetClient taskEngine.(*DockerTaskEngine).taskSteadyStatePollInterval = taskSteadyStatePollInterval diff --git a/agent/stats/service_connect_linux.go b/agent/stats/service_connect_linux.go index 0ca610b5279..30ea6d28fbe 100644 --- a/agent/stats/service_connect_linux.go +++ b/agent/stats/service_connect_linux.go @@ -21,7 +21,6 @@ import ( "strings" "sync" - "github.com/aws/amazon-ecs-agent/agent/api" "github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet" apitask "github.com/aws/amazon-ecs-agent/agent/api/task" @@ -33,7 +32,7 @@ import ( type ServiceConnectStats struct { stats []*ecstcs.GeneralMetricsWrapper - appnetClient api.AppnetClient + appnetClient appnet.AppNetClient sent bool lock sync.RWMutex } @@ -50,7 +49,7 @@ var directionToMetricType = map[string]string{ func newServiceConnectStats() (*ServiceConnectStats, error) { return &ServiceConnectStats{ - appnetClient: appnet.Client(), + appnetClient: appnet.CreateClient(), }, nil } diff --git a/agent/stats/service_connect_linux_test.go b/agent/stats/service_connect_linux_test.go index a6868161536..32f5560109a 100644 --- a/agent/stats/service_connect_linux_test.go +++ b/agent/stats/service_connect_linux_test.go @@ -148,7 +148,7 @@ func TestRetrieveServiceConnectMetrics(t *testing.T) { ts.Start() serviceConnectStats := &ServiceConnectStats{ - appnetClient: appnet.Client(), + appnetClient: appnet.CreateClient(), } serviceConnectStats.retrieveServiceConnectStats(t1) diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/client.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/client.go index 4adf9fac942..97a9fac13e7 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/client.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/client.go @@ -18,12 +18,22 @@ import ( "fmt" "net" "net/http" + + prometheus "github.com/prometheus/client_model/go" ) type appnetClientCtxKey int -type client struct { +// AppnetClient is an interface with customized Appnet client that +// implements the GetStats and DrainInboundConnections +type AppNetClient interface { + GetStats(adminSocketPath string, statsRequest string) (map[string]*prometheus.MetricFamily, error) + DrainInboundConnections(adminSocketPath string, drainRequest string) error +} + +type AppNetAgentClient struct { udsHttpClient http.Client + AppNetClient } const ( @@ -32,23 +42,24 @@ const ( ) // Client retrieves the singleton Appnet client -func Client() *client { - return &client{ +func CreateClient() *AppNetAgentClient { + client := AppNetAgentClient{ udsHttpClient: http.Client{ Transport: &http.Transport{ DialContext: udsDialContext, }, }, } + return &client } func udsDialContext(ctx context.Context, _, _ string) (net.Conn, error) { - udsPath, ok := ctx.Value(udsAddressKey).(string) + udsAddress, ok := ctx.Value(udsAddressKey).(string) if !ok { return nil, fmt.Errorf("appnet client: Path to appnet admin socket was not a string") } - if udsPath == "" { + if udsAddress == "" { return nil, fmt.Errorf("appnet client: Path to appnet admin socket was blank") } - return net.Dial(unixNetworkName, udsPath) + return net.Dial(unixNetworkName, udsAddress) } diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/client_linux.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/client_linux.go index 6d049c7bbeb..68e979161eb 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/client_linux.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/client_linux.go @@ -35,7 +35,7 @@ var ( // GetStats invokes Appnet Agent's stats API to retrieve ServiceConnect stats in prometheus format. This function expects // an Appnet-Agent-hosted HTTP server listening on the UDS path passed in config. -func (cl *client) GetStats(adminSocketPath string, statsRequest string) (map[string]*prometheus.MetricFamily, error) { +func (cl *AppNetAgentClient) GetStats(adminSocketPath string, statsRequest string) (map[string]*prometheus.MetricFamily, error) { resp, err := cl.performAppnetRequest(http.MethodGet, adminSocketPath, statsRequest) if err != nil { return nil, err @@ -49,7 +49,7 @@ func (cl *client) GetStats(adminSocketPath string, statsRequest string) (map[str // DrainInboundConnections invokes Appnet Agent's drain_listeners API which starts draining ServiceConnect inbound connections. // This function expects an Appnet-agent-hosted HTTP server listening on the UDS path passed in config. -func (cl *client) DrainInboundConnections(adminSocketPath string, drainRequest string) error { +func (cl *AppNetAgentClient) DrainInboundConnections(adminSocketPath string, drainRequest string) error { return retry.RetryNWithBackoff(oneSecondBackoffNoJitter, 3, func() error { resp, err := cl.performAppnetRequest(http.MethodGet, adminSocketPath, drainRequest) if err != nil { @@ -64,7 +64,7 @@ func (cl *client) DrainInboundConnections(adminSocketPath string, drainRequest s }) } -func (cl *client) performAppnetRequest(method, udsPath, url string) (*http.Response, error) { +func (cl *AppNetAgentClient) performAppnetRequest(method, udsPath, url string) (*http.Response, error) { ctx := context.WithValue(context.Background(), udsAddressKey, udsPath) req, _ := http.NewRequestWithContext(ctx, method, url, nil) httpClient := cl.udsHttpClient diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/client_other.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/client_other.go index d979d7edda1..5cc6f2b4119 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/client_other.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/client_other.go @@ -21,10 +21,10 @@ import ( prometheus "github.com/prometheus/client_model/go" ) -func (cl *client) GetStats(adminSocketPath string, statsRequest string) (map[string]*prometheus.MetricFamily, error) { +func (cl *AppNetAgentClient) GetStats(adminSocketPath string, statsRequest string) (map[string]*prometheus.MetricFamily, error) { return nil, fmt.Errorf("appnet client: GetStats is not supported in this platform") } -func (cl *client) DrainInboundConnections(adminSocketPath string, drainRequest string) error { +func (cl *AppNetAgentClient) DrainInboundConnections(adminSocketPath string, drainRequest string) error { return fmt.Errorf("appnet client: DrainInboundConnections is not supported in this platform") } diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/generate_mocks.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/generate_mocks.go new file mode 100644 index 00000000000..59e3c57327b --- /dev/null +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/generate_mocks.go @@ -0,0 +1,3 @@ +package appnet + +//go:generate mockgen -destination=mocks/client_mocks.go github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet AppNetClient diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/mocks/client_mocks.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/mocks/client_mocks.go new file mode 100644 index 00000000000..d2752206548 --- /dev/null +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/mocks/client_mocks.go @@ -0,0 +1,64 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet (interfaces: AppNetClient) + +// Package mock_appnet is a generated GoMock package. +package mock_appnet + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + io_prometheus_client "github.com/prometheus/client_model/go" +) + +// MockAppNetClient is a mock of AppNetClient interface. +type MockAppNetClient struct { + ctrl *gomock.Controller + recorder *MockAppNetClientMockRecorder +} + +// MockAppNetClientMockRecorder is the mock recorder for MockAppNetClient. +type MockAppNetClientMockRecorder struct { + mock *MockAppNetClient +} + +// NewMockAppNetClient creates a new mock instance. +func NewMockAppNetClient(ctrl *gomock.Controller) *MockAppNetClient { + mock := &MockAppNetClient{ctrl: ctrl} + mock.recorder = &MockAppNetClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockAppNetClient) EXPECT() *MockAppNetClientMockRecorder { + return m.recorder +} + +// DrainInboundConnections mocks base method. +func (m *MockAppNetClient) DrainInboundConnections(arg0, arg1 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DrainInboundConnections", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// DrainInboundConnections indicates an expected call of DrainInboundConnections. +func (mr *MockAppNetClientMockRecorder) DrainInboundConnections(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DrainInboundConnections", reflect.TypeOf((*MockAppNetClient)(nil).DrainInboundConnections), arg0, arg1) +} + +// GetStats mocks base method. +func (m *MockAppNetClient) GetStats(arg0, arg1 string) (map[string]*io_prometheus_client.MetricFamily, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetStats", arg0, arg1) + ret0, _ := ret[0].(map[string]*io_prometheus_client.MetricFamily) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetStats indicates an expected call of GetStats. +func (mr *MockAppNetClientMockRecorder) GetStats(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetStats", reflect.TypeOf((*MockAppNetClient)(nil).GetStats), arg0, arg1) +} diff --git a/agent/vendor/modules.txt b/agent/vendor/modules.txt index f9422ca91a0..e8dfabab5c5 100644 --- a/agent/vendor/modules.txt +++ b/agent/vendor/modules.txt @@ -13,6 +13,7 @@ github.com/aws/amazon-ecs-agent/ecs-agent/acs/session github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/testconst github.com/aws/amazon-ecs-agent/ecs-agent/api github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet +github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet/mocks github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo github.com/aws/amazon-ecs-agent/ecs-agent/api/eni github.com/aws/amazon-ecs-agent/ecs-agent/api/errors diff --git a/ecs-agent/api/appnet/client.go b/ecs-agent/api/appnet/client.go index 4adf9fac942..97a9fac13e7 100644 --- a/ecs-agent/api/appnet/client.go +++ b/ecs-agent/api/appnet/client.go @@ -18,12 +18,22 @@ import ( "fmt" "net" "net/http" + + prometheus "github.com/prometheus/client_model/go" ) type appnetClientCtxKey int -type client struct { +// AppnetClient is an interface with customized Appnet client that +// implements the GetStats and DrainInboundConnections +type AppNetClient interface { + GetStats(adminSocketPath string, statsRequest string) (map[string]*prometheus.MetricFamily, error) + DrainInboundConnections(adminSocketPath string, drainRequest string) error +} + +type AppNetAgentClient struct { udsHttpClient http.Client + AppNetClient } const ( @@ -32,23 +42,24 @@ const ( ) // Client retrieves the singleton Appnet client -func Client() *client { - return &client{ +func CreateClient() *AppNetAgentClient { + client := AppNetAgentClient{ udsHttpClient: http.Client{ Transport: &http.Transport{ DialContext: udsDialContext, }, }, } + return &client } func udsDialContext(ctx context.Context, _, _ string) (net.Conn, error) { - udsPath, ok := ctx.Value(udsAddressKey).(string) + udsAddress, ok := ctx.Value(udsAddressKey).(string) if !ok { return nil, fmt.Errorf("appnet client: Path to appnet admin socket was not a string") } - if udsPath == "" { + if udsAddress == "" { return nil, fmt.Errorf("appnet client: Path to appnet admin socket was blank") } - return net.Dial(unixNetworkName, udsPath) + return net.Dial(unixNetworkName, udsAddress) } diff --git a/ecs-agent/api/appnet/client_linux.go b/ecs-agent/api/appnet/client_linux.go index 6d049c7bbeb..68e979161eb 100644 --- a/ecs-agent/api/appnet/client_linux.go +++ b/ecs-agent/api/appnet/client_linux.go @@ -35,7 +35,7 @@ var ( // GetStats invokes Appnet Agent's stats API to retrieve ServiceConnect stats in prometheus format. This function expects // an Appnet-Agent-hosted HTTP server listening on the UDS path passed in config. -func (cl *client) GetStats(adminSocketPath string, statsRequest string) (map[string]*prometheus.MetricFamily, error) { +func (cl *AppNetAgentClient) GetStats(adminSocketPath string, statsRequest string) (map[string]*prometheus.MetricFamily, error) { resp, err := cl.performAppnetRequest(http.MethodGet, adminSocketPath, statsRequest) if err != nil { return nil, err @@ -49,7 +49,7 @@ func (cl *client) GetStats(adminSocketPath string, statsRequest string) (map[str // DrainInboundConnections invokes Appnet Agent's drain_listeners API which starts draining ServiceConnect inbound connections. // This function expects an Appnet-agent-hosted HTTP server listening on the UDS path passed in config. -func (cl *client) DrainInboundConnections(adminSocketPath string, drainRequest string) error { +func (cl *AppNetAgentClient) DrainInboundConnections(adminSocketPath string, drainRequest string) error { return retry.RetryNWithBackoff(oneSecondBackoffNoJitter, 3, func() error { resp, err := cl.performAppnetRequest(http.MethodGet, adminSocketPath, drainRequest) if err != nil { @@ -64,7 +64,7 @@ func (cl *client) DrainInboundConnections(adminSocketPath string, drainRequest s }) } -func (cl *client) performAppnetRequest(method, udsPath, url string) (*http.Response, error) { +func (cl *AppNetAgentClient) performAppnetRequest(method, udsPath, url string) (*http.Response, error) { ctx := context.WithValue(context.Background(), udsAddressKey, udsPath) req, _ := http.NewRequestWithContext(ctx, method, url, nil) httpClient := cl.udsHttpClient diff --git a/ecs-agent/api/appnet/client_linux_test.go b/ecs-agent/api/appnet/client_linux_test.go index 55043ed3161..8f6be14ca7c 100644 --- a/ecs-agent/api/appnet/client_linux_test.go +++ b/ecs-agent/api/appnet/client_linux_test.go @@ -177,7 +177,7 @@ func TestGetStats(t *testing.T) { t.Cleanup(func() { ts.Close() }) - stats, err := Client().GetStats(tc.udsPath, testStatsUrl) + stats, err := CreateClient().GetStats(tc.udsPath, testStatsUrl) assert.Equal(t, tc.expectedResult, stats) if tc.isErrorExpected { assert.Error(t, err) @@ -220,7 +220,7 @@ func TestDrainInboundConnections(t *testing.T) { t.Cleanup(func() { ts.Close() }) - err := Client().DrainInboundConnections(tc.udsPath, testDrainUrl) + err := CreateClient().DrainInboundConnections(tc.udsPath, testDrainUrl) if tc.isErrorExpected { assert.Error(t, err) assert.Contains(t, err.Error(), tc.expectedErrorContains) diff --git a/ecs-agent/api/appnet/client_other.go b/ecs-agent/api/appnet/client_other.go index d979d7edda1..5cc6f2b4119 100644 --- a/ecs-agent/api/appnet/client_other.go +++ b/ecs-agent/api/appnet/client_other.go @@ -21,10 +21,10 @@ import ( prometheus "github.com/prometheus/client_model/go" ) -func (cl *client) GetStats(adminSocketPath string, statsRequest string) (map[string]*prometheus.MetricFamily, error) { +func (cl *AppNetAgentClient) GetStats(adminSocketPath string, statsRequest string) (map[string]*prometheus.MetricFamily, error) { return nil, fmt.Errorf("appnet client: GetStats is not supported in this platform") } -func (cl *client) DrainInboundConnections(adminSocketPath string, drainRequest string) error { +func (cl *AppNetAgentClient) DrainInboundConnections(adminSocketPath string, drainRequest string) error { return fmt.Errorf("appnet client: DrainInboundConnections is not supported in this platform") } diff --git a/ecs-agent/api/appnet/generate_mocks.go b/ecs-agent/api/appnet/generate_mocks.go new file mode 100644 index 00000000000..59e3c57327b --- /dev/null +++ b/ecs-agent/api/appnet/generate_mocks.go @@ -0,0 +1,3 @@ +package appnet + +//go:generate mockgen -destination=mocks/client_mocks.go github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet AppNetClient diff --git a/ecs-agent/api/appnet/mocks/client_mocks.go b/ecs-agent/api/appnet/mocks/client_mocks.go new file mode 100644 index 00000000000..d2752206548 --- /dev/null +++ b/ecs-agent/api/appnet/mocks/client_mocks.go @@ -0,0 +1,64 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/aws/amazon-ecs-agent/ecs-agent/api/appnet (interfaces: AppNetClient) + +// Package mock_appnet is a generated GoMock package. +package mock_appnet + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + io_prometheus_client "github.com/prometheus/client_model/go" +) + +// MockAppNetClient is a mock of AppNetClient interface. +type MockAppNetClient struct { + ctrl *gomock.Controller + recorder *MockAppNetClientMockRecorder +} + +// MockAppNetClientMockRecorder is the mock recorder for MockAppNetClient. +type MockAppNetClientMockRecorder struct { + mock *MockAppNetClient +} + +// NewMockAppNetClient creates a new mock instance. +func NewMockAppNetClient(ctrl *gomock.Controller) *MockAppNetClient { + mock := &MockAppNetClient{ctrl: ctrl} + mock.recorder = &MockAppNetClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockAppNetClient) EXPECT() *MockAppNetClientMockRecorder { + return m.recorder +} + +// DrainInboundConnections mocks base method. +func (m *MockAppNetClient) DrainInboundConnections(arg0, arg1 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DrainInboundConnections", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// DrainInboundConnections indicates an expected call of DrainInboundConnections. +func (mr *MockAppNetClientMockRecorder) DrainInboundConnections(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DrainInboundConnections", reflect.TypeOf((*MockAppNetClient)(nil).DrainInboundConnections), arg0, arg1) +} + +// GetStats mocks base method. +func (m *MockAppNetClient) GetStats(arg0, arg1 string) (map[string]*io_prometheus_client.MetricFamily, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetStats", arg0, arg1) + ret0, _ := ret[0].(map[string]*io_prometheus_client.MetricFamily) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetStats indicates an expected call of GetStats. +func (mr *MockAppNetClientMockRecorder) GetStats(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetStats", reflect.TypeOf((*MockAppNetClient)(nil).GetStats), arg0, arg1) +}