Skip to content

Commit

Permalink
add appmesh and eni to network library in ecs-agent module
Browse files Browse the repository at this point in the history
  • Loading branch information
Realmonia committed Jul 17, 2023
1 parent 9de5ab7 commit bab3dac
Show file tree
Hide file tree
Showing 86 changed files with 3,051 additions and 393 deletions.
10 changes: 5 additions & 5 deletions agent/acs/handler/attach_eni_handler_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ 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/utils"
apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
"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"
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/arn"
"github.com/cihub/seelog"
"github.com/pkg/errors"
Expand All @@ -39,7 +39,7 @@ type eniHandler struct {
// 2. Otherwise add the attachment to state, start its ack timer, and save the state
// These are common tasks for handling a task ENI attachment and an instance ENI attachment, so they are put
// into this function to be shared by both attachment handlers
func (eniHandler *eniHandler) HandleENIAttachment(ea *apieni.ENIAttachment) error {
func (eniHandler *eniHandler) HandleENIAttachment(ea *ni.ENIAttachment) error {
attachmentType := ea.AttachmentType
attachmentARN := ea.AttachmentARN
taskARN := ea.TaskARN
Expand All @@ -61,7 +61,7 @@ func (eniHandler *eniHandler) HandleENIAttachment(ea *apieni.ENIAttachment) erro
}

// addENIAttachmentToState adds an ENI attachment to state, and start its ack timer
func (eniHandler *eniHandler) addENIAttachmentToState(ea *apieni.ENIAttachment) error {
func (eniHandler *eniHandler) addENIAttachmentToState(ea *ni.ENIAttachment) error {
attachmentType := ea.AttachmentType
attachmentARN := ea.AttachmentARN
taskARN := ea.TaskARN
Expand All @@ -73,14 +73,14 @@ func (eniHandler *eniHandler) addENIAttachmentToState(ea *apieni.ENIAttachment)
}

switch attachmentType {
case apieni.ENIAttachmentTypeTaskENI:
case ni.ENIAttachmentTypeTaskENI:
taskId, _ := arn.TaskIdFromArn(taskARN)
logger.Info("Adding eni attachment info to state for task", logger.Fields{
field.TaskID: taskId,
"attachmentARN": attachmentARN,
"mac": mac,
})
case apieni.ENIAttachmentTypeInstanceENI:
case ni.ENIAttachmentTypeInstanceENI:
logger.Info("Adding instance eni attachment info to state", logger.Fields{
"attachmentARN": attachmentARN,
"mac": mac,
Expand Down
26 changes: 13 additions & 13 deletions agent/acs/handler/attach_eni_handler_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"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"
apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
)

const (
Expand All @@ -36,12 +36,12 @@ const (

// TestTaskENIAckTimeout tests acknowledge timeout for a task eni before submit the state change
func TestTaskENIAckTimeout(t *testing.T) {
testENIAckTimeout(t, apieni.ENIAttachmentTypeTaskENI)
testENIAckTimeout(t, ni.ENIAttachmentTypeTaskENI)
}

// TestInstanceENIAckTimeout tests acknowledge timeout for an instance level eni before submit the state change
func TestInstanceENIAckTimeout(t *testing.T) {
testENIAckTimeout(t, apieni.ENIAttachmentTypeInstanceENI)
testENIAckTimeout(t, ni.ENIAttachmentTypeInstanceENI)
}

func testENIAckTimeout(t *testing.T, attachmentType string) {
Expand All @@ -52,7 +52,7 @@ func testENIAckTimeout(t *testing.T, attachmentType string) {
dataClient := newTestDataClient(t)

expiresAt := time.Now().Add(time.Millisecond * testconst.WaitTimeoutMillis)
eniAttachment := &apieni.ENIAttachment{
eniAttachment := &ni.ENIAttachment{
AttachmentInfo: attachmentinfo.AttachmentInfo{
TaskARN: taskArn,
AttachmentARN: attachmentArn,
Expand Down Expand Up @@ -86,12 +86,12 @@ func testENIAckTimeout(t *testing.T, attachmentType string) {

// TestTaskENIAckWithinTimeout tests the eni state change was reported before the timeout, for a task eni
func TestTaskENIAckWithinTimeout(t *testing.T) {
testENIAckWithinTimeout(t, apieni.ENIAttachmentTypeTaskENI)
testENIAckWithinTimeout(t, ni.ENIAttachmentTypeTaskENI)
}

// TestInstanceENIAckWithinTimeout tests the eni state change was reported before the timeout, for an instance eni
func TestInstanceENIAckWithinTimeout(t *testing.T) {
testENIAckWithinTimeout(t, apieni.ENIAttachmentTypeInstanceENI)
testENIAckWithinTimeout(t, ni.ENIAttachmentTypeInstanceENI)
}

func testENIAckWithinTimeout(t *testing.T, attachmentType string) {
Expand All @@ -101,7 +101,7 @@ func testENIAckWithinTimeout(t *testing.T, attachmentType string) {
taskEngineState := dockerstate.NewTaskEngineState()
dataClient := data.NewNoopClient()
expiresAt := time.Now().Add(time.Millisecond * testconst.WaitTimeoutMillis)
eniAttachment := &apieni.ENIAttachment{
eniAttachment := &ni.ENIAttachment{
AttachmentInfo: attachmentinfo.AttachmentInfo{
TaskARN: taskArn,
AttachmentARN: attachmentArn,
Expand Down Expand Up @@ -130,12 +130,12 @@ func testENIAckWithinTimeout(t *testing.T, attachmentType string) {

// TestHandleENIAttachmentTaskENI tests handling a new task eni
func TestHandleENIAttachmentTaskENI(t *testing.T) {
testHandleENIAttachment(t, apieni.ENIAttachmentTypeTaskENI, taskArn)
testHandleENIAttachment(t, ni.ENIAttachmentTypeTaskENI, taskArn)
}

// TestHandleENIAttachmentInstanceENI tests handling a new instance eni
func TestHandleENIAttachmentInstanceENI(t *testing.T) {
testHandleENIAttachment(t, apieni.ENIAttachmentTypeInstanceENI, "")
testHandleENIAttachment(t, ni.ENIAttachmentTypeInstanceENI, "")
}

func testHandleENIAttachment(t *testing.T, attachmentType, taskArn string) {
Expand All @@ -146,7 +146,7 @@ func testHandleENIAttachment(t *testing.T, attachmentType, taskArn string) {

taskEngineState := dockerstate.NewTaskEngineState()
expiresAt := time.Now().Add(time.Millisecond * testconst.WaitTimeoutMillis)
eniAttachment := &apieni.ENIAttachment{
eniAttachment := &ni.ENIAttachment{
AttachmentInfo: attachmentinfo.AttachmentInfo{
TaskARN: taskArn,
AttachmentARN: attachmentArn,
Expand Down Expand Up @@ -178,12 +178,12 @@ func testHandleENIAttachment(t *testing.T, attachmentType, taskArn string) {

// TestHandleExpiredENIAttachmentTaskENI tests handling an expired task eni
func TestHandleExpiredENIAttachmentTaskENI(t *testing.T) {
testHandleExpiredENIAttachment(t, apieni.ENIAttachmentTypeTaskENI, taskArn)
testHandleExpiredENIAttachment(t, ni.ENIAttachmentTypeTaskENI, taskArn)
}

// TestHandleExpiredENIAttachmentInstanceENI tests handling an expired instance eni
func TestHandleExpiredENIAttachmentInstanceENI(t *testing.T) {
testHandleExpiredENIAttachment(t, apieni.ENIAttachmentTypeInstanceENI, "")
testHandleExpiredENIAttachment(t, ni.ENIAttachmentTypeInstanceENI, "")
}

func testHandleExpiredENIAttachment(t *testing.T, attachmentType, taskArn string) {
Expand All @@ -196,7 +196,7 @@ func testHandleExpiredENIAttachment(t *testing.T, attachmentType, taskArn string
taskEngineState := dockerstate.NewTaskEngineState()
dataClient := data.NewNoopClient()

eniAttachment := &apieni.ENIAttachment{
eniAttachment := &ni.ENIAttachment{
AttachmentInfo: attachmentinfo.AttachmentInfo{
TaskARN: taskArn,
AttachmentARN: attachmentArn,
Expand Down
4 changes: 2 additions & 2 deletions agent/acs/handler/attach_instance_eni_responder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
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"
apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
)

var testAttachInstanceENIMessage = &ecsacs.AttachInstanceNetworkInterfacesMessage{
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestInstanceENIAckSingleMessageWithDuplicateENIAttachment(t *testing.T) {
// the task engine state.
mockState.EXPECT().
ENIByMac(testconst.RandomMAC).
Return(&apieni.ENIAttachment{
Return(&ni.ENIAttachment{
AttachmentInfo: attachmentinfo.AttachmentInfo{
ExpiresAt: expiresAt,
},
Expand Down
4 changes: 2 additions & 2 deletions agent/acs/handler/attach_task_eni_responder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
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"
apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
)

var testAttachTaskENIMessage = &ecsacs.AttachTaskNetworkInterfacesMessage{
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestTaskENIAckSingleMessageWithDuplicateENIAttachment(t *testing.T) {
// the task engine state.
mockState.EXPECT().
ENIByMac(testconst.RandomMAC).
Return(&apieni.ENIAttachment{
Return(&ni.ENIAttachment{
AttachmentInfo: attachmentinfo.AttachmentInfo{
ExpiresAt: expiresAt,
},
Expand Down
6 changes: 3 additions & 3 deletions agent/acs/handler/payload_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/field"

"github.com/aws/amazon-ecs-agent/agent/api"
apiappmesh "github.com/aws/amazon-ecs-agent/agent/api/appmesh"
apitask "github.com/aws/amazon-ecs-agent/agent/api/task"
apitaskstatus "github.com/aws/amazon-ecs-agent/agent/api/task/status"
"github.com/aws/amazon-ecs-agent/agent/data"
"github.com/aws/amazon-ecs-agent/agent/engine"
"github.com/aws/amazon-ecs-agent/agent/eventhandler"
"github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs"
apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
apiappmesh "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/appmesh"
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"
Expand Down Expand Up @@ -242,7 +242,7 @@ func (payloadHandler *payloadRequestHandler) addPayloadTasks(payload *ecsacs.Pay

// Add ENI information to the task struct.
for _, acsENI := range task.ElasticNetworkInterfaces {
eni, err := apieni.ENIFromACS(acsENI)
eni, err := ni.ENIFromACS(acsENI)
if err != nil {
payloadHandler.handleUnrecognizedTask(task, err, payload)
allTasksOK = false
Expand Down
6 changes: 3 additions & 3 deletions agent/acs/handler/payload_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (
"github.com/aws/amazon-ecs-agent/agent/taskresource"
"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/eni"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
mock_wsclient "github.com/aws/amazon-ecs-agent/ecs-agent/wsclient/mock"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -836,7 +836,7 @@ func TestPayloadHandlerAddedENITrunkToTask(t *testing.T) {
Arn: aws.String("arn"),
ElasticNetworkInterfaces: []*ecsacs.ElasticNetworkInterface{
{
InterfaceAssociationProtocol: aws.String(eni.VLANInterfaceAssociationProtocol),
InterfaceAssociationProtocol: aws.String(ni.VLANInterfaceAssociationProtocol),
AttachmentArn: aws.String("arn"),
Ec2Id: aws.String("ec2id"),
Ipv4Addresses: []*ecsacs.IPv4AddressAssignment{
Expand Down Expand Up @@ -868,7 +868,7 @@ func TestPayloadHandlerAddedENITrunkToTask(t *testing.T) {

taskeni := addedTask.GetPrimaryENI()

assert.Equal(t, taskeni.InterfaceAssociationProtocol, eni.VLANInterfaceAssociationProtocol)
assert.Equal(t, taskeni.InterfaceAssociationProtocol, ni.VLANInterfaceAssociationProtocol)
assert.Equal(t, taskeni.InterfaceVlanProperties.TrunkInterfaceMacAddress, "mac")
assert.Equal(t, taskeni.InterfaceVlanProperties.VlanID, "12345")
}
Expand Down
4 changes: 2 additions & 2 deletions agent/api/ecsclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ 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"
apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
"github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
Expand Down Expand Up @@ -1071,7 +1071,7 @@ func TestSubmitTaskStateChangeWithAttachments(t *testing.T) {

err := client.SubmitTaskStateChange(api.TaskStateChange{
TaskARN: "task_arn",
Attachment: &apieni.ENIAttachment{
Attachment: &ni.ENIAttachment{
AttachmentInfo: attachmentinfo.AttachmentInfo{
AttachmentARN: "eni_arn",
Status: status.AttachmentAttached,
Expand Down
8 changes: 4 additions & 4 deletions agent/api/statechange.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
apitask "github.com/aws/amazon-ecs-agent/agent/api/task"
apitaskstatus "github.com/aws/amazon-ecs-agent/agent/api/task/status"
"github.com/aws/amazon-ecs-agent/agent/statechange"
apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
"github.com/pkg/errors"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -73,7 +73,7 @@ type ManagedAgentStateChange struct {
// SubmitTaskStateChange API
type TaskStateChange struct {
// Attachment is the eni attachment object to send
Attachment *apieni.ENIAttachment
Attachment *ni.ENIAttachment
// TaskArn is the unique identifier for the task
TaskARN string
// Status is the status to send
Expand All @@ -99,7 +99,7 @@ type TaskStateChange struct {
// SubmitAttachmentStateChanges API
type AttachmentStateChange struct {
// Attachment is the eni attachment object to send
Attachment *apieni.ENIAttachment
Attachment *ni.ENIAttachment
}

type ErrShouldNotSendEvent struct {
Expand Down Expand Up @@ -218,7 +218,7 @@ func NewManagedAgentChangeEvent(task *apitask.Task, cont *apicontainer.Container
}

// NewAttachmentStateChangeEvent creates a new attachment state change event
func NewAttachmentStateChangeEvent(eniAttachment *apieni.ENIAttachment) AttachmentStateChange {
func NewAttachmentStateChangeEvent(eniAttachment *ni.ENIAttachment) AttachmentStateChange {
return AttachmentStateChange{
Attachment: eniAttachment,
}
Expand Down
12 changes: 6 additions & 6 deletions agent/api/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"sync"
"time"

apiappmesh "github.com/aws/amazon-ecs-agent/agent/api/appmesh"
apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container"
apicontainerstatus "github.com/aws/amazon-ecs-agent/agent/api/container/status"
"github.com/aws/amazon-ecs-agent/agent/api/serviceconnect"
Expand All @@ -44,11 +43,12 @@ 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"
apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
apierrors "github.com/aws/amazon-ecs-agent/ecs-agent/api/errors"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/field"
apiappmesh "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/appmesh"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/arn"
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/ttime"

Expand Down Expand Up @@ -2776,18 +2776,18 @@ func (task *Task) SetSentStatus(status apitaskstatus.TaskStatus) {
}

// AddTaskENI adds ENI information to the task.
func (task *Task) AddTaskENI(eni *apieni.ENI) {
func (task *Task) AddTaskENI(eni *ni.NetworkInterface) {
task.lock.Lock()
defer task.lock.Unlock()

if task.ENIs == nil {
task.ENIs = make([]*apieni.ENI, 0)
task.ENIs = make([]*ni.NetworkInterface, 0)
}
task.ENIs = append(task.ENIs, eni)
}

// GetTaskENIs returns the list of ENIs for the task.
func (task *Task) GetTaskENIs() []*apieni.ENI {
func (task *Task) GetTaskENIs() []*ni.NetworkInterface {
// TODO: what's the point of locking if we are returning a pointer?
task.lock.RLock()
defer task.lock.RUnlock()
Expand All @@ -2797,7 +2797,7 @@ func (task *Task) GetTaskENIs() []*apieni.ENI {

// GetPrimaryENI returns the primary ENI of the task. Since ACS can potentially send
// multiple ENIs to the agent, the first ENI in the list is considered as the primary ENI.
func (task *Task) GetPrimaryENI() *apieni.ENI {
func (task *Task) GetPrimaryENI() *ni.NetworkInterface {
task.lock.RLock()
defer task.lock.RUnlock()

Expand Down
10 changes: 5 additions & 5 deletions agent/api/task/task_enis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package task
import (
"encoding/json"

apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
)

// TaskENIs type enumerates the list of ENI objects as a type. It is used for
Expand All @@ -29,10 +29,10 @@ import (
// since this is only required for unmarshaling 'Task' object. None of the
// functionality/types in the 'eni' package themselves have any dependencies on this
// type.
type TaskENIs []*apieni.ENI
type TaskENIs []*ni.NetworkInterface

func (taskENIs *TaskENIs) UnmarshalJSON(b []byte) error {
var enis []*apieni.ENI
var enis []*ni.NetworkInterface
// Try to unmarshal this as a list of ENI objects.
err := json.Unmarshal(b, &enis)
if err == nil {
Expand All @@ -43,12 +43,12 @@ func (taskENIs *TaskENIs) UnmarshalJSON(b []byte) error {
// There was an error unmarshaling the byte slice as a list of ENIs. This is the case
// where we're restoring from a previous version of the state file, where the ENI is
// being stored as a standalone object and not as a list.
var eni apieni.ENI
var eni ni.NetworkInterface
err = json.Unmarshal(b, &eni)
if err != nil {
return err
}
enis = []*apieni.ENI{&eni}
enis = []*ni.NetworkInterface{&eni}
*taskENIs = enis
return nil
}
Loading

0 comments on commit bab3dac

Please sign in to comment.