Skip to content

Commit 54efebf

Browse files
authored
Refactor argocd and registry-scanner modules to use CRD Spec instead of Annotations. (#1190)
Signed-off-by: Denis Karpelevich <56302307+dkarpele@users.noreply.github.com>
1 parent 6d7c38d commit 54efebf

36 files changed

+2587
-955
lines changed

api/v1alpha1/imageupdater_types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,18 @@ type CommonUpdateSettings struct {
148148
// This acts as the default if not overridden at a more specific level.
149149
// +optional
150150
// +kubebuilder:default:="semver"
151-
UpdateStrategy string `json:"updateStrategy,omitempty"`
151+
UpdateStrategy *string `json:"updateStrategy,omitempty"`
152152

153153
// ForceUpdate specifies whether updates should be forced.
154154
// This acts as the default if not overridden.
155155
// +optional
156156
// +kubebuilder:default:=false
157-
ForceUpdate bool `json:"forceUpdate,omitempty"`
157+
ForceUpdate *bool `json:"forceUpdate,omitempty"`
158158

159159
// AllowTags is a regex pattern for tags to allow.
160160
// This acts as the default if not overridden.
161161
// +optional
162-
AllowTags string `json:"allowTags,omitempty"`
162+
AllowTags *string `json:"allowTags,omitempty"`
163163

164164
// IgnoreTags is a list of glob-like patterns of tags to ignore.
165165
// This acts as the default and can be overridden at more specific levels.
@@ -170,7 +170,7 @@ type CommonUpdateSettings struct {
170170
// PullSecret is the pull secret to use for images.
171171
// This acts as the default if not overridden.
172172
// +optional
173-
PullSecret string `json:"pullSecret,omitempty"`
173+
PullSecret *string `json:"pullSecret,omitempty"`
174174
}
175175

176176
// WriteBackConfig defines how and where to write back image updates.

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ This enables a CRD-driven approach to automated image updates with Argo CD.
130130
if err != nil || st.IsDir() {
131131
setupLogger.Info("Registry configuration not found or is a directory, using default configuration", "path", cfg.RegistriesConf, "error", err)
132132
} else {
133-
err = registry.LoadRegistryConfiguration(cfg.RegistriesConf, false)
133+
err = registry.LoadRegistryConfiguration(context.Background(), cfg.RegistriesConf, false)
134134
if err != nil {
135135
setupLogger.Error(err, "could not load registry configuration", "path", cfg.RegistriesConf)
136136
return nil
@@ -429,7 +429,7 @@ func (cw *CacheWarmer) Start(ctx context.Context) error {
429429
entries := 0
430430
eps := registry.ConfiguredEndpoints()
431431
for _, ep := range eps {
432-
r, err := registry.GetRegistryEndpoint(ep)
432+
r, err := registry.GetRegistryEndpoint(ctx, ep)
433433
if err == nil {
434434
entries += r.Cache.NumEntries()
435435
}

cmd/test.go

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,76 +56,82 @@ argocd-image-updater test nginx --semver-constraint v1.17.x
5656
argocd-image-updater test nginx --allow-tags '^1.19.\d+(\-.*)*$' --update-strategy latest
5757
`,
5858
Run: func(cmd *cobra.Command, args []string) {
59+
// Create a root context and logger for the command
60+
ctx := context.Background()
61+
logger := log.LoggerFromContext(ctx).WithField("command", "test")
62+
ctx = log.ContextWithLogger(ctx, logger)
63+
5964
if len(args) != 1 {
6065
cmd.HelpFunc()(cmd, args)
61-
log.Fatalf("image needs to be specified")
66+
logger.Fatalf("image needs to be specified")
6267
}
6368

6469
if err := log.SetLogLevel(logLevel); err != nil {
65-
log.Fatalf("could not set log level to %s: %v", logLevel, err)
70+
logger.Fatalf("could not set log level to %s: %v", logLevel, err)
6671
}
6772

6873
var kubeClient *kube.ImageUpdaterKubernetesClient
6974
var err error
75+
7076
if !disableKubernetes {
71-
ctx := context.Background()
7277
kubeClient, err = argocd.GetKubeConfig(ctx, "", kubeConfig)
7378
if err != nil {
74-
log.Fatalf("could not create K8s client: %v", err)
79+
logger.Fatalf("could not create K8s client: %v", err)
7580
}
7681
}
7782

7883
img := image.NewFromIdentifier(args[0])
7984

85+
fields := img.GetLogFields(img.ImageAlias)
86+
imgLogger := logger.WithFields(fields)
87+
imgCtx := log.ContextWithLogger(ctx, imgLogger)
88+
8089
vc := &image.VersionConstraint{
8190
Constraint: semverConstraint,
8291
Strategy: image.StrategySemVer,
8392
}
8493

85-
vc.Strategy = img.ParseUpdateStrategy(strategy)
94+
vc.Strategy = img.ParseUpdateStrategy(imgCtx, strategy)
8695

8796
if allowTags != "" {
88-
vc.MatchFunc, vc.MatchArgs = img.ParseMatchfunc(allowTags)
97+
vc.MatchFunc, vc.MatchArgs = img.ParseMatch(imgCtx, allowTags)
8998
}
9099

91100
vc.IgnoreList = ignoreTags
92101

93-
logCtx := img.LogContext()
94-
logCtx.Infof("retrieving information about image")
102+
imgLogger.Infof("retrieving information about image")
95103

96104
vc.Options = options.NewManifestOptions()
97105
for _, platform := range platforms {
98106
os, arch, variant, err := image.ParsePlatform(platform)
99107
if err != nil {
100-
logCtx.Fatalf("Could not parse platform %s: %v", platform, err)
108+
imgLogger.Fatalf("Could not parse platform %s: %v", platform, err)
101109
}
102110
if os != "linux" && os != "windows" {
103-
log.Warnf("Target platform is '%s/%s', but that's not a supported container platform. Forgot --platforms?", os, arch)
111+
imgLogger.Warnf("Target platform is '%s/%s', but that's not a supported container platform. Forgot --platforms?", os, arch)
104112
}
105113
vc.Options = vc.Options.WithPlatform(os, arch, variant)
106114
}
107115
vc.Options = vc.Options.WithMetadata(vc.Strategy.NeedsMetadata())
108116

109-
vc.Options.WithLogger(logCtx.AddField("application", "test"))
110-
111117
if registriesConfPath != "" {
112-
if err := registry.LoadRegistryConfiguration(registriesConfPath, false); err != nil {
113-
logCtx.Fatalf("could not load registries configuration: %v", err)
118+
if err := registry.LoadRegistryConfiguration(imgCtx, registriesConfPath, false); err != nil {
119+
imgLogger.Fatalf("could not load registries configuration: %v", err)
114120
}
115121
}
116122

117-
ep, err := registry.GetRegistryEndpoint(img.RegistryURL)
123+
ep, err := registry.GetRegistryEndpoint(imgCtx, img.RegistryURL)
118124
if err != nil {
119-
logCtx.Fatalf("could not get registry endpoint: %v", err)
125+
imgLogger.Fatalf("could not get registry endpoint: %v", err)
120126
}
121127

122-
if err := ep.SetEndpointCredentials(kubeClient.KubeClient); err != nil {
123-
logCtx.Fatalf("could not set registry credentials: %v", err)
128+
if err := ep.SetEndpointCredentials(imgCtx, kubeClient.KubeClient); err != nil {
129+
imgLogger.Fatalf("could not set registry credentials: %v", err)
124130
}
125131

126132
checkFlag := func(f *pflag.Flag) {
127133
if f.Name == "rate-limit" {
128-
logCtx.Infof("Overriding registry rate-limit to %d requests per second", rateLimit)
134+
imgLogger.Infof("Overriding registry rate-limit to %d requests per second", rateLimit)
129135
ep.Limiter = ratelimit.New(rateLimit)
130136
}
131137
}
@@ -137,40 +143,40 @@ argocd-image-updater test nginx --allow-tags '^1.19.\d+(\-.*)*$' --update-strate
137143
if credentials != "" {
138144
credSrc, err := image.ParseCredentialSource(credentials, false)
139145
if err != nil {
140-
logCtx.Fatalf("could not parse credential definition '%s': %v", credentials, err)
146+
imgLogger.Fatalf("could not parse credential definition '%s': %v", credentials, err)
141147
}
142-
creds, err = credSrc.FetchCredentials(ep.RegistryAPI, kubeClient.KubeClient)
148+
creds, err = credSrc.FetchCredentials(imgCtx, ep.RegistryAPI, kubeClient.KubeClient)
143149
if err != nil {
144-
logCtx.Fatalf("could not fetch credentials: %v", err)
150+
imgLogger.Fatalf("could not fetch credentials: %v", err)
145151
}
146152
username = creds.Username
147153
password = creds.Password
148154
}
149155

150156
regClient, err := registry.NewClient(ep, username, password)
151157
if err != nil {
152-
logCtx.Fatalf("could not create registry client: %v", err)
158+
imgLogger.Fatalf("could not create registry client: %v", err)
153159
}
154160

155-
logCtx.Infof("Fetching available tags and metadata from registry")
161+
imgLogger.Infof("Fetching available tags and metadata from registry")
156162

157-
tags, err := ep.GetTags(img, regClient, vc)
163+
tags, err := ep.GetTags(imgCtx, img, regClient, vc)
158164
if err != nil {
159-
logCtx.Fatalf("could not get tags: %v", err)
165+
imgLogger.Fatalf("could not get tags: %v", err)
160166
}
161167

162-
logCtx.Infof("Found %d tags in registry", len(tags.Tags()))
168+
imgLogger.Infof("Found %d tags in registry", len(tags.Tags()))
163169

164-
upImg, err := img.GetNewestVersionFromTags(vc, tags)
170+
upImg, err := img.GetNewestVersionFromTags(imgCtx, vc, tags)
165171
if err != nil {
166-
logCtx.Fatalf("could not get updateable image from tags: %v", err)
172+
imgLogger.Fatalf("could not get updateable image from tags: %v", err)
167173
}
168174
if upImg == nil {
169-
logCtx.Infof("no newer version of image found")
175+
imgLogger.Infof("no newer version of image found")
170176
return
171177
}
172178

173-
logCtx.Infof("latest image according to constraint is %s", img.WithTag(upImg))
179+
imgLogger.Infof("latest image according to constraint is %s", img.WithTag(upImg))
174180
},
175181
}
176182

config/samples/argocd-image-updater_v1alpha1_imageupdater.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ spec:
2222
applicationRefs:
2323
- namePattern: "image-updater-001"
2424
images:
25+
- alias: "test1"
26+
imageName: "test:1.1.0"
27+
- namePattern: "image-updater-001"
28+
commonUpdateSettings:
29+
updateStrategy: "latest" # Default strategy if not specified per image
30+
forceUpdate: true
31+
allowTags: "regexp:^[0-9a-f]{7}$"
32+
ignoreTags: [ "latest", "master" ]
33+
pullSecret: "secret:<namespace>/<secret_name>#<field>"
34+
writeBackConfig:
35+
method: "git:secret:argocd-image-updater/git-creds"
36+
gitConfig: # Group git-related settings. Should be ignored when writeBackMethod: "argocd"
37+
repository: "git@github.com:example/example.git"
38+
branch: "main"
39+
writeBackTarget: "helmvalues:/helm/config/test-values.yaml" # OR "kustomization:/config/overlays/bar" OR "helmvalues:/{{ .app.path.path }}/values.yaml" - for ApplicationSet.
40+
images:
41+
- alias: "test1"
42+
imageName: "test1:1.1.0"
43+
- alias: "test2"
44+
imageName: "test2:2.2.0"
2545
- alias: "nginx" # Maps to current '<alias>=' part of image-list annotation. Should clearly separate the image name (for registry) from its initial version/tag. What we are currently using before = sign (image=image:version)
2646
imageName: "nginx:1.17.10" # The actual image name for registry lookup OR 10.42.0.1:30000/test-image:latest
2747
commonUpdateSettings:
@@ -43,6 +63,12 @@ spec:
4363
labelSelectors:
4464
matchLabels:
4565
app: "true"
66+
matchExpressions:
67+
- key: "key"
68+
operator: In
69+
values:
70+
- "value1"
71+
- "value2"
4672
images:
4773
- alias: "nginx"
4874
imageName: "nginx:1.17.10"

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ require (
4343
dario.cat/mergo v1.0.1 // indirect
4444
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
4545
github.com/MakeNowJust/heredoc v1.0.0 // indirect
46-
github.com/Masterminds/semver/v3 v3.3.1 // indirect
46+
github.com/Masterminds/semver/v3 v3.4.0 // indirect
4747
github.com/Microsoft/go-winio v0.6.2 // indirect
4848
github.com/ProtonMail/go-crypto v1.1.6 // indirect
4949
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
@@ -198,7 +198,7 @@ require (
198198

199199
replace (
200200
// Uncomment for local testing
201-
// github.com/argoproj-labs/argocd-image-updater/registry-scanner => ./registry-scanner/
201+
github.com/argoproj-labs/argocd-image-updater/registry-scanner => ./registry-scanner/
202202
github.com/cyphar/filepath-securejoin => github.com/cyphar/filepath-securejoin v0.3.6
203203
github.com/golang/protobuf => github.com/golang/protobuf v1.5.4
204204

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg6
1212
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
1313
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
1414
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
15-
github.com/Masterminds/semver/v3 v3.3.1 h1:QtNSWtVZ3nBfk8mAOu/B6v7FMJ+NHTIgUPi7rj+4nv4=
16-
github.com/Masterminds/semver/v3 v3.3.1/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
15+
github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0=
16+
github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
1717
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
1818
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
1919
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
@@ -31,8 +31,6 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuW
3131
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
3232
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
3333
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
34-
github.com/argoproj-labs/argocd-image-updater/registry-scanner v0.0.0-20250624020913-398db53f47e4 h1:kDbTmJkeg4Z4JSc6ytEqcpv7PuM9lZJej78yQS+AnfY=
35-
github.com/argoproj-labs/argocd-image-updater/registry-scanner v0.0.0-20250624020913-398db53f47e4/go.mod h1:5Q9a7RvrHCgwoZDLN9f9YugZfvZuQWSbrMqRQD4IXTM=
3634
github.com/argoproj/argo-cd/v2 v2.14.15 h1:+rE5bwbCg21mA3ltIvSVI8/KtzRxLLmuDKIyPx93G9k=
3735
github.com/argoproj/argo-cd/v2 v2.14.15/go.mod h1:O5p0wngJjy8Rg9M2x+JH0ifrmyuI40ui2mxIPKLMbSk=
3836
github.com/argoproj/gitops-engine v0.7.1-0.20250521000818-c08b0a72c1f1 h1:Ze4U6kV49vSzlUBhH10HkO52bYKAIXS4tHr/MlNDfdU=

0 commit comments

Comments
 (0)