Merge pull request #192 from hasheddan/fix-updater

Update with desired object in APIUpdatingApplicator
This commit is contained in:
Daniel Mangum 2020-09-04 13:48:39 -05:00 committed by GitHub
commit 938e74dc71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 11 deletions

View File

@ -174,10 +174,9 @@ 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. If the object does exist and no
// ApplyOptions are passed, the update will be a no-op.
// not exist, or updated if it does.
func (a *APIUpdatingApplicator) Apply(ctx context.Context, o runtime.Object, ao ...ApplyOption) error {
m, ok := o.(metav1.Object)
m, ok := o.(Object)
if !ok {
return errors.New("cannot access object metadata")
}
@ -191,19 +190,22 @@ func (a *APIUpdatingApplicator) Apply(ctx context.Context, o runtime.Object, ao
err := a.client.Get(ctx, types.NamespacedName{Name: m.GetName(), Namespace: m.GetNamespace()}, current)
if kerrors.IsNotFound(err) {
// TODO(negz): Apply ApplyOptions here too?
return errors.Wrap(a.client.Create(ctx, o), "cannot create object")
return errors.Wrap(a.client.Create(ctx, m), "cannot create object")
}
if err != nil {
return errors.Wrap(err, "cannot get object")
}
for _, fn := range ao {
if err := fn(ctx, current, o); err != nil {
if err := fn(ctx, current, m); err != nil {
return err
}
}
return errors.Wrap(a.client.Update(ctx, current), "cannot update object")
// NOTE(hasheddan): we must set the resource version of the desired object
// to that of the current or the update will always fail.
m.SetResourceVersion(current.(metav1.Object).GetResourceVersion())
return errors.Wrap(a.client.Update(ctx, m), "cannot update object")
}
// An APIFinalizer adds and removes finalizers to and from a resource.

View File

@ -18,7 +18,6 @@ package resource
import (
"context"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
@ -468,13 +467,10 @@ func TestAPIUpdatingApplicator(t *testing.T) {
c: &test.MockClient{
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 {
fmt.Println(current.Name)
fmt.Println(o.(*object).Name)
if diff := cmp.Diff(*current, *o.(*object)); diff != "" {
if diff := cmp.Diff(*desired, *o.(*object)); diff != "" {
t.Errorf("r: -want, +got:\n%s", diff)
}
return nil