@@ -56,76 +56,82 @@ argocd-image-updater test nginx --semver-constraint v1.17.x
56
56
argocd-image-updater test nginx --allow-tags '^1.19.\d+(\-.*)*$' --update-strategy latest
57
57
` ,
58
58
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
+
59
64
if len (args ) != 1 {
60
65
cmd .HelpFunc ()(cmd , args )
61
- log .Fatalf ("image needs to be specified" )
66
+ logger .Fatalf ("image needs to be specified" )
62
67
}
63
68
64
69
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 )
66
71
}
67
72
68
73
var kubeClient * kube.ImageUpdaterKubernetesClient
69
74
var err error
75
+
70
76
if ! disableKubernetes {
71
- ctx := context .Background ()
72
77
kubeClient , err = argocd .GetKubeConfig (ctx , "" , kubeConfig )
73
78
if err != nil {
74
- log .Fatalf ("could not create K8s client: %v" , err )
79
+ logger .Fatalf ("could not create K8s client: %v" , err )
75
80
}
76
81
}
77
82
78
83
img := image .NewFromIdentifier (args [0 ])
79
84
85
+ fields := img .GetLogFields (img .ImageAlias )
86
+ imgLogger := logger .WithFields (fields )
87
+ imgCtx := log .ContextWithLogger (ctx , imgLogger )
88
+
80
89
vc := & image.VersionConstraint {
81
90
Constraint : semverConstraint ,
82
91
Strategy : image .StrategySemVer ,
83
92
}
84
93
85
- vc .Strategy = img .ParseUpdateStrategy (strategy )
94
+ vc .Strategy = img .ParseUpdateStrategy (imgCtx , strategy )
86
95
87
96
if allowTags != "" {
88
- vc .MatchFunc , vc .MatchArgs = img .ParseMatchfunc ( allowTags )
97
+ vc .MatchFunc , vc .MatchArgs = img .ParseMatch ( imgCtx , allowTags )
89
98
}
90
99
91
100
vc .IgnoreList = ignoreTags
92
101
93
- logCtx := img .LogContext ()
94
- logCtx .Infof ("retrieving information about image" )
102
+ imgLogger .Infof ("retrieving information about image" )
95
103
96
104
vc .Options = options .NewManifestOptions ()
97
105
for _ , platform := range platforms {
98
106
os , arch , variant , err := image .ParsePlatform (platform )
99
107
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 )
101
109
}
102
110
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 )
104
112
}
105
113
vc .Options = vc .Options .WithPlatform (os , arch , variant )
106
114
}
107
115
vc .Options = vc .Options .WithMetadata (vc .Strategy .NeedsMetadata ())
108
116
109
- vc .Options .WithLogger (logCtx .AddField ("application" , "test" ))
110
-
111
117
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 )
114
120
}
115
121
}
116
122
117
- ep , err := registry .GetRegistryEndpoint (img .RegistryURL )
123
+ ep , err := registry .GetRegistryEndpoint (imgCtx , img .RegistryURL )
118
124
if err != nil {
119
- logCtx .Fatalf ("could not get registry endpoint: %v" , err )
125
+ imgLogger .Fatalf ("could not get registry endpoint: %v" , err )
120
126
}
121
127
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 )
124
130
}
125
131
126
132
checkFlag := func (f * pflag.Flag ) {
127
133
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 )
129
135
ep .Limiter = ratelimit .New (rateLimit )
130
136
}
131
137
}
@@ -137,40 +143,40 @@ argocd-image-updater test nginx --allow-tags '^1.19.\d+(\-.*)*$' --update-strate
137
143
if credentials != "" {
138
144
credSrc , err := image .ParseCredentialSource (credentials , false )
139
145
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 )
141
147
}
142
- creds , err = credSrc .FetchCredentials (ep .RegistryAPI , kubeClient .KubeClient )
148
+ creds , err = credSrc .FetchCredentials (imgCtx , ep .RegistryAPI , kubeClient .KubeClient )
143
149
if err != nil {
144
- logCtx .Fatalf ("could not fetch credentials: %v" , err )
150
+ imgLogger .Fatalf ("could not fetch credentials: %v" , err )
145
151
}
146
152
username = creds .Username
147
153
password = creds .Password
148
154
}
149
155
150
156
regClient , err := registry .NewClient (ep , username , password )
151
157
if err != nil {
152
- logCtx .Fatalf ("could not create registry client: %v" , err )
158
+ imgLogger .Fatalf ("could not create registry client: %v" , err )
153
159
}
154
160
155
- logCtx .Infof ("Fetching available tags and metadata from registry" )
161
+ imgLogger .Infof ("Fetching available tags and metadata from registry" )
156
162
157
- tags , err := ep .GetTags (img , regClient , vc )
163
+ tags , err := ep .GetTags (imgCtx , img , regClient , vc )
158
164
if err != nil {
159
- logCtx .Fatalf ("could not get tags: %v" , err )
165
+ imgLogger .Fatalf ("could not get tags: %v" , err )
160
166
}
161
167
162
- logCtx .Infof ("Found %d tags in registry" , len (tags .Tags ()))
168
+ imgLogger .Infof ("Found %d tags in registry" , len (tags .Tags ()))
163
169
164
- upImg , err := img .GetNewestVersionFromTags (vc , tags )
170
+ upImg , err := img .GetNewestVersionFromTags (imgCtx , vc , tags )
165
171
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 )
167
173
}
168
174
if upImg == nil {
169
- logCtx .Infof ("no newer version of image found" )
175
+ imgLogger .Infof ("no newer version of image found" )
170
176
return
171
177
}
172
178
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 ))
174
180
},
175
181
}
176
182
0 commit comments