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 07d6834 commit 3fa0689
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
18 changes: 18 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,20 @@ 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)
gvk := object.GetObjectKind().GroupVersionKind()
ret = append(ret,
"name", object.GetName(),
"kind", gvk.Kind,
"version", gvk.Version,
"group", gvk.Group,
)
if ns := object.GetNamespace(); ns != "" {
ret = append(ret, "namespace", ns)
}

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)
}
3 changes: 2 additions & 1 deletion pkg/resource/providerconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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/resource/fake"
"github.com/crossplane/crossplane-runtime/pkg/test"
)
Expand Down Expand Up @@ -317,7 +318,7 @@ func TestTrack(t *testing.T) {

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
ut := &ProviderConfigUsageTracker{c: tc.fields.c, of: tc.fields.of}
ut := &ProviderConfigUsageTracker{c: tc.fields.c, of: tc.fields.of, log: logging.NewNopLogger()}
got := ut.Track(tc.args.ctx, tc.args.mg)
if diff := cmp.Diff(tc.want, got, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nut.Track(...): -want error, +got error:\n%s\n", tc.reason, diff)
Expand Down
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 3fa0689

Please sign in to comment.