From 62889f416cb60f66b3f04810ef2475c425b8394a Mon Sep 17 00:00:00 2001 From: Aldo Culquicondor Date: Thu, 16 Mar 2023 16:35:39 -0400 Subject: [PATCH] Preserve UID/ResourceVersion in the BindingREST endpoint Change-Id: If4023da10c455963a320fdb9fc2a73c099bea3db --- pkg/registry/core/pod/storage/storage.go | 8 ++++++++ .../k8s.io/apiserver/pkg/endpoints/handlers/create.go | 9 +++++++-- staging/src/k8s.io/apiserver/pkg/registry/rest/rest.go | 7 +++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/registry/core/pod/storage/storage.go b/pkg/registry/core/pod/storage/storage.go index ddc089ab153..5bdf342d068 100644 --- a/pkg/registry/core/pod/storage/storage.go +++ b/pkg/registry/core/pod/storage/storage.go @@ -163,6 +163,7 @@ func (r *BindingREST) Destroy() { } var _ = rest.NamedCreater(&BindingREST{}) +var _ = rest.SubresourceObjectMetaPreserver(&BindingREST{}) // Create ensures a pod is bound to a specific host. func (r *BindingREST) Create(ctx context.Context, name string, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (out runtime.Object, err error) { @@ -191,6 +192,13 @@ func (r *BindingREST) Create(ctx context.Context, name string, obj runtime.Objec return } +// PreserveRequestObjectMetaSystemFieldsOnSubresourceCreate indicates to a +// handler that this endpoint requires the UID and ResourceVersion to use as +// preconditions. Other fields, such as timestamp, are ignored. +func (r *BindingREST) PreserveRequestObjectMetaSystemFieldsOnSubresourceCreate() bool { + return true +} + // setPodHostAndAnnotations sets the given pod's host to 'machine' if and only if // the pod is unassigned and merges the provided annotations with those of the pod. // Returns the current state of the pod, or an error. diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/create.go b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/create.go index 71f4990a026..78c1d2f52a7 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/create.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/create.go @@ -162,8 +162,13 @@ func createHandler(r rest.NamedCreater, scope *RequestScope, admit admission.Int userInfo, _ := request.UserFrom(ctx) if objectMeta, err := meta.Accessor(obj); err == nil { - // Wipe fields which cannot take user-provided values - rest.WipeObjectMetaSystemFields(objectMeta) + preserveObjectMetaSystemFields := false + if c, ok := r.(rest.SubresourceObjectMetaPreserver); ok && len(scope.Subresource) > 0 { + preserveObjectMetaSystemFields = c.PreserveRequestObjectMetaSystemFieldsOnSubresourceCreate() + } + if !preserveObjectMetaSystemFields { + rest.WipeObjectMetaSystemFields(objectMeta) + } // ensure namespace on the object is correct, or error if a conflicting namespace was set in the object if err := rest.EnsureObjectNamespaceMatchesRequestNamespace(rest.ExpectedNamespaceForResource(namespace, scope.Resource), objectMeta); err != nil { diff --git a/staging/src/k8s.io/apiserver/pkg/registry/rest/rest.go b/staging/src/k8s.io/apiserver/pkg/registry/rest/rest.go index b8d47ff4fe3..78b6ea8b0ef 100644 --- a/staging/src/k8s.io/apiserver/pkg/registry/rest/rest.go +++ b/staging/src/k8s.io/apiserver/pkg/registry/rest/rest.go @@ -209,6 +209,13 @@ type NamedCreater interface { Create(ctx context.Context, name string, obj runtime.Object, createValidation ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) } +// SubresourceObjectMetaPreserver adds configuration options to a Creater for subresources. +type SubresourceObjectMetaPreserver interface { + // PreserveRequestObjectMetaSystemFieldsOnSubresourceCreate indicates that a + // handler should preserve fields of ObjectMeta that are managed by the system. + PreserveRequestObjectMetaSystemFieldsOnSubresourceCreate() bool +} + // UpdatedObjectInfo provides information about an updated object to an Updater. // It requires access to the old object in order to return the newly updated object. type UpdatedObjectInfo interface {