From 12e23ed1389070ef45955ae2b74ad698e99ce523 Mon Sep 17 00:00:00 2001 From: Ray Allan Date: Fri, 20 Oct 2023 17:10:11 -0700 Subject: [PATCH] Create Generic Attachment to decouple from ENI and save attachment state. (#3969) --- 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 | 12 +- 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 +- ...ient_test.go => attachment_client_test.go} | 62 ++- agent/data/client.go | 13 +- agent/data/eniattachment_client.go | 2 +- agent/data/noop_client.go | 13 + agent/data/resattachment_client.go | 58 +++ agent/ebs/watcher.go | 21 +- agent/ebs/watcher_test.go | 35 +- agent/engine/data.go | 17 +- 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 | 7 +- agent/eni/watcher/watcher_linux_test.go | 14 +- agent/eni/watcher/watcher_test.go | 25 +- agent/eni/watcher/watcher_windows_test.go | 16 +- agent/eventhandler/attachment_handler.go | 27 +- agent/eventhandler/attachment_handler_test.go | 110 +++-- agent/eventhandler/handler_test.go | 4 +- 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 | 29 ++ .../api/attachment}/attachment_info.go | 6 +- .../api/attachment}/attachment_status.go | 2 +- .../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 .../resource/mocks/ebs_mocks.go | 2 +- .../resource/resource_attachment.go | 46 ++- .../resource/resource_type.go | 0 .../resource/resource_validation.go | 0 .../ecs-agent/api/eni/eniattachment.go | 166 -------- .../model/networkinterface/eniattachment.go | 43 +- agent/vendor/modules.txt | 8 +- .../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 | 29 ++ .../api/attachment}/attachment_info.go | 6 +- .../api/attachment}/attachment_status.go | 2 +- .../attachment_status_test.go | 2 +- .../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 .../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 +- ecs-agent/api/eni/eni.go | 386 ------------------ .../model/networkinterface/eniattachment.go | 43 +- .../networkinterface/eniattachment_test.go | 31 +- 92 files changed, 701 insertions(+), 901 deletions(-) rename agent/data/{eniattachment_client_test.go => attachment_client_test.go} (53%) 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 {ecs-agent/api/attachmentinfo => agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment}/attachment_info.go (92%) rename {ecs-agent/api/status => agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment}/attachment_status.go (98%) 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 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%) delete mode 100644 agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/eni/eniattachment.go create mode 100644 ecs-agent/api/attachment/attachment.go rename {agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo => ecs-agent/api/attachment}/attachment_info.go (92%) rename {agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/status => ecs-agent/api/attachment}/attachment_status.go (98%) rename ecs-agent/api/{status => attachment}/attachment_status_test.go (98%) rename {agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api => 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 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%) delete mode 100644 ecs-agent/api/eni/eni.go diff --git a/agent/acs/session/attach_eni_common.go b/agent/acs/session/attach_eni_common.go index 3f05c429f45..f4f4ba432be 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.GetAttachmentId(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..0281e13f7d5 100644 --- a/agent/acs/session/payload_responder_test.go +++ b/agent/acs/session/payload_responder_test.go @@ -34,10 +34,10 @@ 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" + 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" + ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" "github.com/aws/amazon-ecs-agent/ecs-agent/wsclient" "github.com/aws/aws-sdk-go/aws" "github.com/golang/mock/gomock" @@ -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(ni.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, ni.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..663d9ac07e4 100644 --- a/agent/api/ecsclient/client_test.go +++ b/agent/api/ecsclient/client_test.go @@ -24,9 +24,6 @@ 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/golang/mock/gomock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -37,6 +34,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/config" "github.com/aws/amazon-ecs-agent/agent/ec2" mock_ec2 "github.com/aws/amazon-ecs-agent/agent/ec2/mocks" + "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/async" @@ -1063,9 +1061,9 @@ func TestDiscoverTelemetryEndpointAfterPollEndpointCacheHit(t *testing.T) { } } -// TestSubmitTaskStateChangeWithAttachments tests the SubmitTaskStateChange API +// TestSubmitTaskStateChangeWithENIAttachments tests the SubmitTaskStateChange API // also send the Attachment Status -func TestSubmitTaskStateChangeWithAttachments(t *testing.T) { +func TestSubmitTaskStateChangeWithENIAttachments(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() @@ -1086,9 +1084,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..8651df6386e 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" @@ -98,8 +99,7 @@ type TaskStateChange struct { // AttachmentStateChange represents a state change that needs to be sent to the // SubmitAttachmentStateChanges API type AttachmentStateChange struct { - // Attachment is the eni attachment object to send - Attachment *ni.ENIAttachment + Attachment attachment.Attachment } type ErrShouldNotSendEvent struct { @@ -352,7 +352,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/eniattachment_client_test.go b/agent/data/attachment_client_test.go similarity index 53% rename from agent/data/eniattachment_client_test.go rename to agent/data/attachment_client_test.go index e876b56ec72..248cc3dff18 100644 --- a/agent/data/eniattachment_client_test.go +++ b/agent/data/attachment_client_test.go @@ -19,7 +19,8 @@ package data import ( "testing" - "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/resource" ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" "github.com/stretchr/testify/assert" ) @@ -27,13 +28,15 @@ import ( const ( testAttachmentArn = "arn:aws:ecs:us-west-2:167933679560:attachment/test-arn" testAttachmentArn2 = "arn:aws:ecs:us-west-2:167933679560:attachment/test-arn2" + testAttachmentArn3 = "arn:aws:ecs:us-west-2:123456789012:volume/test-arn3" + testAttachmentArn4 = "arn:aws:ecs:us-west-2:123456789012:volume/test-arn4" ) func TestManageENIAttachments(t *testing.T) { testClient := newTestClient(t) testEniAttachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: testAttachmentArn, AttachStatusSent: false, }, @@ -49,7 +52,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, }, @@ -67,11 +70,49 @@ func TestManageENIAttachments(t *testing.T) { assert.Len(t, res, 0) } +func TestManageResourceAttachments(t *testing.T) { + testClient := newTestClient(t) + + testResAttachment := &resource.ResourceAttachment{ + AttachmentInfo: attachment.AttachmentInfo{ + AttachmentARN: testAttachmentArn3, + AttachStatusSent: false, + }, + } + + assert.NoError(t, testClient.SaveResourceAttachment(testResAttachment)) + testResAttachment.SetSentStatus() + assert.NoError(t, testClient.SaveResourceAttachment(testResAttachment)) + res, err := testClient.GetResourceAttachments() + assert.NoError(t, err) + assert.Len(t, res, 1) + assert.Equal(t, true, res[0].AttachStatusSent) + assert.Equal(t, testAttachmentArn3, res[0].AttachmentARN) + + testResAttachment2 := &resource.ResourceAttachment{ + AttachmentInfo: attachment.AttachmentInfo{ + AttachmentARN: testAttachmentArn4, + AttachStatusSent: true, + }, + } + + assert.NoError(t, testClient.SaveResourceAttachment(testResAttachment2)) + res, err = testClient.GetResourceAttachments() + assert.NoError(t, err) + assert.Len(t, res, 2) + + assert.NoError(t, testClient.DeleteResourceAttachment("test-arn3")) + assert.NoError(t, testClient.DeleteResourceAttachment("test-arn4")) + res, err = testClient.GetResourceAttachments() + assert.NoError(t, err) + assert.Len(t, res, 0) +} + func TestSaveENIAttachmentInvalidID(t *testing.T) { testClient := newTestClient(t) testEniAttachment := &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: "invalid-arn", AttachStatusSent: false, }, @@ -79,3 +120,16 @@ func TestSaveENIAttachmentInvalidID(t *testing.T) { assert.Error(t, testClient.SaveENIAttachment(testEniAttachment)) } + +func TestSaveResAttachmentInvalidID(t *testing.T) { + testClient := newTestClient(t) + + testResAttachment := &resource.ResourceAttachment{ + AttachmentInfo: attachment.AttachmentInfo{ + AttachmentARN: "invalid-arn", + AttachStatusSent: false, + }, + } + + assert.Error(t, testClient.SaveResourceAttachment(testResAttachment)) +} 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..d52c2d5778e 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.GetAttachmentId(eni.AttachmentARN) if err != nil { return errors.Wrap(err, "failed to generate database id") } 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..c93e0450b13 --- /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.GetAttachmentId(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..69c7b1c6dbb 100644 --- a/agent/ebs/watcher.go +++ b/agent/ebs/watcher.go @@ -24,12 +24,9 @@ import ( ecsapi "github.com/aws/amazon-ecs-agent/agent/api" 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 +246,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 +293,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..1b3146a5a21 100644 --- a/agent/engine/data.go +++ b/agent/engine/data.go @@ -40,6 +40,10 @@ func (engine *DockerTaskEngine) LoadState() error { return err } + if err := engine.loadResourceAttachments(); err != nil { + return err + } + return engine.loadENIAttachments() } @@ -119,6 +123,17 @@ func (engine *DockerTaskEngine) loadImageStates() error { return nil } +func (engine *DockerTaskEngine) loadResourceAttachments() error { + resAttachments, err := engine.dataClient.GetResourceAttachments() + if err != nil { + return err + } + 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 +251,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.GetAttachmentId(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..2f3d4136a15 100644 --- a/agent/eni/watcher/watcher.go +++ b/agent/eni/watcher/watcher.go @@ -18,8 +18,6 @@ import ( "fmt" "time" - "github.com/aws/amazon-ecs-agent/ecs-agent/api/status" - "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" "github.com/aws/amazon-ecs-agent/agent/statechange" @@ -27,6 +25,7 @@ import ( "github.com/pkg/errors" "github.com/aws/amazon-ecs-agent/agent/api" + "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" @@ -153,7 +152,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 +163,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..b073afcec84 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), }, @@ -201,7 +199,6 @@ func TestReconcileENIs(t *testing.T) { require.NoError(t, watcher.reconcileOnce(false)) <-done - assert.NotNil(t, event.(api.TaskStateChange).Attachment) assert.Equal(t, randomMAC, event.(api.TaskStateChange).Attachment.MACAddress) select { @@ -238,7 +235,6 @@ func TestReconcileENIsWithRetry(t *testing.T) { require.NoError(t, watcher.reconcileOnce(true)) <-done - require.NotNil(t, event.(api.TaskStateChange).Attachment) assert.Equal(t, randomMAC, event.(api.TaskStateChange).Attachment.MACAddress) select { @@ -250,7 +246,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 +367,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 +382,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..63f1407c241 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,19 @@ 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 typedAttachment := attachmentChange.Attachment.(type) { + case *ni.ENIAttachment: + err := handler.dataClient.SaveENIAttachment(typedAttachment) + if err != nil { + seelog.Errorf("AttachmentHandler: error saving ENI state after submitted attachment state change [%s]: %v", attachmentChange.String(), err) + } + case *resource.ResourceAttachment: + err := handler.dataClient.SaveResourceAttachment(typedAttachment) + if err != nil { + seelog.Errorf("AttachmentHandler: error saving Resource state after submitted attachment state change [%s]: %v", attachmentChange.String(), err) + } + default: + seelog.Errorf("Unable to save attachment state after submitted attachment state change [%s]: unknown attachment type", 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..d12cdcfbef4 100644 --- a/agent/eventhandler/attachment_handler_test.go +++ b/agent/eventhandler/attachment_handler_test.go @@ -25,7 +25,8 @@ 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" + "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/resource" 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" @@ -45,12 +46,12 @@ const ( attachmentARN = "arn:aws:ecs:us-west-2:1234567890:attachment/abc" ) -func TestSendAttachmentEvent(t *testing.T) { +func TestSendENIAttachmentEvent(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mock_api.NewMockECSClient(ctrl) - attachmentEvent := attachmentEvent(attachmentARN) + attachmentEvent := eniAttachmentEvent(attachmentARN) timeoutFunc := func() { t.Error("Timeout sending ENI attach status") @@ -66,7 +67,37 @@ 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() + }) + + require.NoError(t, handler.AddStateChangeEvent(attachmentEvent)) + + wg.Wait() +} + +func TestSendResAttachmentEvent(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + client := mock_api.NewMockECSClient(ctrl) + + attachmentEvent := resAttachmentEvent(attachmentARN) + + timeoutFunc := func() { + t.Error("Timeout sending ENI attach status") + } + assert.NoError(t, attachmentEvent.Attachment.StartTimer(timeoutFunc)) + + ctx, cancel := context.WithCancel(context.Background()) + handler := NewAttachmentEventHandler(ctx, data.NewNoopClient(), client) + defer cancel() + + var wg sync.WaitGroup + wg.Add(1) + + client.EXPECT().SubmitAttachmentStateChange(gomock.Any()).Return(nil).Do(func(change api.AttachmentStateChange) { + assert.NotNil(t, change.Attachment) + assert.Equal(t, attachmentARN, change.Attachment.GetAttachmentARN()) wg.Done() }) @@ -80,7 +111,7 @@ func TestSendAttachmentEventRetries(t *testing.T) { defer ctrl.Finish() client := mock_api.NewMockECSClient(ctrl) - attachmentEvent := attachmentEvent(attachmentARN) + attachmentEvent := eniAttachmentEvent(attachmentARN) timeoutFunc := func() { t.Error("Timeout sending ENI attach status") @@ -104,7 +135,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() }), ) @@ -114,14 +145,14 @@ func TestSendAttachmentEventRetries(t *testing.T) { wg.Wait() } -func TestSendMultipleAttachmentEventsDifferentAttachments(t *testing.T) { +func TestSendMutipleAttachmentEventsMixedAttachments(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mock_api.NewMockECSClient(ctrl) - attachmentEvent1 := attachmentEvent("attachmentARN1") - attachmentEvent2 := attachmentEvent("attachmentARN2") - attachmentEvent3 := attachmentEvent("attachmentARN3") + attachmentEvent1 := eniAttachmentEvent("attachmentARN1") + attachmentEvent2 := resAttachmentEvent("attachmentARN2") + attachmentEvent3 := resAttachmentEvent("attachmentARN3") timeoutFunc := func() { t.Error("Timeout sending ENI attach status") @@ -143,7 +174,7 @@ func TestSendMultipleAttachmentEventsDifferentAttachments(t *testing.T) { mapLock.Lock() defer mapLock.Unlock() - submittedAttachments[change.Attachment.AttachmentARN] = true + submittedAttachments[change.Attachment.GetAttachmentARN()] = true wg.Done() }) @@ -165,7 +196,7 @@ func TestSubmitAttachmentEventSucceeds(t *testing.T) { dataClient := newTestDataClient(t) - attachmentEvent := attachmentEvent(attachmentARN) + attachmentEvent := eniAttachmentEvent(attachmentARN) timeoutFunc := func() { t.Error("Timeout sending ENI attach status") @@ -182,12 +213,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 +229,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 := eniAttachmentEventWithExpiry(attachmentARN, 100*time.Millisecond) // wait until eni attachment expires time.Sleep(200 * time.Millisecond) @@ -214,7 +244,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) { @@ -222,7 +252,7 @@ func TestSubmitAttachmentEventAttachmentIsSent(t *testing.T) { defer ctrl.Finish() client := mock_api.NewMockECSClient(ctrl) - attachmentEvent := attachmentEvent(attachmentARN) + attachmentEvent := resAttachmentEvent(attachmentARN) attachmentEvent.Attachment.SetSentStatus() timeoutFunc := func() { @@ -243,32 +273,40 @@ 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 eniAttachmentEvent(attachmentARN string) api.AttachmentStateChange { + return api.AttachmentStateChange{ + Attachment: &ni.ENIAttachment{ + AttachmentInfo: attachment.AttachmentInfo{ + AttachmentARN: attachmentARN, + AttachStatusSent: false, + ExpiresAt: time.Now().Add(time.Second), + }, + AttachmentType: ni.ENIAttachmentTypeInstanceENI, + }, + } } -func TestAttachmentChangeShouldBeSentAttachmentIsSent(t *testing.T) { - attachmentEvent := attachmentEvent(attachmentARN) - attachmentEvent.Attachment.SetSentStatus() - assert.False(t, attachmentChangeShouldBeSent(&attachmentEvent)) +func resAttachmentEvent(attachmentARN string) api.AttachmentStateChange { + return api.AttachmentStateChange{ + Attachment: &resource.ResourceAttachment{ + AttachmentInfo: attachment.AttachmentInfo{ + AttachmentARN: attachmentARN, + Status: attachment.AttachmentAttached, + AttachStatusSent: false, + ExpiresAt: time.Now().Add(time.Second), + }, + AttachmentType: resource.EBSTaskAttach, + }, + } } -func attachmentEvent(attachmentARN string) api.AttachmentStateChange { +func eniAttachmentEventWithExpiry(attachmentARN string, expiresAfter time.Duration) api.AttachmentStateChange { return api.AttachmentStateChange{ Attachment: &ni.ENIAttachment{ - AttachmentInfo: attachmentinfo.AttachmentInfo{ + AttachmentInfo: attachment.AttachmentInfo{ AttachmentARN: attachmentARN, AttachStatusSent: false, - ExpiresAt: time.Now().Add(time.Second), + ExpiresAt: time.Now().Add(expiresAfter), }, AttachmentType: ni.ENIAttachmentTypeInstanceENI, }, diff --git a/agent/eventhandler/handler_test.go b/agent/eventhandler/handler_test.go index 1d85fa99e0e..cdc8636027c 100644 --- a/agent/eventhandler/handler_test.go +++ b/agent/eventhandler/handler_test.go @@ -46,7 +46,7 @@ func TestHandleEngineEvent(t *testing.T) { contEvent1 := containerEvent(taskARN) contEvent2 := containerEvent(taskARN) taskEvent := taskEvent(taskARN) - attachmentEvent := attachmentEvent("attachmentARN") + attachmentEvent := eniAttachmentEvent("attachmentARN") timeoutFunc := func() { t.Error("Timeout sending ENI attach status") @@ -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..e2a5b19565e 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) +// GetAttachmentId retrieves the ID from an attachment's ARN. +// asssumes arn structure: arn:[partition]:ec2:[region]:[account-id]:[attachment-type]/[resource-id] +func GetAttachmentId(attachmentArn string) (string, error) { + _, err := arn.Parse(attachmentArn) 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", attachmentArn) } - fields := strings.Split(eniAttachmentArn, "/") + fields := strings.Split(attachmentArn, "/") 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", attachmentArn) } return fields[len(fields)-1], nil } diff --git a/agent/utils/utils_test.go b/agent/utils/utils_test.go index 3923acb2eb7..947aa8aafd7 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 TestGetAttachmentId(t *testing.T) { attachmentArn := "arn:aws:ecs:us-west-2:1234567890:attachment/abc" - id, err := GetENIAttachmentId(attachmentArn) + id, err := GetAttachmentId(attachmentArn) require.NoError(t, err) assert.Equal(t, "abc", id) - _, err = GetENIAttachmentId("invalid") + _, err = GetAttachmentId("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..54384b7dfe8 --- /dev/null +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/attachment.go @@ -0,0 +1,29 @@ +// 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 + HasExpired() bool + 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/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/attachment/attachment_info.go similarity index 92% rename from 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/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/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 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/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/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/agent/vendor/github.com/aws/amazon-ecs-agent/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 agent/vendor/github.com/aws/amazon-ecs-agent/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/agent/vendor/github.com/aws/amazon-ecs-agent/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/api/eni/eniattachment.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/eni/eniattachment.go deleted file mode 100644 index 8b45d2f75cd..00000000000 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/eni/eniattachment.go +++ /dev/null @@ -1,166 +0,0 @@ -// 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 eni - -import ( - "fmt" - "sync" - "time" - - "github.com/aws/amazon-ecs-agent/ecs-agent/api/attachmentinfo" - - "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" - "github.com/aws/amazon-ecs-agent/ecs-agent/utils/ttime" - - "github.com/pkg/errors" -) - -const ( - // ENIAttachmentTypeTaskENI represents the type of a task level eni - ENIAttachmentTypeTaskENI = "task-eni" - // ENIAttachmentTypeInstanceENI represents the type of an instance level eni - ENIAttachmentTypeInstanceENI = "instance-eni" -) - -// ENIAttachment contains the information of the eni attachment -type ENIAttachment struct { - attachmentinfo.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 - MACAddress string `json:"macAddress"` - // ackTimer is used to register the expiration timeout callback for unsuccessful - // ENI attachments - ackTimer ttime.Timer - // guard protects access to fields of this struct - guard sync.RWMutex -} - -func getEniAttachmentLogFields(eni *ENIAttachment, duration time.Duration) logger.Fields { - fields := logger.Fields{ - "duration": duration.String(), - "attachmentARN": eni.AttachmentARN, - "attachmentType": eni.AttachmentType, - "attachmentSent": eni.AttachStatusSent, - "mac": eni.MACAddress, - "status": eni.Status.String(), - "expiresAt": eni.ExpiresAt.Format(time.RFC3339), - } - - if eni.AttachmentType != ENIAttachmentTypeInstanceENI { - taskId, _ := arn.TaskIdFromArn(eni.TaskARN) - fields[field.TaskID] = taskId - } - - return fields -} - -// StartTimer starts the ack timer to record the expiration of ENI attachment -func (eni *ENIAttachment) StartTimer(timeoutFunc func()) error { - eni.guard.Lock() - defer eni.guard.Unlock() - - if eni.ackTimer != nil { - // The timer has already been initialized, do nothing - return nil - } - now := time.Now() - duration := eni.ExpiresAt.Sub(now) - if duration <= 0 { - return errors.Errorf("eni attachment: timer expiration is in the past; expiration [%s] < now [%s]", - eni.ExpiresAt.String(), now.String()) - } - logger.Info("Starting ENI ack timer", getEniAttachmentLogFields(eni, duration)) - eni.ackTimer = time.AfterFunc(duration, timeoutFunc) - return nil -} - -// Initialize initializes the fields that can't be populated from loading state file. -// Notably, this initializes the ack timer so that if we times out waiting for the eni to be attached, the attachment -// can be removed from state. -func (eni *ENIAttachment) Initialize(timeoutFunc func()) error { - eni.guard.Lock() - defer eni.guard.Unlock() - - if eni.AttachStatusSent { // eni attachment status has been sent, no need to start ack timer. - return nil - } - - now := time.Now() - duration := eni.ExpiresAt.Sub(now) - if duration <= 0 { - return errors.New("ENI attachment has already expired") - } - - logger.Info("Starting ENI ack timer", getEniAttachmentLogFields(eni, duration)) - eni.ackTimer = time.AfterFunc(duration, timeoutFunc) - return nil -} - -// IsSent checks if the eni attached status has been sent -func (eni *ENIAttachment) IsSent() bool { - eni.guard.RLock() - defer eni.guard.RUnlock() - - return eni.AttachStatusSent -} - -// SetSentStatus marks the eni attached status has been sent -func (eni *ENIAttachment) SetSentStatus() { - eni.guard.Lock() - defer eni.guard.Unlock() - - eni.AttachStatusSent = true -} - -// StopAckTimer stops the ack timer set on the ENI attachment -func (eni *ENIAttachment) StopAckTimer() { - eni.guard.Lock() - defer eni.guard.Unlock() - - eni.ackTimer.Stop() -} - -// HasExpired returns true if the ENI attachment object has exceeded the -// threshold for notifying the backend of the attachment -func (eni *ENIAttachment) HasExpired() bool { - eni.guard.RLock() - defer eni.guard.RUnlock() - - return time.Now().After(eni.ExpiresAt) -} - -// String returns a string representation of the ENI Attachment -func (eni *ENIAttachment) String() string { - eni.guard.RLock() - defer eni.guard.RUnlock() - - return eni.stringUnsafe() -} - -// 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 - if eni.AttachmentType == ENIAttachmentTypeInstanceENI { - return fmt.Sprintf( - "ENI Attachment: attachment=%s attachmentType=%s attachmentSent=%t mac=%s status=%s expiresAt=%s", - eni.AttachmentARN, eni.AttachmentType, eni.AttachStatusSent, eni.MACAddress, eni.Status.String(), eni.ExpiresAt.Format(time.RFC3339)) - } - - return fmt.Sprintf( - "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)) -} 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 23f70f1d140..38f82cb663f 100644 --- a/agent/vendor/modules.txt +++ b/agent/vendor/modules.txt @@ -12,14 +12,12 @@ 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/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..54384b7dfe8 --- /dev/null +++ b/ecs-agent/api/attachment/attachment.go @@ -0,0 +1,29 @@ +// 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 + HasExpired() bool + 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/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 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/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/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 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/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/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/eni/eni.go b/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 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/ecs-agent/api/resource/mocks/ebs_mocks.go b/ecs-agent/api/attachment/resource/mocks/ebs_mocks.go similarity index 95% rename from ecs-agent/api/resource/mocks/ebs_mocks.go rename to ecs-agent/api/attachment/resource/mocks/ebs_mocks.go index 9edfb8c25a6..2c0abac5d48 100644 --- a/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/api/eni/eni.go b/ecs-agent/api/eni/eni.go deleted file mode 100644 index 21cef1748c8..00000000000 --- a/ecs-agent/api/eni/eni.go +++ /dev/null @@ -1,386 +0,0 @@ -// 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 eni - -import ( - "fmt" - "net" - "strings" - "sync" - - "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" - "github.com/aws/amazon-ecs-agent/ecs-agent/logger" - loggerfield "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" - "github.com/aws/aws-sdk-go/aws" - "github.com/pkg/errors" -) - -// ENI contains information of the eni -type ENI struct { - // ID is the id of eni - ID string `json:"ec2Id"` - // LinkName is the name of the ENI on the instance. - // Currently, this field is being used only for Windows and is used during task networking setup. - LinkName string - // MacAddress is the mac address of the eni - MacAddress string - // IPV4Addresses is the ipv4 address associated with the eni - IPV4Addresses []*ENIIPV4Address - // IPV6Addresses is the ipv6 address associated with the eni - IPV6Addresses []*ENIIPV6Address - // SubnetGatewayIPV4Address is the IPv4 address of the subnet gateway of the ENI - SubnetGatewayIPV4Address string `json:",omitempty"` - // DomainNameServers specifies the nameserver IP addresses for the eni - DomainNameServers []string `json:",omitempty"` - // DomainNameSearchList specifies the search list for the domain - // name lookup, for the eni - DomainNameSearchList []string `json:",omitempty"` - // PrivateDNSName is the dns name assigned by the vpc to this eni - PrivateDNSName string `json:",omitempty"` - // InterfaceAssociationProtocol is the type of ENI, valid value: "default", "vlan" - InterfaceAssociationProtocol string `json:",omitempty"` - // InterfaceVlanProperties contains information for an interface - // that is supposed to be used as a VLAN device - InterfaceVlanProperties *InterfaceVlanProperties `json:",omitempty"` - - // Due to historical reasons, the IPv4 subnet prefix length is sent with IPv4 subnet gateway - // address instead of the ENI's IP addresses. However, CNI plugins and many OS APIs expect it - // the other way around. Instead of doing this conversion all the time for each CNI and TMDS - // request, compute it once on demand and cache it here. - ipv4SubnetPrefixLength string - ipv4SubnetCIDRBlock string - ipv6SubnetCIDRBlock string - - // guard protects access to fields of this struct. - guard sync.RWMutex -} - -// InterfaceVlanProperties contains information for an interface that -// is supposed to be used as a VLAN device -type InterfaceVlanProperties struct { - VlanID string - TrunkInterfaceMacAddress string -} - -const ( - // DefaultInterfaceAssociationProtocol represents the standard ENI type. - DefaultInterfaceAssociationProtocol = "default" - - // VLANInterfaceAssociationProtocol represents the ENI with trunking enabled. - VLANInterfaceAssociationProtocol = "vlan" - - // IPv6SubnetPrefixLength is the IPv6 global unicast address prefix length, consisting of - // global routing prefix and subnet ID lengths as specified in IPv6 addressing architecture - // (RFC 4291 section 2.5.4) and IPv6 Global Unicast Address Format (RFC 3587). - // The ACS ENI payload structure does not contain an IPv6 subnet prefix length because "/64" is - // the only allowed length per RFCs above, and the only one that VPC supports. - IPv6SubnetPrefixLength = "64" -) - -var ( - // netInterfaces is the Interfaces() method of net package. - netInterfaces = net.Interfaces -) - -// GetIPV4Addresses returns the list of IPv4 addresses assigned to the ENI. -func (eni *ENI) GetIPV4Addresses() []string { - var addresses []string - for _, addr := range eni.IPV4Addresses { - addresses = append(addresses, addr.Address) - } - - return addresses -} - -// GetIPV6Addresses returns the list of IPv6 addresses assigned to the ENI. -func (eni *ENI) GetIPV6Addresses() []string { - var addresses []string - for _, addr := range eni.IPV6Addresses { - addresses = append(addresses, addr.Address) - } - - return addresses -} - -// GetPrimaryIPv4Address returns the primary IPv4 address assigned to the ENI. -func (eni *ENI) GetPrimaryIPv4Address() string { - var primaryAddr string - for _, addr := range eni.IPV4Addresses { - if addr.Primary { - primaryAddr = addr.Address - break - } - } - - return primaryAddr -} - -// GetPrimaryIPv4AddressWithPrefixLength returns the primary IPv4 address assigned to the ENI with -// its subnet prefix length. -func (eni *ENI) GetPrimaryIPv4AddressWithPrefixLength() string { - return eni.GetPrimaryIPv4Address() + "/" + eni.GetIPv4SubnetPrefixLength() -} - -// GetIPAddressesWithPrefixLength returns the list of all IP addresses assigned to the ENI with -// their subnet prefix length. -func (eni *ENI) GetIPAddressesWithPrefixLength() []string { - var addresses []string - for _, addr := range eni.IPV4Addresses { - addresses = append(addresses, addr.Address+"/"+eni.GetIPv4SubnetPrefixLength()) - } - for _, addr := range eni.IPV6Addresses { - addresses = append(addresses, addr.Address+"/"+IPv6SubnetPrefixLength) - } - - return addresses -} - -// GetIPv4SubnetPrefixLength returns the IPv4 prefix length of the ENI's subnet. -func (eni *ENI) GetIPv4SubnetPrefixLength() string { - if eni.ipv4SubnetPrefixLength == "" && eni.SubnetGatewayIPV4Address != "" { - eni.ipv4SubnetPrefixLength = strings.Split(eni.SubnetGatewayIPV4Address, "/")[1] - } - - return eni.ipv4SubnetPrefixLength -} - -// GetIPv4SubnetCIDRBlock returns the IPv4 CIDR block, if any, of the ENI's subnet. -func (eni *ENI) GetIPv4SubnetCIDRBlock() string { - if eni.ipv4SubnetCIDRBlock == "" && eni.SubnetGatewayIPV4Address != "" { - _, ipv4Net, err := net.ParseCIDR(eni.SubnetGatewayIPV4Address) - if err == nil { - eni.ipv4SubnetCIDRBlock = ipv4Net.String() - } - } - - return eni.ipv4SubnetCIDRBlock -} - -// GetIPv6SubnetCIDRBlock returns the IPv6 CIDR block, if any, of the ENI's subnet. -func (eni *ENI) GetIPv6SubnetCIDRBlock() string { - if eni.ipv6SubnetCIDRBlock == "" && len(eni.IPV6Addresses) > 0 { - ipv6Addr := eni.IPV6Addresses[0].Address + "/" + IPv6SubnetPrefixLength - _, ipv6Net, err := net.ParseCIDR(ipv6Addr) - if err == nil { - eni.ipv6SubnetCIDRBlock = ipv6Net.String() - } - } - - return eni.ipv6SubnetCIDRBlock -} - -// GetSubnetGatewayIPv4Address returns the subnet gateway IPv4 address for the ENI. -func (eni *ENI) GetSubnetGatewayIPv4Address() string { - var gwAddr string - if eni.SubnetGatewayIPV4Address != "" { - gwAddr = strings.Split(eni.SubnetGatewayIPV4Address, "/")[0] - } - - return gwAddr -} - -// GetHostname returns the hostname assigned to the ENI -func (eni *ENI) GetHostname() string { - return eni.PrivateDNSName -} - -// GetLinkName returns the name of the ENI on the instance. -func (eni *ENI) GetLinkName() string { - eni.guard.Lock() - defer eni.guard.Unlock() - - if eni.LinkName == "" { - // Find all interfaces on the instance. - ifaces, err := netInterfaces() - if err != nil { - logger.Error("Failed to find link name:", logger.Fields{ - loggerfield.Error: err, - }) - return "" - } - // Iterate over the list and find the interface with the ENI's MAC address. - for _, iface := range ifaces { - if strings.EqualFold(eni.MacAddress, iface.HardwareAddr.String()) { - eni.LinkName = iface.Name - break - } - } - // If the ENI is not matched by MAC address above, we will fail to - // assign the LinkName. Log that here since CNI will fail with the empty - // name. - if eni.LinkName == "" { - logger.Error("Failed to find LinkName for given MAC", logger.Fields{ - "mac": eni.MacAddress, - }) - } - } - - return eni.LinkName -} - -// IsStandardENI returns true if the ENI is a standard/regular ENI. That is, if it -// has its association protocol as standard. To be backwards compatible, if the -// association protocol is not set for an ENI, it's considered a standard ENI as well. -func (eni *ENI) IsStandardENI() bool { - switch eni.InterfaceAssociationProtocol { - case "", DefaultInterfaceAssociationProtocol: - return true - case VLANInterfaceAssociationProtocol: - return false - default: - return false - } -} - -// String returns a human readable version of the ENI object -func (eni *ENI) String() string { - var ipv4Addresses []string - for _, addr := range eni.IPV4Addresses { - ipv4Addresses = append(ipv4Addresses, addr.Address) - } - var ipv6Addresses []string - for _, addr := range eni.IPV6Addresses { - ipv6Addresses = append(ipv6Addresses, addr.Address) - } - - eniString := "" - - if len(eni.InterfaceAssociationProtocol) == 0 { - eniString += fmt.Sprintf(" ,ENI type: [%s]", eni.InterfaceAssociationProtocol) - } - - if eni.InterfaceVlanProperties != nil { - eniString += fmt.Sprintf(" ,VLan ID: [%s], TrunkInterfaceMacAddress: [%s]", - eni.InterfaceVlanProperties.VlanID, eni.InterfaceVlanProperties.TrunkInterfaceMacAddress) - } - - return fmt.Sprintf( - "eni id:%s, mac: %s, hostname: %s, ipv4addresses: [%s], ipv6addresses: [%s], dns: [%s], dns search: [%s],"+ - " gateway ipv4: [%s][%s]", eni.ID, eni.MacAddress, eni.GetHostname(), strings.Join(ipv4Addresses, ","), - strings.Join(ipv6Addresses, ","), strings.Join(eni.DomainNameServers, ","), - strings.Join(eni.DomainNameSearchList, ","), eni.SubnetGatewayIPV4Address, eniString) -} - -// ENIIPV4Address is the ipv4 information of the eni -type ENIIPV4Address struct { - // Primary indicates whether the ip address is primary - Primary bool - // Address is the ipv4 address associated with eni - Address string -} - -// ENIIPV6Address is the ipv6 information of the eni -type ENIIPV6Address struct { - // Address is the ipv6 address associated with eni - Address string -} - -// ENIFromACS validates the given ACS ENI information and creates an ENI object from it. -func ENIFromACS(acsENI *ecsacs.ElasticNetworkInterface) (*ENI, error) { - err := ValidateENI(acsENI) - if err != nil { - return nil, err - } - - var ipv4Addrs []*ENIIPV4Address - var ipv6Addrs []*ENIIPV6Address - - // Read IPv4 address information of the ENI. - for _, ec2Ipv4 := range acsENI.Ipv4Addresses { - ipv4Addrs = append(ipv4Addrs, &ENIIPV4Address{ - Primary: aws.BoolValue(ec2Ipv4.Primary), - Address: aws.StringValue(ec2Ipv4.PrivateAddress), - }) - } - - // Read IPv6 address information of the ENI. - for _, ec2Ipv6 := range acsENI.Ipv6Addresses { - ipv6Addrs = append(ipv6Addrs, &ENIIPV6Address{ - Address: aws.StringValue(ec2Ipv6.Address), - }) - } - - // Read ENI association properties. - var interfaceVlanProperties InterfaceVlanProperties - - if aws.StringValue(acsENI.InterfaceAssociationProtocol) == VLANInterfaceAssociationProtocol { - interfaceVlanProperties.TrunkInterfaceMacAddress = aws.StringValue(acsENI.InterfaceVlanProperties.TrunkInterfaceMacAddress) - interfaceVlanProperties.VlanID = aws.StringValue(acsENI.InterfaceVlanProperties.VlanId) - } - - eni := &ENI{ - ID: aws.StringValue(acsENI.Ec2Id), - MacAddress: aws.StringValue(acsENI.MacAddress), - IPV4Addresses: ipv4Addrs, - IPV6Addresses: ipv6Addrs, - SubnetGatewayIPV4Address: aws.StringValue(acsENI.SubnetGatewayIpv4Address), - PrivateDNSName: aws.StringValue(acsENI.PrivateDnsName), - InterfaceAssociationProtocol: aws.StringValue(acsENI.InterfaceAssociationProtocol), - InterfaceVlanProperties: &interfaceVlanProperties, - } - - for _, nameserverIP := range acsENI.DomainNameServers { - eni.DomainNameServers = append(eni.DomainNameServers, aws.StringValue(nameserverIP)) - } - for _, nameserverDomain := range acsENI.DomainName { - eni.DomainNameSearchList = append(eni.DomainNameSearchList, aws.StringValue(nameserverDomain)) - } - - return eni, nil -} - -// ValidateENI validates the ENI information sent from ACS. -func ValidateENI(acsENI *ecsacs.ElasticNetworkInterface) error { - // At least one IPv4 address should be associated with the ENI. - if len(acsENI.Ipv4Addresses) < 1 { - return errors.Errorf("eni message validation: no ipv4 addresses in the message") - } - - if acsENI.SubnetGatewayIpv4Address == nil { - return errors.Errorf("eni message validation: no subnet gateway ipv4 address in the message") - } - gwIPv4Addr := aws.StringValue(acsENI.SubnetGatewayIpv4Address) - s := strings.Split(gwIPv4Addr, "/") - if len(s) != 2 { - return errors.Errorf( - "eni message validation: invalid subnet gateway ipv4 address %s", gwIPv4Addr) - } - - if acsENI.MacAddress == nil { - return errors.Errorf("eni message validation: empty eni mac address in the message") - } - - if acsENI.Ec2Id == nil { - return errors.Errorf("eni message validation: empty eni id in the message") - } - - // The association protocol, if specified, must be a supported value. - if (acsENI.InterfaceAssociationProtocol != nil) && - (aws.StringValue(acsENI.InterfaceAssociationProtocol) != VLANInterfaceAssociationProtocol) && - (aws.StringValue(acsENI.InterfaceAssociationProtocol) != DefaultInterfaceAssociationProtocol) { - return errors.Errorf("invalid interface association protocol: %s", - aws.StringValue(acsENI.InterfaceAssociationProtocol)) - } - - // If the interface association protocol is vlan, InterfaceVlanProperties must be specified. - if aws.StringValue(acsENI.InterfaceAssociationProtocol) == VLANInterfaceAssociationProtocol { - if acsENI.InterfaceVlanProperties == nil || - len(aws.StringValue(acsENI.InterfaceVlanProperties.VlanId)) == 0 || - len(aws.StringValue(acsENI.InterfaceVlanProperties.TrunkInterfaceMacAddress)) == 0 { - return errors.New("vlan interface properties missing") - } - } - - return nil -} 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,