@@ -80,7 +80,7 @@ type Controller struct {
80
80
// The Controller will requeue the Request to be processed again if an error is non-nil or
81
81
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
82
82
func (c * Controller ) Reconcile (ctx context.Context , req controllerruntime.Request ) (controllerruntime.Result , error ) {
83
- klog .V (4 ).Infof ("Reconciling Work %s " , req .NamespacedName .String ())
83
+ klog .V (4 ).InfoS ("Reconciling Work" , "work " , req .NamespacedName .String ())
84
84
85
85
work := & workv1alpha1.Work {}
86
86
if err := c .Client .Get (ctx , req .NamespacedName , work ); err != nil {
@@ -94,13 +94,13 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques
94
94
95
95
clusterName , err := names .GetClusterName (work .Namespace )
96
96
if err != nil {
97
- klog .Errorf ( "Failed to get member cluster name for work %s/%s" , work .Namespace , work .Name )
97
+ klog .ErrorS ( err , "Failed to get member cluster name for work" , "namespace" , work .Namespace , "name" , work .Name )
98
98
return controllerruntime.Result {}, err
99
99
}
100
100
101
101
cluster , err := util .GetCluster (c .Client , clusterName )
102
102
if err != nil {
103
- klog .Errorf ( "Failed to get the given member cluster %s " , clusterName )
103
+ klog .ErrorS ( err , "Failed to get the given member cluster" , "cluster " , clusterName )
104
104
return controllerruntime.Result {}, err
105
105
}
106
106
@@ -113,18 +113,19 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques
113
113
}
114
114
115
115
if err := c .updateWorkDispatchingConditionIfNeeded (ctx , work ); err != nil {
116
- klog .Errorf ( "Failed to update work condition type %s. err is %v" , workv1alpha1 .WorkDispatching , err )
116
+ klog .ErrorS ( err , "Failed to update work condition type" , "type" , workv1alpha1 .WorkDispatching )
117
117
return controllerruntime.Result {}, err
118
118
}
119
119
120
120
if util .IsWorkSuspendDispatching (work ) {
121
- klog .V (4 ).Infof ("Skip syncing work(%s/%s) for cluster(%s) as work dispatch is suspended." , work .Namespace , work .Name , cluster .Name )
121
+ klog .V (4 ).InfoS ("Skip syncing work for cluster as work dispatch is suspended." , "namespace" , work .Namespace , "name" , work .Name , "cluster" , cluster .Name )
122
122
return controllerruntime.Result {}, nil
123
123
}
124
124
125
125
if ! util .IsClusterReady (& cluster .Status ) {
126
- klog .Errorf ("Stop syncing the work(%s/%s) for the cluster(%s) as cluster not ready." , work .Namespace , work .Name , cluster .Name )
127
- return controllerruntime.Result {}, fmt .Errorf ("cluster(%s) not ready" , cluster .Name )
126
+ err := fmt .Errorf ("cluster(%s) not ready" , cluster .Name )
127
+ klog .ErrorS (err , "Stop syncing the work for the cluster as cluster not ready." , "namespace" , work .Namespace , "name" , work .Name , "cluster" , cluster .Name )
128
+ return controllerruntime.Result {}, err
128
129
}
129
130
130
131
return c .syncWork (ctx , clusterName , work )
@@ -161,18 +162,18 @@ func (c *Controller) syncWork(ctx context.Context, clusterName string, work *wor
161
162
func (c * Controller ) handleWorkDelete (ctx context.Context , work * workv1alpha1.Work , cluster * clusterv1alpha1.Cluster ) error {
162
163
if ptr .Deref (work .Spec .PreserveResourcesOnDeletion , false ) {
163
164
if err := c .cleanupPolicyClaimMetadata (ctx , work , cluster ); err != nil {
164
- klog .Errorf ( "Failed to remove annotations and labels in on cluster(%s) " , cluster .Name )
165
+ klog .ErrorS ( err , "Failed to remove annotations and labels" , " cluster" , cluster .Name )
165
166
return err
166
167
}
167
- klog .V (4 ).Infof ("Preserving resource on deletion from work(%s/%s) on cluster(%s) " , work .Namespace , work .Name , cluster .Name )
168
+ klog .V (4 ).InfoS ("Preserving resource on deletion from work on cluster" , "namespace" , work .Namespace , "name" , work .Name , "cluster" , cluster .Name )
168
169
return nil
169
170
}
170
171
171
172
// Abort deleting workload if cluster is unready when unjoining cluster, otherwise the unjoin process will be failed.
172
173
if util .IsClusterReady (& cluster .Status ) {
173
174
err := c .tryDeleteWorkload (ctx , cluster .Name , work )
174
175
if err != nil {
175
- klog .Errorf ( "Failed to delete work %v, namespace is %v, err is %v" , work .Name , work .Namespace , err )
176
+ klog .ErrorS ( err , "Failed to delete work" , "name" , work .Name , "namespace" , work .Namespace )
176
177
return err
177
178
}
178
179
} else if cluster .DeletionTimestamp .IsZero () { // cluster is unready, but not terminating
@@ -186,21 +187,19 @@ func (c *Controller) cleanupPolicyClaimMetadata(ctx context.Context, work *workv
186
187
for _ , manifest := range work .Spec .Workload .Manifests {
187
188
workload := & unstructured.Unstructured {}
188
189
if err := workload .UnmarshalJSON (manifest .Raw ); err != nil {
189
- klog .Errorf ( "Failed to unmarshal workload from work(%s/%s), error is: %v" , err , work .GetNamespace (), work .GetName ())
190
+ klog .ErrorS ( err , "Failed to unmarshal workload from work" , "namespace" , work .GetNamespace (), "name" , work .GetName ())
190
191
return err
191
192
}
192
193
193
194
fedKey , err := keys .FederatedKeyFunc (cluster .Name , workload )
194
195
if err != nil {
195
- klog .Errorf ("Failed to get the federated key resource(kind=%s, %s/%s) from member cluster(%s), err is %v " ,
196
- workload .GetKind (), workload .GetNamespace (), workload .GetName (), cluster .Name , err )
196
+ klog .ErrorS (err , "Failed to get the federated key resource from member cluster" , "kind" , workload .GetKind (), "namespace" , workload .GetNamespace (), "name" , workload .GetName (), "cluster" , cluster .Name )
197
197
return err
198
198
}
199
199
200
200
clusterObj , err := helper .GetObjectFromCache (c .RESTMapper , c .InformerManager , fedKey )
201
201
if err != nil {
202
- klog .Errorf ("Failed to get the resource(kind=%s, %s/%s) from member cluster(%s) cache, err is %v " ,
203
- workload .GetKind (), workload .GetNamespace (), workload .GetName (), cluster .Name , err )
202
+ klog .ErrorS (err , "Failed to get the resource from member cluster cache" , "kind" , workload .GetKind (), "namespace" , workload .GetNamespace (), "name" , workload .GetName (), "cluster" , cluster .Name )
204
203
return err
205
204
}
206
205
@@ -215,7 +214,7 @@ func (c *Controller) cleanupPolicyClaimMetadata(ctx context.Context, work *workv
215
214
operationResult , err := c .ObjectWatcher .Update (ctx , cluster .Name , workload , clusterObj )
216
215
metrics .CountUpdateResourceToCluster (err , workload .GetAPIVersion (), workload .GetKind (), cluster .Name , string (operationResult ))
217
216
if err != nil {
218
- klog .Errorf ( "Failed to update metadata in the given member cluster %v, err is %v" , cluster .Name , err )
217
+ klog .ErrorS ( err , "Failed to update metadata in the given member cluster" , "cluster" , cluster .Name )
219
218
return err
220
219
}
221
220
}
@@ -229,14 +228,14 @@ func (c *Controller) tryDeleteWorkload(ctx context.Context, clusterName string,
229
228
workload := & unstructured.Unstructured {}
230
229
err := workload .UnmarshalJSON (manifest .Raw )
231
230
if err != nil {
232
- klog .Errorf ( "Failed to unmarshal workload, error is: %v" , err )
231
+ klog .ErrorS ( err , "Failed to unmarshal workload" )
233
232
return err
234
233
}
235
234
236
235
err = c .ObjectWatcher .Delete (ctx , clusterName , workload )
237
236
metrics .CountDeleteResourceFromCluster (err , workload .GetAPIVersion (), workload .GetKind (), clusterName )
238
237
if err != nil {
239
- klog .Errorf ( "Failed to delete resource in the given member cluster %v, err is %v" , clusterName , err )
238
+ klog .ErrorS ( err , "Failed to delete resource in the given member cluster" , "cluster" , clusterName )
240
239
return err
241
240
}
242
241
}
@@ -266,13 +265,13 @@ func (c *Controller) syncToClusters(ctx context.Context, clusterName string, wor
266
265
workload := & unstructured.Unstructured {}
267
266
err := workload .UnmarshalJSON (manifest .Raw )
268
267
if err != nil {
269
- klog .Errorf ( "Failed to unmarshal workload of the work(%s/%s), error is: %v" , work .GetNamespace (), work .GetName (), err )
268
+ klog .ErrorS ( err , "Failed to unmarshal workload of the work" , "namespace" , work .GetNamespace (), "name" , work .GetName ())
270
269
errs = append (errs , err )
271
270
continue
272
271
}
273
272
274
273
if err = c .tryCreateOrUpdateWorkload (ctx , clusterName , workload ); err != nil {
275
- klog .Errorf ( "Failed to create or update resource(%v/%v) in the given member cluster %s, err is %v" , workload .GetNamespace (), workload .GetName (), clusterName , err )
274
+ klog .ErrorS ( err , "Failed to create or update resource in the given member cluster" , "namespace" , workload .GetNamespace (), "name" , workload .GetName (), "cluster" , clusterName )
276
275
c .eventf (workload , corev1 .EventTypeWarning , events .EventReasonSyncWorkloadFailed , "Failed to create or update resource(%s) in member cluster(%s): %v" , klog .KObj (workload ), clusterName , err )
277
276
errs = append (errs , err )
278
277
continue
@@ -286,15 +285,15 @@ func (c *Controller) syncToClusters(ctx context.Context, clusterName string, wor
286
285
message := fmt .Sprintf ("Failed to apply all manifests (%d/%d): %s" , syncSucceedNum , total , errors .NewAggregate (errs ).Error ())
287
286
err := c .updateAppliedCondition (ctx , work , metav1 .ConditionFalse , "AppliedFailed" , message )
288
287
if err != nil {
289
- klog .Errorf ( "Failed to update applied status for given work %v, namespace is %v, err is %v" , work .Name , work .Namespace , err )
288
+ klog .ErrorS ( err , "Failed to update applied status for given work" , "name" , work .Name , "namespace" , work .Namespace )
290
289
errs = append (errs , err )
291
290
}
292
291
return errors .NewAggregate (errs )
293
292
}
294
293
295
294
err := c .updateAppliedCondition (ctx , work , metav1 .ConditionTrue , "AppliedSuccessful" , "Manifest has been successfully applied" )
296
295
if err != nil {
297
- klog .Errorf ( "Failed to update applied status for given work %v, namespace is %v, err is %v" , work .Name , work .Namespace , err )
296
+ klog .ErrorS ( err , "Failed to update applied status for given work" , "name" , work .Name , "namespace" , work .Namespace )
298
297
return err
299
298
}
300
299
@@ -304,14 +303,14 @@ func (c *Controller) syncToClusters(ctx context.Context, clusterName string, wor
304
303
func (c * Controller ) tryCreateOrUpdateWorkload (ctx context.Context , clusterName string , workload * unstructured.Unstructured ) error {
305
304
fedKey , err := keys .FederatedKeyFunc (clusterName , workload )
306
305
if err != nil {
307
- klog .Errorf ( "Failed to get FederatedKey %s, error: %v" , workload .GetName (), err )
306
+ klog .ErrorS ( err , "Failed to get FederatedKey" , "name" , workload .GetName ())
308
307
return err
309
308
}
310
309
311
310
clusterObj , err := helper .GetObjectFromCache (c .RESTMapper , c .InformerManager , fedKey )
312
311
if err != nil {
313
312
if ! apierrors .IsNotFound (err ) {
314
- klog .Errorf ( "Failed to get the resource(kind=%s, %s/%s) from member cluster(%s), err is %v " , workload .GetKind (), workload .GetNamespace (), workload .GetName (), clusterName , err )
313
+ klog .ErrorS ( err , "Failed to get the resource from member cluster" , "kind" , workload .GetKind (), "namespace" , workload .GetNamespace (), "name" , workload .GetName (), "cluster" , clusterName )
315
314
return err
316
315
}
317
316
err = c .ObjectWatcher .Create (ctx , clusterName , workload )
@@ -389,7 +388,7 @@ func (c *Controller) setStatusCondition(ctx context.Context, work *workv1alpha1.
389
388
func (c * Controller ) eventf (object * unstructured.Unstructured , eventType , reason , messageFmt string , args ... interface {}) {
390
389
ref , err := util .GenEventRef (object )
391
390
if err != nil {
392
- klog .Errorf ( "Ignore event(%s) as failed to build event reference for: kind=%s, %s due to %v" , reason , object .GetKind (), klog .KObj (object ), err )
391
+ klog .ErrorS ( err , "Ignore event as failed to build event reference" , "reason" , reason , "kind" , object .GetKind (), "object" , klog .KObj (object ))
393
392
return
394
393
}
395
394
c .EventRecorder .Eventf (ref , eventType , reason , messageFmt , args ... )
0 commit comments