-
Notifications
You must be signed in to change notification settings - Fork 7
/
v4.go
159 lines (140 loc) · 5.24 KB
/
v4.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
package metadata
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
)
// Based on https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4.html
const (
ecsMetadataUriEnvV4 = "ECS_CONTAINER_METADATA_URI_V4"
labelEcsCluster = "com.amazonaws.ecs.cluster"
labelEcsContainerName = "com.amazonaws.ecs.container-name"
labelEcsTaskArn = "com.amazonaws.ecs.task-arn"
labelEcsTaskDefinitionFamily = "com.amazonaws.ecs.task-definition-family"
labelEcsTaskDefinitionVersion = "com.amazonaws.ecs.task-definition-version"
)
type Limits struct {
CPU float64 `json:"CPU"`
Memory int `json:"Memory"`
}
type LabelsV4 struct {
EcsCluster string
EcsContainerName string
EcsTaskArn string
EcsTaskDefinitionFamily string
EcsTaskDefinitionVersion string
rest map[string]string
}
func (l LabelsV4) Get(name string) string {
return l.rest[name]
}
func (l *LabelsV4) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &l.rest); err != nil {
return err
}
if cluster, ok := l.rest[labelEcsCluster]; ok {
l.EcsCluster = cluster
delete(l.rest, labelEcsCluster)
}
if containerName, ok := l.rest[labelEcsContainerName]; ok {
l.EcsContainerName = containerName
delete(l.rest, labelEcsContainerName)
}
if taskArn, ok := l.rest[labelEcsTaskArn]; ok {
l.EcsTaskArn = taskArn
delete(l.rest, labelEcsTaskArn)
}
if family, ok := l.rest[labelEcsTaskDefinitionFamily]; ok {
l.EcsTaskDefinitionFamily = family
delete(l.rest, labelEcsTaskDefinitionFamily)
}
if version, ok := l.rest[labelEcsTaskDefinitionVersion]; ok {
l.EcsTaskDefinitionVersion = version
delete(l.rest, labelEcsTaskDefinitionVersion)
}
return nil
}
type ContainerMetadataV4 struct {
DockerID string `json:"DockerId"`
Name string `json:"Name"`
DockerName string `json:"DockerName"`
Image string `json:"Image"`
ImageID string `json:"ImageID"`
Labels LabelsV4 `json:"Labels"`
DesiredStatus string `json:"DesiredStatus"`
KnownStatus string `json:"KnownStatus"`
Limits Limits `json:"Limits"`
CreatedAt time.Time `json:"CreatedAt"`
StartedAt time.Time `json:"StartedAt"`
Type string `json:"Type"`
ContainerARN string `json:"ContainerARN"`
LogDriver string `json:"LogDriver"`
LogOptions struct {
AwsLogsCreateGroup string `json:"awslogs-create-group"`
AwsLogsGroup string `json:"awslogs-group"`
AwsLogsStream string `json:"awslogs-stream"`
AwsRegion string `json:"awslogs-region"`
} `json:"LogOptions"`
Networks []struct {
NetworkMode string `json:"NetworkMode"`
IPv4Addresses []string `json:"IPv4Addresses"`
AttachmentIndex int `json:"AttachmentIndex"`
IPv4SubnetCIDRBlock string `json:"IPv4SubnetCIDRBlock"`
MACAddress string `json:"MACAddress"`
DomainNameServers []string `json:"DomainNameServers"`
DomainNameSearchList []string `json:"DomainNameSearchList"`
PrivateDNSName string `json:"PrivateDNSName"`
SubnetGatewayIpv4Address string `json:"SubnetGatewayIpv4Address"`
} `json:"Networks"`
}
type TaskMetadataV4 struct {
Cluster string `json:"Cluster"`
TaskARN string `json:"TaskARN"`
Family string `json:"Family"`
Revision string `json:"Revision"`
DesiredStatus string `json:"DesiredStatus"`
KnownStatus string `json:"KnownStatus"`
Limits Limits `json:"Limits"`
PullStartedAt time.Time `json:"PullStartedAt"`
PullStoppedAt time.Time `json:"PullStoppedAt"`
AvailabilityZone string `json:"AvailabilityZone"`
LaunchType string `json:"LaunchType"`
Containers []ContainerMetadataV4 `json:"Containers"`
}
// Retrieve ECS Task Metadata in V4 format
func GetTaskV4(ctx context.Context, client *http.Client) (*TaskMetadataV4, error) {
metadataUrl := os.Getenv(ecsMetadataUriEnvV4)
if metadataUrl == "" {
return nil, fmt.Errorf("missing metadata uri in environment (%s)", ecsMetadataUriEnvV4)
}
taskMetadata := &TaskMetadataV4{}
body, err := fetch(ctx, client, fmt.Sprintf("%s/task", metadataUrl))
if err != nil {
return nil, fmt.Errorf("could not retrieve task metadata v4: %w", err)
}
err = json.Unmarshal(body, &taskMetadata)
if err != nil {
return nil, fmt.Errorf("could not unmarshal into task metadata v4: %w", err)
}
return taskMetadata, nil
}
// Retrieve ECS Container Metadata in V4 format
func GetContainerV4(ctx context.Context, client *http.Client) (*ContainerMetadataV4, error) {
metadataUrl := os.Getenv(ecsMetadataUriEnvV4)
if metadataUrl == "" {
return nil, fmt.Errorf("missing metadata uri in environment (%s)", ecsMetadataUriEnvV4)
}
containerMetadata := &ContainerMetadataV4{}
body, err := fetch(ctx, client, metadataUrl)
if err != nil {
return nil, fmt.Errorf("could not retrieve container metadata v4: %w", err)
}
err = json.Unmarshal(body, &containerMetadata)
if err != nil {
return nil, fmt.Errorf("could not unmarshal into container metadata v4: %w", err)
}
return containerMetadata, nil
}