-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
225 lines (198 loc) · 5.31 KB
/
config.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package ecrm
import (
"errors"
"fmt"
"io"
"log"
"os"
"time"
"github.com/fujiwara/ecrm/wildcard"
"github.com/goccy/go-yaml"
"github.com/k1LoW/duration"
)
var (
DefaultKeepCount = 5
DefaultExpiresStr = "30d"
DefaultKeepTagPatterns = []string{"latest"}
)
type Config struct {
Clusters []*ClusterConfig `yaml:"clusters"`
TaskDefinitions []*TaskdefConfig `yaml:"task_definitions"`
LambdaFunctions []*LambdaConfig `yaml:"lambda_functions"`
Repositories []*RepositoryConfig `yaml:"repositories"`
}
func (c *Config) Validate() error {
if c.Clusters == nil {
log.Println("[warn] clusters are not defined. No ECS clusters will be scanned to find images now using.")
}
for _, cc := range c.Clusters {
if err := cc.Validate(); err != nil {
return err
}
}
if c.TaskDefinitions == nil {
log.Println("[warn] task_definitions are not defined. No task definitions will be scanned to find images now using.")
}
for _, tc := range c.TaskDefinitions {
if err := tc.Validate(); err != nil {
return err
}
}
if c.LambdaFunctions == nil {
log.Println("[warn] lambda_functions are not defined. No Lambda functions will be scanned to find using images.")
}
for _, lc := range c.LambdaFunctions {
if err := lc.Validate(); err != nil {
return err
}
}
for _, rc := range c.Repositories {
if err := rc.Validate(); err != nil {
return err
}
}
return nil
}
type ClusterConfig struct {
Name string `yaml:"name,omitempty"`
NamePattern string `yaml:"name_pattern,omitempty"`
}
func (c *ClusterConfig) Validate() error {
if c.Name == "" && c.NamePattern == "" {
return errors.New("cluster name or name_pattern is required")
}
return nil
}
func (c *ClusterConfig) Match(name string) bool {
name = clusterArnToName(name)
if c.Name == name {
return true
}
return wildcard.Match(c.NamePattern, name)
}
type RepositoryName string
type RepositoryConfig struct {
Name RepositoryName `yaml:"name,omitempty"`
NamePattern string `yaml:"name_pattern,omitempty"`
Expires string `yaml:"expires,omitempty"`
KeepCount int64 `yaml:"keep_count,omitempty"`
KeepTagPatterns []string `yaml:"keep_tag_patterns,omitempty"`
expireBefore time.Time
}
func (r *RepositoryConfig) Validate() error {
now := time.Now()
if r.Name != "" && r.NamePattern != "" {
return errors.New("repositories name and name_pattern are exclusive")
}
if r.Expires != "" {
if d, err := duration.Parse(r.Expires); err != nil {
return err
} else {
r.expireBefore = now.Add(-d)
}
} else {
return fmt.Errorf("repository %s%s expires is required", r.Name, r.NamePattern)
}
if len(r.KeepTagPatterns) == 0 {
log.Printf(
"[warn] keep_tag_patterns are not defined. set default keep_tag_patterns to %v",
DefaultKeepTagPatterns,
)
r.KeepTagPatterns = DefaultKeepTagPatterns
}
return nil
}
func (r *RepositoryConfig) MatchName(name RepositoryName) bool {
if r.Name == name {
return true
}
return wildcard.Match(r.NamePattern, string(name))
}
func (r *RepositoryConfig) MatchTag(tag string) bool {
for _, pattern := range r.KeepTagPatterns {
if wildcard.Match(pattern, tag) {
return true
}
}
return false
}
func (r *RepositoryConfig) IsExpired(at time.Time) bool {
return at.Before(r.expireBefore)
}
func LoadConfig(path string) (*Config, error) {
log.Println("[info] loading config file:", path)
f, err := os.Open(path)
if err != nil {
return nil, err
}
b, err := io.ReadAll(f)
if err != nil {
return nil, err
}
c := &Config{}
if err := yaml.Unmarshal(b, c); err != nil {
return nil, err
}
if err := c.Validate(); err != nil {
return nil, err
}
return c, nil
}
type TaskdefConfig struct {
Name string `yaml:"name,omitempty"`
NamePattern string `yaml:"name_pattern,omitempty"`
KeepCount int64 `yaml:"keep_count,omitempty"`
}
func (c *TaskdefConfig) Validate() error {
if c.Name != "" && c.NamePattern != "" {
return errors.New("task_definitions name and name_pattern are exclusive")
}
if c.KeepCount == 0 {
log.Printf(
"[warn] keep_count for task definition %s%s is not defined. set default keep_count to %d",
c.Name,
c.NamePattern,
DefaultKeepCount,
)
c.KeepCount = int64(DefaultKeepCount)
}
return nil
}
func (c *TaskdefConfig) Match(name string) bool {
if c.Name == name {
return true
}
return wildcard.Match(c.NamePattern, name)
}
type LambdaConfig struct {
Name string `yaml:"name,omitempty"`
NamePattern string `yaml:"name_pattern,omitempty"`
KeepCount int64 `yaml:"keep_count,omitempty"`
KeepAliase *bool `yaml:"keep_aliase,omitempty"` // for backward compatibility
}
func (c *LambdaConfig) Validate() error {
if c.Name != "" && c.NamePattern != "" {
return errors.New("lambda_functions name and name_pattern are exclusive")
}
if c.KeepCount == 0 {
log.Printf(
"[warn] keep_count for lambda_functions %s%s is not defined. Using default keep_count=%d",
c.Name,
c.NamePattern,
DefaultKeepCount,
)
c.KeepCount = int64(DefaultKeepCount)
}
if c.KeepAliase != nil {
log.Printf(
"[warn] \"keep_aliase\" is obsoleted. All aliased versions are always kept. Please remove it from the lambda_functions section.",
)
}
return nil
}
func (c *LambdaConfig) Match(name string) bool {
if c.Name == name {
return true
}
return wildcard.Match(c.NamePattern, name)
}