From 2bfefd2df643d23ec976a57d4f0d341ca4132df6 Mon Sep 17 00:00:00 2001 From: Ray Allan Date: Mon, 16 Oct 2023 20:05:54 +0000 Subject: [PATCH] Create Generic Attachment to decouple from ENI. --- agent/acs/session/attach_eni_common.go | 2 +- agent/acs/session/attach_eni_common_test.go | 10 ++-- .../attach_instance_eni_responder_test.go | 4 +- .../session/attach_task_eni_responder_test.go | 4 +- agent/acs/session/payload_responder_test.go | 8 +-- agent/api/ecsclient/client.go | 6 +- agent/api/ecsclient/client_test.go | 7 +-- agent/api/statechange.go | 6 +- agent/api/task/task.go | 2 +- agent/api/task/task_attachment_handler.go | 4 +- .../api/task/task_attachment_handler_test.go | 2 +- agent/api/task/task_test.go | 2 +- agent/api/task/taskvolume.go | 2 +- agent/api/task/taskvolume_test.go | 2 +- agent/app/data_test.go | 4 +- agent/data/client.go | 13 ++++- agent/data/eniattachment_client.go | 2 +- agent/data/eniattachment_client_test.go | 8 +-- agent/data/noop_client.go | 13 +++++ agent/data/resattachment_client.go | 58 +++++++++++++++++++ agent/ebs/watcher.go | 22 +------ agent/ebs/watcher_test.go | 35 ++++++----- agent/engine/data.go | 19 +++++- agent/engine/data_test.go | 4 +- agent/engine/docker_task_engine_linux_test.go | 7 +-- agent/engine/docker_task_engine_test.go | 11 ++-- .../dockerstate/docker_task_engine_state.go | 2 +- agent/engine/dockerstate/dockerstate_test.go | 25 ++++---- .../dockerstate/mocks/dockerstate_mocks.go | 2 +- agent/eni/watcher/watcher.go | 9 +-- agent/eni/watcher/watcher_linux_test.go | 12 ++-- agent/eni/watcher/watcher_test.go | 25 ++++---- agent/eni/watcher/watcher_windows_test.go | 16 +++-- agent/eventhandler/attachment_handler.go | 29 ++++++---- agent/eventhandler/attachment_handler_test.go | 51 +++++++--------- agent/eventhandler/handler_test.go | 2 +- agent/eventhandler/task_handler_test.go | 4 +- agent/eventhandler/task_handler_types_test.go | 10 ++-- agent/sighandlers/termination_handler_test.go | 4 +- agent/stats/engine_unix_test.go | 2 +- agent/taskresource/volume/dockervolume_ebs.go | 2 +- .../volume/dockervolume_ebs_test.go | 2 +- agent/utils/utils.go | 13 +++-- agent/utils/utils_test.go | 6 +- .../session/attach_instance_eni_responder.go | 7 +-- .../acs/session/attach_resource_responder.go | 9 ++- .../acs/session/attach_task_eni_responder.go | 7 +-- .../ecs-agent/api/attachment/attachment.go | 28 +++++++++ .../attachment_info.go | 6 +- .../attachment_status.go | 2 +- .../ecs-agent/api/{ => attachment}/eni/eni.go | 0 .../api/{ => attachment}/eni/eniattachment.go | 44 +++++++++++++- .../resource/ebs_discovery.go | 0 .../resource/ebs_discovery_linux.go | 0 .../resource/ebs_discovery_windows.go | 0 .../attachment}/resource/generate_mocks.go | 2 +- .../{ => attachment}/resource/interfaces.go | 0 .../attachment}/resource/mocks/ebs_mocks.go | 2 +- .../resource/resource_attachment.go | 46 ++++++++++----- .../resource/resource_type.go | 0 .../resource/resource_validation.go | 0 .../model/networkinterface/eniattachment.go | 43 +++++++++++++- agent/vendor/modules.txt | 9 ++- .../session/attach_instance_eni_responder.go | 7 +-- .../acs/session/attach_resource_responder.go | 9 ++- .../session/attach_resource_responder_test.go | 2 +- .../acs/session/attach_task_eni_responder.go | 7 +-- ecs-agent/acs/session/mocks/session_mock.go | 2 +- ecs-agent/api/attachment/attachment.go | 28 +++++++++ .../attachment_info.go | 6 +- .../attachment_status.go | 2 +- .../attachment_status_test.go | 2 +- ecs-agent/api/{ => attachment}/eni/eni.go | 0 .../api/{ => attachment}/eni/eni_test.go | 0 .../api/{ => attachment}/eni/eniattachment.go | 44 +++++++++++++- .../eni/eniattachment_test.go | 31 +++++----- .../resource/ebs_discovery.go | 0 .../resource/ebs_discovery_linux.go | 0 .../resource/ebs_discovery_linux_test.go | 0 .../resource/ebs_discovery_windows.go | 0 .../resource/ebs_discovery_windows_test.go | 0 .../attachment}/resource/generate_mocks.go | 2 +- .../{ => attachment}/resource/interfaces.go | 0 .../attachment}/resource/mocks/ebs_mocks.go | 2 +- .../resource/resource_attachment.go | 46 ++++++++++----- .../resource/resource_attachment_test.go | 27 +++++---- .../resource/resource_type.go | 0 .../resource/resource_validation.go | 0 .../resource/resource_validation_test.go | 0 .../api/ecs/statechange/statechange_test.go | 9 ++- .../model/networkinterface/eniattachment.go | 43 +++++++++++++- .../networkinterface/eniattachment_test.go | 31 +++++----- 92 files changed, 640 insertions(+), 338 deletions(-) create mode 100644 agent/data/resattachment_client.go create mode 100644 agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/attachment.go rename agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/{attachmentinfo => attachment}/attachment_info.go (92%) rename agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/{status => attachment}/attachment_status.go (98%) rename agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/{ => attachment}/eni/eni.go (100%) rename agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/{ => attachment}/eni/eniattachment.go (83%) rename agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/{ => attachment}/resource/ebs_discovery.go (100%) rename agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/{ => attachment}/resource/ebs_discovery_linux.go (100%) rename agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/{ => attachment}/resource/ebs_discovery_windows.go (100%) rename {ecs-agent/api => agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment}/resource/generate_mocks.go (84%) rename agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/{ => attachment}/resource/interfaces.go (100%) rename {ecs-agent/api => agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment}/resource/mocks/ebs_mocks.go (95%) rename agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/{ => attachment}/resource/resource_attachment.go (90%) rename agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/{ => attachment}/resource/resource_type.go (100%) rename agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/{ => attachment}/resource/resource_validation.go (100%) create mode 100644 ecs-agent/api/attachment/attachment.go rename ecs-agent/api/{attachmentinfo => attachment}/attachment_info.go (92%) rename ecs-agent/api/{status => attachment}/attachment_status.go (98%) rename ecs-agent/api/{status => attachment}/attachment_status_test.go (98%) rename ecs-agent/api/{ => attachment}/eni/eni.go (100%) rename ecs-agent/api/{ => attachment}/eni/eni_test.go (100%) rename ecs-agent/api/{ => attachment}/eni/eniattachment.go (83%) rename ecs-agent/api/{ => attachment}/eni/eniattachment_test.go (87%) rename ecs-agent/api/{ => attachment}/resource/ebs_discovery.go (100%) rename ecs-agent/api/{ => attachment}/resource/ebs_discovery_linux.go (100%) rename ecs-agent/api/{ => attachment}/resource/ebs_discovery_linux_test.go (100%) rename ecs-agent/api/{ => attachment}/resource/ebs_discovery_windows.go (100%) rename ecs-agent/api/{ => attachment}/resource/ebs_discovery_windows_test.go (100%) rename {agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api => ecs-agent/api/attachment}/resource/generate_mocks.go (84%) rename ecs-agent/api/{ => attachment}/resource/interfaces.go (100%) rename {agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api => ecs-agent/api/attachment}/resource/mocks/ebs_mocks.go (95%) rename ecs-agent/api/{ => attachment}/resource/resource_attachment.go (90%) rename ecs-agent/api/{ => attachment}/resource/resource_attachment_test.go (89%) rename ecs-agent/api/{ => attachment}/resource/resource_type.go (100%) rename ecs-agent/api/{ => attachment}/resource/resource_validation.go (100%) rename ecs-agent/api/{ => attachment}/resource/resource_validation_test.go (100%) diff --git a/agent/acs/session/attach_eni_common.go b/agent/acs/session/attach_eni_common.go index 3f05c429f45..c5ef01729d7 100644 --- a/agent/acs/session/attach_eni_common.go +++ b/agent/acs/session/attach_eni_common.go @@ -127,7 +127,7 @@ func (eniHandler *eniHandler) removeENIAttachmentData(mac string) { seelog.Errorf("Unable to retrieve ENI Attachment for mac address %s: ", mac) return } - attachmentId, err := utils.GetENIAttachmentId(attachmentToRemove.AttachmentARN) + attachmentId, err := utils.GetResourceAttachmentId(attachmentToRemove.AttachmentARN) if err != nil { seelog.Errorf("Failed to get attachment id for %s: %v", attachmentToRemove.AttachmentARN, err) } else { diff --git a/agent/acs/session/attach_eni_common_test.go b/agent/acs/session/attach_eni_common_test.go index 5a93729ef0d..2c813bd3385 100644 --- a/agent/acs/session/attach_eni_common_test.go +++ b/agent/acs/session/attach_eni_common_test.go @@ -26,7 +26,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/data" "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/testconst" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" ) @@ -53,7 +53,7 @@ func testENIAckTimeout(t *testing.T, attachmentType string) { expiresAt := time.Now().Add(time.Millisecond * testconst.WaitTimeoutMillis) eniAttachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: testconst.TaskARN, AttachmentARN: attachmentArn, ExpiresAt: expiresAt, @@ -99,7 +99,7 @@ func testENIAckWithinTimeout(t *testing.T, attachmentType string) { dataClient := data.NewNoopClient() expiresAt := time.Now().Add(time.Millisecond * testconst.WaitTimeoutMillis) eniAttachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: testconst.TaskARN, AttachmentARN: attachmentArn, ExpiresAt: expiresAt, @@ -141,7 +141,7 @@ func testHandleENIAttachment(t *testing.T, attachmentType, taskArn string) { taskEngineState := dockerstate.NewTaskEngineState() expiresAt := time.Now().Add(time.Millisecond * testconst.WaitTimeoutMillis) eniAttachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskArn, AttachmentARN: attachmentArn, ExpiresAt: expiresAt, @@ -188,7 +188,7 @@ func testHandleExpiredENIAttachment(t *testing.T, attachmentType, taskArn string dataClient := data.NewNoopClient() eniAttachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskArn, AttachmentARN: attachmentArn, ExpiresAt: expiresAt, diff --git a/agent/acs/session/attach_instance_eni_responder_test.go b/agent/acs/session/attach_instance_eni_responder_test.go index 974f22a3c28..d4abec66a4e 100644 --- a/agent/acs/session/attach_instance_eni_responder_test.go +++ b/agent/acs/session/attach_instance_eni_responder_test.go @@ -31,7 +31,7 @@ import ( "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" acssession "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/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" ) @@ -113,7 +113,7 @@ func TestInstanceENIAckSingleMessageWithDuplicateENIAttachment(t *testing.T) { mockState.EXPECT(). ENIByMac(testconst.RandomMAC). Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ ExpiresAt: expiresAt, }, }, true). diff --git a/agent/acs/session/attach_task_eni_responder_test.go b/agent/acs/session/attach_task_eni_responder_test.go index ba04db1804f..94f3e4cf8b0 100644 --- a/agent/acs/session/attach_task_eni_responder_test.go +++ b/agent/acs/session/attach_task_eni_responder_test.go @@ -31,7 +31,7 @@ import ( "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" acssession "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/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" ) @@ -113,7 +113,7 @@ func TestTaskENIAckSingleMessageWithDuplicateENIAttachment(t *testing.T) { mockState.EXPECT(). ENIByMac(testconst.RandomMAC). Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ ExpiresAt: expiresAt, }, }, true). diff --git a/agent/acs/session/payload_responder_test.go b/agent/acs/session/payload_responder_test.go index 5e9b149315f..33e862831fc 100644 --- a/agent/acs/session/payload_responder_test.go +++ b/agent/acs/session/payload_responder_test.go @@ -34,8 +34,8 @@ import ( "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" acssession "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/eni" - apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" + eniattachment "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/eni" + apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status" "github.com/aws/amazon-ecs-agent/ecs-agent/credentials" "github.com/aws/amazon-ecs-agent/ecs-agent/wsclient" @@ -858,7 +858,7 @@ func TestHandlePayloadMessageAddedENITrunkToTask(t *testing.T) { Arn: aws.String(testconst.TaskARN), ElasticNetworkInterfaces: []*ecsacs.ElasticNetworkInterface{ { - InterfaceAssociationProtocol: aws.String(eni.VLANInterfaceAssociationProtocol), + InterfaceAssociationProtocol: aws.String(eniattachment.VLANInterfaceAssociationProtocol), AttachmentArn: aws.String(attachmentARN), Ec2Id: aws.String(ec2ID), Ipv4Addresses: []*ecsacs.IPv4AddressAssignment{ @@ -890,7 +890,7 @@ func TestHandlePayloadMessageAddedENITrunkToTask(t *testing.T) { // Validate the added task has the ENI trunk information as expected. taskeni := addedTask.GetPrimaryENI() - assert.Equal(t, eni.VLANInterfaceAssociationProtocol, taskeni.InterfaceAssociationProtocol) + assert.Equal(t, eniattachment.VLANInterfaceAssociationProtocol, taskeni.InterfaceAssociationProtocol) assert.Equal(t, testconst.RandomMAC, taskeni.InterfaceVlanProperties.TrunkInterfaceMacAddress) assert.Equal(t, vlanID, taskeni.InterfaceVlanProperties.VlanID) } diff --git a/agent/api/ecsclient/client.go b/agent/api/ecsclient/client.go index eb6c9ee0bf4..e0b46cd39a6 100644 --- a/agent/api/ecsclient/client.go +++ b/agent/api/ecsclient/client.go @@ -657,14 +657,14 @@ func (client *APIECSClient) SubmitContainerStateChange(change api.ContainerState } func (client *APIECSClient) SubmitAttachmentStateChange(change api.AttachmentStateChange) error { - attachmentStatus := change.Attachment.Status.String() + attachmentStatus := change.Attachment.GetAttachmentStatus() req := ecs.SubmitAttachmentStateChangesInput{ Cluster: &client.config.Cluster, Attachments: []*ecs.AttachmentStateChange{ { - AttachmentArn: aws.String(change.Attachment.AttachmentARN), - Status: aws.String(attachmentStatus), + AttachmentArn: aws.String(change.Attachment.GetAttachmentARN()), + Status: aws.String(attachmentStatus.String()), }, }, } diff --git a/agent/api/ecsclient/client_test.go b/agent/api/ecsclient/client_test.go index b137d54efed..861bcdf7aae 100644 --- a/agent/api/ecsclient/client_test.go +++ b/agent/api/ecsclient/client_test.go @@ -24,8 +24,7 @@ import ( "testing" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" @@ -1086,9 +1085,9 @@ func TestSubmitTaskStateChangeWithAttachments(t *testing.T) { err := client.SubmitTaskStateChange(api.TaskStateChange{ TaskARN: "task_arn", Attachment: &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: "eni_arn", - Status: status.AttachmentAttached, + Status: attachment.AttachmentAttached, }, }, }) diff --git a/agent/api/statechange.go b/agent/api/statechange.go index 75243ae72e2..6cbc844f5cb 100644 --- a/agent/api/statechange.go +++ b/agent/api/statechange.go @@ -21,6 +21,7 @@ import ( apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container" apitask "github.com/aws/amazon-ecs-agent/agent/api/task" "github.com/aws/amazon-ecs-agent/agent/statechange" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status" apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" @@ -99,7 +100,8 @@ type TaskStateChange struct { // SubmitAttachmentStateChanges API type AttachmentStateChange struct { // Attachment is the eni attachment object to send - Attachment *ni.ENIAttachment + //Attachment *ni.ENIAttachment + Attachment attachment.Attachment } type ErrShouldNotSendEvent struct { @@ -352,7 +354,7 @@ func (change *TaskStateChange) String() string { // String returns a human readable string representation of this object func (change *AttachmentStateChange) String() string { if change.Attachment != nil { - return fmt.Sprintf("%s -> %s, %s", change.Attachment.AttachmentARN, change.Attachment.Status.String(), + return fmt.Sprintf("%s -> %v, %s", change.Attachment.GetAttachmentARN(), change.Attachment.GetAttachmentStatus(), change.Attachment.String()) } diff --git a/agent/api/task/task.go b/agent/api/task/task.go index 689f1c20282..2b363790533 100644 --- a/agent/api/task/task.go +++ b/agent/api/task/task.go @@ -41,9 +41,9 @@ import ( taskresourcevolume "github.com/aws/amazon-ecs-agent/agent/taskresource/volume" "github.com/aws/amazon-ecs-agent/agent/utils" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" + apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status" apierrors "github.com/aws/amazon-ecs-agent/ecs-agent/api/errors" - apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status" "github.com/aws/amazon-ecs-agent/ecs-agent/credentials" "github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs" diff --git a/agent/api/task/task_attachment_handler.go b/agent/api/task/task_attachment_handler.go index bcaef0bfbeb..7b987f5b46a 100644 --- a/agent/api/task/task_attachment_handler.go +++ b/agent/api/task/task_attachment_handler.go @@ -19,12 +19,12 @@ import ( "github.com/aws/amazon-ecs-agent/agent/api/serviceconnect" taskresourcevolume "github.com/aws/amazon-ecs-agent/agent/taskresource/volume" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" - apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" + apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/aws-sdk-go/aws" ) -// AttachmentHandler defines an interface to handel attachment received from ACS. +// AttachmentHandler defines an interface to handle attachment received from ACS. type AttachmentHandler interface { parseAttachment(acsAttachment *ecsacs.Attachment) error validateAttachment(acsTask *ecsacs.Task, task *Task) error diff --git a/agent/api/task/task_attachment_handler_test.go b/agent/api/task/task_attachment_handler_test.go index 98e007ad267..31a638de80b 100644 --- a/agent/api/task/task_attachment_handler_test.go +++ b/agent/api/task/task_attachment_handler_test.go @@ -25,7 +25,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/api/serviceconnect" taskresourcevolume "github.com/aws/amazon-ecs-agent/agent/taskresource/volume" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" - apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" + apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" "github.com/aws/aws-sdk-go/aws" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/agent/api/task/task_test.go b/agent/api/task/task_test.go index 7bddfb78e31..8c2a5420687 100644 --- a/agent/api/task/task_test.go +++ b/agent/api/task/task_test.go @@ -48,8 +48,8 @@ import ( taskresourcevolume "github.com/aws/amazon-ecs-agent/agent/taskresource/volume" "github.com/aws/amazon-ecs-agent/agent/utils" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" + apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status" - apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status" "github.com/aws/amazon-ecs-agent/ecs-agent/credentials" mock_credentials "github.com/aws/amazon-ecs-agent/ecs-agent/credentials/mocks" diff --git a/agent/api/task/taskvolume.go b/agent/api/task/taskvolume.go index e230fe1d96f..6f8a1894d8a 100644 --- a/agent/api/task/taskvolume.go +++ b/agent/api/task/taskvolume.go @@ -21,7 +21,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/taskresource/fsxwindowsfileserver" taskresourcetypes "github.com/aws/amazon-ecs-agent/agent/taskresource/types" taskresourcevolume "github.com/aws/amazon-ecs-agent/agent/taskresource/volume" - apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" + apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" "github.com/cihub/seelog" "github.com/pkg/errors" diff --git a/agent/api/task/taskvolume_test.go b/agent/api/task/taskvolume_test.go index 9c16a53775e..56f6ccbbdde 100644 --- a/agent/api/task/taskvolume_test.go +++ b/agent/api/task/taskvolume_test.go @@ -28,8 +28,8 @@ import ( mock_dockerapi "github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi/mocks" "github.com/aws/amazon-ecs-agent/agent/taskresource" taskresourcevolume "github.com/aws/amazon-ecs-agent/agent/taskresource/volume" + apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status" - apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" "github.com/docker/docker/api/types/volume" "github.com/golang/mock/gomock" diff --git a/agent/app/data_test.go b/agent/app/data_test.go index 99bacc33fac..aefea6d07fb 100644 --- a/agent/app/data_test.go +++ b/agent/app/data_test.go @@ -29,7 +29,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" "github.com/aws/amazon-ecs-agent/agent/engine/image" "github.com/aws/amazon-ecs-agent/agent/statemanager" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/ecs-agent/eventstream" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" "github.com/golang/mock/gomock" @@ -76,7 +76,7 @@ var ( } testENIAttachment = &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: testAttachmentArn, AttachStatusSent: false, }, diff --git a/agent/data/client.go b/agent/data/client.go index c756f7ff977..a5058c03bb2 100644 --- a/agent/data/client.go +++ b/agent/data/client.go @@ -21,6 +21,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/api/task" "github.com/aws/amazon-ecs-agent/agent/data/transformationfunctions" "github.com/aws/amazon-ecs-agent/agent/engine/image" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" generaldata "github.com/aws/amazon-ecs-agent/ecs-agent/data" "github.com/aws/amazon-ecs-agent/ecs-agent/modeltransformer" "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" @@ -35,6 +36,7 @@ const ( tasksBucketName = "tasks" imagesBucketName = "images" eniAttachmentsBucketName = "eniattachments" + resAttachmentsBucketName = "resattachments" metadataBucketName = "metadata" emptyAgentVersionMsg = "No version info available in boltDB. Either this is a fresh instance, or we were using state file to persist data. Transformer not applicable." ) @@ -48,6 +50,7 @@ var ( containersBucketName, tasksBucketName, eniAttachmentsBucketName, + resAttachmentsBucketName, metadataBucketName, } ) @@ -84,9 +87,17 @@ type Client interface { SaveENIAttachment(*networkinterface.ENIAttachment) error // DeleteENIAttachment deletes the data of an ENI atttachment. DeleteENIAttachment(string) error - // GetENIAttachments gets the data of all the ENI attachment. + // GetENIAttachments gets the data of all the ENI attachments. GetENIAttachments() ([]*networkinterface.ENIAttachment, error) + // SaveResourceAttachment saves the data of a resource attachment. + // This includes the EBS Attachment type + SaveResourceAttachment(*resource.ResourceAttachment) error + // DeleteResourceAttachment deletes the data of a resource atttachment. + DeleteResourceAttachment(string) error + // GetResourceAttachments gets the data of all the resouce attachments. + GetResourceAttachments() ([]*resource.ResourceAttachment, error) + // SaveMetadata saves a key value pair of metadata. SaveMetadata(string, string) error // GetMetadata gets the value of a certain kind of metadata. diff --git a/agent/data/eniattachment_client.go b/agent/data/eniattachment_client.go index d002e3b36b8..44ab72c338e 100644 --- a/agent/data/eniattachment_client.go +++ b/agent/data/eniattachment_client.go @@ -24,7 +24,7 @@ import ( ) func (c *client) SaveENIAttachment(eni *ni.ENIAttachment) error { - id, err := utils.GetENIAttachmentId(eni.AttachmentARN) + id, err := utils.GetResourceAttachmentId(eni.AttachmentARN) if err != nil { return errors.Wrap(err, "failed to generate database id") } diff --git a/agent/data/eniattachment_client_test.go b/agent/data/eniattachment_client_test.go index e876b56ec72..a3b7efaa2a1 100644 --- a/agent/data/eniattachment_client_test.go +++ b/agent/data/eniattachment_client_test.go @@ -19,7 +19,7 @@ package data import ( "testing" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" "github.com/stretchr/testify/assert" ) @@ -33,7 +33,7 @@ func TestManageENIAttachments(t *testing.T) { testClient := newTestClient(t) testEniAttachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: testAttachmentArn, AttachStatusSent: false, }, @@ -49,7 +49,7 @@ func TestManageENIAttachments(t *testing.T) { assert.Equal(t, testAttachmentArn, res[0].AttachmentARN) testEniAttachment2 := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: testAttachmentArn2, AttachStatusSent: true, }, @@ -71,7 +71,7 @@ func TestSaveENIAttachmentInvalidID(t *testing.T) { testClient := newTestClient(t) testEniAttachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: "invalid-arn", AttachStatusSent: false, }, diff --git a/agent/data/noop_client.go b/agent/data/noop_client.go index cbad81bc2a4..3c38ee436be 100644 --- a/agent/data/noop_client.go +++ b/agent/data/noop_client.go @@ -17,6 +17,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/api/container" "github.com/aws/amazon-ecs-agent/agent/api/task" "github.com/aws/amazon-ecs-agent/agent/engine/image" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" ) @@ -80,6 +81,18 @@ func (c *noopClient) GetENIAttachments() ([]*networkinterface.ENIAttachment, err return nil, nil } +func (c *noopClient) SaveResourceAttachment(*resource.ResourceAttachment) error { + return nil +} + +func (c *noopClient) DeleteResourceAttachment(string) error { + return nil +} + +func (c *noopClient) GetResourceAttachments() ([]*resource.ResourceAttachment, error) { + return nil, nil +} + func (c *noopClient) SaveMetadata(string, string) error { return nil } diff --git a/agent/data/resattachment_client.go b/agent/data/resattachment_client.go new file mode 100644 index 00000000000..76590f2d967 --- /dev/null +++ b/agent/data/resattachment_client.go @@ -0,0 +1,58 @@ +// 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 data + +import ( + "encoding/json" + + "github.com/aws/amazon-ecs-agent/agent/utils" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" + + "github.com/pkg/errors" + bolt "go.etcd.io/bbolt" +) + +func (c *client) SaveResourceAttachment(res *resource.ResourceAttachment) error { + id, err := utils.GetResourceAttachmentId(res.AttachmentARN) + if err != nil { + return errors.Wrap(err, "failed to generate database id") + } + return c.DB.Batch(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte(resAttachmentsBucketName)) + return c.Accessor.PutObject(b, id, res) + }) +} + +func (c *client) DeleteResourceAttachment(id string) error { + return c.DB.Batch(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte(resAttachmentsBucketName)) + return b.Delete([]byte(id)) + }) +} + +func (c *client) GetResourceAttachments() ([]*resource.ResourceAttachment, error) { + var resAttachments []*resource.ResourceAttachment + err := c.DB.View(func(tx *bolt.Tx) error { + bucket := tx.Bucket([]byte(resAttachmentsBucketName)) + return c.Accessor.Walk(bucket, func(id string, data []byte) error { + resAttachment := resource.ResourceAttachment{} + if err := json.Unmarshal(data, &resAttachment); err != nil { + return err + } + resAttachments = append(resAttachments, &resAttachment) + return nil + }) + }) + return resAttachments, err +} diff --git a/agent/ebs/watcher.go b/agent/ebs/watcher.go index cfa4e154536..3e8ecf825f6 100644 --- a/agent/ebs/watcher.go +++ b/agent/ebs/watcher.go @@ -22,14 +22,12 @@ import ( "time" ecsapi "github.com/aws/amazon-ecs-agent/agent/api" + //ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" ecsengine "github.com/aws/amazon-ecs-agent/agent/engine" "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" - attachmentinfo "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - apiebs "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" - apiattachmentstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + apiebs "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" csi "github.com/aws/amazon-ecs-agent/ecs-agent/csiclient" md "github.com/aws/amazon-ecs-agent/ecs-agent/manageddaemon" - apieni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" log "github.com/cihub/seelog" v1 "k8s.io/api/core/v1" @@ -249,7 +247,6 @@ func (w *EBSWatcher) notifyAttachedEBS(volumeId string) error { if err := w.sendEBSStateChange(ebs); err != nil { return fmt.Errorf("Unable to send state EBS change, %s", err) } - ebs.SetSentStatus() ebs.StopAckTimer() log.Infof("We've set sent status for %v", ebs.EBSToString()) return nil @@ -297,21 +294,8 @@ func (w *EBSWatcher) sendEBSStateChange(ebsvol *apiebs.ResourceAttachment) error } func (w *EBSWatcher) emitEBSAttachedEvent(ebsvol *apiebs.ResourceAttachment) { - attachmentInfo := attachmentinfo.AttachmentInfo{ - AttachmentARN: ebsvol.GetAttachmentARN(), - Status: apiattachmentstatus.AttachmentAttached, - ExpiresAt: ebsvol.GetExpiresAt(), - ClusterARN: ebsvol.GetClusterARN(), - ContainerInstanceARN: ebsvol.GetContainerInstanceARN(), - } - eniWrapper := apieni.ENIAttachment{AttachmentInfo: attachmentInfo} - // TODO update separate out ENI and EBS attachment types in attachment - // handler. For now we use fake task ENI with dummy fields - eniWrapper.AttachmentType = apieni.ENIAttachmentTypeTaskENI - eniWrapper.MACAddress = "ebs1" - eniWrapper.StartTimer(func() {}) attachmentChange := ecsapi.AttachmentStateChange{ - Attachment: &eniWrapper, + Attachment: ebsvol, } log.Debugf("Emitting EBS volume attached event for: %v", ebsvol) w.taskEngine.StateChangeEvents() <- attachmentChange diff --git a/agent/ebs/watcher_test.go b/agent/ebs/watcher_test.go index 6fc6ef2fc27..e454bb9dd2d 100644 --- a/agent/ebs/watcher_test.go +++ b/agent/ebs/watcher_test.go @@ -28,10 +28,9 @@ import ( mock_engine "github.com/aws/amazon-ecs-agent/agent/engine/mocks" taskresourcevolume "github.com/aws/amazon-ecs-agent/agent/taskresource/volume" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/testconst" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - apiebs "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" - mock_ebs_discovery "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/mocks" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" + apiebs "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" + mock_ebs_discovery "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/mocks" md "github.com/aws/amazon-ecs-agent/ecs-agent/manageddaemon" "github.com/golang/mock/gomock" @@ -84,12 +83,12 @@ func TestHandleEBSAttachmentHappyCase(t *testing.T) { expiresAt := time.Now().Add(time.Millisecond * testconst.WaitTimeoutMillis) ebsAttachment := &apiebs.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, TaskClusterARN: taskClusterARN, ContainerInstanceARN: containerInstanceARN, ExpiresAt: expiresAt, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, AttachmentARN: resourceAttachmentARN, }, AttachmentProperties: testAttachmentProperties, @@ -153,12 +152,12 @@ func TestHandleExpiredEBSAttachment(t *testing.T) { expiresAt := time.Now().Add(-1 * time.Millisecond) ebsAttachment := &apiebs.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, TaskClusterARN: taskClusterARN, ContainerInstanceARN: containerInstanceARN, ExpiresAt: expiresAt, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, AttachmentARN: resourceAttachmentARN, }, AttachmentProperties: testAttachmentProperties, @@ -199,12 +198,12 @@ func TestHandleDuplicateEBSAttachment(t *testing.T) { } ebsAttachment1 := &apiebs.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, TaskClusterARN: taskClusterARN, ContainerInstanceARN: containerInstanceARN, ExpiresAt: expiresAt, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, AttachmentARN: resourceAttachmentARN, }, AttachmentProperties: testAttachmentProperties1, @@ -221,12 +220,12 @@ func TestHandleDuplicateEBSAttachment(t *testing.T) { } ebsAttachment2 := &apiebs.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, TaskClusterARN: taskClusterARN, ContainerInstanceARN: containerInstanceARN, ExpiresAt: expiresAt, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, AttachmentARN: resourceAttachmentARN, }, AttachmentProperties: testAttachmentProperties2, @@ -290,12 +289,12 @@ func TestHandleInvalidTypeEBSAttachment(t *testing.T) { expiresAt := time.Now().Add(time.Millisecond * testconst.WaitTimeoutMillis) ebsAttachment := &apiebs.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, TaskClusterARN: taskClusterARN, ContainerInstanceARN: containerInstanceARN, ExpiresAt: expiresAt, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, AttachmentARN: resourceAttachmentARN, }, AttachmentProperties: testAttachmentProperties, @@ -336,12 +335,12 @@ func TestHandleEBSAckTimeout(t *testing.T) { expiresAt := time.Now().Add(time.Millisecond * testconst.WaitTimeoutMillis) ebsAttachment := &apiebs.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, TaskClusterARN: taskClusterARN, ContainerInstanceARN: containerInstanceARN, ExpiresAt: expiresAt, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, AttachmentARN: resourceAttachmentARN, }, AttachmentProperties: testAttachmentProperties, @@ -382,12 +381,12 @@ func TestHandleMismatchEBSAttachment(t *testing.T) { expiresAt := time.Now().Add(time.Millisecond * testconst.WaitTimeoutMillis) ebsAttachment := &apiebs.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, TaskClusterARN: taskClusterARN, ContainerInstanceARN: containerInstanceARN, ExpiresAt: expiresAt, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, AttachmentARN: resourceAttachmentARN, }, AttachmentProperties: testAttachmentProperties, diff --git a/agent/engine/data.go b/agent/engine/data.go index eae131792e6..be4bde24569 100644 --- a/agent/engine/data.go +++ b/agent/engine/data.go @@ -40,6 +40,11 @@ func (engine *DockerTaskEngine) LoadState() error { return err } + // for now this will only load EBS Attachments -- see TODO below + if err := engine.loadResourceAttachments(); err != nil { + return err + } + return engine.loadENIAttachments() } @@ -119,6 +124,18 @@ func (engine *DockerTaskEngine) loadImageStates() error { return nil } +func (engine *DockerTaskEngine) loadResourceAttachments() error { + resAttachments, err := engine.dataClient.GetResourceAttachments() + if err != nil { + return err + } + // TODO update docker state to handle more than just EBS Resource Type + for _, resAttachment := range resAttachments { + engine.state.AddEBSAttachment(resAttachment) + } + return nil +} + func (engine *DockerTaskEngine) loadENIAttachments() error { eniAttachments, err := engine.dataClient.GetENIAttachments() if err != nil { @@ -236,7 +253,7 @@ func (engine *DockerTaskEngine) removeENIAttachmentData(mac string) { seelog.Warnf("Unable to retrieve ENI Attachment for mac address %s: ", mac) return } - attachmentId, err := utils.GetENIAttachmentId(attachmentToRemove.AttachmentARN) + attachmentId, err := utils.GetResourceAttachmentId(attachmentToRemove.AttachmentARN) if err != nil { seelog.Errorf("Failed to get attachment id for %s: %v", attachmentToRemove.AttachmentARN, err) } else { diff --git a/agent/engine/data_test.go b/agent/engine/data_test.go index c58329eeb07..36665545e4b 100644 --- a/agent/engine/data_test.go +++ b/agent/engine/data_test.go @@ -26,7 +26,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/data" "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" "github.com/aws/amazon-ecs-agent/agent/engine/image" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -78,7 +78,7 @@ var ( } testENIAttachment = &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: testAttachmentArn, AttachStatusSent: false, }, diff --git a/agent/engine/docker_task_engine_linux_test.go b/agent/engine/docker_task_engine_linux_test.go index 378e70c3ffb..627b53770db 100644 --- a/agent/engine/docker_task_engine_linux_test.go +++ b/agent/engine/docker_task_engine_linux_test.go @@ -51,9 +51,8 @@ import ( 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" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status" "github.com/aws/amazon-ecs-agent/ecs-agent/credentials" "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/appmesh" @@ -225,10 +224,10 @@ func TestDeleteTask(t *testing.T) { } attachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: "TaskARN", AttachmentARN: testAttachmentArn, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, AttachStatusSent: true, }, MACAddress: "MACAddress", diff --git a/agent/engine/docker_task_engine_test.go b/agent/engine/docker_task_engine_test.go index 7fc3e486929..dc4f3793199 100644 --- a/agent/engine/docker_task_engine_test.go +++ b/agent/engine/docker_task_engine_test.go @@ -57,10 +57,9 @@ import ( mock_taskresource "github.com/aws/amazon-ecs-agent/agent/taskresource/mocks" "github.com/aws/amazon-ecs-agent/agent/taskresource/ssmsecret" taskresourcevolume "github.com/aws/amazon-ecs-agent/agent/taskresource/volume" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status" apierrors "github.com/aws/amazon-ecs-agent/ecs-agent/api/errors" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status" "github.com/aws/amazon-ecs-agent/ecs-agent/credentials" mock_credentials "github.com/aws/amazon-ecs-agent/ecs-agent/credentials/mocks" @@ -2425,10 +2424,10 @@ func TestSynchronizeENIAttachment(t *testing.T) { testTask := testdata.LoadTask("sleep5") expiresAt := time.Now().Unix() + 1 attachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: "TaskARN", AttachmentARN: "AttachmentARN", - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, MACAddress: "MACAddress", @@ -2461,10 +2460,10 @@ func TestSynchronizeENIAttachmentRemoveData(t *testing.T) { dockerTaskEngine := taskEngine.(*DockerTaskEngine) attachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: "TaskARN", AttachmentARN: testAttachmentArn, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, AttachStatusSent: false, }, MACAddress: "MACAddress", diff --git a/agent/engine/dockerstate/docker_task_engine_state.go b/agent/engine/dockerstate/docker_task_engine_state.go index bdbaa5f99a2..40fadd2615c 100644 --- a/agent/engine/dockerstate/docker_task_engine_state.go +++ b/agent/engine/dockerstate/docker_task_engine_state.go @@ -21,7 +21,7 @@ import ( apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container" apitask "github.com/aws/amazon-ecs-agent/agent/api/task" "github.com/aws/amazon-ecs-agent/agent/engine/image" - apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" + apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" "github.com/cihub/seelog" ) diff --git a/agent/engine/dockerstate/dockerstate_test.go b/agent/engine/dockerstate/dockerstate_test.go index 46f1ce6b61d..2e17cdc2d31 100644 --- a/agent/engine/dockerstate/dockerstate_test.go +++ b/agent/engine/dockerstate/dockerstate_test.go @@ -22,9 +22,8 @@ import ( apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container" apitask "github.com/aws/amazon-ecs-agent/agent/api/task" "github.com/aws/amazon-ecs-agent/agent/engine/image" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" + apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" "github.com/stretchr/testify/assert" ) @@ -104,7 +103,7 @@ func TestAddRemoveENIAttachment(t *testing.T) { state := NewTaskEngineState() attachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: "taskarn", AttachmentARN: "eni1", }, @@ -133,7 +132,7 @@ func TestAddRemoveEBSAttachment(t *testing.T) { state := NewTaskEngineState() attachment := &apiresource.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: "taskarn", AttachmentARN: "ebs1", }, @@ -162,11 +161,11 @@ func TestAddPendingEBSAttachment(t *testing.T) { state := NewTaskEngineState() pendingAttachment := &apiresource.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: "taskarn1", AttachmentARN: "ebs1", AttachStatusSent: false, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, }, AttachmentProperties: testAttachmentProperties, AttachmentType: apiresource.EBSTaskAttach, @@ -182,11 +181,11 @@ func TestAddPendingEBSAttachment(t *testing.T) { } foundAttachment := &apiresource.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: "taskarn2", AttachmentARN: "ebs2", AttachStatusSent: true, - Status: status.AttachmentAttached, + Status: attachment.AttachmentAttached, }, AttachmentProperties: testSentAttachmentProperties, AttachmentType: apiresource.EBSTaskAttach, @@ -217,11 +216,11 @@ func TestAddPendingEBSAttachmentExclusion(t *testing.T) { // not attached but sent should be included (||) sentAttachment := &apiresource.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: "taskarn1", AttachmentARN: "ebs1", AttachStatusSent: true, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, }, AttachmentProperties: testAttachmentProperties, AttachmentType: apiresource.EBSTaskAttach, @@ -229,11 +228,11 @@ func TestAddPendingEBSAttachmentExclusion(t *testing.T) { // attached and sent attachment should be excluded (&&) foundAttachment := &apiresource.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: "taskarn2", AttachmentARN: "ebs2", AttachStatusSent: true, - Status: status.AttachmentAttached, + Status: attachment.AttachmentAttached, }, AttachmentProperties: testSentAttachmentProperties, AttachmentType: apiresource.EBSTaskAttach, diff --git a/agent/engine/dockerstate/mocks/dockerstate_mocks.go b/agent/engine/dockerstate/mocks/dockerstate_mocks.go index 73a577dc1a8..be114a322bd 100644 --- a/agent/engine/dockerstate/mocks/dockerstate_mocks.go +++ b/agent/engine/dockerstate/mocks/dockerstate_mocks.go @@ -24,7 +24,7 @@ import ( container "github.com/aws/amazon-ecs-agent/agent/api/container" task "github.com/aws/amazon-ecs-agent/agent/api/task" image "github.com/aws/amazon-ecs-agent/agent/engine/image" - resource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" + resource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" networkinterface "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" gomock "github.com/golang/mock/gomock" ) diff --git a/agent/eni/watcher/watcher.go b/agent/eni/watcher/watcher.go index 7101236a364..03ff89e92ba 100644 --- a/agent/eni/watcher/watcher.go +++ b/agent/eni/watcher/watcher.go @@ -18,7 +18,7 @@ import ( "fmt" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" "github.com/aws/amazon-ecs-agent/agent/statechange" @@ -27,6 +27,7 @@ import ( "github.com/pkg/errors" "github.com/aws/amazon-ecs-agent/agent/api" + apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/eni" apierrors "github.com/aws/amazon-ecs-agent/ecs-agent/api/errors" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" "github.com/aws/amazon-ecs-agent/ecs-agent/utils/retry" @@ -142,7 +143,7 @@ func (eniWatcher *ENIWatcher) sendENIStateChange(mac string) error { // We found an ENI, which has the expiration time set in future and // needs to be acknowledged as having been 'attached' to the Instance - if eni.AttachmentType == ni.ENIAttachmentTypeInstanceENI { + if eni.AttachmentType == apieni.ENIAttachmentTypeInstanceENI { go eniWatcher.emitInstanceENIAttachedEvent(eni) } else { go eniWatcher.emitTaskENIAttachedEvent(eni) @@ -153,7 +154,7 @@ func (eniWatcher *ENIWatcher) sendENIStateChange(mac string) error { // emitTaskENIChangeEvent sends a state change event for a task ENI attachment to the event channel with eni status as // attached func (eniWatcher *ENIWatcher) emitTaskENIAttachedEvent(eni *ni.ENIAttachment) { - eni.Status = status.AttachmentAttached + eni.Status = attachment.AttachmentAttached log.Infof("Emitting task ENI attached event for: %s", eni.String()) eniWatcher.eniChangeEvent <- api.TaskStateChange{ TaskARN: eni.TaskARN, @@ -164,7 +165,7 @@ func (eniWatcher *ENIWatcher) emitTaskENIAttachedEvent(eni *ni.ENIAttachment) { // emitInstanceENIChangeEvent sends a state change event for an instance ENI attachment to the event channel with eni // status as attached func (eniWatcher *ENIWatcher) emitInstanceENIAttachedEvent(eni *ni.ENIAttachment) { - eni.Status = status.AttachmentAttached + eni.Status = attachment.AttachmentAttached log.Infof("Emitting instance ENI attached event for: %s", eni.String()) eniWatcher.eniChangeEvent <- api.NewAttachmentStateChangeEvent(eni) } diff --git a/agent/eni/watcher/watcher_linux_test.go b/agent/eni/watcher/watcher_linux_test.go index c353b895724..343618c51f0 100644 --- a/agent/eni/watcher/watcher_linux_test.go +++ b/agent/eni/watcher/watcher_linux_test.go @@ -24,8 +24,6 @@ import ( "testing" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" - "github.com/deniswernert/udev" "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" @@ -35,7 +33,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/api" "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" "github.com/aws/amazon-ecs-agent/agent/statechange" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" mock_dockerstate "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate/mocks" @@ -87,7 +85,7 @@ func TestWatcherInit(t *testing.T) { taskEngineState := dockerstate.NewTaskEngineState() taskEngineState.AddENIAttachment(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachStatusSent: false, ExpiresAt: time.Unix(time.Now().Unix()+10, 0), }, @@ -250,7 +248,7 @@ func TestReconcileENIsWithRetry(t *testing.T) { func getMockAttachment() *ni.ENIAttachment { return &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachStatusSent: false, ExpiresAt: time.Unix(time.Now().Unix()+10, 0), }, @@ -371,7 +369,7 @@ func TestUdevAddEvent(t *testing.T) { }, }, nil), mockStateManager.EXPECT().ENIByMac(randomMAC).Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ ExpiresAt: time.Unix(time.Now().Unix()+10, 0), }, }, true), @@ -386,7 +384,7 @@ func TestUdevAddEvent(t *testing.T) { eniChangeEvent := <-eventChannel taskStateChange, ok := eniChangeEvent.(api.TaskStateChange) require.True(t, ok) - assert.Equal(t, status.AttachmentAttached, taskStateChange.Attachment.Status) + assert.Equal(t, attachment.AttachmentAttached, taskStateChange.Attachment.Status) var waitForClose sync.WaitGroup waitForClose.Add(2) diff --git a/agent/eni/watcher/watcher_test.go b/agent/eni/watcher/watcher_test.go index 553a2b91808..715f9a5e765 100644 --- a/agent/eni/watcher/watcher_test.go +++ b/agent/eni/watcher/watcher_test.go @@ -21,8 +21,7 @@ import ( "testing" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/agent/api" "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" @@ -66,7 +65,7 @@ func TestSendENIStateChange(t *testing.T) { gomock.InOrder( mockStateManager.EXPECT().ENIByMac(randomMAC).Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ ExpiresAt: time.Now().Add(expirationTimeAddition), }, }, true), @@ -79,7 +78,7 @@ func TestSendENIStateChange(t *testing.T) { eniChangeEvent := <-eventChannel taskStateChange, ok := eniChangeEvent.(api.TaskStateChange) require.True(t, ok) - assert.Equal(t, status.AttachmentAttached, taskStateChange.Attachment.Status) + assert.Equal(t, attachment.AttachmentAttached, taskStateChange.Attachment.Status) } // Test for SendENIStateChange. We call the method for an Unmanaged ENI. Therefore we get an error. @@ -112,7 +111,7 @@ func TestSendENIStateChangeAlreadySent(t *testing.T) { gomock.InOrder( mockStateManager.EXPECT().ENIByMac(randomMAC).Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachStatusSent: true, ExpiresAt: time.Now().Add(expirationTimeAddition), }, @@ -137,7 +136,7 @@ func TestSendENIStateChangeExpired(t *testing.T) { gomock.InOrder( mockStateManager.EXPECT().ENIByMac(randomMAC).Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachStatusSent: false, ExpiresAt: time.Now().Add(expirationTimeSubtraction), }, @@ -163,7 +162,7 @@ func TestSendENIStateChangeWithRetries(t *testing.T) { gomock.InOrder( mockStateManager.EXPECT().ENIByMac(randomMAC).Return(nil, false), mockStateManager.EXPECT().ENIByMac(randomMAC).Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ ExpiresAt: time.Now().Add(expirationTimeAddition), }, MACAddress: randomMAC, @@ -177,7 +176,7 @@ func TestSendENIStateChangeWithRetries(t *testing.T) { eniChangeEvent := <-eventChannel taskStateChange, ok := eniChangeEvent.(api.TaskStateChange) require.True(t, ok) - assert.Equal(t, status.AttachmentAttached, taskStateChange.Attachment.Status) + assert.Equal(t, attachment.AttachmentAttached, taskStateChange.Attachment.Status) } // Test for SendENIStateChangeWithRetries. We call this method for an expired ENI. @@ -194,7 +193,7 @@ func TestSendENIStateChangeWithRetriesDoesNotRetryExpiredENI(t *testing.T) { // mean that it doesn't get retried. mockStateManager.EXPECT().ENIByMac(randomMAC).Return( &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachStatusSent: false, ExpiresAt: time.Now().Add(expirationTimeSubtraction), }, @@ -221,7 +220,7 @@ func TestSendENIStateChangeWithAttachmentTypeInstanceENI(t *testing.T) { gomock.InOrder( mockStateManager.EXPECT().ENIByMac(randomMAC).Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ ExpiresAt: time.Now().Add(expirationTimeAddition), }, AttachmentType: ni.ENIAttachmentTypeInstanceENI, @@ -235,7 +234,7 @@ func TestSendENIStateChangeWithAttachmentTypeInstanceENI(t *testing.T) { eniChangeEvent := <-eventChannel attachmentStateChange, ok := eniChangeEvent.(api.AttachmentStateChange) require.True(t, ok) - assert.Equal(t, status.AttachmentAttached, attachmentStateChange.Attachment.Status) + assert.Equal(t, attachment.AttachmentAttached, attachmentStateChange.Attachment.GetAttachmentStatus()) } // TestSendENIStateChangeWithAttachmentTypeTaskENI tests that we send the attachment state change @@ -250,7 +249,7 @@ func TestSendENIStateChangeWithAttachmentTypeTaskENI(t *testing.T) { gomock.InOrder( mockStateManager.EXPECT().ENIByMac(randomMAC).Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ ExpiresAt: time.Now().Add(expirationTimeAddition), }, AttachmentType: ni.ENIAttachmentTypeTaskENI, @@ -264,5 +263,5 @@ func TestSendENIStateChangeWithAttachmentTypeTaskENI(t *testing.T) { eniChangeEvent := <-eventChannel taskStateChange, ok := eniChangeEvent.(api.TaskStateChange) require.True(t, ok) - assert.Equal(t, status.AttachmentAttached, taskStateChange.Attachment.Status) + assert.Equal(t, attachment.AttachmentAttached, taskStateChange.Attachment.Status) } diff --git a/agent/eni/watcher/watcher_windows_test.go b/agent/eni/watcher/watcher_windows_test.go index 5de513257e7..014c538a8bb 100644 --- a/agent/eni/watcher/watcher_windows_test.go +++ b/agent/eni/watcher/watcher_windows_test.go @@ -23,8 +23,6 @@ import ( "testing" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" - "github.com/aws/amazon-ecs-agent/agent/api" "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" mock_dockerstate "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate/mocks" @@ -32,7 +30,7 @@ import ( mock_iphelperwrapper "github.com/aws/amazon-ecs-agent/agent/eni/iphelperwrapper/mocks" mock_networkutils "github.com/aws/amazon-ecs-agent/agent/eni/networkutils/mocks" "github.com/aws/amazon-ecs-agent/agent/statechange" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" "github.com/golang/mock/gomock" @@ -151,7 +149,7 @@ func TestReconcileOnce(t *testing.T) { mockStateManager := mock_dockerstate.NewMockTaskEngineState(mockCtrl) mockStateManager.EXPECT().ENIByMac(macAddress1). Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachStatusSent: false, ExpiresAt: time.Now().Add(expirationTimeAddition), }, @@ -162,7 +160,7 @@ func TestReconcileOnce(t *testing.T) { waitForEvents.Done() }). Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachStatusSent: true, }, MACAddress: macAddress2, @@ -259,7 +257,7 @@ func TestEventHandlerSuccess(t *testing.T) { mockNetworkUtils.EXPECT().GetInterfaceMACByIndex(interfaceIndex1, gomock.Any(), sendENIStateChangeRetryTimeout).Return(macAddress1, nil), mockStateManager.EXPECT().ENIByMac(macAddress1). Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ ExpiresAt: time.Now().Add(expirationTimeAddition), }, MACAddress: macAddress1, @@ -281,7 +279,7 @@ func TestEventHandlerSuccess(t *testing.T) { taskStateChange, ok := event.(api.TaskStateChange) assert.True(t, ok) - assert.Equal(t, status.AttachmentAttached, taskStateChange.Attachment.Status) + assert.Equal(t, attachment.AttachmentAttached, taskStateChange.Attachment.Status) var wait sync.WaitGroup wait.Add(1) @@ -349,7 +347,7 @@ func TestEventHandlerENIStatusAlreadySent(t *testing.T) { mockNetworkUtils.EXPECT().GetInterfaceMACByIndex(interfaceIndex1, gomock.Any(), sendENIStateChangeRetryTimeout).Return(macAddress1, nil), mockStateManager.EXPECT().ENIByMac(macAddress1). Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachStatusSent: true, }, MACAddress: macAddress1, @@ -432,7 +430,7 @@ func TestEventHandlerExpiredENI(t *testing.T) { mockNetworkUtils.EXPECT().GetInterfaceMACByIndex(interfaceIndex1, gomock.Any(), sendENIStateChangeRetryTimeout).Return(macAddress1, nil), mockStateManager.EXPECT().ENIByMac(macAddress1). Return(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ ExpiresAt: time.Now().Add(expirationTimeSubtraction), }, MACAddress: macAddress1, diff --git a/agent/eventhandler/attachment_handler.go b/agent/eventhandler/attachment_handler.go index e9947e0269c..95758dd9565 100644 --- a/agent/eventhandler/attachment_handler.go +++ b/agent/eventhandler/attachment_handler.go @@ -22,6 +22,8 @@ import ( "github.com/aws/amazon-ecs-agent/agent/api" "github.com/aws/amazon-ecs-agent/agent/data" "github.com/aws/amazon-ecs-agent/agent/statechange" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" + ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" "github.com/aws/amazon-ecs-agent/ecs-agent/utils/retry" "github.com/cihub/seelog" ) @@ -92,7 +94,7 @@ func (eventHandler *AttachmentEventHandler) AddStateChangeEvent(change statechan return fmt.Errorf("eventhandler: received malformed attachment state change event: %v", event) } - attachmentARN := event.Attachment.AttachmentARN + attachmentARN := event.Attachment.GetAttachmentARN() eventHandler.lock.Lock() if _, ok := eventHandler.attachmentARNToHandler[attachmentARN]; !ok { eventHandler.attachmentARNToHandler[attachmentARN] = &attachmentHandler{ @@ -127,7 +129,7 @@ func (handler *attachmentHandler) submitAttachmentEvent(attachmentChange *api.At } func (handler *attachmentHandler) submitAttachmentEventOnce(attachmentChange *api.AttachmentStateChange) error { - if !attachmentChangeShouldBeSent(attachmentChange) { + if !attachmentChange.Attachment.ShouldNotify() { seelog.Debugf("AttachmentHandler: not sending attachment state change [%s] as it should not be sent", attachmentChange.String()) // if the attachment state change should not be sent, we don't need to retry anymore so return nil here return nil @@ -143,14 +145,21 @@ func (handler *attachmentHandler) submitAttachmentEventOnce(attachmentChange *ap attachmentChange.Attachment.SetSentStatus() attachmentChange.Attachment.StopAckTimer() - err := handler.dataClient.SaveENIAttachment(attachmentChange.Attachment) - if err != nil { - seelog.Errorf("AttachmentHandler: error saving state after submitted attachment state change [%s]: %v", attachmentChange.String(), err) + switch attachmentChange.Attachment.(type) { + case *ni.ENIAttachment: + eniAtt := attachmentChange.Attachment.(*ni.ENIAttachment) + err := handler.dataClient.SaveENIAttachment(eniAtt) + if err != nil { + seelog.Errorf("AttachmentHandler: error saving ENI state after submitted attachment state change [%s]: %v", attachmentChange.String(), err) + } + case *resource.ResourceAttachment: + resAtt := attachmentChange.Attachment.(*resource.ResourceAttachment) + err := handler.dataClient.SaveResourceAttachment(resAtt) + if err != nil { + seelog.Errorf("AttachmentHandler: error saving Resource state after submitted attachment state change [%s]: %v", attachmentChange.String(), err) + } + case nil: + seelog.Errorf("Unable to save attachment state after submitted attachment state change [%s]: attachment type is nil", attachmentChange.String()) } return nil } - -// attachmentChangeShouldBeSent checks whether an attachment state change should be sent to backend -func attachmentChangeShouldBeSent(attachmentChange *api.AttachmentStateChange) bool { - return !attachmentChange.Attachment.HasExpired() && !attachmentChange.Attachment.IsSent() -} diff --git a/agent/eventhandler/attachment_handler_test.go b/agent/eventhandler/attachment_handler_test.go index d50c2c7f81e..bbfc3f78969 100644 --- a/agent/eventhandler/attachment_handler_test.go +++ b/agent/eventhandler/attachment_handler_test.go @@ -25,7 +25,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/api" mock_api "github.com/aws/amazon-ecs-agent/agent/api/mocks" "github.com/aws/amazon-ecs-agent/agent/data" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" apierrors "github.com/aws/amazon-ecs-agent/ecs-agent/api/errors" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" "github.com/aws/amazon-ecs-agent/ecs-agent/utils/retry" @@ -66,7 +66,7 @@ func TestSendAttachmentEvent(t *testing.T) { client.EXPECT().SubmitAttachmentStateChange(gomock.Any()).Return(nil).Do(func(change api.AttachmentStateChange) { assert.NotNil(t, change.Attachment) - assert.Equal(t, attachmentARN, change.Attachment.AttachmentARN) + assert.Equal(t, attachmentARN, change.Attachment.GetAttachmentARN()) wg.Done() }) @@ -104,7 +104,7 @@ func TestSendAttachmentEventRetries(t *testing.T) { client.EXPECT().SubmitAttachmentStateChange(gomock.Any()).Return(retriable).Do(func(interface{}) { wg.Done() }), client.EXPECT().SubmitAttachmentStateChange(gomock.Any()).Return(nil).Do(func(change api.AttachmentStateChange) { assert.NotNil(t, change.Attachment) - assert.Equal(t, attachmentARN, change.Attachment.AttachmentARN) + assert.Equal(t, attachmentARN, change.Attachment.GetAttachmentARN()) wg.Done() }), ) @@ -143,7 +143,7 @@ func TestSendMultipleAttachmentEventsDifferentAttachments(t *testing.T) { mapLock.Lock() defer mapLock.Unlock() - submittedAttachments[change.Attachment.AttachmentARN] = true + submittedAttachments[change.Attachment.GetAttachmentARN()] = true wg.Done() }) @@ -182,12 +182,12 @@ func TestSubmitAttachmentEventSucceeds(t *testing.T) { client.EXPECT().SubmitAttachmentStateChange(gomock.Any()).Return(nil).Do(func(change api.AttachmentStateChange) { assert.NotNil(t, change.Attachment) - assert.Equal(t, attachmentARN, change.Attachment.AttachmentARN) + assert.Equal(t, attachmentARN, change.Attachment.GetAttachmentARN()) }) handler.submitAttachmentEvent(&attachmentEvent) - assert.True(t, attachmentEvent.Attachment.AttachStatusSent) + assert.True(t, attachmentEvent.Attachment.IsSent()) res, err := dataClient.GetENIAttachments() assert.NoError(t, err) assert.Len(t, res, 1) @@ -198,8 +198,7 @@ func TestSubmitAttachmentEventAttachmentExpired(t *testing.T) { defer ctrl.Finish() client := mock_api.NewMockECSClient(ctrl) - attachmentEvent := attachmentEvent(attachmentARN) - attachmentEvent.Attachment.ExpiresAt = time.Now().Add(100 * time.Millisecond) + attachmentEvent := attachmentEventWithExpiry(attachmentARN, 100*time.Millisecond) // wait until eni attachment expires time.Sleep(200 * time.Millisecond) @@ -214,7 +213,7 @@ func TestSubmitAttachmentEventAttachmentExpired(t *testing.T) { handler.submitAttachmentEvent(&attachmentEvent) // no SubmitAttachmentStateChange should happen and attach status should not be sent - assert.False(t, attachmentEvent.Attachment.AttachStatusSent) + assert.False(t, attachmentEvent.Attachment.IsSent()) } func TestSubmitAttachmentEventAttachmentIsSent(t *testing.T) { @@ -243,29 +242,10 @@ func TestSubmitAttachmentEventAttachmentIsSent(t *testing.T) { attachmentEvent.Attachment.StopAckTimer() } -func TestAttachmentChangeShouldBeSent(t *testing.T) { - attachmentEvent := attachmentEvent(attachmentARN) - assert.True(t, attachmentChangeShouldBeSent(&attachmentEvent)) -} - -func TestAttachmentChangeShouldBeSentAttachmentExpired(t *testing.T) { - attachmentEvent := attachmentEvent(attachmentARN) - attachmentEvent.Attachment.ExpiresAt = time.Now() - time.Sleep(10 * time.Millisecond) - - assert.False(t, attachmentChangeShouldBeSent(&attachmentEvent)) -} - -func TestAttachmentChangeShouldBeSentAttachmentIsSent(t *testing.T) { - attachmentEvent := attachmentEvent(attachmentARN) - attachmentEvent.Attachment.SetSentStatus() - assert.False(t, attachmentChangeShouldBeSent(&attachmentEvent)) -} - func attachmentEvent(attachmentARN string) api.AttachmentStateChange { return api.AttachmentStateChange{ Attachment: &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: attachmentARN, AttachStatusSent: false, ExpiresAt: time.Now().Add(time.Second), @@ -274,3 +254,16 @@ func attachmentEvent(attachmentARN string) api.AttachmentStateChange { }, } } + +func attachmentEventWithExpiry(attachmentARN string, expiresAfter time.Duration) api.AttachmentStateChange { + return api.AttachmentStateChange{ + Attachment: &ni.ENIAttachment{ + AttachmentInfo: attachment.AttachmentInfo{ + AttachmentARN: attachmentARN, + AttachStatusSent: false, + ExpiresAt: time.Now().Add(expiresAfter), + }, + AttachmentType: ni.ENIAttachmentTypeInstanceENI, + }, + } +} diff --git a/agent/eventhandler/handler_test.go b/agent/eventhandler/handler_test.go index 1d85fa99e0e..ba4e346cd0e 100644 --- a/agent/eventhandler/handler_test.go +++ b/agent/eventhandler/handler_test.go @@ -62,7 +62,7 @@ func TestHandleEngineEvent(t *testing.T) { client.EXPECT().SubmitAttachmentStateChange(gomock.Any()).Do(func(change api.AttachmentStateChange) { assert.NotNil(t, change.Attachment) - assert.Equal(t, "attachmentARN", change.Attachment.AttachmentARN) + assert.Equal(t, "attachmentARN", change.Attachment.GetAttachmentARN()) wg.Done() }) diff --git a/agent/eventhandler/task_handler_test.go b/agent/eventhandler/task_handler_test.go index 159f878a24e..325a6ce5e66 100644 --- a/agent/eventhandler/task_handler_test.go +++ b/agent/eventhandler/task_handler_test.go @@ -33,7 +33,7 @@ import ( mock_dockerstate "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate/mocks" "github.com/aws/amazon-ecs-agent/agent/statechange" "github.com/aws/amazon-ecs-agent/agent/utils" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status" apierrors "github.com/aws/amazon-ecs-agent/ecs-agent/api/errors" apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status" @@ -377,7 +377,7 @@ func TestENISentStatusChange(t *testing.T) { } eniAttachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachStatusSent: false, ExpiresAt: time.Now().Add(time.Second), diff --git a/agent/eventhandler/task_handler_types_test.go b/agent/eventhandler/task_handler_types_test.go index 8ad35b761ea..d5b3478169e 100644 --- a/agent/eventhandler/task_handler_types_test.go +++ b/agent/eventhandler/task_handler_types_test.go @@ -25,7 +25,7 @@ import ( apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container" apitask "github.com/aws/amazon-ecs-agent/agent/api/task" "github.com/aws/amazon-ecs-agent/agent/data" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status" apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" @@ -307,7 +307,7 @@ func TestShouldTaskAttachmentEventBeSent(t *testing.T) { event: newSendableTaskEvent(api.TaskStateChange{ Status: apitaskstatus.TaskStatusNone, Attachment: &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ ExpiresAt: time.Unix(time.Now().Unix()-1, 0), AttachStatusSent: false, }, @@ -324,7 +324,7 @@ func TestShouldTaskAttachmentEventBeSent(t *testing.T) { event: newSendableTaskEvent(api.TaskStateChange{ Status: apitaskstatus.TaskStatusNone, Attachment: &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ ExpiresAt: time.Unix(time.Now().Unix()+10, 0), AttachStatusSent: true, }, @@ -338,7 +338,7 @@ func TestShouldTaskAttachmentEventBeSent(t *testing.T) { event: newSendableTaskEvent(api.TaskStateChange{ Status: apitaskstatus.TaskStatusNone, Attachment: &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ ExpiresAt: time.Unix(time.Now().Unix()+10, 0), AttachStatusSent: false, }, @@ -470,7 +470,7 @@ func TestSetAttachmentSentStatus(t *testing.T) { dataClient := newTestDataClient(t) testAttachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachStatusSent: true, ExpiresAt: time.Unix(time.Now().Unix()+100, 0), AttachmentARN: testAttachmentARN, diff --git a/agent/sighandlers/termination_handler_test.go b/agent/sighandlers/termination_handler_test.go index 54325b78869..e6d8d183c31 100644 --- a/agent/sighandlers/termination_handler_test.go +++ b/agent/sighandlers/termination_handler_test.go @@ -26,7 +26,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/engine" "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" "github.com/aws/amazon-ecs-agent/agent/engine/image" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" "github.com/stretchr/testify/assert" @@ -62,7 +62,7 @@ func TestFinalSave(t *testing.T) { } eniAttachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: eniAttachmentArn, AttachStatusSent: false, diff --git a/agent/stats/engine_unix_test.go b/agent/stats/engine_unix_test.go index 5bfbb5dea91..c79efe14876 100644 --- a/agent/stats/engine_unix_test.go +++ b/agent/stats/engine_unix_test.go @@ -27,7 +27,7 @@ import ( mock_dockerapi "github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi/mocks" mock_resolver "github.com/aws/amazon-ecs-agent/agent/stats/resolver/mock" taskresourcevolume "github.com/aws/amazon-ecs-agent/agent/taskresource/volume" - apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" + apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status" "github.com/aws/amazon-ecs-agent/ecs-agent/csiclient" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" diff --git a/agent/taskresource/volume/dockervolume_ebs.go b/agent/taskresource/volume/dockervolume_ebs.go index 4ef1bbdf5aa..3835e9b8bd9 100644 --- a/agent/taskresource/volume/dockervolume_ebs.go +++ b/agent/taskresource/volume/dockervolume_ebs.go @@ -17,7 +17,7 @@ import ( "fmt" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" - apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" + apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/aws-sdk-go/aws" ) diff --git a/agent/taskresource/volume/dockervolume_ebs_test.go b/agent/taskresource/volume/dockervolume_ebs_test.go index 5d4f9af626c..ede690cadef 100644 --- a/agent/taskresource/volume/dockervolume_ebs_test.go +++ b/agent/taskresource/volume/dockervolume_ebs_test.go @@ -19,7 +19,7 @@ import ( "testing" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" - apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" + apiresource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" "github.com/aws/aws-sdk-go/aws" "github.com/stretchr/testify/assert" ) diff --git a/agent/utils/utils.go b/agent/utils/utils.go index b3e73a05f29..3647a3e62c1 100644 --- a/agent/utils/utils.go +++ b/agent/utils/utils.go @@ -228,15 +228,16 @@ func GetTaskID(taskARN string) (string, error) { return fields[len(fields)-1], nil } -// GetENIAttachmentId retrieves the attachment ID from eni attachment ARN. -func GetENIAttachmentId(eniAttachmentArn string) (string, error) { - _, err := arn.Parse(eniAttachmentArn) +// GetResourceAttachmentId retrieves the resource ID from an attachment ARN. +// asssumes arn structure: arn:[partition]:ec2:[region]:[account-id]:[type]/[resource-id] +func GetResourceAttachmentId(resAttachmentArn string) (string, error) { + _, err := arn.Parse(resAttachmentArn) if err != nil { - return "", errors.Errorf("failed to get eni attachment id: eni attachment arn format invalid: %s", eniAttachmentArn) + return "", errors.Errorf("failed to get resource attachment id: resource attachment arn format invalid: %s", resAttachmentArn) } - fields := strings.Split(eniAttachmentArn, "/") + fields := strings.Split(resAttachmentArn, "/") if len(fields) < 2 { - return "", errors.Errorf("failed to get eni attachment id: eni attachment arn invalid: %s", eniAttachmentArn) + return "", errors.Errorf("failed to get resource attachment id: resource attachment arn invalid: %s", resAttachmentArn) } return fields[len(fields)-1], nil } diff --git a/agent/utils/utils_test.go b/agent/utils/utils_test.go index 3923acb2eb7..f5de6e44f40 100644 --- a/agent/utils/utils_test.go +++ b/agent/utils/utils_test.go @@ -186,12 +186,12 @@ func TestGetTaskID(t *testing.T) { assert.Error(t, err) } -func TestGetENIAttachmentId(t *testing.T) { +func TestGetResourceAttachmentId(t *testing.T) { attachmentArn := "arn:aws:ecs:us-west-2:1234567890:attachment/abc" - id, err := GetENIAttachmentId(attachmentArn) + id, err := GetResourceAttachmentId(attachmentArn) require.NoError(t, err) assert.Equal(t, "abc", id) - _, err = GetENIAttachmentId("invalid") + _, err = GetResourceAttachmentId("invalid") assert.Error(t, err) } diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/attach_instance_eni_responder.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/attach_instance_eni_responder.go index e23fb144092..fbbc3730d79 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/attach_instance_eni_responder.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/attach_instance_eni_responder.go @@ -21,8 +21,7 @@ import ( "github.com/pkg/errors" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" @@ -99,9 +98,9 @@ func (r *attachInstanceENIResponder) handleInstanceENIFromMessage(eni *ecsacs.El messageID, clusterARN, containerInstanceARN string, receivedAt time.Time, waitTimeoutMs int64) { expiresAt := receivedAt.Add(time.Duration(waitTimeoutMs) * time.Millisecond) err := r.eniHandler.HandleENIAttachment(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: aws.StringValue(eni.AttachmentArn), - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: expiresAt, AttachStatusSent: false, ClusterARN: clusterARN, diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/attach_resource_responder.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/attach_resource_responder.go index 2045de56d7a..29b3c8a3c5b 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/attach_resource_responder.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/attach_resource_responder.go @@ -18,9 +18,8 @@ import ( "time" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" "github.com/aws/amazon-ecs-agent/ecs-agent/metrics" @@ -92,14 +91,14 @@ func (r *attachResourceResponder) handleAttachMessage(message *ecsacs.ConfirmAtt }) expiresAt := receivedAt.Add(time.Duration(waitTimeoutMs) * time.Millisecond) go r.resourceHandler.HandleResourceAttachment(&resource.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: aws.StringValue(message.TaskArn), TaskClusterARN: aws.StringValue(message.TaskClusterArn), ClusterARN: aws.StringValue(message.ClusterArn), ContainerInstanceARN: aws.StringValue(message.ContainerInstanceArn), ExpiresAt: expiresAt, AttachmentARN: aws.StringValue(message.Attachment.AttachmentArn), - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, }, AttachmentProperties: attachmentProperties, AttachmentType: aws.StringValue(message.Attachment.AttachmentType), diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/attach_task_eni_responder.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/attach_task_eni_responder.go index ba226aea08d..389d6f72fa5 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/attach_task_eni_responder.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/attach_task_eni_responder.go @@ -21,8 +21,7 @@ import ( "github.com/pkg/errors" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" @@ -100,10 +99,10 @@ func (r *attachTaskENIResponder) handleTaskENIFromMessage(eni *ecsacs.ElasticNet messageID, taskARN, clusterARN, containerInstanceARN string, receivedAt time.Time, waitTimeoutMs int64) { expiresAt := receivedAt.Add(time.Duration(waitTimeoutMs) * time.Millisecond) err := r.eniHandler.HandleENIAttachment(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: aws.StringValue(eni.AttachmentArn), - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: expiresAt, AttachStatusSent: false, ClusterARN: clusterARN, diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/attachment.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/attachment.go new file mode 100644 index 00000000000..213c3190ce6 --- /dev/null +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/attachment.go @@ -0,0 +1,28 @@ +// 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 attachment + +type Attachment interface { + GetAttachmentARN() string + GetAttachmentStatus() AttachmentStatus + GetAttachmentType() string + IsSent() bool + SetAttachedStatus() + SetSentStatus() + ShouldAttach() bool + ShouldNotify() bool + StartTimer(func()) error + StopAckTimer() + String() string +} diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo/attachment_info.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/attachment_info.go similarity index 92% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo/attachment_info.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/attachment_info.go index 6f929c246e8..8b2d1e956ec 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo/attachment_info.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/attachment_info.go @@ -11,12 +11,10 @@ // express or implied. See the License for the specific language governing // permissions and limitations under the License. -package attachmentinfo +package attachment import ( "time" - - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" ) type AttachmentInfo struct { @@ -25,7 +23,7 @@ type AttachmentInfo struct { // AttachmentARN is the identifier for the attachment AttachmentARN string `json:"attachmentArn"` // Status is the status of the attachment: none/attached/detached - Status status.AttachmentStatus `json:"status"` + Status AttachmentStatus `json:"status"` // ExpiresAt is the timestamp past which the attachment is considered // unsuccessful. The SubmitTaskStateChange API, with the attachment information // should be invoked before this timestamp. diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/status/attachment_status.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/attachment_status.go similarity index 98% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/status/attachment_status.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/attachment_status.go index be8d9646d9d..93f0ba49ea7 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/status/attachment_status.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/attachment_status.go @@ -11,7 +11,7 @@ // express or implied. See the License for the specific language governing // permissions and limitations under the License. -package status +package attachment const ( // AttachmentNone is zero state of a task when received attach message from acs diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/eni/eni.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/eni/eni.go similarity index 100% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/eni/eni.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/eni/eni.go diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/eni/eniattachment.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/eni/eniattachment.go similarity index 83% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/eni/eniattachment.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/eni/eniattachment.go index 8b45d2f75cd..292b5b9dddf 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/eni/eniattachment.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/eni/eniattachment.go @@ -18,8 +18,7 @@ import ( "sync" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" "github.com/aws/amazon-ecs-agent/ecs-agent/utils/arn" @@ -37,7 +36,7 @@ const ( // ENIAttachment contains the information of the eni attachment type ENIAttachment struct { - attachmentinfo.AttachmentInfo + attachment.AttachmentInfo // AttachmentType is the type of the eni attachment, can either be "task-eni" or "instance-eni" AttachmentType string `json:"attachmentType"` // MACAddress is the mac address of eni @@ -118,6 +117,13 @@ func (eni *ENIAttachment) IsSent() bool { return eni.AttachStatusSent } +// SetAttachedStatus marks the eni status as attached +func (eni *ENIAttachment) SetAttachedStatus() { + eni.guard.Lock() + defer eni.guard.Unlock() + eni.Status = attachment.AttachmentAttached +} + // SetSentStatus marks the eni attached status has been sent func (eni *ENIAttachment) SetSentStatus() { eni.guard.Lock() @@ -151,6 +157,18 @@ func (eni *ENIAttachment) String() string { return eni.stringUnsafe() } +func (eni *ENIAttachment) GetAttachmentARN() string { + eni.guard.RLock() + defer eni.guard.RUnlock() + return eni.AttachmentARN +} + +func (eni *ENIAttachment) GetAttachmentStatus() attachment.AttachmentStatus { + eni.guard.RLock() + defer eni.guard.RUnlock() + return eni.Status +} + // stringUnsafe returns a string representation of the ENI Attachment func (eni *ENIAttachment) stringUnsafe() string { // skip TaskArn field for instance level eni attachment since it won't have a task arn @@ -164,3 +182,23 @@ func (eni *ENIAttachment) stringUnsafe() string { "ENI Attachment: task=%s attachment=%s attachmentType=%s attachmentSent=%t mac=%s status=%s expiresAt=%s", eni.TaskARN, eni.AttachmentARN, eni.AttachmentType, eni.AttachStatusSent, eni.MACAddress, eni.Status.String(), eni.ExpiresAt.Format(time.RFC3339)) } + +func (eni *ENIAttachment) GetAttachmentType() string { + eni.guard.RLock() + defer eni.guard.RUnlock() + + return eni.AttachmentType +} + +func (eni *ENIAttachment) ShouldAttach() bool { + eni.guard.RLock() + defer eni.guard.RUnlock() + return !(eni.Status == attachment.AttachmentAttached) && !eni.AttachStatusSent && !(time.Now().After(eni.ExpiresAt)) +} + +// should notify not sent/not expired +func (eni *ENIAttachment) ShouldNotify() bool { + eni.guard.RLock() + defer eni.guard.RUnlock() + return !eni.AttachStatusSent && !(time.Now().After(eni.ExpiresAt)) +} diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/ebs_discovery.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/ebs_discovery.go similarity index 100% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/ebs_discovery.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/ebs_discovery.go diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/ebs_discovery_linux.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/ebs_discovery_linux.go similarity index 100% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/ebs_discovery_linux.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/ebs_discovery_linux.go diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/ebs_discovery_windows.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/ebs_discovery_windows.go similarity index 100% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/ebs_discovery_windows.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/ebs_discovery_windows.go diff --git a/ecs-agent/api/resource/generate_mocks.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/generate_mocks.go similarity index 84% rename from ecs-agent/api/resource/generate_mocks.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/generate_mocks.go index a66369fc121..6c86d8825ee 100644 --- a/ecs-agent/api/resource/generate_mocks.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/generate_mocks.go @@ -11,6 +11,6 @@ // express or implied. See the License for the specific language governing // permissions and limitations under the License. -//go:generate mockgen -destination=mocks/ebs_mocks.go -copyright_file=../../../scripts/copyright_file github.com/aws/amazon-ecs-agent/ecs-agent/api/resource EBSDiscovery +//go:generate mockgen -destination=mocks/ebs_mocks.go -copyright_file=../../../../scripts/copyright_file github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource EBSDiscovery package resource diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/interfaces.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/interfaces.go similarity index 100% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/interfaces.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/interfaces.go diff --git a/ecs-agent/api/resource/mocks/ebs_mocks.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/mocks/ebs_mocks.go similarity index 95% rename from ecs-agent/api/resource/mocks/ebs_mocks.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/mocks/ebs_mocks.go index 9edfb8c25a6..2c0abac5d48 100644 --- a/ecs-agent/api/resource/mocks/ebs_mocks.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/mocks/ebs_mocks.go @@ -13,7 +13,7 @@ // // Code generated by MockGen. DO NOT EDIT. -// Source: github.com/aws/amazon-ecs-agent/ecs-agent/api/resource (interfaces: EBSDiscovery) +// Source: github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource (interfaces: EBSDiscovery) // Package mock_resource is a generated GoMock package. package mock_resource diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/resource_attachment.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/resource_attachment.go similarity index 90% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/resource_attachment.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/resource_attachment.go index 0091298d522..f4666867cef 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/resource_attachment.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/resource_attachment.go @@ -19,14 +19,13 @@ import ( "sync" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/utils/ttime" ) type ResourceAttachment struct { - attachmentinfo.AttachmentInfo + attachment.AttachmentInfo // AttachmentType is the type of the resource attachment which can be "amazonebs" for EBS attach tasks. AttachmentType string `json:"AttachmentType,omitempty"` // AttachmentProperties is a map storing (name, value) representation of attachment properties. @@ -215,7 +214,7 @@ func (ra *ResourceAttachment) IsAttached() bool { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.Status == status.AttachmentAttached + return ra.Status == attachment.AttachmentAttached } // SetAttachedStatus marks the resouce attachment as attached once it's been found on the host @@ -223,7 +222,7 @@ func (ra *ResourceAttachment) SetAttachedStatus() { ra.guard.Lock() defer ra.guard.Unlock() - ra.Status = status.AttachmentAttached + ra.Status = attachment.AttachmentAttached } // StopAckTimer stops the ack timer set on the resource attachment @@ -254,7 +253,6 @@ func (ra *ResourceAttachment) SetError(err error) { func (ra *ResourceAttachment) GetError() error { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.err } @@ -262,7 +260,6 @@ func (ra *ResourceAttachment) GetError() error { func (ra *ResourceAttachment) EBSToString() string { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.ebsToStringUnsafe() } @@ -287,42 +284,42 @@ func (ra *ResourceAttachment) GetAttachmentProperties(key string) string { func (ra *ResourceAttachment) GetAttachmentType() string { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.AttachmentType } func (ra *ResourceAttachment) SetDeviceName(deviceName string) { ra.guard.Lock() defer ra.guard.Unlock() - ra.AttachmentProperties[DeviceNameKey] = deviceName } func (ra *ResourceAttachment) GetAttachmentARN() string { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.AttachmentARN } -func (ra *ResourceAttachment) GetExpiresAt() time.Time { +func (ra *ResourceAttachment) GetStatus() attachment.AttachmentStatus { ra.guard.RLock() defer ra.guard.RUnlock() + return ra.Status +} +func (ra *ResourceAttachment) GetExpiresAt() time.Time { + ra.guard.RLock() + defer ra.guard.RUnlock() return ra.ExpiresAt } func (ra *ResourceAttachment) GetClusterARN() string { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.ClusterARN } func (ra *ResourceAttachment) GetContainerInstanceARN() string { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.ContainerInstanceARN } @@ -330,14 +327,31 @@ func (ra *ResourceAttachment) GetContainerInstanceARN() string { func (ra *ResourceAttachment) ShouldAttach() bool { ra.guard.RLock() defer ra.guard.RUnlock() - - return !(ra.Status == status.AttachmentAttached) && !ra.AttachStatusSent && !(time.Now().After(ra.ExpiresAt)) + return !(ra.Status == attachment.AttachmentAttached) && !ra.AttachStatusSent && !(time.Now().After(ra.ExpiresAt)) } // should notify when attached, and not sent/not expired func (ra *ResourceAttachment) ShouldNotify() bool { ra.guard.RLock() defer ra.guard.RUnlock() + return (ra.Status == attachment.AttachmentAttached) && !ra.AttachStatusSent && !(time.Now().After(ra.ExpiresAt)) +} + +func (ra *ResourceAttachment) String() string { + ra.guard.RLock() + defer ra.guard.RUnlock() + return ra.stringUnsafe() +} - return (ra.Status == status.AttachmentAttached) && !ra.AttachStatusSent && !(time.Now().After(ra.ExpiresAt)) +func (ra *ResourceAttachment) GetAttachmentStatus() attachment.AttachmentStatus { + ra.guard.RLock() + defer ra.guard.RUnlock() + return ra.Status +} + +// stringUnsafe returns a string representation of the ENI Attachment +func (ra *ResourceAttachment) stringUnsafe() string { + return fmt.Sprintf( + "Resource Attachment: attachment=%s attachmentType=%s attachmentSent=%t status=%s expiresAt=%s", + ra.AttachmentARN, ra.AttachmentType, ra.AttachStatusSent, ra.Status.String(), ra.ExpiresAt.Format(time.RFC3339)) } diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/resource_type.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/resource_type.go similarity index 100% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/resource_type.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/resource_type.go diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/resource_validation.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/resource_validation.go similarity index 100% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/resource_validation.go rename to agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/resource_validation.go diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface/eniattachment.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface/eniattachment.go index 347f0714968..3ff374e4e7a 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface/eniattachment.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface/eniattachment.go @@ -18,7 +18,7 @@ import ( "sync" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" "github.com/aws/amazon-ecs-agent/ecs-agent/utils/arn" @@ -36,7 +36,7 @@ const ( // ENIAttachment contains the information of the eni attachment type ENIAttachment struct { - attachmentinfo.AttachmentInfo + attachment.AttachmentInfo // AttachmentType is the type of the eni attachment, can either be "task-eni" or "instance-eni" AttachmentType string `json:"attachmentType"` // MACAddress is the mac address of eni @@ -117,6 +117,13 @@ func (eni *ENIAttachment) IsSent() bool { return eni.AttachStatusSent } +// SetAttachedStatus marks the eni status as attached +func (eni *ENIAttachment) SetAttachedStatus() { + eni.guard.Lock() + defer eni.guard.Unlock() + eni.Status = attachment.AttachmentAttached +} + // SetSentStatus marks the eni attached status has been sent func (eni *ENIAttachment) SetSentStatus() { eni.guard.Lock() @@ -150,6 +157,18 @@ func (eni *ENIAttachment) String() string { return eni.stringUnsafe() } +func (eni *ENIAttachment) GetAttachmentARN() string { + eni.guard.RLock() + defer eni.guard.RUnlock() + return eni.AttachmentARN +} + +func (eni *ENIAttachment) GetAttachmentStatus() attachment.AttachmentStatus { + eni.guard.RLock() + defer eni.guard.RUnlock() + return eni.Status +} + // stringUnsafe returns a string representation of the ENI Attachment func (eni *ENIAttachment) stringUnsafe() string { // skip TaskArn field for instance level eni attachment since it won't have a task arn @@ -163,3 +182,23 @@ func (eni *ENIAttachment) stringUnsafe() string { "ENI Attachment: task=%s attachment=%s attachmentType=%s attachmentSent=%t mac=%s status=%s expiresAt=%s", eni.TaskARN, eni.AttachmentARN, eni.AttachmentType, eni.AttachStatusSent, eni.MACAddress, eni.Status.String(), eni.ExpiresAt.Format(time.RFC3339)) } + +func (eni *ENIAttachment) GetAttachmentType() string { + eni.guard.RLock() + defer eni.guard.RUnlock() + + return eni.AttachmentType +} + +func (eni *ENIAttachment) ShouldAttach() bool { + eni.guard.RLock() + defer eni.guard.RUnlock() + return !(eni.Status == attachment.AttachmentAttached) && !eni.AttachStatusSent && !(time.Now().After(eni.ExpiresAt)) +} + +// should notify when attached, and not sent/not expired +func (eni *ENIAttachment) ShouldNotify() bool { + eni.guard.RLock() + defer eni.guard.RUnlock() + return !eni.AttachStatusSent && !(time.Now().After(eni.ExpiresAt)) +} diff --git a/agent/vendor/modules.txt b/agent/vendor/modules.txt index bfc197d7bd7..522decfcdea 100644 --- a/agent/vendor/modules.txt +++ b/agent/vendor/modules.txt @@ -12,14 +12,13 @@ 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/attachment +github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/eni +github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource +github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource/mocks github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status -github.com/aws/amazon-ecs-agent/ecs-agent/api/eni github.com/aws/amazon-ecs-agent/ecs-agent/api/errors github.com/aws/amazon-ecs-agent/ecs-agent/api/mocks -github.com/aws/amazon-ecs-agent/ecs-agent/api/resource -github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/mocks -github.com/aws/amazon-ecs-agent/ecs-agent/api/status github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status github.com/aws/amazon-ecs-agent/ecs-agent/async github.com/aws/amazon-ecs-agent/ecs-agent/async/mocks diff --git a/ecs-agent/acs/session/attach_instance_eni_responder.go b/ecs-agent/acs/session/attach_instance_eni_responder.go index e23fb144092..fbbc3730d79 100644 --- a/ecs-agent/acs/session/attach_instance_eni_responder.go +++ b/ecs-agent/acs/session/attach_instance_eni_responder.go @@ -21,8 +21,7 @@ import ( "github.com/pkg/errors" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" @@ -99,9 +98,9 @@ func (r *attachInstanceENIResponder) handleInstanceENIFromMessage(eni *ecsacs.El messageID, clusterARN, containerInstanceARN string, receivedAt time.Time, waitTimeoutMs int64) { expiresAt := receivedAt.Add(time.Duration(waitTimeoutMs) * time.Millisecond) err := r.eniHandler.HandleENIAttachment(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: aws.StringValue(eni.AttachmentArn), - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: expiresAt, AttachStatusSent: false, ClusterARN: clusterARN, diff --git a/ecs-agent/acs/session/attach_resource_responder.go b/ecs-agent/acs/session/attach_resource_responder.go index 2045de56d7a..29b3c8a3c5b 100644 --- a/ecs-agent/acs/session/attach_resource_responder.go +++ b/ecs-agent/acs/session/attach_resource_responder.go @@ -18,9 +18,8 @@ import ( "time" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" "github.com/aws/amazon-ecs-agent/ecs-agent/metrics" @@ -92,14 +91,14 @@ func (r *attachResourceResponder) handleAttachMessage(message *ecsacs.ConfirmAtt }) expiresAt := receivedAt.Add(time.Duration(waitTimeoutMs) * time.Millisecond) go r.resourceHandler.HandleResourceAttachment(&resource.ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: aws.StringValue(message.TaskArn), TaskClusterARN: aws.StringValue(message.TaskClusterArn), ClusterARN: aws.StringValue(message.ClusterArn), ContainerInstanceARN: aws.StringValue(message.ContainerInstanceArn), ExpiresAt: expiresAt, AttachmentARN: aws.StringValue(message.Attachment.AttachmentArn), - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, }, AttachmentProperties: attachmentProperties, AttachmentType: aws.StringValue(message.Attachment.AttachmentType), diff --git a/ecs-agent/acs/session/attach_resource_responder_test.go b/ecs-agent/acs/session/attach_resource_responder_test.go index 5fe331581c4..4a7fff2972a 100644 --- a/ecs-agent/acs/session/attach_resource_responder_test.go +++ b/ecs-agent/acs/session/attach_resource_responder_test.go @@ -29,7 +29,7 @@ import ( "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/testconst" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" ) const ( diff --git a/ecs-agent/acs/session/attach_task_eni_responder.go b/ecs-agent/acs/session/attach_task_eni_responder.go index ba226aea08d..389d6f72fa5 100644 --- a/ecs-agent/acs/session/attach_task_eni_responder.go +++ b/ecs-agent/acs/session/attach_task_eni_responder.go @@ -21,8 +21,7 @@ import ( "github.com/pkg/errors" "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" @@ -100,10 +99,10 @@ func (r *attachTaskENIResponder) handleTaskENIFromMessage(eni *ecsacs.ElasticNet messageID, taskARN, clusterARN, containerInstanceARN string, receivedAt time.Time, waitTimeoutMs int64) { expiresAt := receivedAt.Add(time.Duration(waitTimeoutMs) * time.Millisecond) err := r.eniHandler.HandleENIAttachment(&ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: aws.StringValue(eni.AttachmentArn), - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: expiresAt, AttachStatusSent: false, ClusterARN: clusterARN, diff --git a/ecs-agent/acs/session/mocks/session_mock.go b/ecs-agent/acs/session/mocks/session_mock.go index 54bd2104409..0b2a7756a9b 100644 --- a/ecs-agent/acs/session/mocks/session_mock.go +++ b/ecs-agent/acs/session/mocks/session_mock.go @@ -22,7 +22,7 @@ import ( reflect "reflect" ecsacs "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" - resource "github.com/aws/amazon-ecs-agent/ecs-agent/api/resource" + resource "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" networkinterface "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" gomock "github.com/golang/mock/gomock" ) diff --git a/ecs-agent/api/attachment/attachment.go b/ecs-agent/api/attachment/attachment.go new file mode 100644 index 00000000000..213c3190ce6 --- /dev/null +++ b/ecs-agent/api/attachment/attachment.go @@ -0,0 +1,28 @@ +// 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 attachment + +type Attachment interface { + GetAttachmentARN() string + GetAttachmentStatus() AttachmentStatus + GetAttachmentType() string + IsSent() bool + SetAttachedStatus() + SetSentStatus() + ShouldAttach() bool + ShouldNotify() bool + StartTimer(func()) error + StopAckTimer() + String() string +} diff --git a/ecs-agent/api/attachmentinfo/attachment_info.go b/ecs-agent/api/attachment/attachment_info.go similarity index 92% rename from ecs-agent/api/attachmentinfo/attachment_info.go rename to ecs-agent/api/attachment/attachment_info.go index 6f929c246e8..8b2d1e956ec 100644 --- a/ecs-agent/api/attachmentinfo/attachment_info.go +++ b/ecs-agent/api/attachment/attachment_info.go @@ -11,12 +11,10 @@ // express or implied. See the License for the specific language governing // permissions and limitations under the License. -package attachmentinfo +package attachment import ( "time" - - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" ) type AttachmentInfo struct { @@ -25,7 +23,7 @@ type AttachmentInfo struct { // AttachmentARN is the identifier for the attachment AttachmentARN string `json:"attachmentArn"` // Status is the status of the attachment: none/attached/detached - Status status.AttachmentStatus `json:"status"` + Status AttachmentStatus `json:"status"` // ExpiresAt is the timestamp past which the attachment is considered // unsuccessful. The SubmitTaskStateChange API, with the attachment information // should be invoked before this timestamp. diff --git a/ecs-agent/api/status/attachment_status.go b/ecs-agent/api/attachment/attachment_status.go similarity index 98% rename from ecs-agent/api/status/attachment_status.go rename to ecs-agent/api/attachment/attachment_status.go index be8d9646d9d..93f0ba49ea7 100644 --- a/ecs-agent/api/status/attachment_status.go +++ b/ecs-agent/api/attachment/attachment_status.go @@ -11,7 +11,7 @@ // express or implied. See the License for the specific language governing // permissions and limitations under the License. -package status +package attachment const ( // AttachmentNone is zero state of a task when received attach message from acs diff --git a/ecs-agent/api/status/attachment_status_test.go b/ecs-agent/api/attachment/attachment_status_test.go similarity index 98% rename from ecs-agent/api/status/attachment_status_test.go rename to ecs-agent/api/attachment/attachment_status_test.go index 7eea5778ed3..3df4129f8f5 100644 --- a/ecs-agent/api/status/attachment_status_test.go +++ b/ecs-agent/api/attachment/attachment_status_test.go @@ -14,7 +14,7 @@ // express or implied. See the License for the specific language governing // permissions and limitations under the License. -package status +package attachment import ( "testing" diff --git a/ecs-agent/api/eni/eni.go b/ecs-agent/api/attachment/eni/eni.go similarity index 100% rename from ecs-agent/api/eni/eni.go rename to ecs-agent/api/attachment/eni/eni.go diff --git a/ecs-agent/api/eni/eni_test.go b/ecs-agent/api/attachment/eni/eni_test.go similarity index 100% rename from ecs-agent/api/eni/eni_test.go rename to ecs-agent/api/attachment/eni/eni_test.go diff --git a/ecs-agent/api/eni/eniattachment.go b/ecs-agent/api/attachment/eni/eniattachment.go similarity index 83% rename from ecs-agent/api/eni/eniattachment.go rename to ecs-agent/api/attachment/eni/eniattachment.go index 8b45d2f75cd..292b5b9dddf 100644 --- a/ecs-agent/api/eni/eniattachment.go +++ b/ecs-agent/api/attachment/eni/eniattachment.go @@ -18,8 +18,7 @@ import ( "sync" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" "github.com/aws/amazon-ecs-agent/ecs-agent/utils/arn" @@ -37,7 +36,7 @@ const ( // ENIAttachment contains the information of the eni attachment type ENIAttachment struct { - attachmentinfo.AttachmentInfo + attachment.AttachmentInfo // AttachmentType is the type of the eni attachment, can either be "task-eni" or "instance-eni" AttachmentType string `json:"attachmentType"` // MACAddress is the mac address of eni @@ -118,6 +117,13 @@ func (eni *ENIAttachment) IsSent() bool { return eni.AttachStatusSent } +// SetAttachedStatus marks the eni status as attached +func (eni *ENIAttachment) SetAttachedStatus() { + eni.guard.Lock() + defer eni.guard.Unlock() + eni.Status = attachment.AttachmentAttached +} + // SetSentStatus marks the eni attached status has been sent func (eni *ENIAttachment) SetSentStatus() { eni.guard.Lock() @@ -151,6 +157,18 @@ func (eni *ENIAttachment) String() string { return eni.stringUnsafe() } +func (eni *ENIAttachment) GetAttachmentARN() string { + eni.guard.RLock() + defer eni.guard.RUnlock() + return eni.AttachmentARN +} + +func (eni *ENIAttachment) GetAttachmentStatus() attachment.AttachmentStatus { + eni.guard.RLock() + defer eni.guard.RUnlock() + return eni.Status +} + // stringUnsafe returns a string representation of the ENI Attachment func (eni *ENIAttachment) stringUnsafe() string { // skip TaskArn field for instance level eni attachment since it won't have a task arn @@ -164,3 +182,23 @@ func (eni *ENIAttachment) stringUnsafe() string { "ENI Attachment: task=%s attachment=%s attachmentType=%s attachmentSent=%t mac=%s status=%s expiresAt=%s", eni.TaskARN, eni.AttachmentARN, eni.AttachmentType, eni.AttachStatusSent, eni.MACAddress, eni.Status.String(), eni.ExpiresAt.Format(time.RFC3339)) } + +func (eni *ENIAttachment) GetAttachmentType() string { + eni.guard.RLock() + defer eni.guard.RUnlock() + + return eni.AttachmentType +} + +func (eni *ENIAttachment) ShouldAttach() bool { + eni.guard.RLock() + defer eni.guard.RUnlock() + return !(eni.Status == attachment.AttachmentAttached) && !eni.AttachStatusSent && !(time.Now().After(eni.ExpiresAt)) +} + +// should notify not sent/not expired +func (eni *ENIAttachment) ShouldNotify() bool { + eni.guard.RLock() + defer eni.guard.RUnlock() + return !eni.AttachStatusSent && !(time.Now().After(eni.ExpiresAt)) +} diff --git a/ecs-agent/api/eni/eniattachment_test.go b/ecs-agent/api/attachment/eni/eniattachment_test.go similarity index 87% rename from ecs-agent/api/eni/eniattachment_test.go rename to ecs-agent/api/attachment/eni/eniattachment_test.go index 58d1d1b1bf2..12fa6ef2108 100644 --- a/ecs-agent/api/eni/eniattachment_test.go +++ b/ecs-agent/api/attachment/eni/eniattachment_test.go @@ -22,8 +22,7 @@ import ( "testing" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/stretchr/testify/assert" ) @@ -39,11 +38,11 @@ const ( func TestMarshalUnmarshal(t *testing.T) { expiresAt := time.Now() attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: expiresAt, }, MACAddress: mac, @@ -69,11 +68,11 @@ func TestMarshalUnmarshal(t *testing.T) { func TestMarshalUnmarshalWithAttachmentType(t *testing.T) { expiresAt := time.Now() attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: expiresAt, }, AttachmentType: attachmentType, @@ -101,11 +100,11 @@ func TestMarshalUnmarshalWithAttachmentType(t *testing.T) { func TestStartTimerErrorWhenExpiresAtIsInThePast(t *testing.T) { expiresAt := time.Now().Unix() - 1 attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, MACAddress: mac, @@ -124,11 +123,11 @@ func TestHasExpired(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(tc.expiresAt, 0), }, MACAddress: mac, @@ -147,10 +146,10 @@ func TestInitialize(t *testing.T) { expiresAt := time.Now().Unix() + 1 attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, MACAddress: mac, @@ -162,10 +161,10 @@ func TestInitialize(t *testing.T) { func TestInitializeExpired(t *testing.T) { expiresAt := time.Now().Unix() - 1 attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, MACAddress: mac, @@ -176,11 +175,11 @@ func TestInitializeExpired(t *testing.T) { func TestInitializeExpiredButAlreadySent(t *testing.T) { expiresAt := time.Now().Unix() - 1 attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, MACAddress: mac, diff --git a/ecs-agent/api/resource/ebs_discovery.go b/ecs-agent/api/attachment/resource/ebs_discovery.go similarity index 100% rename from ecs-agent/api/resource/ebs_discovery.go rename to ecs-agent/api/attachment/resource/ebs_discovery.go diff --git a/ecs-agent/api/resource/ebs_discovery_linux.go b/ecs-agent/api/attachment/resource/ebs_discovery_linux.go similarity index 100% rename from ecs-agent/api/resource/ebs_discovery_linux.go rename to ecs-agent/api/attachment/resource/ebs_discovery_linux.go diff --git a/ecs-agent/api/resource/ebs_discovery_linux_test.go b/ecs-agent/api/attachment/resource/ebs_discovery_linux_test.go similarity index 100% rename from ecs-agent/api/resource/ebs_discovery_linux_test.go rename to ecs-agent/api/attachment/resource/ebs_discovery_linux_test.go diff --git a/ecs-agent/api/resource/ebs_discovery_windows.go b/ecs-agent/api/attachment/resource/ebs_discovery_windows.go similarity index 100% rename from ecs-agent/api/resource/ebs_discovery_windows.go rename to ecs-agent/api/attachment/resource/ebs_discovery_windows.go diff --git a/ecs-agent/api/resource/ebs_discovery_windows_test.go b/ecs-agent/api/attachment/resource/ebs_discovery_windows_test.go similarity index 100% rename from ecs-agent/api/resource/ebs_discovery_windows_test.go rename to ecs-agent/api/attachment/resource/ebs_discovery_windows_test.go diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/generate_mocks.go b/ecs-agent/api/attachment/resource/generate_mocks.go similarity index 84% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/generate_mocks.go rename to ecs-agent/api/attachment/resource/generate_mocks.go index a66369fc121..6c86d8825ee 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/generate_mocks.go +++ b/ecs-agent/api/attachment/resource/generate_mocks.go @@ -11,6 +11,6 @@ // express or implied. See the License for the specific language governing // permissions and limitations under the License. -//go:generate mockgen -destination=mocks/ebs_mocks.go -copyright_file=../../../scripts/copyright_file github.com/aws/amazon-ecs-agent/ecs-agent/api/resource EBSDiscovery +//go:generate mockgen -destination=mocks/ebs_mocks.go -copyright_file=../../../../scripts/copyright_file github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource EBSDiscovery package resource diff --git a/ecs-agent/api/resource/interfaces.go b/ecs-agent/api/attachment/resource/interfaces.go similarity index 100% rename from ecs-agent/api/resource/interfaces.go rename to ecs-agent/api/attachment/resource/interfaces.go diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/mocks/ebs_mocks.go b/ecs-agent/api/attachment/resource/mocks/ebs_mocks.go similarity index 95% rename from agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/mocks/ebs_mocks.go rename to ecs-agent/api/attachment/resource/mocks/ebs_mocks.go index 9edfb8c25a6..2c0abac5d48 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/resource/mocks/ebs_mocks.go +++ b/ecs-agent/api/attachment/resource/mocks/ebs_mocks.go @@ -13,7 +13,7 @@ // // Code generated by MockGen. DO NOT EDIT. -// Source: github.com/aws/amazon-ecs-agent/ecs-agent/api/resource (interfaces: EBSDiscovery) +// Source: github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource (interfaces: EBSDiscovery) // Package mock_resource is a generated GoMock package. package mock_resource diff --git a/ecs-agent/api/resource/resource_attachment.go b/ecs-agent/api/attachment/resource/resource_attachment.go similarity index 90% rename from ecs-agent/api/resource/resource_attachment.go rename to ecs-agent/api/attachment/resource/resource_attachment.go index 0091298d522..f4666867cef 100644 --- a/ecs-agent/api/resource/resource_attachment.go +++ b/ecs-agent/api/attachment/resource/resource_attachment.go @@ -19,14 +19,13 @@ import ( "sync" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/utils/ttime" ) type ResourceAttachment struct { - attachmentinfo.AttachmentInfo + attachment.AttachmentInfo // AttachmentType is the type of the resource attachment which can be "amazonebs" for EBS attach tasks. AttachmentType string `json:"AttachmentType,omitempty"` // AttachmentProperties is a map storing (name, value) representation of attachment properties. @@ -215,7 +214,7 @@ func (ra *ResourceAttachment) IsAttached() bool { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.Status == status.AttachmentAttached + return ra.Status == attachment.AttachmentAttached } // SetAttachedStatus marks the resouce attachment as attached once it's been found on the host @@ -223,7 +222,7 @@ func (ra *ResourceAttachment) SetAttachedStatus() { ra.guard.Lock() defer ra.guard.Unlock() - ra.Status = status.AttachmentAttached + ra.Status = attachment.AttachmentAttached } // StopAckTimer stops the ack timer set on the resource attachment @@ -254,7 +253,6 @@ func (ra *ResourceAttachment) SetError(err error) { func (ra *ResourceAttachment) GetError() error { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.err } @@ -262,7 +260,6 @@ func (ra *ResourceAttachment) GetError() error { func (ra *ResourceAttachment) EBSToString() string { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.ebsToStringUnsafe() } @@ -287,42 +284,42 @@ func (ra *ResourceAttachment) GetAttachmentProperties(key string) string { func (ra *ResourceAttachment) GetAttachmentType() string { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.AttachmentType } func (ra *ResourceAttachment) SetDeviceName(deviceName string) { ra.guard.Lock() defer ra.guard.Unlock() - ra.AttachmentProperties[DeviceNameKey] = deviceName } func (ra *ResourceAttachment) GetAttachmentARN() string { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.AttachmentARN } -func (ra *ResourceAttachment) GetExpiresAt() time.Time { +func (ra *ResourceAttachment) GetStatus() attachment.AttachmentStatus { ra.guard.RLock() defer ra.guard.RUnlock() + return ra.Status +} +func (ra *ResourceAttachment) GetExpiresAt() time.Time { + ra.guard.RLock() + defer ra.guard.RUnlock() return ra.ExpiresAt } func (ra *ResourceAttachment) GetClusterARN() string { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.ClusterARN } func (ra *ResourceAttachment) GetContainerInstanceARN() string { ra.guard.RLock() defer ra.guard.RUnlock() - return ra.ContainerInstanceARN } @@ -330,14 +327,31 @@ func (ra *ResourceAttachment) GetContainerInstanceARN() string { func (ra *ResourceAttachment) ShouldAttach() bool { ra.guard.RLock() defer ra.guard.RUnlock() - - return !(ra.Status == status.AttachmentAttached) && !ra.AttachStatusSent && !(time.Now().After(ra.ExpiresAt)) + return !(ra.Status == attachment.AttachmentAttached) && !ra.AttachStatusSent && !(time.Now().After(ra.ExpiresAt)) } // should notify when attached, and not sent/not expired func (ra *ResourceAttachment) ShouldNotify() bool { ra.guard.RLock() defer ra.guard.RUnlock() + return (ra.Status == attachment.AttachmentAttached) && !ra.AttachStatusSent && !(time.Now().After(ra.ExpiresAt)) +} + +func (ra *ResourceAttachment) String() string { + ra.guard.RLock() + defer ra.guard.RUnlock() + return ra.stringUnsafe() +} - return (ra.Status == status.AttachmentAttached) && !ra.AttachStatusSent && !(time.Now().After(ra.ExpiresAt)) +func (ra *ResourceAttachment) GetAttachmentStatus() attachment.AttachmentStatus { + ra.guard.RLock() + defer ra.guard.RUnlock() + return ra.Status +} + +// stringUnsafe returns a string representation of the ENI Attachment +func (ra *ResourceAttachment) stringUnsafe() string { + return fmt.Sprintf( + "Resource Attachment: attachment=%s attachmentType=%s attachmentSent=%t status=%s expiresAt=%s", + ra.AttachmentARN, ra.AttachmentType, ra.AttachStatusSent, ra.Status.String(), ra.ExpiresAt.Format(time.RFC3339)) } diff --git a/ecs-agent/api/resource/resource_attachment_test.go b/ecs-agent/api/attachment/resource/resource_attachment_test.go similarity index 89% rename from ecs-agent/api/resource/resource_attachment_test.go rename to ecs-agent/api/attachment/resource/resource_attachment_test.go index b9ccfbbf879..10ef4fd4c17 100644 --- a/ecs-agent/api/resource/resource_attachment_test.go +++ b/ecs-agent/api/attachment/resource/resource_attachment_test.go @@ -22,8 +22,7 @@ import ( "testing" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/stretchr/testify/assert" ) @@ -48,11 +47,11 @@ var ( func TestMarshalUnmarshal(t *testing.T) { expiresAt := time.Now() attachment := &ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: expiresAt, }, AttachmentProperties: testAttachmentProperties, @@ -86,11 +85,11 @@ func TestMarshalUnmarshal(t *testing.T) { func TestStartTimerErrorWhenExpiresAtIsInThePast(t *testing.T) { expiresAt := time.Now().Unix() - 1 attachment := &ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, AttachmentProperties: testAttachmentProperties, @@ -110,11 +109,11 @@ func TestHasExpired(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { attachment := &ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(tc.expiresAt, 0), }, AttachmentProperties: testAttachmentProperties, @@ -134,10 +133,10 @@ func TestInitialize(t *testing.T) { expiresAt := time.Now().Unix() + 1 attachment := &ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, AttachmentProperties: testAttachmentProperties, @@ -150,10 +149,10 @@ func TestInitialize(t *testing.T) { func TestInitializeExpired(t *testing.T) { expiresAt := time.Now().Unix() - 1 attachment := &ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, AttachmentProperties: testAttachmentProperties, @@ -165,11 +164,11 @@ func TestInitializeExpired(t *testing.T) { func TestInitializeExpiredButAlreadySent(t *testing.T) { expiresAt := time.Now().Unix() - 1 attachment := &ResourceAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, AttachmentProperties: testAttachmentProperties, diff --git a/ecs-agent/api/resource/resource_type.go b/ecs-agent/api/attachment/resource/resource_type.go similarity index 100% rename from ecs-agent/api/resource/resource_type.go rename to ecs-agent/api/attachment/resource/resource_type.go diff --git a/ecs-agent/api/resource/resource_validation.go b/ecs-agent/api/attachment/resource/resource_validation.go similarity index 100% rename from ecs-agent/api/resource/resource_validation.go rename to ecs-agent/api/attachment/resource/resource_validation.go diff --git a/ecs-agent/api/resource/resource_validation_test.go b/ecs-agent/api/attachment/resource/resource_validation_test.go similarity index 100% rename from ecs-agent/api/resource/resource_validation_test.go rename to ecs-agent/api/attachment/resource/resource_validation_test.go diff --git a/ecs-agent/api/ecs/statechange/statechange_test.go b/ecs-agent/api/ecs/statechange/statechange_test.go index 2b28600d9e7..1e5299dcbd3 100644 --- a/ecs-agent/api/ecs/statechange/statechange_test.go +++ b/ecs-agent/api/ecs/statechange/statechange_test.go @@ -22,10 +22,9 @@ import ( "testing" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status" mock_statechange "github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/statechange/mocks" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status" "github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" @@ -106,7 +105,7 @@ func TestTaskStateChangeString(t *testing.T) { TaskARN: taskArn, Status: apitaskstatus.TaskRunning, Attachment: &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: attachmentArn, }, }, @@ -148,9 +147,9 @@ func TestTaskStateChangeString(t *testing.T) { func TestAttachmentStateChangeString(t *testing.T) { change := &AttachmentStateChange{ Attachment: &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: attachmentArn, - Status: status.AttachmentAttached, + Status: attachment.AttachmentAttached, TaskARN: taskArn, AttachStatusSent: true, ExpiresAt: dummyTime, diff --git a/ecs-agent/netlib/model/networkinterface/eniattachment.go b/ecs-agent/netlib/model/networkinterface/eniattachment.go index 347f0714968..3ff374e4e7a 100644 --- a/ecs-agent/netlib/model/networkinterface/eniattachment.go +++ b/ecs-agent/netlib/model/networkinterface/eniattachment.go @@ -18,7 +18,7 @@ import ( "sync" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" "github.com/aws/amazon-ecs-agent/ecs-agent/utils/arn" @@ -36,7 +36,7 @@ const ( // ENIAttachment contains the information of the eni attachment type ENIAttachment struct { - attachmentinfo.AttachmentInfo + attachment.AttachmentInfo // AttachmentType is the type of the eni attachment, can either be "task-eni" or "instance-eni" AttachmentType string `json:"attachmentType"` // MACAddress is the mac address of eni @@ -117,6 +117,13 @@ func (eni *ENIAttachment) IsSent() bool { return eni.AttachStatusSent } +// SetAttachedStatus marks the eni status as attached +func (eni *ENIAttachment) SetAttachedStatus() { + eni.guard.Lock() + defer eni.guard.Unlock() + eni.Status = attachment.AttachmentAttached +} + // SetSentStatus marks the eni attached status has been sent func (eni *ENIAttachment) SetSentStatus() { eni.guard.Lock() @@ -150,6 +157,18 @@ func (eni *ENIAttachment) String() string { return eni.stringUnsafe() } +func (eni *ENIAttachment) GetAttachmentARN() string { + eni.guard.RLock() + defer eni.guard.RUnlock() + return eni.AttachmentARN +} + +func (eni *ENIAttachment) GetAttachmentStatus() attachment.AttachmentStatus { + eni.guard.RLock() + defer eni.guard.RUnlock() + return eni.Status +} + // stringUnsafe returns a string representation of the ENI Attachment func (eni *ENIAttachment) stringUnsafe() string { // skip TaskArn field for instance level eni attachment since it won't have a task arn @@ -163,3 +182,23 @@ func (eni *ENIAttachment) stringUnsafe() string { "ENI Attachment: task=%s attachment=%s attachmentType=%s attachmentSent=%t mac=%s status=%s expiresAt=%s", eni.TaskARN, eni.AttachmentARN, eni.AttachmentType, eni.AttachStatusSent, eni.MACAddress, eni.Status.String(), eni.ExpiresAt.Format(time.RFC3339)) } + +func (eni *ENIAttachment) GetAttachmentType() string { + eni.guard.RLock() + defer eni.guard.RUnlock() + + return eni.AttachmentType +} + +func (eni *ENIAttachment) ShouldAttach() bool { + eni.guard.RLock() + defer eni.guard.RUnlock() + return !(eni.Status == attachment.AttachmentAttached) && !eni.AttachStatusSent && !(time.Now().After(eni.ExpiresAt)) +} + +// should notify when attached, and not sent/not expired +func (eni *ENIAttachment) ShouldNotify() bool { + eni.guard.RLock() + defer eni.guard.RUnlock() + return !eni.AttachStatusSent && !(time.Now().After(eni.ExpiresAt)) +} diff --git a/ecs-agent/netlib/model/networkinterface/eniattachment_test.go b/ecs-agent/netlib/model/networkinterface/eniattachment_test.go index 96bf056f838..17015019007 100644 --- a/ecs-agent/netlib/model/networkinterface/eniattachment_test.go +++ b/ecs-agent/netlib/model/networkinterface/eniattachment_test.go @@ -22,8 +22,7 @@ import ( "testing" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment" "github.com/stretchr/testify/assert" ) @@ -39,11 +38,11 @@ const ( func TestMarshalUnmarshal(t *testing.T) { expiresAt := time.Now() attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: expiresAt, }, MACAddress: mac, @@ -69,11 +68,11 @@ func TestMarshalUnmarshal(t *testing.T) { func TestMarshalUnmarshalWithAttachmentType(t *testing.T) { expiresAt := time.Now() attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: expiresAt, }, AttachmentType: attachmentType, @@ -101,11 +100,11 @@ func TestMarshalUnmarshalWithAttachmentType(t *testing.T) { func TestStartTimerErrorWhenExpiresAtIsInThePast(t *testing.T) { expiresAt := time.Now().Unix() - 1 attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, MACAddress: mac, @@ -124,11 +123,11 @@ func TestHasExpired(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(tc.expiresAt, 0), }, MACAddress: mac, @@ -147,10 +146,10 @@ func TestInitialize(t *testing.T) { expiresAt := time.Now().Unix() + 1 attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, MACAddress: mac, @@ -162,10 +161,10 @@ func TestInitialize(t *testing.T) { func TestInitializeExpired(t *testing.T) { expiresAt := time.Now().Unix() - 1 attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, MACAddress: mac, @@ -176,11 +175,11 @@ func TestInitializeExpired(t *testing.T) { func TestInitializeExpiredButAlreadySent(t *testing.T) { expiresAt := time.Now().Unix() - 1 attachment := &ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ TaskARN: taskARN, AttachmentARN: attachmentARN, AttachStatusSent: attachSent, - Status: status.AttachmentNone, + Status: attachment.AttachmentNone, ExpiresAt: time.Unix(expiresAt, 0), }, MACAddress: mac,