diff --git a/pkg/registry/rest/create.go b/pkg/registry/rest/create.go index 619acc3e0..388ca5233 100644 --- a/pkg/registry/rest/create.go +++ b/pkg/registry/rest/create.go @@ -81,7 +81,7 @@ func BeforeCreate(strategy RESTCreateStrategy, ctx context.Context, obj runtime. if !ValidNamespace(ctx, objectMeta) { return errors.NewBadRequest("the namespace of the provided object does not match the namespace sent on the request") } - } else { + } else if len(objectMeta.GetNamespace()) > 0 { objectMeta.SetNamespace(metav1.NamespaceNone) } objectMeta.SetDeletionTimestamp(nil) @@ -98,7 +98,9 @@ func BeforeCreate(strategy RESTCreateStrategy, ctx context.Context, obj runtime. } // ClusterName is ignored and should not be saved - objectMeta.SetClusterName("") + if len(objectMeta.GetClusterName()) > 0 { + objectMeta.SetClusterName("") + } if errs := strategy.Validate(ctx, obj); len(errs) > 0 { return errors.NewInvalid(kind.GroupKind(), objectMeta.GetName(), errs) diff --git a/pkg/registry/rest/update.go b/pkg/registry/rest/update.go index 24e918604..147541bcf 100644 --- a/pkg/registry/rest/update.go +++ b/pkg/registry/rest/update.go @@ -83,6 +83,7 @@ func validateCommonFields(obj, old runtime.Object, strategy RESTUpdateStrategy) // BeforeUpdate ensures that common operations for all resources are performed on update. It only returns // errors that can be converted to api.Status. It will invoke update validation with the provided existing // and updated objects. +// It sets zero values only if the object does not have a zero value for the respective field. func BeforeUpdate(strategy RESTUpdateStrategy, ctx context.Context, obj, old runtime.Object) error { objectMeta, kind, kerr := objectMetaAndKind(strategy, obj) if kerr != nil { @@ -92,9 +93,10 @@ func BeforeUpdate(strategy RESTUpdateStrategy, ctx context.Context, obj, old run if !ValidNamespace(ctx, objectMeta) { return errors.NewBadRequest("the namespace of the provided object does not match the namespace sent on the request") } - } else { + } else if len(objectMeta.GetNamespace()) > 0 { objectMeta.SetNamespace(metav1.NamespaceNone) } + // Ensure requests cannot update generation oldMeta, err := meta.Accessor(old) if err != nil { @@ -111,7 +113,9 @@ func BeforeUpdate(strategy RESTUpdateStrategy, ctx context.Context, obj, old run strategy.PrepareForUpdate(ctx, obj, old) // ClusterName is ignored and should not be saved - objectMeta.SetClusterName("") + if len(objectMeta.GetClusterName()) > 0 { + objectMeta.SetClusterName("") + } // Use the existing UID if none is provided if len(objectMeta.GetUID()) == 0 { objectMeta.SetUID(oldMeta.GetUID())