Skip to content

Commit

Permalink
pkg/resource: add LogDiff apply option
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
  • Loading branch information
sttts committed Aug 1, 2023
1 parent 8d05f34 commit d3a9ba0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
16 changes: 16 additions & 0 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ package logging

import (
"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// A Logger logs messages. Messages may be supplemented by structured data.
Expand Down Expand Up @@ -92,3 +93,18 @@ func (l logrLogger) Debug(msg string, keysAndValues ...any) {
func (l logrLogger) WithValues(keysAndValues ...any) Logger {
return logrLogger{log: l.log.WithValues(keysAndValues...)} //nolint:logrlint // False positive - logrlint thinks there's an odd number of args.
}

// ForResource returns logging values for a resource.
func ForResource(object client.Object) []string {
ret := make([]string, 0, 10)
ret = append(ret, "name", object.GetName())
if ns := object.GetNamespace(); ns != "" {
ret = append(ret, "namespace", ns)
}
gvk := object.GetObjectKind().GroupVersionKind()
ret = append(ret, "kind", gvk.Kind)
ret = append(ret, "version", gvk.Version)
ret = append(ret, "group", gvk.Group)

return ret
}
15 changes: 12 additions & 3 deletions pkg/resource/providerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/meta"
)

Expand Down Expand Up @@ -123,13 +124,20 @@ func (fn TrackerFn) Track(ctx context.Context, mg Managed) error {
// A ProviderConfigUsageTracker tracks usages of a ProviderConfig by creating or
// updating the appropriate ProviderConfigUsage.
type ProviderConfigUsageTracker struct {
c Applicator
of ProviderConfigUsage
c Applicator
of ProviderConfigUsage
log logging.Logger
}

// NewProviderConfigUsageTracker creates a ProviderConfigUsageTracker.
func NewProviderConfigUsageTracker(c client.Client, of ProviderConfigUsage) *ProviderConfigUsageTracker {
return &ProviderConfigUsageTracker{c: NewAPIUpdatingApplicator(c), of: of}
return &ProviderConfigUsageTracker{c: NewAPIUpdatingApplicator(c), of: of, log: logging.NewNopLogger()}
}

// WithLogger adds a logger to the ProviderConfigUsageTracker.
func (u *ProviderConfigUsageTracker) WithLogger(l logging.Logger) *ProviderConfigUsageTracker {
u.log = l
return u
}

// Track that the supplied Managed resource is using the ProviderConfig it
Expand Down Expand Up @@ -160,6 +168,7 @@ func (u *ProviderConfigUsageTracker) Track(ctx context.Context, mg Managed) erro
AllowUpdateIf(func(current, _ runtime.Object) bool {
return current.(ProviderConfigUsage).GetProviderConfigReference() != pcu.GetProviderConfigReference()
}),
LogDiff(u.log.WithValues(logging.ForResource(pcu))),
)
return errors.Wrap(Ignore(IsNotAllowed, err), errApplyPCU)
}
25 changes: 25 additions & 0 deletions pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package resource

import (
"context"
"encoding/json"
"strings"

jsonpatch "github.com/evanphx/json-patch/v5"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -32,6 +34,7 @@ import (

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/meta"
)

Expand Down Expand Up @@ -299,6 +302,28 @@ func UpdateFn(fn func(current, desired runtime.Object)) ApplyOption {
}
}

// LogDiff returns an ApplyOption that logs the difference between the current and desired state.
func LogDiff(log logging.Logger) func(ctx context.Context, current, desired runtime.Object) error {
return func(ctx context.Context, current, desired runtime.Object) error {
oldData, err := json.Marshal(current)
if err != nil {
return errors.Wrapf(err, "failed to Marshal old data for %T", current)
}
newData, err := json.Marshal(desired)
if err != nil {
return errors.Wrapf(err, "failed to Marshal new data for %T", desired)
}
patchBytes, err := jsonpatch.CreateMergePatch(oldData, newData)
if err != nil {
return errors.Wrapf(err, "failed to create patch for %T", current)
}

log.WithValues("diff", string(patchBytes)).Info("patching object")

return nil
}
}

type errNotControllable struct{ error }

func (e errNotControllable) NotControllable() bool {
Expand Down

0 comments on commit d3a9ba0

Please sign in to comment.