-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ecrm.go
158 lines (139 loc) · 3.79 KB
/
ecrm.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
package ecrm
import (
"context"
"errors"
"fmt"
"log"
"strings"
"github.com/Songmu/prompter"
"github.com/aws/aws-sdk-go-v2/aws"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecr"
ecrTypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
"github.com/samber/lo"
)
const (
MediaTypeSociIndex = "application/vnd.amazon.soci.index.v1+json"
)
var untaggedStr = "__UNTAGGED__"
type App struct {
Version string
awsCfg aws.Config
ecr *ecr.Client
region string
}
func New(ctx context.Context) (*App, error) {
cfg, err := awsConfig.LoadDefaultConfig(ctx)
if err != nil {
return nil, err
}
return &App{
region: cfg.Region,
awsCfg: cfg,
ecr: ecr.NewFromConfig(cfg),
}, nil
}
func (app *App) Run(ctx context.Context, path string, opt *Option) error {
if err := opt.Validate(); err != nil {
return fmt.Errorf("invalid option: %w", err)
}
c, err := LoadConfig(path)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
scanner := NewScanner(app.awsCfg)
if err := scanner.LoadFiles(opt.ScannedFiles); err != nil {
return fmt.Errorf("failed to load scanned image URIs: %w", err)
}
if opt.Scan {
if err := scanner.Scan(ctx, c); err != nil {
return fmt.Errorf("failed to scan: %w", err)
}
}
log.Println("[info] total", len(scanner.Images), "image URIs in use")
if opt.ScanOnly {
return ShowScanResult(scanner, opt)
}
planner := NewPlanner(app.awsCfg)
sums, candidates, err := planner.Plan(ctx, c.Repositories, scanner.Images, opt.Repository)
if err != nil {
return fmt.Errorf("failed to plan: %w", err)
}
if err := ShowSummary(sums, opt); err != nil {
return fmt.Errorf("failed to show summary: %w", err)
}
if !opt.Delete {
return nil
}
for _, name := range candidates.RepositoryNames() {
if err := app.DeleteImages(ctx, name, candidates[name], opt.Force); err != nil {
return fmt.Errorf("failed to delete images: %w", err)
}
}
return nil
}
func ShowScanResult(s *Scanner, opt *Option) error {
w, err := opt.OutputWriter()
if err != nil {
return fmt.Errorf("failed to open output: %w", err)
}
defer w.Close()
if err := s.Save(w); err != nil {
return fmt.Errorf("failed to save scanned image URIs: %w", err)
}
return nil
}
func ShowSummary(s SummaryTable, opt *Option) error {
w, err := opt.OutputWriter()
if err != nil {
return fmt.Errorf("failed to open output: %w", err)
}
defer w.Close()
return s.Print(w, opt.Format)
}
const batchDeleteImageIdsLimit = 100
const batchGetImageLimit = 100
// DeleteImages deletes images from the repository
func (app *App) DeleteImages(ctx context.Context, repo RepositoryName, ids []ecrTypes.ImageIdentifier, force bool) error {
if len(ids) == 0 {
log.Println("[info] no need to delete images on", repo)
return nil
}
if !force {
if !prompter.YN(fmt.Sprintf("Do you delete %d images on %s?", len(ids), repo), false) {
return errors.New("aborted")
}
}
for _, id := range ids {
log.Printf("[notice] Deleting %s %s", repo, *id.ImageDigest)
}
chunkIDs := lo.Chunk(ids, batchDeleteImageIdsLimit)
var deletedCount int
defer func() {
log.Printf("[info] Deleted %d images on %s", deletedCount, repo)
}()
for _, ids := range chunkIDs {
output, err := app.ecr.BatchDeleteImage(ctx, &ecr.BatchDeleteImageInput{
ImageIds: ids,
RepositoryName: aws.String(string(repo)),
})
if err != nil {
return err
}
deletedCount += len(output.ImageIds)
}
return nil
}
func (app *App) GenerateConfig(ctx context.Context, path string) error {
g := NewGenerator(app.awsCfg)
return g.GenerateConfig(ctx, path)
}
func imageTag(d ecrTypes.ImageDetail) (string, bool) {
if len(d.ImageTags) > 1 {
return "{" + strings.Join(d.ImageTags, ",") + "}", true
} else if len(d.ImageTags) == 1 {
return d.ImageTags[0], true
} else {
return untaggedStr, false
}
}