@@ -62,7 +62,6 @@ const (
62
62
// Reconcile the chaos records
63
63
func (r * Reconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
64
64
obj := r .Object .DeepCopyObject ().(v1alpha1.InnerObjectWithSelector )
65
-
66
65
if err := r .Client .Get (context .TODO (), req .NamespacedName , obj ); err != nil {
67
66
if apierrors .IsNotFound (err ) {
68
67
r .Log .Info ("chaos not found" )
@@ -79,11 +78,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
79
78
records := obj .GetStatus ().Experiment .Records
80
79
selectors := obj .GetSelectorSpecs ()
81
80
81
+ logger := r .Log .WithValues ("name" , obj .GetName (), "namespace" , obj .GetNamespace (), "kind" , obj .GetObjectKind ().GroupVersionKind ().Kind )
82
+
82
83
if records == nil {
83
84
for name , sel := range selectors {
84
85
targets , err := r .Selector .Select (context .TODO (), sel )
85
86
if err != nil {
86
- r . Log .Error (err , "fail to select" )
87
+ logger .Error (err , "fail to select" )
87
88
r .Recorder .Event (obj , recorder.Failed {
88
89
Activity : "select targets" ,
89
90
Err : err .Error (),
@@ -92,7 +93,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
92
93
}
93
94
94
95
if len (targets ) == 0 {
95
- r . Log .Info ("no target has been selected" )
96
+ logger .Info ("no target has been selected" )
96
97
r .Recorder .Event (obj , recorder.Failed {
97
98
Activity : "select targets" ,
98
99
Err : "no target has been selected" ,
@@ -115,7 +116,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
115
116
needRetry := false
116
117
for index , record := range records {
117
118
var err error
118
- r .Log .Info ("iterating record" , "record" , record , "desiredPhase" , desiredPhase )
119
+ idLogger := logger .WithValues ("id" , records [index ].Id )
120
+ idLogger .Info ("iterating record" , "record" , record , "desiredPhase" , desiredPhase )
119
121
120
122
// The whole running logic is a cycle:
121
123
// Not Injected -> Not Injected/* -> Injected -> Injected/* -> Not Injected
@@ -146,15 +148,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
146
148
}
147
149
148
150
if operation == Apply {
149
- r . Log . Info ("apply chaos" , "id" , records [ index ]. Id )
151
+ idLogger . Info ("apply chaos" )
150
152
record .Phase , err = r .Impl .Apply (context .TODO (), index , records , obj )
151
153
if record .Phase != originalPhase {
152
154
shouldUpdate = true
153
155
}
154
156
if err != nil {
155
157
// TODO: add backoff and retry mechanism
156
158
// but the retry shouldn't block other resource process
157
- r . Log .Error (err , "fail to apply chaos" )
159
+ idLogger .Error (err , "fail to apply chaos" )
158
160
applyFailedEvent := newRecordEvent (v1alpha1 .TypeFailed , v1alpha1 .Apply , err .Error ())
159
161
records [index ].Events = append (records [index ].Events , * applyFailedEvent )
160
162
r .Recorder .Event (obj , recorder.Failed {
@@ -176,15 +178,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
176
178
})
177
179
}
178
180
} else if operation == Recover {
179
- r . Log . Info ("recover chaos" , "id" , records [ index ]. Id )
181
+ idLogger . Info ("recover chaos" )
180
182
record .Phase , err = r .Impl .Recover (context .TODO (), index , records , obj )
181
183
if record .Phase != originalPhase {
182
184
shouldUpdate = true
183
185
}
184
186
if err != nil {
185
187
// TODO: add backoff and retry mechanism
186
188
// but the retry shouldn't block other resource process
187
- r . Log .Error (err , "fail to recover chaos" )
189
+ idLogger .Error (err , "fail to recover chaos" )
188
190
recoverFailedEvent := newRecordEvent (v1alpha1 .TypeFailed , v1alpha1 .Recover , err .Error ())
189
191
records [index ].Events = append (records [index ].Events , * recoverFailedEvent )
190
192
r .Recorder .Event (obj , recorder.Failed {
@@ -215,11 +217,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
215
217
}
216
218
if shouldUpdate {
217
219
updateError := retry .RetryOnConflict (retry .DefaultBackoff , func () error {
218
- r . Log .Info ("updating records" , "records" , records )
220
+ logger .Info ("updating records" , "records" , records )
219
221
obj := r .Object .DeepCopyObject ().(v1alpha1.InnerObjectWithSelector )
220
222
221
223
if err := r .Client .Get (context .TODO (), req .NamespacedName , obj ); err != nil {
222
- r . Log .Error (err , "unable to get chaos" )
224
+ logger .Error (err , "unable to get chaos" )
223
225
return err
224
226
}
225
227
@@ -232,7 +234,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
232
234
return r .Client .Update (context .TODO (), obj )
233
235
})
234
236
if updateError != nil {
235
- r . Log .Error (updateError , "fail to update" )
237
+ logger .Error (updateError , "fail to update" )
236
238
r .Recorder .Event (obj , recorder.Failed {
237
239
Activity : "update records" ,
238
240
Err : updateError .Error (),
0 commit comments