strategy.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 podpreset
  14. import (
  15. "context"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. "k8s.io/apiserver/pkg/storage/names"
  19. "k8s.io/kubernetes/pkg/api/legacyscheme"
  20. "k8s.io/kubernetes/pkg/apis/settings"
  21. "k8s.io/kubernetes/pkg/apis/settings/validation"
  22. )
  23. // podPresetStrategy implements verification logic for Pod Presets.
  24. type podPresetStrategy struct {
  25. runtime.ObjectTyper
  26. names.NameGenerator
  27. }
  28. // Strategy is the default logic that applies when creating and updating Pod Preset objects.
  29. var Strategy = podPresetStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  30. // NamespaceScoped returns true because all Pod Presets need to be within a namespace.
  31. func (podPresetStrategy) NamespaceScoped() bool {
  32. return true
  33. }
  34. // PrepareForCreate clears the status of a Pod Preset before creation.
  35. func (podPresetStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  36. pip := obj.(*settings.PodPreset)
  37. pip.Generation = 1
  38. }
  39. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  40. func (podPresetStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  41. newPodPreset := obj.(*settings.PodPreset)
  42. oldPodPreset := old.(*settings.PodPreset)
  43. // Update is not allowed
  44. newPodPreset.Spec = oldPodPreset.Spec
  45. }
  46. // Validate validates a new PodPreset.
  47. func (podPresetStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  48. pip := obj.(*settings.PodPreset)
  49. return validation.ValidatePodPreset(pip)
  50. }
  51. // Canonicalize normalizes the object after validation.
  52. func (podPresetStrategy) Canonicalize(obj runtime.Object) {}
  53. // AllowCreateOnUpdate is false for PodPreset; this means POST is needed to create one.
  54. func (podPresetStrategy) AllowCreateOnUpdate() bool {
  55. return false
  56. }
  57. // ValidateUpdate is the default update validation for an end user.
  58. func (podPresetStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  59. validationErrorList := validation.ValidatePodPreset(obj.(*settings.PodPreset))
  60. updateErrorList := validation.ValidatePodPresetUpdate(obj.(*settings.PodPreset), old.(*settings.PodPreset))
  61. return append(validationErrorList, updateErrorList...)
  62. }
  63. // AllowUnconditionalUpdate is the default update policy for Pod Preset objects.
  64. func (podPresetStrategy) AllowUnconditionalUpdate() bool {
  65. return true
  66. }