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 <hidde@hhh.computer>
This commit is contained in:
Hidde Beydals 2023-02-27 10:44:27 +01:00
parent 8c4b2885b2
commit 9153649f23
No known key found for this signature in database
GPG Key ID: 979F380FC2341744
1 changed files with 24 additions and 1 deletions

View File

@ -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
}