Skip to content

Commit

Permalink
Merge pull request #176 from hasheddan/resverauto
Browse files Browse the repository at this point in the history
Update with object that is gotten in APIUpdatingApplicator
  • Loading branch information
negz authored May 12, 2020
2 parents 926e188 + a964b65 commit 290de73
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
8 changes: 5 additions & 3 deletions pkg/resource/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ func NewAPIPatchingApplicator(c client.Client) *APIPatchingApplicator {
}

// Apply changes to the supplied object. The object will be created if it does
// not exist, or patched if it does.
// not exist, or patched if it does. If the object does it exist it will always
// be patched, regardless of resource version.
func (a *APIPatchingApplicator) Apply(ctx context.Context, o runtime.Object, ao ...ApplyOption) error {
m, ok := o.(metav1.Object)
if !ok {
Expand Down Expand Up @@ -173,7 +174,8 @@ func NewAPIUpdatingApplicator(c client.Client) *APIUpdatingApplicator {
}

// Apply changes to the supplied object. The object will be created if it does
// not exist, or updated if it does.
// not exist, or updated if it does. If the object does exist and no
// ApplyOptions are passed, the update will be a no-op.
func (a *APIUpdatingApplicator) Apply(ctx context.Context, o runtime.Object, ao ...ApplyOption) error {
m, ok := o.(metav1.Object)
if !ok {
Expand Down Expand Up @@ -201,7 +203,7 @@ func (a *APIUpdatingApplicator) Apply(ctx context.Context, o runtime.Object, ao
}
}

return errors.Wrap(a.client.Update(ctx, o), "cannot update object")
return errors.Wrap(a.client.Update(ctx, current), "cannot update object")
}

// An APIFinalizer adds and removes finalizers to and from a resource.
Expand Down
47 changes: 29 additions & 18 deletions pkg/resource/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package resource

import (
"context"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -230,8 +231,8 @@ func TestPropagateConnection(t *testing.T) {

func TestAPIPatchingApplicator(t *testing.T) {
errBoom := errors.New("boom")
named := &object{}
named.SetName("barry")
desired := &object{}
desired.SetName("desired")

type args struct {
ctx context.Context
Expand Down Expand Up @@ -317,31 +318,31 @@ func TestAPIPatchingApplicator(t *testing.T) {
c: &test.MockClient{
MockGet: test.NewMockGetFn(kerrors.NewNotFound(schema.GroupResource{}, "")),
MockCreate: test.NewMockCreateFn(nil, func(o runtime.Object) error {
*o.(*object) = *named
*o.(*object) = *desired
return nil
}),
},
args: args{
o: &object{},
o: desired,
},
want: want{
o: named,
o: desired,
},
},
"Patched": {
reason: "No error should be returned if we successfully patch an existing object",
c: &test.MockClient{
MockGet: test.NewMockGetFn(nil),
MockPatch: test.NewMockPatchFn(nil, func(o runtime.Object) error {
*o.(*object) = *named
*o.(*object) = *desired
return nil
}),
},
args: args{
o: &object{},
o: desired,
},
want: want{
o: named,
o: desired,
},
},
}
Expand All @@ -362,8 +363,10 @@ func TestAPIPatchingApplicator(t *testing.T) {

func TestAPIUpdatingApplicator(t *testing.T) {
errBoom := errors.New("boom")
named := &object{}
named.SetName("barry")
desired := &object{}
desired.SetName("desired")
current := &object{}
current.SetName("current")

type args struct {
ctx context.Context
Expand Down Expand Up @@ -449,31 +452,39 @@ func TestAPIUpdatingApplicator(t *testing.T) {
c: &test.MockClient{
MockGet: test.NewMockGetFn(kerrors.NewNotFound(schema.GroupResource{}, "")),
MockCreate: test.NewMockCreateFn(nil, func(o runtime.Object) error {
*o.(*object) = *named
*o.(*object) = *desired
return nil
}),
},
args: args{
o: &object{},
o: desired,
},
want: want{
o: named,
o: desired,
},
},
"Updated": {
reason: "No error should be returned if we successfully update an existing object",
reason: "No error should be returned if we successfully update an existing object. If no ApplyOption is passed the existing should not be modified",
c: &test.MockClient{
MockGet: test.NewMockGetFn(nil),
MockGet: test.NewMockGetFn(nil, func(o runtime.Object) error {
*o.(*object) = *current
fmt.Println(current.Name)
return nil
}),
MockUpdate: test.NewMockUpdateFn(nil, func(o runtime.Object) error {
*o.(*object) = *named
fmt.Println(current.Name)
fmt.Println(o.(*object).Name)
if diff := cmp.Diff(*current, *o.(*object)); diff != "" {
t.Errorf("r: -want, +got:\n%s", diff)
}
return nil
}),
},
args: args{
o: &object{},
o: desired,
},
want: want{
o: named,
o: desired,
},
},
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ func (fn ApplyFn) Apply(ctx context.Context, o runtime.Object, ao ...ApplyOption
// desired object. ApplyOptions are not called if no current object exists.
type ApplyOption func(ctx context.Context, current, desired runtime.Object) error

// UpdateFn returns an ApplyOption that is used to modify the current object to
// match fields of the desired.
func UpdateFn(fn func(current, desired runtime.Object)) ApplyOption {
return func(_ context.Context, c, d runtime.Object) error {
fn(c, d)
return nil
}
}

// MustBeControllableBy requires that the current object is controllable by an
// object with the supplied UID. An object is controllable if its controller
// reference matches the supplied UID, or it has no controller reference.
Expand Down

0 comments on commit 290de73

Please sign in to comment.