From 9153649f23d907e057ba347daa0c59ef09102870 Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Mon, 27 Feb 2023 10:44:27 +0100 Subject: [PATCH] diff: add premature diff debug log As there are currently no other utilities to properly see what change the controller detected, this allows people to have an insight into the observed changes by configuring the controller with `--log-level=debug`. Signed-off-by: Hidde Beydals --- internal/diff/differ.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/internal/diff/differ.go b/internal/diff/differ.go index 03f9c6d..a232331 100644 --- a/internal/diff/differ.go +++ b/internal/diff/differ.go @@ -19,6 +19,10 @@ package diff import ( "context" "fmt" + "github.com/fluxcd/pkg/runtime/logger" + "github.com/google/go-cmp/cmp" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + ctrl "sigs.k8s.io/controller-runtime" "strings" "helm.sh/helm/v3/pkg/release" @@ -106,7 +110,7 @@ func (d *Differ) Diff(ctx context.Context, rel *release.Release) (*ssa.ChangeSet } } - entry, _, _, err := resourceManager.Diff(ctx, obj, ssa.DiffOptions{ + entry, releaseObject, clusterObject, err := resourceManager.Diff(ctx, obj, ssa.DiffOptions{ Exclusions: map[string]string{ MetadataKey: MetadataDisabledValue, }, @@ -115,10 +119,23 @@ func (d *Differ) Diff(ctx context.Context, rel *release.Release) (*ssa.ChangeSet errs = append(errs, err) } + if entry == nil { + continue + } + switch entry.Action { case ssa.CreatedAction, ssa.ConfiguredAction: diff = true changeSet.Add(*entry) + + if entry.Action == ssa.ConfiguredAction { + // TODO: remove this once we have a better way to log the diff + // for example using a custom dyff reporter, or a flux CLI command + ctrl.LoggerFrom(ctx).V(logger.DebugLevel).Info(entry.Subject + " diff:" + cmp.Diff( + unstructuredWithoutStatus(releaseObject).UnstructuredContent(), + unstructuredWithoutStatus(clusterObject).UnstructuredContent(), + )) + } case ssa.SkippedAction: changeSet.Add(*entry) } @@ -130,3 +147,9 @@ func (d *Differ) Diff(ctx context.Context, rel *release.Release) (*ssa.ChangeSet } return changeSet, diff, err } + +func unstructuredWithoutStatus(obj *unstructured.Unstructured) *unstructured.Unstructured { + obj = obj.DeepCopy() + delete(obj.Object, "status") + return obj +}