Skip to content

Commit 07a7a8c

Browse files
Tracker upgrade should use gvk from scheme
1 parent c000ea8 commit 07a7a8c

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

pkg/client/fake/client.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141

4242
type versionedTracker struct {
4343
testing.ObjectTracker
44+
scheme *runtime.Scheme
4445
}
4546

4647
type fakeClient struct {
@@ -74,15 +75,15 @@ func NewFakeClientWithScheme(clientScheme *runtime.Scheme, initObjs ...client.Ob
7475
}
7576
}
7677
return &fakeClient{
77-
tracker: versionedTracker{tracker},
78+
tracker: versionedTracker{ObjectTracker: tracker, scheme: clientScheme},
7879
scheme: clientScheme,
7980
}
8081
}
8182

8283
func (t versionedTracker) Create(gvr schema.GroupVersionResource, obj runtime.Object, ns string) error {
8384
accessor, err := meta.Accessor(obj)
8485
if err != nil {
85-
return err
86+
return fmt.Errorf("failed to get accessor for object: %v", err)
8687
}
8788
if accessor.GetName() == "" {
8889
return apierrors.NewInvalid(
@@ -114,11 +115,20 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob
114115
field.ErrorList{field.Required(field.NewPath("metadata.name"), "name is required")})
115116
}
116117

118+
gvk := obj.GetObjectKind().GroupVersionKind()
119+
emptyGvk := schema.GroupVersionKind{}
120+
if gvk == emptyGvk {
121+
gvk, err = apiutil.GVKForObject(obj, t.scheme)
122+
if err != nil {
123+
return err
124+
}
125+
}
126+
117127
oldObject, err := t.ObjectTracker.Get(gvr, ns, accessor.GetName())
118128
if err != nil {
119129
// If the resource is not found and the resource allows create on update, issue a
120130
// create instead.
121-
if apierrors.IsNotFound(err) && allowsCreateOnUpdate(obj) {
131+
if apierrors.IsNotFound(err) && allowsCreateOnUpdate(gvk) {
122132
return t.Create(gvr, obj, ns)
123133
}
124134
return err
@@ -131,7 +141,7 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob
131141

132142
// If the new object does not have the resource version set and it allows unconditional update,
133143
// default it to the resource version of the existing resource
134-
if accessor.GetResourceVersion() == "" && allowsUnconditionalUpdate(obj) {
144+
if accessor.GetResourceVersion() == "" && allowsUnconditionalUpdate(gvk) {
135145
accessor.SetResourceVersion(oldAccessor.GetResourceVersion())
136146
}
137147
if accessor.GetResourceVersion() != oldAccessor.GetResourceVersion() {
@@ -429,8 +439,7 @@ func (sw *fakeStatusWriter) Patch(ctx context.Context, obj client.Object, patch
429439
return sw.client.Patch(ctx, obj, patch, opts...)
430440
}
431441

432-
func allowsUnconditionalUpdate(obj runtime.Object) bool {
433-
gvk := obj.GetObjectKind().GroupVersionKind()
442+
func allowsUnconditionalUpdate(gvk schema.GroupVersionKind) bool {
434443
switch gvk.Group {
435444
case "apps":
436445
switch gvk.Kind {
@@ -500,8 +509,7 @@ func allowsUnconditionalUpdate(obj runtime.Object) bool {
500509
return false
501510
}
502511

503-
func allowsCreateOnUpdate(obj runtime.Object) bool {
504-
gvk := obj.GetObjectKind().GroupVersionKind()
512+
func allowsCreateOnUpdate(gvk schema.GroupVersionKind) bool {
505513
switch gvk.Group {
506514
case "coordination":
507515
switch gvk.Kind {

0 commit comments

Comments
 (0)