strategy.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright 2015 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 podsecuritypolicy
  14. import (
  15. "context"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. "k8s.io/apiserver/pkg/registry/rest"
  19. "k8s.io/apiserver/pkg/storage/names"
  20. "k8s.io/kubernetes/pkg/api/legacyscheme"
  21. psputil "k8s.io/kubernetes/pkg/api/podsecuritypolicy"
  22. "k8s.io/kubernetes/pkg/apis/policy"
  23. "k8s.io/kubernetes/pkg/apis/policy/validation"
  24. )
  25. // strategy implements behavior for PodSecurityPolicy objects
  26. type strategy struct {
  27. runtime.ObjectTyper
  28. names.NameGenerator
  29. }
  30. // Strategy is the default logic that applies when creating and updating PodSecurityPolicy
  31. // objects via the REST API.
  32. var Strategy = strategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  33. var _ = rest.RESTCreateStrategy(Strategy)
  34. var _ = rest.RESTUpdateStrategy(Strategy)
  35. func (strategy) NamespaceScoped() bool {
  36. return false
  37. }
  38. func (strategy) AllowCreateOnUpdate() bool {
  39. return false
  40. }
  41. func (strategy) AllowUnconditionalUpdate() bool {
  42. return true
  43. }
  44. func (strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  45. psp := obj.(*policy.PodSecurityPolicy)
  46. psputil.DropDisabledFields(&psp.Spec, nil)
  47. }
  48. func (strategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  49. newPsp := obj.(*policy.PodSecurityPolicy)
  50. oldPsp := old.(*policy.PodSecurityPolicy)
  51. psputil.DropDisabledFields(&newPsp.Spec, &oldPsp.Spec)
  52. }
  53. func (strategy) Canonicalize(obj runtime.Object) {
  54. }
  55. func (strategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  56. return validation.ValidatePodSecurityPolicy(obj.(*policy.PodSecurityPolicy))
  57. }
  58. func (strategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  59. return validation.ValidatePodSecurityPolicyUpdate(old.(*policy.PodSecurityPolicy), obj.(*policy.PodSecurityPolicy))
  60. }