strategy.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 podtemplate
  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/api/pod"
  21. api "k8s.io/kubernetes/pkg/apis/core"
  22. corevalidation "k8s.io/kubernetes/pkg/apis/core/validation"
  23. )
  24. // podTemplateStrategy implements behavior for PodTemplates
  25. type podTemplateStrategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating PodTemplate
  30. // objects via the REST API.
  31. var Strategy = podTemplateStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  32. // NamespaceScoped is true for pod templates.
  33. func (podTemplateStrategy) NamespaceScoped() bool {
  34. return true
  35. }
  36. // PrepareForCreate clears fields that are not allowed to be set by end users on creation.
  37. func (podTemplateStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  38. template := obj.(*api.PodTemplate)
  39. pod.DropDisabledTemplateFields(&template.Template, nil)
  40. }
  41. // Validate validates a new pod template.
  42. func (podTemplateStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  43. template := obj.(*api.PodTemplate)
  44. allErrs := corevalidation.ValidatePodTemplate(template)
  45. allErrs = append(allErrs, corevalidation.ValidateConditionalPodTemplate(&template.Template, nil, field.NewPath("template"))...)
  46. return allErrs
  47. }
  48. // Canonicalize normalizes the object after validation.
  49. func (podTemplateStrategy) Canonicalize(obj runtime.Object) {
  50. }
  51. // AllowCreateOnUpdate is false for pod templates.
  52. func (podTemplateStrategy) AllowCreateOnUpdate() bool {
  53. return false
  54. }
  55. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  56. func (podTemplateStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  57. newTemplate := obj.(*api.PodTemplate)
  58. oldTemplate := old.(*api.PodTemplate)
  59. pod.DropDisabledTemplateFields(&newTemplate.Template, &oldTemplate.Template)
  60. }
  61. // ValidateUpdate is the default update validation for an end user.
  62. func (podTemplateStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  63. template := obj.(*api.PodTemplate)
  64. oldTemplate := old.(*api.PodTemplate)
  65. allErrs := corevalidation.ValidatePodTemplateUpdate(template, oldTemplate)
  66. allErrs = append(allErrs, corevalidation.ValidateConditionalPodTemplate(&template.Template, &oldTemplate.Template, field.NewPath("template"))...)
  67. return allErrs
  68. }
  69. func (podTemplateStrategy) AllowUnconditionalUpdate() bool {
  70. return true
  71. }
  72. func (podTemplateStrategy) Export(ctx context.Context, obj runtime.Object, exact bool) error {
  73. // Do nothing
  74. return nil
  75. }