/
options.go
290 lines (271 loc) · 8.97 KB
/
options.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package image
import (
"fmt"
"regexp"
"runtime"
"strings"
"github.com/argoproj-labs/argocd-image-updater/pkg/common"
"github.com/argoproj-labs/argocd-image-updater/pkg/options"
)
// GetParameterHelmImageName gets the value for image-name option for the image
// from a set of annotations
func (img *ContainerImage) GetParameterHelmImageName(annotations map[string]string) string {
key := fmt.Sprintf(common.HelmParamImageNameAnnotation, img.normalizedSymbolicName())
val, ok := annotations[key]
if !ok {
return ""
}
return val
}
// GetParameterHelmImageTag gets the value for image-tag option for the image
// from a set of annotations
func (img *ContainerImage) GetParameterHelmImageTag(annotations map[string]string) string {
key := fmt.Sprintf(common.HelmParamImageTagAnnotation, img.normalizedSymbolicName())
val, ok := annotations[key]
if !ok {
return ""
}
return val
}
// GetParameterHelmImageSpec gets the value for image-spec option for the image
// from a set of annotations
func (img *ContainerImage) GetParameterHelmImageSpec(annotations map[string]string) string {
key := fmt.Sprintf(common.HelmParamImageSpecAnnotation, img.normalizedSymbolicName())
val, ok := annotations[key]
if !ok {
return ""
}
return val
}
// GetParameterKustomizeImageName gets the value for image-spec option for the
// image from a set of annotations
func (img *ContainerImage) GetParameterKustomizeImageName(annotations map[string]string) string {
key := fmt.Sprintf(common.KustomizeApplicationNameAnnotation, img.normalizedSymbolicName())
val, ok := annotations[key]
if !ok {
return ""
}
return val
}
// HasForceUpdateOptionAnnotation gets the value for force-update option for the
// image from a set of annotations
func (img *ContainerImage) HasForceUpdateOptionAnnotation(annotations map[string]string) bool {
forceUpdateAnnotations := []string{
fmt.Sprintf(common.ForceUpdateOptionAnnotation, img.normalizedSymbolicName()),
common.ApplicationWideForceUpdateOptionAnnotation,
}
var forceUpdateVal = ""
for _, key := range forceUpdateAnnotations {
if val, ok := annotations[key]; ok {
forceUpdateVal = val
break
}
}
return forceUpdateVal == "true"
}
// GetParameterSort gets and validates the value for the sort option for the
// image from a set of annotations
func (img *ContainerImage) GetParameterUpdateStrategy(annotations map[string]string) UpdateStrategy {
updateStrategyAnnotations := []string{
fmt.Sprintf(common.UpdateStrategyAnnotation, img.normalizedSymbolicName()),
common.ApplicationWideUpdateStrategyAnnotation,
}
var updateStrategyVal = ""
for _, key := range updateStrategyAnnotations {
if val, ok := annotations[key]; ok {
updateStrategyVal = val
break
}
}
logCtx := img.LogContext()
if updateStrategyVal == "" {
logCtx.Tracef("No sort option found")
// Default is sort by version
return StrategySemVer
}
logCtx.Tracef("Found update strategy %s", updateStrategyVal)
return img.ParseUpdateStrategy(updateStrategyVal)
}
func (img *ContainerImage) ParseUpdateStrategy(val string) UpdateStrategy {
logCtx := img.LogContext()
switch strings.ToLower(val) {
case "semver":
return StrategySemVer
case "latest":
return StrategyLatest
case "name":
return StrategyName
case "digest":
return StrategyDigest
default:
logCtx.Warnf("Unknown sort option %s -- using semver", val)
return StrategySemVer
}
}
// GetParameterMatch returns the match function and pattern to use for matching
// tag names. If an invalid option is found, it returns MatchFuncNone as the
// default, to prevent accidental matches.
func (img *ContainerImage) GetParameterMatch(annotations map[string]string) (MatchFuncFn, interface{}) {
allowTagsAnnotations := []string{
fmt.Sprintf(common.AllowTagsOptionAnnotation, img.normalizedSymbolicName()),
common.ApplicationWideAllowTagsOptionAnnotation,
}
var allowTagsVal = ""
for _, key := range allowTagsAnnotations {
if val, ok := annotations[key]; ok {
allowTagsVal = val
break
}
}
logCtx := img.LogContext()
if allowTagsVal == "" {
// The old match-tag annotation is deprecated and will be subject to removal
// in a future version.
key := fmt.Sprintf(common.OldMatchOptionAnnotation, img.normalizedSymbolicName())
val, ok := annotations[key]
if ok {
logCtx.Warnf("The 'tag-match' annotation is deprecated and subject to removal. Please use 'allow-tags' annotation instead.")
allowTagsVal = val
}
}
if allowTagsVal == "" {
logCtx.Tracef("No match annotation found")
return MatchFuncAny, ""
}
return img.ParseMatchfunc(allowTagsVal)
}
// ParseMatchfunc returns a matcher function and its argument from given value
func (img *ContainerImage) ParseMatchfunc(val string) (MatchFuncFn, interface{}) {
logCtx := img.LogContext()
// The special value "any" doesn't take any parameter
if strings.ToLower(val) == "any" {
return MatchFuncAny, nil
}
opt := strings.SplitN(val, ":", 2)
if len(opt) != 2 {
logCtx.Warnf("Invalid match option syntax '%s', ignoring", val)
return MatchFuncNone, nil
}
switch strings.ToLower(opt[0]) {
case "regexp":
re, err := regexp.Compile(opt[1])
if err != nil {
logCtx.Warnf("Could not compile regexp '%s'", opt[1])
return MatchFuncNone, nil
}
return MatchFuncRegexp, re
default:
logCtx.Warnf("Unknown match function: %s", opt[0])
return MatchFuncNone, nil
}
}
// GetParameterPullSecret retrieves an image's pull secret credentials
func (img *ContainerImage) GetParameterPullSecret(annotations map[string]string) *CredentialSource {
pullSecretAnnotations := []string{
fmt.Sprintf(common.PullSecretAnnotation, img.normalizedSymbolicName()),
common.ApplicationWidePullSecretAnnotation,
}
var pullSecretVal = ""
for _, key := range pullSecretAnnotations {
if val, ok := annotations[key]; ok {
pullSecretVal = val
break
}
}
logCtx := img.LogContext()
if pullSecretVal == "" {
logCtx.Tracef("No pull-secret annotation found")
return nil
}
credSrc, err := ParseCredentialSource(pullSecretVal, false)
if err != nil {
logCtx.Warnf("Invalid credential reference specified: %s", pullSecretVal)
return nil
}
return credSrc
}
// GetParameterIgnoreTags retrieves a list of tags to ignore from a comma-separated string
func (img *ContainerImage) GetParameterIgnoreTags(annotations map[string]string) []string {
ignoreTagsAnnotations := []string{
fmt.Sprintf(common.IgnoreTagsOptionAnnotation, img.normalizedSymbolicName()),
common.ApplicationWideIgnoreTagsOptionAnnotation,
}
var ignoreTagsVal = ""
for _, key := range ignoreTagsAnnotations {
if val, ok := annotations[key]; ok {
ignoreTagsVal = val
break
}
}
logCtx := img.LogContext()
if ignoreTagsVal == "" {
logCtx.Tracef("No ignore-tags annotation found")
return nil
}
ignoreList := make([]string, 0)
tags := strings.Split(strings.TrimSpace(ignoreTagsVal), ",")
for _, tag := range tags {
// We ignore empty tags
trimmed := strings.TrimSpace(tag)
if trimmed != "" {
ignoreList = append(ignoreList, trimmed)
}
}
return ignoreList
}
// GetPlatformOptions sets up platform constraints for an image. If no platform
// is specified in the annotations, we restrict the platform for images to the
// platform we're executed on unless unrestricted is set to true, in which case
// we do not setup a platform restriction if no platform annotation is found.
func (img *ContainerImage) GetPlatformOptions(annotations map[string]string, unrestricted bool) *options.ManifestOptions {
logCtx := img.LogContext()
var opts *options.ManifestOptions = options.NewManifestOptions()
key := fmt.Sprintf(common.PlatformsAnnotation, img.normalizedSymbolicName())
val, ok := annotations[key]
if !ok {
if !unrestricted {
os := runtime.GOOS
arch := runtime.GOARCH
variant := ""
if strings.Contains(runtime.GOARCH, "/") {
a := strings.SplitN(runtime.GOARCH, "/", 2)
arch = a[0]
variant = a[1]
}
logCtx.Tracef("Using runtime platform constraint %s", options.PlatformKey(os, arch, variant))
opts = opts.WithPlatform(os, arch, variant)
}
} else {
platforms := strings.Split(val, ",")
for _, ps := range platforms {
pt := strings.TrimSpace(ps)
os, arch, variant, err := ParsePlatform(pt)
if err != nil {
// If the platform identifier could not be parsed, we set the
// constraint intentionally to the invalid value so we don't
// end up updating to the wrong architecture possibly.
os = ps
logCtx.Warnf("could not parse platform identifier '%v': invalid format", pt)
}
logCtx.Tracef("Adding platform constraint %s", options.PlatformKey(os, arch, variant))
opts = opts.WithPlatform(os, arch, variant)
}
}
return opts
}
func ParsePlatform(platformID string) (string, string, string, error) {
p := strings.SplitN(platformID, "/", 3)
if len(p) < 2 {
return "", "", "", fmt.Errorf("could not parse platform constraint '%s'", platformID)
}
os := p[0]
arch := p[1]
variant := ""
if len(p) == 3 {
variant = p[2]
}
return os, arch, variant, nil
}
func (img *ContainerImage) normalizedSymbolicName() string {
return strings.ReplaceAll(img.ImageAlias, "/", "_")
}