validation.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright 2016 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 validation
  14. import (
  15. "reflect"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. unversionedvalidation "k8s.io/kubernetes/pkg/api/unversioned/validation"
  19. apivalidation "k8s.io/kubernetes/pkg/api/validation"
  20. "k8s.io/kubernetes/pkg/apis/apps"
  21. "k8s.io/kubernetes/pkg/labels"
  22. "k8s.io/kubernetes/pkg/util/validation/field"
  23. )
  24. // ValidatePetSetName can be used to check whether the given PetSet name is valid.
  25. // Prefix indicates this name will be used as part of generation, in which case
  26. // trailing dashes are allowed.
  27. func ValidatePetSetName(name string, prefix bool) []string {
  28. // TODO: Validate that there's name for the suffix inserted by the pets.
  29. // Currently this is just "-index". In the future we may allow a user
  30. // specified list of suffixes and we need to validate the longest one.
  31. return apivalidation.NameIsDNSSubdomain(name, prefix)
  32. }
  33. // Validates the given template and ensures that it is in accordance with the desired selector.
  34. func ValidatePodTemplateSpecForPetSet(template *api.PodTemplateSpec, selector labels.Selector, fldPath *field.Path) field.ErrorList {
  35. allErrs := field.ErrorList{}
  36. if template == nil {
  37. allErrs = append(allErrs, field.Required(fldPath, ""))
  38. } else {
  39. if !selector.Empty() {
  40. // Verify that the PetSet selector matches the labels in template.
  41. labels := labels.Set(template.Labels)
  42. if !selector.Matches(labels) {
  43. allErrs = append(allErrs, field.Invalid(fldPath.Child("metadata", "labels"), template.Labels, "`selector` does not match template `labels`"))
  44. }
  45. }
  46. // TODO: Add validation for PodSpec, currently this will check volumes, which we know will
  47. // fail. We should really check that the union of the given volumes and volumeClaims match
  48. // volume mounts in the containers.
  49. // allErrs = append(allErrs, apivalidation.ValidatePodTemplateSpec(template, fldPath)...)
  50. allErrs = append(allErrs, unversionedvalidation.ValidateLabels(template.Labels, fldPath.Child("labels"))...)
  51. allErrs = append(allErrs, apivalidation.ValidateAnnotations(template.Annotations, fldPath.Child("annotations"))...)
  52. allErrs = append(allErrs, apivalidation.ValidatePodSpecificAnnotations(template.Annotations, &template.Spec, fldPath.Child("annotations"))...)
  53. }
  54. return allErrs
  55. }
  56. // ValidatePetSetSpec tests if required fields in the PetSet spec are set.
  57. func ValidatePetSetSpec(spec *apps.PetSetSpec, fldPath *field.Path) field.ErrorList {
  58. allErrs := field.ErrorList{}
  59. allErrs = append(allErrs, apivalidation.ValidateNonnegativeField(int64(spec.Replicas), fldPath.Child("replicas"))...)
  60. if spec.Selector == nil {
  61. allErrs = append(allErrs, field.Required(fldPath.Child("selector"), ""))
  62. } else {
  63. allErrs = append(allErrs, unversionedvalidation.ValidateLabelSelector(spec.Selector, fldPath.Child("selector"))...)
  64. if len(spec.Selector.MatchLabels)+len(spec.Selector.MatchExpressions) == 0 {
  65. allErrs = append(allErrs, field.Invalid(fldPath.Child("selector"), spec.Selector, "empty selector is not valid for petset."))
  66. }
  67. }
  68. selector, err := unversioned.LabelSelectorAsSelector(spec.Selector)
  69. if err != nil {
  70. allErrs = append(allErrs, field.Invalid(fldPath.Child("selector"), spec.Selector, ""))
  71. } else {
  72. allErrs = append(allErrs, ValidatePodTemplateSpecForPetSet(&spec.Template, selector, fldPath.Child("template"))...)
  73. }
  74. if spec.Template.Spec.RestartPolicy != api.RestartPolicyAlways {
  75. allErrs = append(allErrs, field.NotSupported(fldPath.Child("template", "spec", "restartPolicy"), spec.Template.Spec.RestartPolicy, []string{string(api.RestartPolicyAlways)}))
  76. }
  77. return allErrs
  78. }
  79. // ValidatePetSet validates a PetSet.
  80. func ValidatePetSet(petSet *apps.PetSet) field.ErrorList {
  81. allErrs := apivalidation.ValidateObjectMeta(&petSet.ObjectMeta, true, ValidatePetSetName, field.NewPath("metadata"))
  82. allErrs = append(allErrs, ValidatePetSetSpec(&petSet.Spec, field.NewPath("spec"))...)
  83. return allErrs
  84. }
  85. // ValidatePetSetUpdate tests if required fields in the PetSet are set.
  86. func ValidatePetSetUpdate(petSet, oldPetSet *apps.PetSet) field.ErrorList {
  87. allErrs := apivalidation.ValidateObjectMetaUpdate(&petSet.ObjectMeta, &oldPetSet.ObjectMeta, field.NewPath("metadata"))
  88. // TODO: For now we're taking the safe route and disallowing all updates to
  89. // spec except for Replicas, for scaling, and Template.Spec.containers.image
  90. // for rolling-update. Enable others on a case by case basis.
  91. restoreReplicas := petSet.Spec.Replicas
  92. petSet.Spec.Replicas = oldPetSet.Spec.Replicas
  93. restoreContainers := petSet.Spec.Template.Spec.Containers
  94. petSet.Spec.Template.Spec.Containers = oldPetSet.Spec.Template.Spec.Containers
  95. if !reflect.DeepEqual(petSet.Spec, oldPetSet.Spec) {
  96. allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "updates to petset spec for fields other than 'replicas' are forbidden."))
  97. }
  98. petSet.Spec.Replicas = restoreReplicas
  99. petSet.Spec.Template.Spec.Containers = restoreContainers
  100. allErrs = append(allErrs, apivalidation.ValidateNonnegativeField(int64(petSet.Spec.Replicas), field.NewPath("spec", "replicas"))...)
  101. containerErrs, _ := apivalidation.ValidateContainerUpdates(petSet.Spec.Template.Spec.Containers, oldPetSet.Spec.Template.Spec.Containers, field.NewPath("spec").Child("template").Child("containers"))
  102. allErrs = append(allErrs, containerErrs...)
  103. return allErrs
  104. }
  105. // ValidatePetSetStatusUpdate tests if required fields in the PetSet are set.
  106. func ValidatePetSetStatusUpdate(petSet, oldPetSet *apps.PetSet) field.ErrorList {
  107. allErrs := field.ErrorList{}
  108. allErrs = append(allErrs, apivalidation.ValidateObjectMetaUpdate(&petSet.ObjectMeta, &oldPetSet.ObjectMeta, field.NewPath("metadata"))...)
  109. // TODO: Validate status.
  110. return allErrs
  111. }