-
Notifications
You must be signed in to change notification settings - Fork 0
/
attach_task_eni_handler.go
161 lines (136 loc) · 6.07 KB
/
attach_task_eni_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 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 handler
import (
"time"
"github.com/aws/amazon-ecs-agent/agent/acs/model/ecsacs"
apieni "github.com/aws/amazon-ecs-agent/agent/api/eni"
"github.com/aws/amazon-ecs-agent/agent/data"
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
"github.com/aws/amazon-ecs-agent/agent/wsclient"
"github.com/aws/aws-sdk-go/aws"
"github.com/cihub/seelog"
"github.com/pkg/errors"
"context"
)
// attachTaskENIHandler handles task ENI attach operation for the ACS client
type attachTaskENIHandler struct {
messageBuffer chan *ecsacs.AttachTaskNetworkInterfacesMessage
ctx context.Context
cancel context.CancelFunc
cluster *string
containerInstance *string
acsClient wsclient.ClientServer
state dockerstate.TaskEngineState
dataClient data.Client
}
// newAttachTaskENIHandler returns an instance of the attachENIHandler struct
func newAttachTaskENIHandler(ctx context.Context,
cluster string,
containerInstanceArn string,
acsClient wsclient.ClientServer,
taskEngineState dockerstate.TaskEngineState,
dataClient data.Client) attachTaskENIHandler {
// Create a cancelable context from the parent context
derivedContext, cancel := context.WithCancel(ctx)
return attachTaskENIHandler{
messageBuffer: make(chan *ecsacs.AttachTaskNetworkInterfacesMessage),
ctx: derivedContext,
cancel: cancel,
cluster: aws.String(cluster),
containerInstance: aws.String(containerInstanceArn),
acsClient: acsClient,
state: taskEngineState,
dataClient: dataClient,
}
}
// handlerFunc returns a function to enqueue requests onto attachENIHandler buffer
func (attachTaskENIHandler *attachTaskENIHandler) handlerFunc() func(message *ecsacs.AttachTaskNetworkInterfacesMessage) {
return func(message *ecsacs.AttachTaskNetworkInterfacesMessage) {
attachTaskENIHandler.messageBuffer <- message
}
}
// start invokes handleMessages to ack each enqueued request
func (attachTaskENIHandler *attachTaskENIHandler) start() {
go attachTaskENIHandler.handleMessages()
}
// stop is used to invoke a cancellation function
func (attachTaskENIHandler *attachTaskENIHandler) stop() {
attachTaskENIHandler.cancel()
}
// handleMessages handles each message one at a time
func (attachTaskENIHandler *attachTaskENIHandler) handleMessages() {
for {
select {
case <-attachTaskENIHandler.ctx.Done():
return
case message := <-attachTaskENIHandler.messageBuffer:
if err := attachTaskENIHandler.handleSingleMessage(message); err != nil {
seelog.Warnf("Unable to handle ENI Attachment message [%s]: %v", message.String(), err)
}
}
}
}
// handleSingleMessage acks the message received
func (attachTaskENIHandler *attachTaskENIHandler) handleSingleMessage(message *ecsacs.AttachTaskNetworkInterfacesMessage) error {
receivedAt := time.Now()
// Validate fields in the message
if err := validateAttachTaskNetworkInterfacesMessage(message); err != nil {
return errors.Wrapf(err,
"attach eni message handler: error validating AttachTaskNetworkInterface message received from ECS")
}
// Send ACK
go sendAck(attachTaskENIHandler.acsClient, message.ClusterArn, message.ContainerInstanceArn, message.MessageId)
// Handle the attachment
attachmentARN := aws.StringValue(message.ElasticNetworkInterfaces[0].AttachmentArn)
taskARN := aws.StringValue(message.TaskArn)
mac := aws.StringValue(message.ElasticNetworkInterfaces[0].MacAddress)
expiresAt := receivedAt.Add(time.Duration(aws.Int64Value(message.WaitTimeoutMs)) * time.Millisecond)
return handleENIAttachment(apieni.ENIAttachmentTypeTaskENI, attachmentARN, taskARN, mac, expiresAt, attachTaskENIHandler.state, attachTaskENIHandler.dataClient)
}
// validateAttachTaskNetworkInterfacesMessage performs validation checks on the
// AttachTaskNetworkInterfacesMessage
func validateAttachTaskNetworkInterfacesMessage(message *ecsacs.AttachTaskNetworkInterfacesMessage) error {
if message == nil {
return errors.Errorf("attach eni handler validation: empty AttachTaskNetworkInterface message received from ECS")
}
messageId := aws.StringValue(message.MessageId)
if messageId == "" {
return errors.Errorf("attach eni handler validation: message id not set in AttachTaskNetworkInterface message received from ECS")
}
clusterArn := aws.StringValue(message.ClusterArn)
if clusterArn == "" {
return errors.Errorf("attach eni handler validation: clusterArn not set in AttachTaskNetworkInterface message received from ECS")
}
containerInstanceArn := aws.StringValue(message.ContainerInstanceArn)
if containerInstanceArn == "" {
return errors.Errorf("attach eni handler validation: containerInstanceArn not set in AttachTaskNetworkInterface message received from ECS")
}
enis := message.ElasticNetworkInterfaces
if len(enis) != 1 {
return errors.Errorf("attach eni handler validation: incorrect number of ENIs in AttachTaskNetworkInterface message received from ECS. Obtained %d", len(enis))
}
eni := enis[0]
if aws.StringValue(eni.MacAddress) == "" {
return errors.Errorf("attach eni handler validation: MACAddress not listed in AttachTaskNetworkInterface message received from ECS")
}
taskArn := aws.StringValue(message.TaskArn)
if taskArn == "" {
return errors.Errorf("attach eni handler validation: taskArn not set in AttachTaskNetworkInterface message received from ECS")
}
timeout := aws.Int64Value(message.WaitTimeoutMs)
if timeout <= 0 {
return errors.Errorf("attach eni handler validation: invalid timeout listed in AttachTaskNetworkInterface message received from ECS")
}
return nil
}