Skip to content

Commit

Permalink
Merge pull request #1213 from fabriziopandini/tracker-upgrade-should-…
Browse files Browse the repository at this point in the history
…use-gvk-from-scheme

🐛 Tracker upgrade should use gvk from scheme
  • Loading branch information
k8s-ci-robot committed Oct 9, 2020
2 parents c000ea8 + 87480a7 commit bd97e08
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (

type versionedTracker struct {
testing.ObjectTracker
scheme *runtime.Scheme
}

type fakeClient struct {
Expand Down Expand Up @@ -74,15 +75,15 @@ func NewFakeClientWithScheme(clientScheme *runtime.Scheme, initObjs ...client.Ob
}
}
return &fakeClient{
tracker: versionedTracker{tracker},
tracker: versionedTracker{ObjectTracker: tracker, scheme: clientScheme},
scheme: clientScheme,
}
}

func (t versionedTracker) Create(gvr schema.GroupVersionResource, obj runtime.Object, ns string) error {
accessor, err := meta.Accessor(obj)
if err != nil {
return err
return fmt.Errorf("failed to get accessor for object: %v", err)
}
if accessor.GetName() == "" {
return apierrors.NewInvalid(
Expand Down Expand Up @@ -114,11 +115,19 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob
field.ErrorList{field.Required(field.NewPath("metadata.name"), "name is required")})
}

gvk := obj.GetObjectKind().GroupVersionKind()
if gvk.Empty() {
gvk, err = apiutil.GVKForObject(obj, t.scheme)
if err != nil {
return err
}
}

oldObject, err := t.ObjectTracker.Get(gvr, ns, accessor.GetName())
if err != nil {
// If the resource is not found and the resource allows create on update, issue a
// create instead.
if apierrors.IsNotFound(err) && allowsCreateOnUpdate(obj) {
if apierrors.IsNotFound(err) && allowsCreateOnUpdate(gvk) {
return t.Create(gvr, obj, ns)
}
return err
Expand All @@ -131,7 +140,7 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob

// If the new object does not have the resource version set and it allows unconditional update,
// default it to the resource version of the existing resource
if accessor.GetResourceVersion() == "" && allowsUnconditionalUpdate(obj) {
if accessor.GetResourceVersion() == "" && allowsUnconditionalUpdate(gvk) {
accessor.SetResourceVersion(oldAccessor.GetResourceVersion())
}
if accessor.GetResourceVersion() != oldAccessor.GetResourceVersion() {
Expand Down Expand Up @@ -429,8 +438,7 @@ func (sw *fakeStatusWriter) Patch(ctx context.Context, obj client.Object, patch
return sw.client.Patch(ctx, obj, patch, opts...)
}

func allowsUnconditionalUpdate(obj runtime.Object) bool {
gvk := obj.GetObjectKind().GroupVersionKind()
func allowsUnconditionalUpdate(gvk schema.GroupVersionKind) bool {
switch gvk.Group {
case "apps":
switch gvk.Kind {
Expand Down Expand Up @@ -500,8 +508,7 @@ func allowsUnconditionalUpdate(obj runtime.Object) bool {
return false
}

func allowsCreateOnUpdate(obj runtime.Object) bool {
gvk := obj.GetObjectKind().GroupVersionKind()
func allowsCreateOnUpdate(gvk schema.GroupVersionKind) bool {
switch gvk.Group {
case "coordination":
switch gvk.Kind {
Expand Down

0 comments on commit bd97e08

Please sign in to comment.