strategy.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 deployment
  14. import (
  15. "context"
  16. appsv1beta1 "k8s.io/api/apps/v1beta1"
  17. appsv1beta2 "k8s.io/api/apps/v1beta2"
  18. extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
  19. apiequality "k8s.io/apimachinery/pkg/api/equality"
  20. apivalidation "k8s.io/apimachinery/pkg/api/validation"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/apimachinery/pkg/runtime/schema"
  23. "k8s.io/apimachinery/pkg/util/validation/field"
  24. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  25. "k8s.io/apiserver/pkg/registry/rest"
  26. "k8s.io/apiserver/pkg/storage/names"
  27. "k8s.io/kubernetes/pkg/api/legacyscheme"
  28. "k8s.io/kubernetes/pkg/api/pod"
  29. "k8s.io/kubernetes/pkg/apis/apps"
  30. "k8s.io/kubernetes/pkg/apis/apps/validation"
  31. corevalidation "k8s.io/kubernetes/pkg/apis/core/validation"
  32. )
  33. // deploymentStrategy implements behavior for Deployments.
  34. type deploymentStrategy struct {
  35. runtime.ObjectTyper
  36. names.NameGenerator
  37. }
  38. // Strategy is the default logic that applies when creating and updating Deployment
  39. // objects via the REST API.
  40. var Strategy = deploymentStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  41. // DefaultGarbageCollectionPolicy returns OrphanDependents for extensions/v1beta1, apps/v1beta1, and apps/v1beta2 for backwards compatibility,
  42. // and DeleteDependents for all other versions.
  43. func (deploymentStrategy) DefaultGarbageCollectionPolicy(ctx context.Context) rest.GarbageCollectionPolicy {
  44. var groupVersion schema.GroupVersion
  45. if requestInfo, found := genericapirequest.RequestInfoFrom(ctx); found {
  46. groupVersion = schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}
  47. }
  48. switch groupVersion {
  49. case extensionsv1beta1.SchemeGroupVersion, appsv1beta1.SchemeGroupVersion, appsv1beta2.SchemeGroupVersion:
  50. // for back compatibility
  51. return rest.OrphanDependents
  52. default:
  53. return rest.DeleteDependents
  54. }
  55. }
  56. // NamespaceScoped is true for deployment.
  57. func (deploymentStrategy) NamespaceScoped() bool {
  58. return true
  59. }
  60. // PrepareForCreate clears fields that are not allowed to be set by end users on creation.
  61. func (deploymentStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  62. deployment := obj.(*apps.Deployment)
  63. deployment.Status = apps.DeploymentStatus{}
  64. deployment.Generation = 1
  65. pod.DropDisabledTemplateFields(&deployment.Spec.Template, nil)
  66. }
  67. // Validate validates a new deployment.
  68. func (deploymentStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  69. deployment := obj.(*apps.Deployment)
  70. allErrs := validation.ValidateDeployment(deployment)
  71. allErrs = append(allErrs, corevalidation.ValidateConditionalPodTemplate(&deployment.Spec.Template, nil, field.NewPath("spec.template"))...)
  72. return allErrs
  73. }
  74. // Canonicalize normalizes the object after validation.
  75. func (deploymentStrategy) Canonicalize(obj runtime.Object) {
  76. }
  77. // AllowCreateOnUpdate is false for deployments.
  78. func (deploymentStrategy) AllowCreateOnUpdate() bool {
  79. return false
  80. }
  81. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  82. func (deploymentStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  83. newDeployment := obj.(*apps.Deployment)
  84. oldDeployment := old.(*apps.Deployment)
  85. newDeployment.Status = oldDeployment.Status
  86. pod.DropDisabledTemplateFields(&newDeployment.Spec.Template, &oldDeployment.Spec.Template)
  87. // Spec updates bump the generation so that we can distinguish between
  88. // scaling events and template changes, annotation updates bump the generation
  89. // because annotations are copied from deployments to their replica sets.
  90. if !apiequality.Semantic.DeepEqual(newDeployment.Spec, oldDeployment.Spec) ||
  91. !apiequality.Semantic.DeepEqual(newDeployment.Annotations, oldDeployment.Annotations) {
  92. newDeployment.Generation = oldDeployment.Generation + 1
  93. }
  94. }
  95. // ValidateUpdate is the default update validation for an end user.
  96. func (deploymentStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  97. newDeployment := obj.(*apps.Deployment)
  98. oldDeployment := old.(*apps.Deployment)
  99. allErrs := validation.ValidateDeploymentUpdate(newDeployment, oldDeployment)
  100. allErrs = append(allErrs, corevalidation.ValidateConditionalPodTemplate(&newDeployment.Spec.Template, &oldDeployment.Spec.Template, field.NewPath("spec.template"))...)
  101. // Update is not allowed to set Spec.Selector for all groups/versions except extensions/v1beta1.
  102. // If RequestInfo is nil, it is better to revert to old behavior (i.e. allow update to set Spec.Selector)
  103. // to prevent unintentionally breaking users who may rely on the old behavior.
  104. // TODO(#50791): after apps/v1beta1 and extensions/v1beta1 are removed,
  105. // move selector immutability check inside ValidateDeploymentUpdate().
  106. if requestInfo, found := genericapirequest.RequestInfoFrom(ctx); found {
  107. groupVersion := schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}
  108. switch groupVersion {
  109. case appsv1beta1.SchemeGroupVersion, extensionsv1beta1.SchemeGroupVersion:
  110. // no-op for compatibility
  111. default:
  112. // disallow mutation of selector
  113. allErrs = append(allErrs, apivalidation.ValidateImmutableField(newDeployment.Spec.Selector, oldDeployment.Spec.Selector, field.NewPath("spec").Child("selector"))...)
  114. }
  115. }
  116. return allErrs
  117. }
  118. func (deploymentStrategy) AllowUnconditionalUpdate() bool {
  119. return true
  120. }
  121. type deploymentStatusStrategy struct {
  122. deploymentStrategy
  123. }
  124. // StatusStrategy is the default logic invoked when updating object status.
  125. var StatusStrategy = deploymentStatusStrategy{Strategy}
  126. // PrepareForUpdate clears fields that are not allowed to be set by end users on update of status
  127. func (deploymentStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  128. newDeployment := obj.(*apps.Deployment)
  129. oldDeployment := old.(*apps.Deployment)
  130. newDeployment.Spec = oldDeployment.Spec
  131. newDeployment.Labels = oldDeployment.Labels
  132. }
  133. // ValidateUpdate is the default update validation for an end user updating status
  134. func (deploymentStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  135. return validation.ValidateDeploymentStatusUpdate(obj.(*apps.Deployment), old.(*apps.Deployment))
  136. }