-
Notifications
You must be signed in to change notification settings - Fork 7
/
v3.go
96 lines (83 loc) · 3.08 KB
/
v3.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
package metadata
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
)
// Based on https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v3.html
const (
ecsMetadataUriEnvV3 = "ECS_CONTAINER_METADATA_URI"
)
type ContainerMetadataV3 struct {
DockerID string `json:"DockerId"`
Name string `json:"Name"`
DockerName string `json:"DockerName"`
Image string `json:"Image"`
ImageID string `json:"ImageID"`
Labels struct {
EcsCluster string `json:"com.amazonaws.ecs.cluster"`
EcsContainerName string `json:"com.amazonaws.ecs.container-name"`
EcsTaskArn string `json:"com.amazonaws.ecs.task-arn"`
EcsTaskDefinitionFamily string `json:"com.amazonaws.ecs.task-definition-family"`
EcsTaskDefinitionVersion string `json:"com.amazonaws.ecs.task-definition-version"`
} `json:"Labels"`
DesiredStatus string `json:"DesiredStatus"`
KnownStatus string `json:"KnownStatus"`
Limits struct {
CPU int `json:"CPU"`
Memory int `json:"Memory"`
} `json:"Limits"`
CreatedAt time.Time `json:"CreatedAt"`
StartedAt time.Time `json:"StartedAt,omitempty"`
Type string `json:"Type"`
Networks []struct {
NetworkMode string `json:"NetworkMode"`
IPv4Addresses []string `json:"IPv4Addresses"`
} `json:"Networks"`
}
type TaskMetadataV3 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"`
Containers []ContainerMetadataV3 `json:"Containers"`
Limits struct {
CPU float64 `json:"CPU"`
Memory int `json:"Memory"`
} `json:"Limits"`
PullStartedAt time.Time `json:"PullStartedAt"`
PullStoppedAt time.Time `json:"PullStoppedAt"`
}
// Retrieve ECS Task Metadata in V3 format
func GetTaskV3(ctx context.Context, client *http.Client) (*TaskMetadataV3, error) {
metadataUrl := os.Getenv(ecsMetadataUriEnvV3)
if metadataUrl == "" {
return nil, fmt.Errorf("missing metadata uri in environment (%s)", ecsMetadataUriEnvV3)
}
taskMetadata := &TaskMetadataV3{}
body, err := fetch(ctx, client, fmt.Sprintf("%s/task", metadataUrl))
err = json.Unmarshal(body, &taskMetadata)
if err != nil {
return nil, fmt.Errorf("could not unmarshal into task metadata (v3): %w", err)
}
return taskMetadata, nil
}
// Retrieve ECS Container Metadata in V3 format
func GetContainerV3(ctx context.Context, client *http.Client) (*ContainerMetadataV3, error) {
metadataUrl := os.Getenv(ecsMetadataUriEnvV3)
if metadataUrl == "" {
return nil, fmt.Errorf("missing metadata uri in environment (%s)", ecsMetadataUriEnvV3)
}
contaienrMetadata := &ContainerMetadataV3{}
body, err := fetch(ctx, client, fmt.Sprintf("%s", metadataUrl))
err = json.Unmarshal(body, &contaienrMetadata)
if err != nil {
return nil, fmt.Errorf("could not unmarshal into container metadata (v3): %w", err)
}
return contaienrMetadata, nil
}