Skip to content

Commit bd97e08

Browse files
authored
Merge pull request #1213 from fabriziopandini/tracker-upgrade-should-use-gvk-from-scheme
🐛 Tracker upgrade should use gvk from scheme
2 parents c000ea8 + 87480a7 commit bd97e08

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

pkg/client/fake/client.go

Lines changed: 15 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,19 @@ 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+
if gvk.Empty() {
120+
gvk, err = apiutil.GVKForObject(obj, t.scheme)
121+
if err != nil {
122+
return err
123+
}
124+
}
125+
117126
oldObject, err := t.ObjectTracker.Get(gvr, ns, accessor.GetName())
118127
if err != nil {
119128
// If the resource is not found and the resource allows create on update, issue a
120129
// create instead.
121-
if apierrors.IsNotFound(err) && allowsCreateOnUpdate(obj) {
130+
if apierrors.IsNotFound(err) && allowsCreateOnUpdate(gvk) {
122131
return t.Create(gvr, obj, ns)
123132
}
124133
return err
@@ -131,7 +140,7 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob
131140

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

432-
func allowsUnconditionalUpdate(obj runtime.Object) bool {
433-
gvk := obj.GetObjectKind().GroupVersionKind()
441+
func allowsUnconditionalUpdate(gvk schema.GroupVersionKind) bool {
434442
switch gvk.Group {
435443
case "apps":
436444
switch gvk.Kind {
@@ -500,8 +508,7 @@ func allowsUnconditionalUpdate(obj runtime.Object) bool {
500508
return false
501509
}
502510

503-
func allowsCreateOnUpdate(obj runtime.Object) bool {
504-
gvk := obj.GetObjectKind().GroupVersionKind()
511+
func allowsCreateOnUpdate(gvk schema.GroupVersionKind) bool {
505512
switch gvk.Group {
506513
case "coordination":
507514
switch gvk.Kind {

0 commit comments

Comments
 (0)