From 8dd82c2391503c10ec67cabc373997f464a58507 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Fri, 30 Apr 2021 21:46:54 -0400 Subject: [PATCH] Add WarningsOnCreate,WarningsOnUpdate Kubernetes-commit: 8c8a4cf3e4a18e97359ce750530a4fa27bbd3b88 --- pkg/registry/generic/registry/store_test.go | 6 +++++ pkg/registry/rest/create.go | 25 +++++++++++++++++++++ pkg/registry/rest/create_update.go | 20 +++++++++++++++++ pkg/registry/rest/update.go | 25 +++++++++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/pkg/registry/generic/registry/store_test.go b/pkg/registry/generic/registry/store_test.go index 308c338e7..8466a30fb 100644 --- a/pkg/registry/generic/registry/store_test.go +++ b/pkg/registry/generic/registry/store_test.go @@ -115,9 +115,15 @@ func (t *testRESTStrategy) PrepareForUpdate(ctx context.Context, obj, old runtim func (t *testRESTStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { return nil } +func (t *testRESTStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { + return nil +} func (t *testRESTStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { return nil } +func (t *testRESTStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { + return nil +} func (t *testRESTStrategy) Canonicalize(obj runtime.Object) {} func NewTestGenericStoreRegistry(t *testing.T) (factory.DestroyFunc, *Store) { diff --git a/pkg/registry/rest/create.go b/pkg/registry/rest/create.go index dd70a4eb7..950260e45 100644 --- a/pkg/registry/rest/create.go +++ b/pkg/registry/rest/create.go @@ -31,6 +31,7 @@ import ( "k8s.io/apiserver/pkg/features" "k8s.io/apiserver/pkg/storage/names" utilfeature "k8s.io/apiserver/pkg/util/feature" + "k8s.io/apiserver/pkg/warning" ) // RESTCreateStrategy defines the minimum validation, accepted input, and @@ -59,6 +60,26 @@ type RESTCreateStrategy interface { // before the object is persisted. This method should not mutate the // object. Validate(ctx context.Context, obj runtime.Object) field.ErrorList + // WarningsOnCreate returns warnings to the client performing a create. + // WarningsOnCreate is invoked after default fields in the object have been filled in + // and after Validate has passed, before Canonicalize is called, and the object is persisted. + // This method must not mutate the object. + // + // Be brief; limit warnings to 120 characters if possible. + // Don't include a "Warning:" prefix in the message (that is added by clients on output). + // Warnings returned about a specific field should be formatted as "path.to.field: message". + // For example: `spec.imagePullSecrets[0].name: invalid empty name ""` + // + // Use warning messages to describe problems the client making the API request should correct or be aware of. + // For example: + // - use of deprecated fields/labels/annotations that will stop working in a future release + // - use of obsolete fields/labels/annotations that are non-functional + // - malformed or invalid specifications that prevent successful handling of the submitted object, + // but are not rejected by validation for compatibility reasons + // + // Warnings should not be returned for fields which cannot be resolved by the caller. + // For example, do not warn about spec fields in a subresource creation request. + WarningsOnCreate(ctx context.Context, obj runtime.Object) []string // Canonicalize allows an object to be mutated into a canonical form. This // ensures that code that operates on these objects can rely on the common // form for things like comparison. Canonicalize is invoked after @@ -113,6 +134,10 @@ func BeforeCreate(strategy RESTCreateStrategy, ctx context.Context, obj runtime. return errors.NewInvalid(kind.GroupKind(), objectMeta.GetName(), errs) } + for _, w := range strategy.WarningsOnCreate(ctx, obj) { + warning.AddWarning(ctx, "", w) + } + strategy.Canonicalize(obj) return nil diff --git a/pkg/registry/rest/create_update.go b/pkg/registry/rest/create_update.go index 37d6c8f8a..acef76fa6 100644 --- a/pkg/registry/rest/create_update.go +++ b/pkg/registry/rest/create_update.go @@ -39,6 +39,26 @@ type RESTCreateUpdateStrategy interface { // filled in before the object is persisted. This method should not mutate // the object. ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList + // WarningsOnUpdate returns warnings to the client performing the update. + // WarningsOnUpdate is invoked after default fields in the object have been filled in + // and after ValidateUpdate has passed, before Canonicalize is called, and before the object is persisted. + // This method must not mutate either object. + // + // Be brief; limit warnings to 120 characters if possible. + // Don't include a "Warning:" prefix in the message (that is added by clients on output). + // Warnings returned about a specific field should be formatted as "path.to.field: message". + // For example: `spec.imagePullSecrets[0].name: invalid empty name ""` + // + // Use warning messages to describe problems the client making the API request should correct or be aware of. + // For example: + // - use of deprecated fields/labels/annotations that will stop working in a future release + // - use of obsolete fields/labels/annotations that are non-functional + // - malformed or invalid specifications that prevent successful handling of the submitted object, + // but are not rejected by validation for compatibility reasons + // + // Warnings should not be returned for fields which cannot be resolved by the caller. + // For example, do not warn about spec fields in a status update. + WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string // AllowUnconditionalUpdate returns true if the object can be updated // unconditionally (irrespective of the latest resource version), when // there is no resource version specified in the object. diff --git a/pkg/registry/rest/update.go b/pkg/registry/rest/update.go index 0741b84ec..ffcca33c0 100644 --- a/pkg/registry/rest/update.go +++ b/pkg/registry/rest/update.go @@ -30,6 +30,7 @@ import ( "k8s.io/apiserver/pkg/admission" "k8s.io/apiserver/pkg/features" utilfeature "k8s.io/apiserver/pkg/util/feature" + "k8s.io/apiserver/pkg/warning" ) // RESTUpdateStrategy defines the minimum validation, accepted input, and @@ -51,6 +52,26 @@ type RESTUpdateStrategy interface { // filled in before the object is persisted. This method should not mutate // the object. ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList + // WarningsOnUpdate returns warnings to the client performing the update. + // WarningsOnUpdate is invoked after default fields in the object have been filled in + // and after ValidateUpdate has passed, before Canonicalize is called, and before the object is persisted. + // This method must not mutate either object. + // + // Be brief; limit warnings to 120 characters if possible. + // Don't include a "Warning:" prefix in the message (that is added by clients on output). + // Warnings returned about a specific field should be formatted as "path.to.field: message". + // For example: `spec.imagePullSecrets[0].name: invalid empty name ""` + // + // Use warning messages to describe problems the client making the API request should correct or be aware of. + // For example: + // - use of deprecated fields/labels/annotations that will stop working in a future release + // - use of obsolete fields/labels/annotations that are non-functional + // - malformed or invalid specifications that prevent successful handling of the submitted object, + // but are not rejected by validation for compatibility reasons + // + // Warnings should not be returned for fields which cannot be resolved by the caller. + // For example, do not warn about spec fields in a status update. + WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string // Canonicalize allows an object to be mutated into a canonical form. This // ensures that code that operates on these objects can rely on the common // form for things like comparison. Canonicalize is invoked after @@ -144,6 +165,10 @@ func BeforeUpdate(strategy RESTUpdateStrategy, ctx context.Context, obj, old run return errors.NewInvalid(kind.GroupKind(), objectMeta.GetName(), errs) } + for _, w := range strategy.WarningsOnUpdate(ctx, obj, old) { + warning.AddWarning(ctx, "", w) + } + strategy.Canonicalize(obj) return nil