@@ -41,6 +41,7 @@ import (
41
41
42
42
type versionedTracker struct {
43
43
testing.ObjectTracker
44
+ scheme * runtime.Scheme
44
45
}
45
46
46
47
type fakeClient struct {
@@ -74,15 +75,15 @@ func NewFakeClientWithScheme(clientScheme *runtime.Scheme, initObjs ...client.Ob
74
75
}
75
76
}
76
77
return & fakeClient {
77
- tracker : versionedTracker {tracker },
78
+ tracker : versionedTracker {ObjectTracker : tracker , scheme : clientScheme },
78
79
scheme : clientScheme ,
79
80
}
80
81
}
81
82
82
83
func (t versionedTracker ) Create (gvr schema.GroupVersionResource , obj runtime.Object , ns string ) error {
83
84
accessor , err := meta .Accessor (obj )
84
85
if err != nil {
85
- return err
86
+ return fmt . Errorf ( "failed to get accessor for object: %v" , err )
86
87
}
87
88
if accessor .GetName () == "" {
88
89
return apierrors .NewInvalid (
@@ -114,11 +115,19 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob
114
115
field.ErrorList {field .Required (field .NewPath ("metadata.name" ), "name is required" )})
115
116
}
116
117
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
+
117
126
oldObject , err := t .ObjectTracker .Get (gvr , ns , accessor .GetName ())
118
127
if err != nil {
119
128
// If the resource is not found and the resource allows create on update, issue a
120
129
// create instead.
121
- if apierrors .IsNotFound (err ) && allowsCreateOnUpdate (obj ) {
130
+ if apierrors .IsNotFound (err ) && allowsCreateOnUpdate (gvk ) {
122
131
return t .Create (gvr , obj , ns )
123
132
}
124
133
return err
@@ -131,7 +140,7 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob
131
140
132
141
// If the new object does not have the resource version set and it allows unconditional update,
133
142
// default it to the resource version of the existing resource
134
- if accessor .GetResourceVersion () == "" && allowsUnconditionalUpdate (obj ) {
143
+ if accessor .GetResourceVersion () == "" && allowsUnconditionalUpdate (gvk ) {
135
144
accessor .SetResourceVersion (oldAccessor .GetResourceVersion ())
136
145
}
137
146
if accessor .GetResourceVersion () != oldAccessor .GetResourceVersion () {
@@ -429,8 +438,7 @@ func (sw *fakeStatusWriter) Patch(ctx context.Context, obj client.Object, patch
429
438
return sw .client .Patch (ctx , obj , patch , opts ... )
430
439
}
431
440
432
- func allowsUnconditionalUpdate (obj runtime.Object ) bool {
433
- gvk := obj .GetObjectKind ().GroupVersionKind ()
441
+ func allowsUnconditionalUpdate (gvk schema.GroupVersionKind ) bool {
434
442
switch gvk .Group {
435
443
case "apps" :
436
444
switch gvk .Kind {
@@ -500,8 +508,7 @@ func allowsUnconditionalUpdate(obj runtime.Object) bool {
500
508
return false
501
509
}
502
510
503
- func allowsCreateOnUpdate (obj runtime.Object ) bool {
504
- gvk := obj .GetObjectKind ().GroupVersionKind ()
511
+ func allowsCreateOnUpdate (gvk schema.GroupVersionKind ) bool {
505
512
switch gvk .Group {
506
513
case "coordination" :
507
514
switch gvk .Kind {
0 commit comments