strategy.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 daemonset
  14. import (
  15. "context"
  16. appsv1beta2 "k8s.io/api/apps/v1beta2"
  17. extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
  18. apiequality "k8s.io/apimachinery/pkg/api/equality"
  19. apivalidation "k8s.io/apimachinery/pkg/api/validation"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/apimachinery/pkg/runtime/schema"
  22. "k8s.io/apimachinery/pkg/util/validation/field"
  23. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  24. "k8s.io/apiserver/pkg/registry/rest"
  25. "k8s.io/apiserver/pkg/storage/names"
  26. "k8s.io/kubernetes/pkg/api/legacyscheme"
  27. "k8s.io/kubernetes/pkg/api/pod"
  28. "k8s.io/kubernetes/pkg/apis/apps"
  29. "k8s.io/kubernetes/pkg/apis/apps/validation"
  30. corevalidation "k8s.io/kubernetes/pkg/apis/core/validation"
  31. )
  32. // daemonSetStrategy implements verification logic for daemon sets.
  33. type daemonSetStrategy struct {
  34. runtime.ObjectTyper
  35. names.NameGenerator
  36. }
  37. // Strategy is the default logic that applies when creating and updating DaemonSet objects.
  38. var Strategy = daemonSetStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  39. // DefaultGarbageCollectionPolicy returns OrphanDependents for extensions/v1beta1 and apps/v1beta2 for backwards compatibility,
  40. // and DeleteDependents for all other versions.
  41. func (daemonSetStrategy) DefaultGarbageCollectionPolicy(ctx context.Context) rest.GarbageCollectionPolicy {
  42. var groupVersion schema.GroupVersion
  43. if requestInfo, found := genericapirequest.RequestInfoFrom(ctx); found {
  44. groupVersion = schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}
  45. }
  46. switch groupVersion {
  47. case extensionsv1beta1.SchemeGroupVersion, appsv1beta2.SchemeGroupVersion:
  48. // for back compatibility
  49. return rest.OrphanDependents
  50. default:
  51. return rest.DeleteDependents
  52. }
  53. }
  54. // NamespaceScoped returns true because all DaemonSets need to be within a namespace.
  55. func (daemonSetStrategy) NamespaceScoped() bool {
  56. return true
  57. }
  58. // PrepareForCreate clears the status of a daemon set before creation.
  59. func (daemonSetStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  60. daemonSet := obj.(*apps.DaemonSet)
  61. daemonSet.Status = apps.DaemonSetStatus{}
  62. daemonSet.Generation = 1
  63. if daemonSet.Spec.TemplateGeneration < 1 {
  64. daemonSet.Spec.TemplateGeneration = 1
  65. }
  66. pod.DropDisabledTemplateFields(&daemonSet.Spec.Template, nil)
  67. }
  68. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  69. func (daemonSetStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  70. newDaemonSet := obj.(*apps.DaemonSet)
  71. oldDaemonSet := old.(*apps.DaemonSet)
  72. pod.DropDisabledTemplateFields(&newDaemonSet.Spec.Template, &oldDaemonSet.Spec.Template)
  73. // update is not allowed to set status
  74. newDaemonSet.Status = oldDaemonSet.Status
  75. // update is not allowed to set TemplateGeneration
  76. newDaemonSet.Spec.TemplateGeneration = oldDaemonSet.Spec.TemplateGeneration
  77. // Any changes to the spec increment the generation number, any changes to the
  78. // status should reflect the generation number of the corresponding object. We push
  79. // the burden of managing the status onto the clients because we can't (in general)
  80. // know here what version of spec the writer of the status has seen. It may seem like
  81. // we can at first -- since obj contains spec -- but in the future we will probably make
  82. // status its own object, and even if we don't, writes may be the result of a
  83. // read-update-write loop, so the contents of spec may not actually be the spec that
  84. // the manager has *seen*.
  85. //
  86. // TODO: Any changes to a part of the object that represents desired state (labels,
  87. // annotations etc) should also increment the generation.
  88. if !apiequality.Semantic.DeepEqual(oldDaemonSet.Spec.Template, newDaemonSet.Spec.Template) {
  89. newDaemonSet.Spec.TemplateGeneration = oldDaemonSet.Spec.TemplateGeneration + 1
  90. newDaemonSet.Generation = oldDaemonSet.Generation + 1
  91. return
  92. }
  93. if !apiequality.Semantic.DeepEqual(oldDaemonSet.Spec, newDaemonSet.Spec) {
  94. newDaemonSet.Generation = oldDaemonSet.Generation + 1
  95. }
  96. }
  97. // Validate validates a new daemon set.
  98. func (daemonSetStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  99. daemonSet := obj.(*apps.DaemonSet)
  100. allErrs := validation.ValidateDaemonSet(daemonSet)
  101. allErrs = append(allErrs, corevalidation.ValidateConditionalPodTemplate(&daemonSet.Spec.Template, nil, field.NewPath("spec.template"))...)
  102. return allErrs
  103. }
  104. // Canonicalize normalizes the object after validation.
  105. func (daemonSetStrategy) Canonicalize(obj runtime.Object) {
  106. }
  107. // AllowCreateOnUpdate is false for daemon set; this means a POST is
  108. // needed to create one
  109. func (daemonSetStrategy) AllowCreateOnUpdate() bool {
  110. return false
  111. }
  112. // ValidateUpdate is the default update validation for an end user.
  113. func (daemonSetStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  114. newDaemonSet := obj.(*apps.DaemonSet)
  115. oldDaemonSet := old.(*apps.DaemonSet)
  116. allErrs := validation.ValidateDaemonSet(obj.(*apps.DaemonSet))
  117. allErrs = append(allErrs, validation.ValidateDaemonSetUpdate(newDaemonSet, oldDaemonSet)...)
  118. allErrs = append(allErrs, corevalidation.ValidateConditionalPodTemplate(&newDaemonSet.Spec.Template, &oldDaemonSet.Spec.Template, field.NewPath("spec.template"))...)
  119. // Update is not allowed to set Spec.Selector for apps/v1 and apps/v1beta2 (allowed for extensions/v1beta1).
  120. // If RequestInfo is nil, it is better to revert to old behavior (i.e. allow update to set Spec.Selector)
  121. // to prevent unintentionally breaking users who may rely on the old behavior.
  122. // TODO(#50791): after extensions/v1beta1 is removed, move selector immutability check inside ValidateDaemonSetUpdate().
  123. if requestInfo, found := genericapirequest.RequestInfoFrom(ctx); found {
  124. groupVersion := schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}
  125. switch groupVersion {
  126. case extensionsv1beta1.SchemeGroupVersion:
  127. // no-op for compatibility
  128. default:
  129. // disallow mutation of selector
  130. allErrs = append(allErrs, apivalidation.ValidateImmutableField(newDaemonSet.Spec.Selector, oldDaemonSet.Spec.Selector, field.NewPath("spec").Child("selector"))...)
  131. }
  132. }
  133. return allErrs
  134. }
  135. // AllowUnconditionalUpdate is the default update policy for daemon set objects.
  136. func (daemonSetStrategy) AllowUnconditionalUpdate() bool {
  137. return true
  138. }
  139. type daemonSetStatusStrategy struct {
  140. daemonSetStrategy
  141. }
  142. // StatusStrategy is the default logic invoked when updating object status.
  143. var StatusStrategy = daemonSetStatusStrategy{Strategy}
  144. func (daemonSetStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  145. newDaemonSet := obj.(*apps.DaemonSet)
  146. oldDaemonSet := old.(*apps.DaemonSet)
  147. newDaemonSet.Spec = oldDaemonSet.Spec
  148. }
  149. func (daemonSetStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  150. return validation.ValidateDaemonSetStatusUpdate(obj.(*apps.DaemonSet), old.(*apps.DaemonSet))
  151. }