Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Tracker upgrade should use gvk from scheme #1213

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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