-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.go
109 lines (98 loc) · 2.99 KB
/
util.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
package ecrm
import (
"context"
"log"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/ecr"
ecrTypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/lambda"
lambdaTypes "github.com/aws/aws-sdk-go-v2/service/lambda/types"
ociTypes "github.com/google/go-containerregistry/pkg/v1/types"
)
func isContainerImage(d ecrTypes.ImageDetail) bool {
t := ociTypes.MediaType(aws.ToString(d.ArtifactMediaType))
return t == ociTypes.DockerConfigJSON || t == ociTypes.OCIConfigJSON
}
func isImageIndex(d ecrTypes.ImageDetail) bool {
if aws.ToString(d.ArtifactMediaType) != "" {
return false
}
switch ociTypes.MediaType(aws.ToString(d.ImageManifestMediaType)) {
case ociTypes.OCIImageIndex:
return true
case ociTypes.DockerManifestList:
return true
}
return false
}
func isSociIndex(d ecrTypes.ImageDetail) bool {
return ociTypes.MediaType(aws.ToString(d.ArtifactMediaType)) == MediaTypeSociIndex
}
func taskDefinitionFamilies(ctx context.Context, client *ecs.Client) ([]string, error) {
tds := make([]string, 0)
p := ecs.NewListTaskDefinitionFamiliesPaginator(client, &ecs.ListTaskDefinitionFamiliesInput{})
for p.HasMorePages() {
td, err := p.NextPage(ctx)
if err != nil {
return nil, err
}
log.Println("[debug] task definition families:", td.Families)
tds = append(tds, td.Families...)
}
return tds, nil
}
func clusterArns(ctx context.Context, client *ecs.Client) ([]string, error) {
clusters := make([]string, 0)
p := ecs.NewListClustersPaginator(client, &ecs.ListClustersInput{})
for p.HasMorePages() {
co, err := p.NextPage(ctx)
if err != nil {
return nil, err
}
clusters = append(clusters, co.ClusterArns...)
}
return clusters, nil
}
func lambdaFunctions(ctx context.Context, client *lambda.Client) ([]lambdaTypes.FunctionConfiguration, error) {
fns := make([]lambdaTypes.FunctionConfiguration, 0)
p := lambda.NewListFunctionsPaginator(client, &lambda.ListFunctionsInput{})
for p.HasMorePages() {
r, err := p.NextPage(ctx)
if err != nil {
return nil, err
}
for _, fn := range r.Functions {
if fn.PackageType != "Image" {
continue
}
log.Printf("[debug] lambda function %s PackageType %s", *fn.FunctionName, fn.PackageType)
fns = append(fns, fn)
}
}
return fns, nil
}
func ecrRepositories(ctx context.Context, client *ecr.Client) ([]ecrTypes.Repository, error) {
repos := make([]ecrTypes.Repository, 0)
p := ecr.NewDescribeRepositoriesPaginator(client, &ecr.DescribeRepositoriesInput{})
for p.HasMorePages() {
repo, err := p.NextPage(ctx)
if err != nil {
return nil, err
}
repos = append(repos, repo.Repositories...)
}
return repos, nil
}
func arnToName(name, removePrefix string) string {
if arn.IsARN(name) {
a, _ := arn.Parse(name)
return strings.Replace(a.Resource, removePrefix, "", 1)
}
return name
}
func clusterArnToName(arn string) string {
return arnToName(arn, "cluster/")
}