-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecs.go
More file actions
106 lines (91 loc) · 2.74 KB
/
Copy pathecs.go
File metadata and controls
106 lines (91 loc) · 2.74 KB
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
package audit
import (
"context"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/service/ecs"
ecstypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/loilo-inc/canarycage/v6/awsiface"
)
type ecsTool struct {
Ecs awsiface.EcsClient
}
type EcsTool interface {
GetServiceImageInfos(ctx context.Context, cluster string, service string) ([]ImageInfo, error)
}
func newEcsTool(ecsClient awsiface.EcsClient) EcsTool {
return &ecsTool{Ecs: ecsClient}
}
func (t *ecsTool) GetServiceImageInfos(ctx context.Context, cluster string, service string) ([]ImageInfo, error) {
res, err := t.Ecs.DescribeServices(ctx, &ecs.DescribeServicesInput{
Cluster: &cluster,
Services: []string{service},
})
if err != nil {
return nil, err
}
if len(res.Services) == 0 || res.Services[0].TaskDefinition == nil {
return nil, fmt.Errorf("service not found: %s/%s", cluster, service)
}
taskDefinition := *res.Services[0].TaskDefinition
tdRes, err := t.Ecs.DescribeTaskDefinition(ctx, &ecs.DescribeTaskDefinitionInput{
TaskDefinition: &taskDefinition,
})
if err != nil {
return nil, err
}
td := tdRes.TaskDefinition
if td == nil {
return nil, fmt.Errorf("task definition not found: %s", taskDefinition)
}
arch := ecstypes.CPUArchitectureX8664
if td.RuntimePlatform != nil && td.RuntimePlatform.CpuArchitecture != "" {
arch = td.RuntimePlatform.CpuArchitecture
}
if len(td.ContainerDefinitions) == 0 {
return nil, fmt.Errorf("no container definitions found for task definition: %s", taskDefinition)
}
images := make([]ImageInfo, 0, len(td.ContainerDefinitions))
for _, cd := range td.ContainerDefinitions {
if cd.Name == nil || cd.Image == nil {
return nil, fmt.Errorf("container definition is missing name or image: %s", taskDefinition)
}
parsed := ParseImageInfo(*cd.Image)
images = append(images, ImageInfo{
ContainerName: *cd.Name,
PlatformArch: arch,
Registry: parsed.Registry,
Repository: parsed.Repository,
Tag: parsed.Tag,
})
}
return images, nil
}
type ParsedImageInfo struct {
Registry string
Repository string
Tag string
}
func ParseImageInfo(image string) ParsedImageInfo {
parts := strings.Split(image, "/")
if len(parts) == 1 {
repository, tag := splitRepoTag(image)
return ParsedImageInfo{Repository: repository, Tag: tag}
}
registry := parts[0]
repoAndTag := strings.Join(parts[1:], "/")
repository, tag := splitRepoTag(repoAndTag)
return ParsedImageInfo{Registry: registry, Repository: repository, Tag: tag}
}
func splitRepoTag(value string) (string, string) {
repository := value
tag := "latest"
if strings.Contains(value, ":") {
parts := strings.SplitN(value, ":", 2)
repository = parts[0]
if parts[1] != "" {
tag = parts[1]
}
}
return repository, tag
}