Skip to content

Commit 62a43ea

Browse files
authored
refactor: Refactor logging to provide more context information (argoproj-labs#357)
* refactor: Refactor logging to provide more context information Signed-off-by: jannfis <jann@mistrust.net> * More context Signed-off-by: jannfis <jann@mistrust.net> * Fix unit tests Signed-off-by: jannfis <jann@mistrust.net>
1 parent a163f39 commit 62a43ea

File tree

12 files changed

+145
-91
lines changed

12 files changed

+145
-91
lines changed

cmd/test.go

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,54 +74,55 @@ argocd-image-updater test nginx --allow-tags '^1.19.\d+(\-.*)*$' --update-strate
7474
}
7575
}
7676

77+
img := image.NewFromIdentifier(args[0])
78+
7779
vc := &image.VersionConstraint{
7880
Constraint: semverConstraint,
7981
Strategy: image.StrategySemVer,
8082
}
8183

82-
vc.Strategy = image.ParseUpdateStrategy(strategy)
84+
vc.Strategy = img.ParseUpdateStrategy(strategy)
8385

8486
if allowTags != "" {
85-
vc.MatchFunc, vc.MatchArgs = image.ParseMatchfunc(allowTags)
87+
vc.MatchFunc, vc.MatchArgs = img.ParseMatchfunc(allowTags)
8688
}
8789

8890
vc.IgnoreList = ignoreTags
8991

90-
img := image.NewFromIdentifier(args[0])
91-
log.WithContext().
92-
AddField("registry", img.RegistryURL).
93-
AddField("image_name", img.ImageName).
94-
Infof("getting image")
92+
logCtx := img.LogContext()
93+
logCtx.Infof("retrieving information about image")
9594

9695
vc.Options = options.NewManifestOptions()
9796
if platform != "" {
9897
os, arch, variant, err := image.ParsePlatform(platform)
9998
if err != nil {
100-
log.Fatalf("Platform %s: %v", platform, err)
99+
logCtx.Fatalf("Platform %s: %v", platform, err)
101100
}
102101
vc.Options = vc.Options.
103102
WithPlatform(os, arch, variant).
104103
WithMetadata(vc.Strategy.NeedsMetadata())
105104
}
106105

106+
vc.Options.WithLogger(logCtx.AddField("application", "test"))
107+
107108
if registriesConfPath != "" {
108109
if err := registry.LoadRegistryConfiguration(registriesConfPath, false); err != nil {
109-
log.Fatalf("could not load registries configuration: %v", err)
110+
logCtx.Fatalf("could not load registries configuration: %v", err)
110111
}
111112
}
112113

113114
ep, err := registry.GetRegistryEndpoint(img.RegistryURL)
114115
if err != nil {
115-
log.Fatalf("could not get registry endpoint: %v", err)
116+
logCtx.Fatalf("could not get registry endpoint: %v", err)
116117
}
117118

118119
if err := ep.SetEndpointCredentials(kubeClient); err != nil {
119-
log.Fatalf("could not set registry credentials: %v", err)
120+
logCtx.Fatalf("could not set registry credentials: %v", err)
120121
}
121122

122123
checkFlag := func(f *pflag.Flag) {
123124
if f.Name == "rate-limit" {
124-
log.Infof("Overriding registry rate-limit to %d requests per second", rateLimit)
125+
logCtx.Infof("Overriding registry rate-limit to %d requests per second", rateLimit)
125126
ep.Limiter = ratelimit.New(rateLimit)
126127
}
127128
}
@@ -133,44 +134,40 @@ argocd-image-updater test nginx --allow-tags '^1.19.\d+(\-.*)*$' --update-strate
133134
if credentials != "" {
134135
credSrc, err := image.ParseCredentialSource(credentials, false)
135136
if err != nil {
136-
log.Fatalf("could not parse credential definition '%s': %v", credentials, err)
137+
logCtx.Fatalf("could not parse credential definition '%s': %v", credentials, err)
137138
}
138139
creds, err = credSrc.FetchCredentials(ep.RegistryAPI, kubeClient)
139140
if err != nil {
140-
log.Fatalf("could not fetch credentials: %v", err)
141+
logCtx.Fatalf("could not fetch credentials: %v", err)
141142
}
142143
username = creds.Username
143144
password = creds.Password
144145
}
145146

146147
regClient, err := registry.NewClient(ep, username, password)
147148
if err != nil {
148-
log.Fatalf("could not create registry client: %v", err)
149+
logCtx.Fatalf("could not create registry client: %v", err)
149150
}
150151

151-
log.WithContext().
152-
AddField("image_name", img.ImageName).
153-
Infof("Fetching available tags and metadata from registry")
152+
logCtx.Infof("Fetching available tags and metadata from registry")
154153

155154
tags, err := ep.GetTags(img, regClient, vc)
156155
if err != nil {
157-
log.Fatalf("could not get tags: %v", err)
156+
logCtx.Fatalf("could not get tags: %v", err)
158157
}
159158

160-
log.WithContext().
161-
AddField("image_name", img.ImageName).
162-
Infof("Found %d tags in registry", len(tags.Tags()))
159+
logCtx.Infof("Found %d tags in registry", len(tags.Tags()))
163160

164161
upImg, err := img.GetNewestVersionFromTags(vc, tags)
165162
if err != nil {
166-
log.Fatalf("could not get updateable image from tags: %v", err)
163+
logCtx.Fatalf("could not get updateable image from tags: %v", err)
167164
}
168165
if upImg == nil {
169-
log.Infof("no newer version of image found")
166+
logCtx.Infof("no newer version of image found")
170167
return
171168
}
172169

173-
log.Infof("latest image according to constraint is %s", img.WithTag(upImg))
170+
logCtx.Infof("latest image according to constraint is %s", img.WithTag(upImg))
174171
},
175172
}
176173

pkg/argocd/argocd.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,27 +152,27 @@ func FilterApplicationsForUpdate(apps []v1alpha1.Application, patterns []string)
152152
var appsForUpdate = make(map[string]ApplicationImages)
153153

154154
for _, app := range apps {
155-
155+
logCtx := log.WithContext().AddField("application", app.GetName())
156156
// Check whether application has our annotation set
157157
annotations := app.GetAnnotations()
158158
if _, ok := annotations[common.ImageUpdaterAnnotation]; !ok {
159-
log.Tracef("skipping app '%s' of type '%s' because required annotation is missing", app.GetName(), app.Status.SourceType)
159+
logCtx.Tracef("skipping app '%s' of type '%s' because required annotation is missing", app.GetName(), app.Status.SourceType)
160160
continue
161161
}
162162

163163
// Check for valid application type
164164
if !IsValidApplicationType(&app) {
165-
log.Warnf("skipping app '%s' of type '%s' because it's not of supported source type", app.GetName(), app.Status.SourceType)
165+
logCtx.Warnf("skipping app '%s' of type '%s' because it's not of supported source type", app.GetName(), app.Status.SourceType)
166166
continue
167167
}
168168

169169
// Check if application name matches requested patterns
170170
if !nameMatchesPattern(app.GetName(), patterns) {
171-
log.Debugf("Skipping app '%s' because it does not match requested patterns", app.GetName())
171+
logCtx.Debugf("Skipping app '%s' because it does not match requested patterns", app.GetName())
172172
continue
173173
}
174174

175-
log.Tracef("processing app '%s' of type '%v'", app.GetName(), app.Status.SourceType)
175+
logCtx.Tracef("processing app '%s' of type '%v'", app.GetName(), app.Status.SourceType)
176176
imageList := parseImageList(annotations)
177177
appImages := ApplicationImages{}
178178
appImages.Application = app
@@ -408,7 +408,7 @@ func SetKustomizeImage(app *v1alpha1.Application, newImage *image.ContainerImage
408408
ksImageParam = newImage.GetFullNameWithTag()
409409
}
410410

411-
log.Tracef("Setting Kustomize parameter %s", ksImageParam)
411+
log.WithContext().AddField("application", app.GetName()).Tracef("Setting Kustomize parameter %s", ksImageParam)
412412

413413
if app.Spec.Source.Kustomize == nil {
414414
app.Spec.Source.Kustomize = &v1alpha1.ApplicationSourceKustomize{}

pkg/argocd/git.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ type changeWriter func(app *v1alpha1.Application, wbc *WriteBackConfig, gitC git
129129
// commitChanges commits any changes required for updating one or more images
130130
// after the UpdateApplication cycle has finished.
131131
func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeList []ChangeEntry, write changeWriter) error {
132+
logCtx := log.WithContext().AddField("application", app.GetName())
132133
creds, err := wbc.GetCreds(app)
133134
if err != nil {
134135
return fmt.Errorf("could not get creds for repo '%s': %v", app.Spec.Source.RepoURL, err)
@@ -142,7 +143,7 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
142143
defer func() {
143144
err := os.RemoveAll(tempRoot)
144145
if err != nil {
145-
log.Errorf("could not remove temp dir: %v", err)
146+
logCtx.Errorf("could not remove temp dir: %v", err)
146147
}
147148
}()
148149
gitC, err = git.NewClientExt(app.Spec.Source.RepoURL, tempRoot, creds, false, false, "")
@@ -177,10 +178,10 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
177178
if wbc.GitBranch != "" {
178179
checkOutBranch = wbc.GitBranch
179180
}
180-
log.Tracef("targetRevision for update is '%s'", checkOutBranch)
181+
logCtx.Tracef("targetRevision for update is '%s'", checkOutBranch)
181182
if checkOutBranch == "" || checkOutBranch == "HEAD" {
182183
checkOutBranch, err = gitC.SymRefToBranch(checkOutBranch)
183-
log.Infof("resolved remote default branch to '%s' and using that for operations", checkOutBranch)
184+
logCtx.Infof("resolved remote default branch to '%s' and using that for operations", checkOutBranch)
184185
if err != nil {
185186
return err
186187
}
@@ -198,10 +199,10 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
198199
pushBranch := checkOutBranch
199200

200201
if wbc.GitWriteBranch != "" {
201-
log.Debugf("Using branch template: %s", wbc.GitWriteBranch)
202+
logCtx.Debugf("Using branch template: %s", wbc.GitWriteBranch)
202203
pushBranch = TemplateBranchName(wbc.GitWriteBranch, changeList)
203204
if pushBranch != "" {
204-
log.Debugf("Creating branch '%s' and using that for push operations", pushBranch)
205+
logCtx.Debugf("Creating branch '%s' and using that for push operations", pushBranch)
205206
err = gitC.Branch(checkOutBranch, pushBranch)
206207
if err != nil {
207208
return err
@@ -223,7 +224,7 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
223224
if err != nil {
224225
return fmt.Errorf("cold not create temp file: %v", err)
225226
}
226-
log.Debugf("Writing commit message to %s", cm.Name())
227+
logCtx.Debugf("Writing commit message to %s", cm.Name())
227228
err = ioutil.WriteFile(cm.Name(), []byte(wbc.GitCommitMessage), 0600)
228229
if err != nil {
229230
_ = cm.Close()
@@ -247,6 +248,7 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
247248
}
248249

249250
func writeOverrides(app *v1alpha1.Application, _ *WriteBackConfig, gitC git.Client) (err error, skip bool) {
251+
logCtx := log.WithContext().AddField("application", app.GetName())
250252
targetExists := true
251253
targetFile := path.Join(gitC.Root(), app.Spec.Source.Path, fmt.Sprintf(".argocd-source-%s.yaml", app.Name))
252254
_, err = os.Stat(targetFile)
@@ -272,7 +274,7 @@ func writeOverrides(app *v1alpha1.Application, _ *WriteBackConfig, gitC git.Clie
272274
return err, false
273275
}
274276
if string(data) == string(override) {
275-
log.Debugf("target parameter file and marshaled data are the same, skipping commit.")
277+
logCtx.Debugf("target parameter file and marshaled data are the same, skipping commit.")
276278
return nil, true
277279
}
278280
}
@@ -292,6 +294,7 @@ var _ changeWriter = writeOverrides
292294

293295
// writeKustomization writes any changes required for updating one or more images to a kustomization.yml
294296
func writeKustomization(app *v1alpha1.Application, wbc *WriteBackConfig, gitC git.Client) (err error, skip bool) {
297+
logCtx := log.WithContext().AddField("application", app.GetName())
295298
if oldDir, err := os.Getwd(); err != nil {
296299
return err, false
297300
} else {
@@ -305,7 +308,7 @@ func writeKustomization(app *v1alpha1.Application, wbc *WriteBackConfig, gitC gi
305308
return err, false
306309
}
307310

308-
log.Infof("updating base %s", base)
311+
logCtx.Infof("updating base %s", base)
309312

310313
kustFile := findKustomization(base)
311314
if kustFile == "" {

pkg/argocd/update.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ func UpdateApplication(updateConf *UpdateConfiguration, state *SyncIterationStat
197197
vc.Strategy = applicationImage.GetParameterUpdateStrategy(updateConf.UpdateApp.Application.Annotations)
198198
vc.MatchFunc, vc.MatchArgs = applicationImage.GetParameterMatch(updateConf.UpdateApp.Application.Annotations)
199199
vc.IgnoreList = applicationImage.GetParameterIgnoreTags(updateConf.UpdateApp.Application.Annotations)
200-
vc.Options = applicationImage.GetPlatformOptions(updateConf.UpdateApp.Application.Annotations, updateConf.IgnorePlatforms).WithMetadata(vc.Strategy.NeedsMetadata())
200+
vc.Options = applicationImage.
201+
GetPlatformOptions(updateConf.UpdateApp.Application.Annotations, updateConf.IgnorePlatforms).
202+
WithMetadata(vc.Strategy.NeedsMetadata()).
203+
WithLogger(imgCtx.AddField("application", app))
201204

202205
// The endpoint can provide default credentials for pulling images
203206
err = rep.SetEndpointCredentials(updateConf.KubeClient)
@@ -358,7 +361,7 @@ func setAppImage(app *v1alpha1.Application, img *image.ContainerImage) error {
358361
} else if appType == ApplicationTypeHelm {
359362
err = SetHelmImage(app, img)
360363
} else {
361-
err = fmt.Errorf("Could not update application %s - neither Helm nor Kustomize application", app)
364+
err = fmt.Errorf("could not update application %s - neither Helm nor Kustomize application", app)
362365
}
363366
return err
364367
}

pkg/image/image.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strings"
55
"time"
66

7+
"github.com/argoproj-labs/argocd-image-updater/pkg/log"
78
"github.com/argoproj-labs/argocd-image-updater/pkg/tag"
89

910
"github.com/distribution/distribution/v3/reference"
@@ -245,3 +246,17 @@ func getImageDigestFromTag(tagStr string) (string, string) {
245246
return a[0], a[1]
246247
}
247248
}
249+
250+
// LogContext returns a log context for the given image, with required fields
251+
// set to the image's information.
252+
func (img *ContainerImage) LogContext() *log.LogContext {
253+
logCtx := log.WithContext()
254+
logCtx.AddField("image_name", img.GetFullNameWithoutTag())
255+
logCtx.AddField("image_alias", img.ImageAlias)
256+
logCtx.AddField("registry_url", img.RegistryURL)
257+
if img.ImageTag != nil {
258+
logCtx.AddField("image_tag", img.ImageTag.TagName)
259+
logCtx.AddField("image_digest", img.ImageTag.TagDigest)
260+
}
261+
return logCtx
262+
}

0 commit comments

Comments
 (0)