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
|
package image
import (
"strings"
"time"
"github.com/distribution/distribution/v3/reference"
"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/log"
"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/tag"
)
type ContainerImage struct {
RegistryURL string
ImageName string
ImageTag *tag.ImageTag
ImageAlias string
HelmParamImageName string
HelmParamImageVersion string
KustomizeImage *ContainerImage
original string
}
type ContainerImageList []*ContainerImage
// NewFromIdentifier parses an image identifier and returns a populated ContainerImage
func NewFromIdentifier(identifier string) *ContainerImage {
imgRef := identifier
alias := ""
if strings.Contains(identifier, "=") {
n := strings.SplitN(identifier, "=", 2)
imgRef = n[1]
alias = n[0]
}
if parsed, err := reference.ParseNormalizedNamed(imgRef); err == nil {
img := ContainerImage{}
img.RegistryURL = reference.Domain(parsed)
// remove default registry for backwards-compatibility
if img.RegistryURL == "docker.io" && !strings.HasPrefix(imgRef, "docker.io") {
img.RegistryURL = ""
}
img.ImageAlias = alias
img.ImageName = reference.Path(parsed)
// if library/ was added to the image name, remove it
if !strings.HasPrefix(imgRef, "library/") {
img.ImageName = strings.TrimPrefix(img.ImageName, "library/")
}
if digested, ok := parsed.(reference.Digested); ok {
img.ImageTag = &tag.ImageTag{
TagDigest: string(digested.Digest()),
}
} else if tagged, ok := parsed.(reference.Tagged); ok {
img.ImageTag = &tag.ImageTag{
TagName: tagged.Tag(),
}
}
img.original = identifier
return &img
}
// if distribution couldn't parse it, fall back to the legacy parsing logic
img := ContainerImage{}
img.RegistryURL = getRegistryFromIdentifier(identifier)
img.ImageAlias, img.ImageName, img.ImageTag = getImageTagFromIdentifier(identifier)
img.original = identifier
return &img
}
// String returns the string representation of given ContainerImage
func (img *ContainerImage) String() string {
str := ""
if img.ImageAlias != "" {
str += img.ImageAlias
str += "="
}
str += img.GetFullNameWithTag()
return str
}
func (img *ContainerImage) GetFullNameWithoutTag() string {
str := ""
if img.RegistryURL != "" {
str += img.RegistryURL + "/"
}
str += img.ImageName
return str
}
// GetFullNameWithTag returns the complete image slug, including the registry
// and any tag digest or tag name set for the image.
func (img *ContainerImage) GetFullNameWithTag() string {
str := ""
if img.RegistryURL != "" {
str += img.RegistryURL + "/"
}
str += img.ImageName
if img.ImageTag != nil {
if img.ImageTag.TagName != "" {
str += ":"
str += img.ImageTag.TagName
}
if img.ImageTag.TagDigest != "" {
str += "@"
str += img.ImageTag.TagDigest
}
}
return str
}
// GetTagWithDigest returns tag name along with any tag digest set for the image
func (img *ContainerImage) GetTagWithDigest() string {
str := ""
if img.ImageTag != nil {
if img.ImageTag.TagName != "" {
str += img.ImageTag.TagName
}
if img.ImageTag.TagDigest != "" {
if str == "" {
str += "latest"
}
str += "@"
str += img.ImageTag.TagDigest
}
}
return str
}
func (img *ContainerImage) Original() string {
return img.original
}
// IsUpdatable checks whether the given image can be updated with newTag while
// taking tagSpec into account. tagSpec must be given as a semver compatible
// version spec, i.e. ^1.0 or ~2.1
func (img *ContainerImage) IsUpdatable(newTag, tagSpec string) bool {
return false
}
// WithTag returns a copy of img with new tag information set
func (img *ContainerImage) WithTag(newTag *tag.ImageTag) *ContainerImage {
nimg := &ContainerImage{}
nimg.RegistryURL = img.RegistryURL
nimg.ImageName = img.ImageName
nimg.ImageTag = newTag
nimg.ImageAlias = img.ImageAlias
nimg.HelmParamImageName = img.HelmParamImageName
nimg.HelmParamImageVersion = img.HelmParamImageVersion
return nimg
}
func (img *ContainerImage) DiffersFrom(other *ContainerImage, checkVersion bool) bool {
return img.RegistryURL != other.RegistryURL || img.ImageName != other.ImageName || (checkVersion && img.ImageTag.TagName != other.ImageTag.TagName)
}
// ContainsImage checks whether img is contained in a list of images
func (list *ContainerImageList) ContainsImage(img *ContainerImage, checkVersion bool) *ContainerImage {
// if there is a KustomizeImage override, check it for a match first
if img.KustomizeImage != nil {
if kustomizeMatch := list.ContainsImage(img.KustomizeImage, checkVersion); kustomizeMatch != nil {
return kustomizeMatch
}
}
for _, image := range *list {
if img.ImageName == image.ImageName && image.RegistryURL == img.RegistryURL {
if !checkVersion || image.ImageTag.TagName == img.ImageTag.TagName {
return image
}
}
}
return nil
}
func (list *ContainerImageList) Originals() []string {
results := make([]string, len(*list))
for i, img := range *list {
results[i] = img.Original()
}
return results
}
// String Returns the name of all images as a string, separated using comma
func (list *ContainerImageList) String() string {
imgNameList := make([]string, 0)
for _, image := range *list {
imgNameList = append(imgNameList, image.String())
}
return strings.Join(imgNameList, ",")
}
// Gets the registry URL from an image identifier
func getRegistryFromIdentifier(identifier string) string {
var imageString string
comp := strings.Split(identifier, "=")
if len(comp) > 1 {
imageString = comp[1]
} else {
imageString = identifier
}
comp = strings.Split(imageString, "/")
if len(comp) > 1 && strings.Contains(comp[0], ".") {
return comp[0]
} else {
return ""
}
}
// Gets the image name and tag from an image identifier
func getImageTagFromIdentifier(identifier string) (string, string, *tag.ImageTag) {
var imageString string
var sourceName string
// The original name is prepended to the image name, separated by =
comp := strings.SplitN(identifier, "=", 2)
if len(comp) == 2 {
sourceName = comp[0]
imageString = comp[1]
} else {
imageString = identifier
}
// Strip any repository identifier from the string
comp = strings.Split(imageString, "/")
if len(comp) > 1 && strings.Contains(comp[0], ".") {
imageString = strings.Join(comp[1:], "/")
}
// We can either have a tag name or a digest reference, or both
// jannfis/test-image:0.1
// gcr.io/jannfis/test-image:0.1
// gcr.io/jannfis/test-image@sha256:abcde
// gcr.io/jannfis/test-image:test-tag@sha256:abcde
if strings.Contains(imageString, "@") {
comp = strings.SplitN(imageString, "@", 2)
colonPos := strings.LastIndex(comp[0], ":")
slashPos := strings.LastIndex(comp[0], "/")
if colonPos > slashPos {
// first half (before @) contains image and tag name
return sourceName, comp[0][:colonPos], tag.NewImageTag(comp[0][colonPos+1:], time.Unix(0, 0), comp[1])
} else {
// first half contains image name without tag name
return sourceName, comp[0], tag.NewImageTag("", time.Unix(0, 0), comp[1])
}
} else {
comp = strings.SplitN(imageString, ":", 2)
if len(comp) != 2 {
return sourceName, imageString, nil
} else {
tagName, tagDigest := getImageDigestFromTag(comp[1])
return sourceName, comp[0], tag.NewImageTag(tagName, time.Unix(0, 0), tagDigest)
}
}
}
func getImageDigestFromTag(tagStr string) (string, string) {
a := strings.Split(tagStr, "@")
if len(a) != 2 {
return tagStr, ""
} else {
return a[0], a[1]
}
}
// LogContext returns a log context for the given image, with required fields
// set to the image's information.
func (img *ContainerImage) LogContext() *log.LogContext {
logCtx := log.WithContext()
logCtx.AddField("image_name", img.GetFullNameWithoutTag())
logCtx.AddField("image_alias", img.ImageAlias)
logCtx.AddField("registry_url", img.RegistryURL)
if img.ImageTag != nil {
logCtx.AddField("image_tag", img.ImageTag.TagName)
logCtx.AddField("image_digest", img.ImageTag.TagDigest)
}
return logCtx
}
|