Skip to content

Commit cc8afb2

Browse files
authored
Merge pull request kubernetes#74040 from ajatprabha/issue_73648
add ResourceVersion to DeleteOptions.Preconditions
2 parents c67e16c + 4bd0c4a commit cc8afb2

File tree

13 files changed

+302
-166
lines changed

13 files changed

+302
-166
lines changed

api/openapi-spec/swagger.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/registry/core/namespace/storage/storage.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ func (r *REST) Delete(ctx context.Context, name string, options *metav1.DeleteOp
147147
)
148148
return nil, false, err
149149
}
150+
if options.Preconditions.ResourceVersion != nil && *options.Preconditions.ResourceVersion != namespace.ResourceVersion {
151+
err = apierrors.NewConflict(
152+
api.Resource("namespaces"),
153+
name,
154+
fmt.Errorf("Precondition failed: ResourceVersion in precondition: %v, ResourceVersion in object meta: %v", *options.Preconditions.ResourceVersion, namespace.ResourceVersion),
155+
)
156+
return nil, false, err
157+
}
150158

151159
// upon first request to delete, we switch the phase to start namespace termination
152160
// TODO: enhance graceful deletion's calls to DeleteStrategy to allow phase change and finalizer patterns
@@ -156,7 +164,7 @@ func (r *REST) Delete(ctx context.Context, name string, options *metav1.DeleteOp
156164
return nil, false, err
157165
}
158166

159-
preconditions := storage.Preconditions{UID: options.Preconditions.UID}
167+
preconditions := storage.Preconditions{UID: options.Preconditions.UID, ResourceVersion: options.Preconditions.ResourceVersion}
160168

161169
out := r.store.NewFunc()
162170
err = r.store.Storage.GuaranteedUpdate(

staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresourcedefinition/etcd.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ func (r *REST) Delete(ctx context.Context, name string, options *metav1.DeleteOp
9292
)
9393
return nil, false, err
9494
}
95+
if options.Preconditions.ResourceVersion != nil && *options.Preconditions.ResourceVersion != crd.ResourceVersion {
96+
err = apierrors.NewConflict(
97+
apiextensions.Resource("customresourcedefinitions"),
98+
name,
99+
fmt.Errorf("Precondition failed: ResourceVersion in precondition: %v, ResourceVersion in object meta: %v", *options.Preconditions.ResourceVersion, crd.ResourceVersion),
100+
)
101+
return nil, false, err
102+
}
95103

96104
// upon first request to delete, add our finalizer and then delegate
97105
if crd.DeletionTimestamp.IsZero() {
@@ -100,7 +108,7 @@ func (r *REST) Delete(ctx context.Context, name string, options *metav1.DeleteOp
100108
return nil, false, err
101109
}
102110

103-
preconditions := storage.Preconditions{UID: options.Preconditions.UID}
111+
preconditions := storage.Preconditions{UID: options.Preconditions.UID, ResourceVersion: options.Preconditions.ResourceVersion}
104112

105113
out := r.Store.NewFunc()
106114
err = r.Store.Storage.GuaranteedUpdate(

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/generated.pb.go

Lines changed: 201 additions & 159 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/generated.proto

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/helpers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ func NewUIDPreconditions(uid string) *Preconditions {
228228
return &Preconditions{UID: &u}
229229
}
230230

231+
// NewRVDeletionPrecondition returns a DeleteOptions with a ResourceVersion precondition set.
232+
func NewRVDeletionPrecondition(rv string) *DeleteOptions {
233+
p := Preconditions{ResourceVersion: &rv}
234+
return &DeleteOptions{Preconditions: &p}
235+
}
236+
231237
// HasObjectMetaSystemFieldValues returns true if fields that are managed by the system on ObjectMeta have values.
232238
func HasObjectMetaSystemFieldValues(meta Object) bool {
233239
return !meta.GetCreationTimestamp().Time.IsZero() ||

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,9 @@ type Preconditions struct {
575575
// Specifies the target UID.
576576
// +optional
577577
UID *types.UID `json:"uid,omitempty" protobuf:"bytes,1,opt,name=uid,casttype=k8s.io/apimachinery/pkg/types.UID"`
578+
// Specifies the target ResourceVersion
579+
// +optional
580+
ResourceVersion *string `json:"resourceVersion,omitempty" protobuf:"bytes,2,opt,name=resourceVersion"`
578581
}
579582

580583
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types_swagger_doc_generated.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ func (e *Store) Update(ctx context.Context, name string, objInfo rest.UpdatedObj
448448
storagePreconditions := &storage.Preconditions{}
449449
if preconditions := objInfo.Preconditions(); preconditions != nil {
450450
storagePreconditions.UID = preconditions.UID
451+
storagePreconditions.ResourceVersion = preconditions.ResourceVersion
451452
}
452453

453454
out := e.NewFunc()
@@ -879,6 +880,7 @@ func (e *Store) Delete(ctx context.Context, name string, options *metav1.DeleteO
879880
var preconditions storage.Preconditions
880881
if options.Preconditions != nil {
881882
preconditions.UID = options.Preconditions.UID
883+
preconditions.ResourceVersion = options.Preconditions.ResourceVersion
882884
}
883885
graceful, pendingGraceful, err := rest.BeforeDelete(e.DeleteStrategy, ctx, obj, options)
884886
if err != nil {

0 commit comments

Comments
 (0)