From 7a55558d8c335d5c51912451e114e9ff35bccc3b Mon Sep 17 00:00:00 2001 From: Mike Tonks <103417258+miketonks-form3@users.noreply.github.com> Date: Tue, 12 Dec 2023 13:29:35 +0000 Subject: [PATCH] chore: Add log fields for apply chaos and surrounding code (#4278) * chore: Add log fields for apply chaos and surrounding code Signed-off-by: Mike Tonks <michael.tonks@form3.tech> * chore: Update CHANGELOG Signed-off-by: Mike Tonks <michael.tonks@form3.tech> --------- Signed-off-by: Mike Tonks <michael.tonks@form3.tech> --- CHANGELOG.md | 1 + controllers/common/records/controller.go | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73013e942a..937b27370c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ For more information and how-to, see [RFC: Keep A Changelog](https://github.com/ - Add `values.schema.json` [#4205](https://github.com/chaos-mesh/chaos-mesh/pull/4205) - Add [`GreptimeDB`](https://greptime.com) to ADOPTERS.md [#4245](https://github.com/chaos-mesh/chaos-mesh/pull/4245) - Support configurable chaos-dns-server pod affinities[#4260](https://github.com/chaos-mesh/chaos-mesh/pull/4260) +- Add experiment `name`, `namespace` and `kind` to "apply chaos" and "recover chaos" log messages [4278](https://github.com/chaos-mesh/chaos-mesh/pull/4278) ### Changed diff --git a/controllers/common/records/controller.go b/controllers/common/records/controller.go index c76af07919..be913279e6 100644 --- a/controllers/common/records/controller.go +++ b/controllers/common/records/controller.go @@ -62,7 +62,6 @@ const ( // Reconcile the chaos records func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { obj := r.Object.DeepCopyObject().(v1alpha1.InnerObjectWithSelector) - if err := r.Client.Get(context.TODO(), req.NamespacedName, obj); err != nil { if apierrors.IsNotFound(err) { r.Log.Info("chaos not found") @@ -79,11 +78,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu records := obj.GetStatus().Experiment.Records selectors := obj.GetSelectorSpecs() + logger := r.Log.WithValues("name", obj.GetName(), "namespace", obj.GetNamespace(), "kind", obj.GetObjectKind().GroupVersionKind().Kind) + if records == nil { for name, sel := range selectors { targets, err := r.Selector.Select(context.TODO(), sel) if err != nil { - r.Log.Error(err, "fail to select") + logger.Error(err, "fail to select") r.Recorder.Event(obj, recorder.Failed{ Activity: "select targets", Err: err.Error(), @@ -92,7 +93,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu } if len(targets) == 0 { - r.Log.Info("no target has been selected") + logger.Info("no target has been selected") r.Recorder.Event(obj, recorder.Failed{ Activity: "select targets", Err: "no target has been selected", @@ -115,7 +116,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu needRetry := false for index, record := range records { var err error - r.Log.Info("iterating record", "record", record, "desiredPhase", desiredPhase) + idLogger := logger.WithValues("id", records[index].Id) + idLogger.Info("iterating record", "record", record, "desiredPhase", desiredPhase) // The whole running logic is a cycle: // Not Injected -> Not Injected/* -> Injected -> Injected/* -> Not Injected @@ -146,7 +148,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu } if operation == Apply { - r.Log.Info("apply chaos", "id", records[index].Id) + idLogger.Info("apply chaos") record.Phase, err = r.Impl.Apply(context.TODO(), index, records, obj) if record.Phase != originalPhase { shouldUpdate = true @@ -154,7 +156,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu if err != nil { // TODO: add backoff and retry mechanism // but the retry shouldn't block other resource process - r.Log.Error(err, "fail to apply chaos") + idLogger.Error(err, "fail to apply chaos") applyFailedEvent := newRecordEvent(v1alpha1.TypeFailed, v1alpha1.Apply, err.Error()) records[index].Events = append(records[index].Events, *applyFailedEvent) r.Recorder.Event(obj, recorder.Failed{ @@ -176,7 +178,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu }) } } else if operation == Recover { - r.Log.Info("recover chaos", "id", records[index].Id) + idLogger.Info("recover chaos") record.Phase, err = r.Impl.Recover(context.TODO(), index, records, obj) if record.Phase != originalPhase { shouldUpdate = true @@ -184,7 +186,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu if err != nil { // TODO: add backoff and retry mechanism // but the retry shouldn't block other resource process - r.Log.Error(err, "fail to recover chaos") + idLogger.Error(err, "fail to recover chaos") recoverFailedEvent := newRecordEvent(v1alpha1.TypeFailed, v1alpha1.Recover, err.Error()) records[index].Events = append(records[index].Events, *recoverFailedEvent) r.Recorder.Event(obj, recorder.Failed{ @@ -215,11 +217,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu } if shouldUpdate { updateError := retry.RetryOnConflict(retry.DefaultBackoff, func() error { - r.Log.Info("updating records", "records", records) + logger.Info("updating records", "records", records) obj := r.Object.DeepCopyObject().(v1alpha1.InnerObjectWithSelector) if err := r.Client.Get(context.TODO(), req.NamespacedName, obj); err != nil { - r.Log.Error(err, "unable to get chaos") + logger.Error(err, "unable to get chaos") return err } @@ -232,7 +234,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu return r.Client.Update(context.TODO(), obj) }) if updateError != nil { - r.Log.Error(updateError, "fail to update") + logger.Error(updateError, "fail to update") r.Recorder.Event(obj, recorder.Failed{ Activity: "update records", Err: updateError.Error(),