@@ -473,6 +473,16 @@ func (e *Store) Update(ctx context.Context, name string, objInfo rest.UpdatedObj
473
473
// deleteObj is only used in case a deletion is carried out
474
474
var deleteObj runtime.Object
475
475
err = e .Storage .GuaranteedUpdate (ctx , key , out , true , storagePreconditions , func (existing runtime.Object , res storage.ResponseMeta ) (runtime.Object , * uint64 , error ) {
476
+ existingResourceVersion , err := e .Storage .Versioner ().ObjectResourceVersion (existing )
477
+ if err != nil {
478
+ return nil , nil , err
479
+ }
480
+ if existingResourceVersion == 0 {
481
+ if ! e .UpdateStrategy .AllowCreateOnUpdate () && ! forceAllowCreate {
482
+ return nil , nil , apierrors .NewNotFound (qualifiedResource , name )
483
+ }
484
+ }
485
+
476
486
// Given the existing object, get the new object
477
487
obj , err := objInfo .UpdatedObject (ctx , existing )
478
488
if err != nil {
@@ -483,20 +493,13 @@ func (e *Store) Update(ctx context.Context, name string, objInfo rest.UpdatedObj
483
493
// the user does not have a resource version, then we populate it with
484
494
// the latest version. Else, we check that the version specified by
485
495
// the user matches the version of latest storage object.
486
- resourceVersion , err := e .Storage .Versioner ().ObjectResourceVersion (obj )
496
+ newResourceVersion , err := e .Storage .Versioner ().ObjectResourceVersion (obj )
487
497
if err != nil {
488
498
return nil , nil , err
489
499
}
490
- doUnconditionalUpdate := resourceVersion == 0 && e .UpdateStrategy .AllowUnconditionalUpdate ()
500
+ doUnconditionalUpdate := newResourceVersion == 0 && e .UpdateStrategy .AllowUnconditionalUpdate ()
491
501
492
- version , err := e .Storage .Versioner ().ObjectResourceVersion (existing )
493
- if err != nil {
494
- return nil , nil , err
495
- }
496
- if version == 0 {
497
- if ! e .UpdateStrategy .AllowCreateOnUpdate () && ! forceAllowCreate {
498
- return nil , nil , apierrors .NewNotFound (qualifiedResource , name )
499
- }
502
+ if existingResourceVersion == 0 {
500
503
creating = true
501
504
creatingObj = obj
502
505
if err := rest .BeforeCreate (e .CreateStrategy , ctx , obj ); err != nil {
@@ -529,15 +532,15 @@ func (e *Store) Update(ctx context.Context, name string, objInfo rest.UpdatedObj
529
532
} else {
530
533
// Check if the object's resource version matches the latest
531
534
// resource version.
532
- if resourceVersion == 0 {
535
+ if newResourceVersion == 0 {
533
536
// TODO: The Invalid error should have a field for Resource.
534
537
// After that field is added, we should fill the Resource and
535
538
// leave the Kind field empty. See the discussion in #18526.
536
539
qualifiedKind := schema.GroupKind {Group : qualifiedResource .Group , Kind : qualifiedResource .Resource }
537
- fieldErrList := field.ErrorList {field .Invalid (field .NewPath ("metadata" ).Child ("resourceVersion" ), resourceVersion , "must be specified for an update" )}
540
+ fieldErrList := field.ErrorList {field .Invalid (field .NewPath ("metadata" ).Child ("resourceVersion" ), newResourceVersion , "must be specified for an update" )}
538
541
return nil , nil , apierrors .NewInvalid (qualifiedKind , name , fieldErrList )
539
542
}
540
- if resourceVersion != version {
543
+ if newResourceVersion != existingResourceVersion {
541
544
return nil , nil , apierrors .NewConflict (qualifiedResource , name , fmt .Errorf (OptimisticLockErrorMsg ))
542
545
}
543
546
}
0 commit comments