strategy.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 statefulset
  14. import (
  15. "context"
  16. appsv1beta1 "k8s.io/api/apps/v1beta1"
  17. appsv1beta2 "k8s.io/api/apps/v1beta2"
  18. apiequality "k8s.io/apimachinery/pkg/api/equality"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/runtime/schema"
  21. "k8s.io/apimachinery/pkg/util/validation/field"
  22. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  23. "k8s.io/apiserver/pkg/registry/rest"
  24. "k8s.io/apiserver/pkg/storage/names"
  25. "k8s.io/kubernetes/pkg/api/legacyscheme"
  26. "k8s.io/kubernetes/pkg/api/pod"
  27. "k8s.io/kubernetes/pkg/apis/apps"
  28. "k8s.io/kubernetes/pkg/apis/apps/validation"
  29. corevalidation "k8s.io/kubernetes/pkg/apis/core/validation"
  30. )
  31. // statefulSetStrategy implements verification logic for Replication StatefulSets.
  32. type statefulSetStrategy struct {
  33. runtime.ObjectTyper
  34. names.NameGenerator
  35. }
  36. // Strategy is the default logic that applies when creating and updating Replication StatefulSet objects.
  37. var Strategy = statefulSetStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  38. // DefaultGarbageCollectionPolicy returns OrphanDependents for apps/v1beta1 and apps/v1beta2 for backwards compatibility,
  39. // and DeleteDependents for all other versions.
  40. func (statefulSetStrategy) DefaultGarbageCollectionPolicy(ctx context.Context) rest.GarbageCollectionPolicy {
  41. var groupVersion schema.GroupVersion
  42. if requestInfo, found := genericapirequest.RequestInfoFrom(ctx); found {
  43. groupVersion = schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}
  44. }
  45. switch groupVersion {
  46. case appsv1beta1.SchemeGroupVersion, appsv1beta2.SchemeGroupVersion:
  47. // for back compatibility
  48. return rest.OrphanDependents
  49. default:
  50. return rest.DeleteDependents
  51. }
  52. }
  53. // NamespaceScoped returns true because all StatefulSet' need to be within a namespace.
  54. func (statefulSetStrategy) NamespaceScoped() bool {
  55. return true
  56. }
  57. // PrepareForCreate clears the status of an StatefulSet before creation.
  58. func (statefulSetStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  59. statefulSet := obj.(*apps.StatefulSet)
  60. // create cannot set status
  61. statefulSet.Status = apps.StatefulSetStatus{}
  62. statefulSet.Generation = 1
  63. pod.DropDisabledTemplateFields(&statefulSet.Spec.Template, nil)
  64. }
  65. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  66. func (statefulSetStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  67. newStatefulSet := obj.(*apps.StatefulSet)
  68. oldStatefulSet := old.(*apps.StatefulSet)
  69. // Update is not allowed to set status
  70. newStatefulSet.Status = oldStatefulSet.Status
  71. pod.DropDisabledTemplateFields(&newStatefulSet.Spec.Template, &oldStatefulSet.Spec.Template)
  72. // Any changes to the spec increment the generation number, any changes to the
  73. // status should reflect the generation number of the corresponding object.
  74. // See metav1.ObjectMeta description for more information on Generation.
  75. if !apiequality.Semantic.DeepEqual(oldStatefulSet.Spec, newStatefulSet.Spec) {
  76. newStatefulSet.Generation = oldStatefulSet.Generation + 1
  77. }
  78. }
  79. // Validate validates a new StatefulSet.
  80. func (statefulSetStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  81. statefulSet := obj.(*apps.StatefulSet)
  82. allErrs := validation.ValidateStatefulSet(statefulSet)
  83. allErrs = append(allErrs, corevalidation.ValidateConditionalPodTemplate(&statefulSet.Spec.Template, nil, field.NewPath("spec.template"))...)
  84. return allErrs
  85. }
  86. // Canonicalize normalizes the object after validation.
  87. func (statefulSetStrategy) Canonicalize(obj runtime.Object) {
  88. }
  89. // AllowCreateOnUpdate is false for StatefulSet; this means POST is needed to create one.
  90. func (statefulSetStrategy) AllowCreateOnUpdate() bool {
  91. return false
  92. }
  93. // ValidateUpdate is the default update validation for an end user.
  94. func (statefulSetStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  95. newStatefulSet := obj.(*apps.StatefulSet)
  96. oldStatefulSet := old.(*apps.StatefulSet)
  97. validationErrorList := validation.ValidateStatefulSet(newStatefulSet)
  98. updateErrorList := validation.ValidateStatefulSetUpdate(newStatefulSet, oldStatefulSet)
  99. updateErrorList = append(updateErrorList, corevalidation.ValidateConditionalPodTemplate(&newStatefulSet.Spec.Template, &oldStatefulSet.Spec.Template, field.NewPath("spec.template"))...)
  100. return append(validationErrorList, updateErrorList...)
  101. }
  102. // AllowUnconditionalUpdate is the default update policy for StatefulSet objects.
  103. func (statefulSetStrategy) AllowUnconditionalUpdate() bool {
  104. return true
  105. }
  106. type statefulSetStatusStrategy struct {
  107. statefulSetStrategy
  108. }
  109. // StatusStrategy is the default logic invoked when updating object status.
  110. var StatusStrategy = statefulSetStatusStrategy{Strategy}
  111. // PrepareForUpdate clears fields that are not allowed to be set by end users on update of status
  112. func (statefulSetStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  113. newStatefulSet := obj.(*apps.StatefulSet)
  114. oldStatefulSet := old.(*apps.StatefulSet)
  115. // status changes are not allowed to update spec
  116. newStatefulSet.Spec = oldStatefulSet.Spec
  117. }
  118. // ValidateUpdate is the default update validation for an end user updating status
  119. func (statefulSetStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  120. // TODO: Validate status updates.
  121. return validation.ValidateStatefulSetStatusUpdate(obj.(*apps.StatefulSet), old.(*apps.StatefulSet))
  122. }