-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcombined.go
71 lines (60 loc) · 1.85 KB
/
combined.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
package metadata
import (
"context"
"fmt"
"net/http"
"os"
)
// Will retrieve the task metadata for compatibility
func Get(ctx context.Context, client *http.Client) (interface{}, error) {
return GetTask(ctx, client)
}
// Will retrieve the task metadata
// for your current Fargate environment (either V3 or V4)
// based on the environment variables that are present
func GetTask(ctx context.Context, client *http.Client) (interface{}, error) {
// If the ECS Metadata URI for v4 is set,
// use this. When running on platform version 4,
// v3 might also still be set, though we prioritize the newer format
isV4 := os.Getenv(ecsMetadataUriEnvV4) != ""
if isV4 {
return GetTaskV4(ctx, client)
}
// If the Metadata URI for v4 wasn't set,
// check for v3
isV3 := os.Getenv(ecsMetadataUriEnvV3) != ""
if isV3 {
return GetTaskV3(ctx, client)
}
return nil, fmt.Errorf("could not resolve ECS Task metadata")
}
// Will retrieve the container metadata
// for your current Fargate environment (either V3 or V4)
// based on the environment variables that are present
func GetContainer(ctx context.Context, client *http.Client) (interface{}, error) {
// If the ECS Metadata URI for v4 is set,
// use this. When running on platform version 4,
// v3 might also still be set, though we prioritize the newer format
isV4 := os.Getenv(ecsMetadataUriEnvV4) != ""
if isV4 {
return GetContainerV4(ctx, client)
}
// If the Metadata URI for v4 wasn't set,
// check for v3
isV3 := os.Getenv(ecsMetadataUriEnvV3) != ""
if isV3 {
return GetContainerV3(ctx, client)
}
return nil, fmt.Errorf("could not resolve ECS Task metadata")
}
// Will check whether the environment
// has available metadata URIs to use for fetching
func Has() bool {
if os.Getenv(ecsMetadataUriEnvV3) != "" {
return true
}
if os.Getenv(ecsMetadataUriEnvV4) != "" {
return true
}
return false
}