strategy.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package validatingwebhookconfiguration
  14. import (
  15. "context"
  16. "reflect"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/util/validation/field"
  19. "k8s.io/apiserver/pkg/storage/names"
  20. "k8s.io/kubernetes/pkg/api/legacyscheme"
  21. "k8s.io/kubernetes/pkg/apis/admissionregistration"
  22. "k8s.io/kubernetes/pkg/apis/admissionregistration/validation"
  23. )
  24. // validatingWebhookConfigurationStrategy implements verification logic for validatingWebhookConfiguration.
  25. type validatingWebhookConfigurationStrategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating validatingWebhookConfiguration objects.
  30. var Strategy = validatingWebhookConfigurationStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  31. // NamespaceScoped returns true because all validatingWebhookConfiguration' need to be within a namespace.
  32. func (validatingWebhookConfigurationStrategy) NamespaceScoped() bool {
  33. return false
  34. }
  35. // PrepareForCreate clears the status of an validatingWebhookConfiguration before creation.
  36. func (validatingWebhookConfigurationStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  37. ic := obj.(*admissionregistration.ValidatingWebhookConfiguration)
  38. ic.Generation = 1
  39. }
  40. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  41. func (validatingWebhookConfigurationStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  42. newIC := obj.(*admissionregistration.ValidatingWebhookConfiguration)
  43. oldIC := old.(*admissionregistration.ValidatingWebhookConfiguration)
  44. // Any changes to the spec increment the generation number, any changes to the
  45. // status should reflect the generation number of the corresponding object.
  46. // See metav1.ObjectMeta description for more information on Generation.
  47. if !reflect.DeepEqual(oldIC.Webhooks, newIC.Webhooks) {
  48. newIC.Generation = oldIC.Generation + 1
  49. }
  50. }
  51. // Validate validates a new validatingWebhookConfiguration.
  52. func (validatingWebhookConfigurationStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  53. return validation.ValidateValidatingWebhookConfiguration(obj.(*admissionregistration.ValidatingWebhookConfiguration))
  54. }
  55. // Canonicalize normalizes the object after validation.
  56. func (validatingWebhookConfigurationStrategy) Canonicalize(obj runtime.Object) {
  57. }
  58. // AllowCreateOnUpdate is true for validatingWebhookConfiguration; this means you may create one with a PUT request.
  59. func (validatingWebhookConfigurationStrategy) AllowCreateOnUpdate() bool {
  60. return false
  61. }
  62. // ValidateUpdate is the default update validation for an end user.
  63. func (validatingWebhookConfigurationStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  64. return validation.ValidateValidatingWebhookConfigurationUpdate(obj.(*admissionregistration.ValidatingWebhookConfiguration), old.(*admissionregistration.ValidatingWebhookConfiguration))
  65. }
  66. // AllowUnconditionalUpdate is the default update policy for validatingWebhookConfiguration objects. Status update should
  67. // only be allowed if version match.
  68. func (validatingWebhookConfigurationStrategy) AllowUnconditionalUpdate() bool {
  69. return false
  70. }