This repository has been archived by the owner on Jun 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ecs_agent_introspection.go
141 lines (117 loc) · 3.36 KB
/
ecs_agent_introspection.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
package ecstaskports
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/pkg/errors"
)
func discoverWithTaskIntrospection() ([]PortMapping, error) {
im, err := getECSInstanceMetadata()
if err != nil {
return nil, err
}
dockerID, err := getDockerContainerID()
if err != nil {
return nil, err
}
tm, err := getECSTaskMetadata(dockerID)
if err != nil {
return nil, err
}
return getECSTaskPorts(im.Cluster, tm.Arn)
}
// ECS agent introspection URL as per https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-introspection.html
const introspectionURL = "http://172.17.0.1:51678/v1/"
type ecsInstanceMetadata struct {
Cluster string `json:"Cluster"`
ContainerInstanceArn string `json:"ContainerInstanceArn"`
Version string `json:"Version"`
}
type ecsInstanceTaskIntrospectionMetadata struct {
Arn string `json:"Arn"`
DesiredStatus string `json:"DesiredStatus"`
KnownStatus string `json:"KnownStatus"`
Family string `json:"Family"`
Version string `json:"Version"`
Containers []struct {
DockerID string `json:"DockerId"`
DockerName string `json:"DockerName"`
Name string `json:"Name"`
} `json:"Containers"`
}
func getECSInstanceMetadata() (ecsInstanceMetadata, error) {
var metadata ecsInstanceMetadata
client := &http.Client{
Timeout: time.Second * 3,
}
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/metadata", introspectionURL), nil)
resp, err := client.Do(req)
if err != nil {
return metadata, errors.Wrap(err, "Get instance metadata error")
}
respBody, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(respBody, &metadata)
if err != nil {
return metadata, errors.Wrap(err, "Unmarshal instance metadata error")
}
return metadata, nil
}
func getECSTaskMetadata(dockerID string) (ecsInstanceTaskIntrospectionMetadata, error) {
var task ecsInstanceTaskIntrospectionMetadata
client := &http.Client{
Timeout: time.Second * 3,
}
req, _ := http.NewRequest(
"GET",
fmt.Sprintf("%s/tasks?dockerid=%s", introspectionURL, dockerID),
nil,
)
resp, err := client.Do(req)
if err != nil {
return task, errors.Wrap(err, "Get task metadata error")
}
respBody, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(respBody, &task)
if err != nil {
return task, errors.Wrap(err, "Unmarshal task metadata error")
}
return task, nil
}
func getECSTaskPorts(cluster string, taskARN string) ([]PortMapping, error) {
var addresses []PortMapping
s, _ := session.NewSession(&aws.Config{Region: aws.String(GetEC2InstanceRegion())})
ecsClient := ecs.New(s)
result, err := ecsClient.DescribeTasks(&ecs.DescribeTasksInput{
Cluster: aws.String(cluster),
Tasks: []*string{
aws.String(taskARN),
},
})
if err != nil {
return addresses, errors.Wrap(err, "ECS DescribeTasks error")
}
var t *ecs.Task
for _, task := range result.Tasks {
if aws.StringValue(task.TaskArn) == taskARN {
t = task
}
}
if t == nil {
return addresses, errors.New("No described task matched task ARN")
}
for _, nb := range t.Containers[0].NetworkBindings {
addresses = append(
addresses,
PortMapping{
ContainerPort: int(aws.Int64Value(nb.ContainerPort)),
HostPort: int(aws.Int64Value(nb.HostPort)),
},
)
}
return addresses, nil
}