strategy.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 mutatingwebhookconfiguration
  14. import (
  15. "context"
  16. "reflect"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. "k8s.io/apimachinery/pkg/util/validation/field"
  20. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  21. "k8s.io/apiserver/pkg/storage/names"
  22. "k8s.io/kubernetes/pkg/api/legacyscheme"
  23. "k8s.io/kubernetes/pkg/apis/admissionregistration"
  24. "k8s.io/kubernetes/pkg/apis/admissionregistration/validation"
  25. )
  26. // mutatingWebhookConfigurationStrategy implements verification logic for mutatingWebhookConfiguration.
  27. type mutatingWebhookConfigurationStrategy struct {
  28. runtime.ObjectTyper
  29. names.NameGenerator
  30. }
  31. // Strategy is the default logic that applies when creating and updating mutatingWebhookConfiguration objects.
  32. var Strategy = mutatingWebhookConfigurationStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  33. // NamespaceScoped returns true because all mutatingWebhookConfiguration' need to be within a namespace.
  34. func (mutatingWebhookConfigurationStrategy) NamespaceScoped() bool {
  35. return false
  36. }
  37. // PrepareForCreate clears the status of an mutatingWebhookConfiguration before creation.
  38. func (mutatingWebhookConfigurationStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  39. ic := obj.(*admissionregistration.MutatingWebhookConfiguration)
  40. ic.Generation = 1
  41. }
  42. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  43. func (mutatingWebhookConfigurationStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  44. newIC := obj.(*admissionregistration.MutatingWebhookConfiguration)
  45. oldIC := old.(*admissionregistration.MutatingWebhookConfiguration)
  46. // Any changes to the spec increment the generation number, any changes to the
  47. // status should reflect the generation number of the corresponding object.
  48. // See metav1.ObjectMeta description for more information on Generation.
  49. if !reflect.DeepEqual(oldIC.Webhooks, newIC.Webhooks) {
  50. newIC.Generation = oldIC.Generation + 1
  51. }
  52. }
  53. // Validate validates a new mutatingWebhookConfiguration.
  54. func (mutatingWebhookConfigurationStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  55. var groupVersion schema.GroupVersion
  56. if requestInfo, found := genericapirequest.RequestInfoFrom(ctx); found {
  57. groupVersion = schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}
  58. }
  59. ic := obj.(*admissionregistration.MutatingWebhookConfiguration)
  60. return validation.ValidateMutatingWebhookConfiguration(ic, groupVersion)
  61. }
  62. // Canonicalize normalizes the object after validation.
  63. func (mutatingWebhookConfigurationStrategy) Canonicalize(obj runtime.Object) {
  64. }
  65. // AllowCreateOnUpdate is true for mutatingWebhookConfiguration; this means you may create one with a PUT request.
  66. func (mutatingWebhookConfigurationStrategy) AllowCreateOnUpdate() bool {
  67. return false
  68. }
  69. // ValidateUpdate is the default update validation for an end user.
  70. func (mutatingWebhookConfigurationStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  71. var groupVersion schema.GroupVersion
  72. if requestInfo, found := genericapirequest.RequestInfoFrom(ctx); found {
  73. groupVersion = schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}
  74. }
  75. return validation.ValidateMutatingWebhookConfigurationUpdate(obj.(*admissionregistration.MutatingWebhookConfiguration), old.(*admissionregistration.MutatingWebhookConfiguration), groupVersion)
  76. }
  77. // AllowUnconditionalUpdate is the default update policy for mutatingWebhookConfiguration objects. Status update should
  78. // only be allowed if version match.
  79. func (mutatingWebhookConfigurationStrategy) AllowUnconditionalUpdate() bool {
  80. return false
  81. }