validation.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "encoding/json"
  16. apivalidation "k8s.io/kubernetes/pkg/api/validation"
  17. "k8s.io/kubernetes/pkg/apis/autoscaling"
  18. "k8s.io/kubernetes/pkg/apis/extensions"
  19. "k8s.io/kubernetes/pkg/controller/podautoscaler"
  20. "k8s.io/kubernetes/pkg/util/validation/field"
  21. )
  22. func ValidateScale(scale *autoscaling.Scale) field.ErrorList {
  23. allErrs := field.ErrorList{}
  24. allErrs = append(allErrs, apivalidation.ValidateObjectMeta(&scale.ObjectMeta, true, apivalidation.NameIsDNSSubdomain, field.NewPath("metadata"))...)
  25. if scale.Spec.Replicas < 0 {
  26. allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "replicas"), scale.Spec.Replicas, "must be greater than or equal to 0"))
  27. }
  28. return allErrs
  29. }
  30. // ValidateHorizontalPodAutoscaler can be used to check whether the given autoscaler name is valid.
  31. // Prefix indicates this name will be used as part of generation, in which case trailing dashes are allowed.
  32. var ValidateHorizontalPodAutoscalerName = apivalidation.ValidateReplicationControllerName
  33. func validateHorizontalPodAutoscalerSpec(autoscaler autoscaling.HorizontalPodAutoscalerSpec, fldPath *field.Path) field.ErrorList {
  34. allErrs := field.ErrorList{}
  35. if autoscaler.MinReplicas != nil && *autoscaler.MinReplicas < 1 {
  36. allErrs = append(allErrs, field.Invalid(fldPath.Child("minReplicas"), *autoscaler.MinReplicas, "must be greater than 0"))
  37. }
  38. if autoscaler.MaxReplicas < 1 {
  39. allErrs = append(allErrs, field.Invalid(fldPath.Child("maxReplicas"), autoscaler.MaxReplicas, "must be greater than 0"))
  40. }
  41. if autoscaler.MinReplicas != nil && autoscaler.MaxReplicas < *autoscaler.MinReplicas {
  42. allErrs = append(allErrs, field.Invalid(fldPath.Child("maxReplicas"), autoscaler.MaxReplicas, "must be greater than or equal to `minReplicas`"))
  43. }
  44. if autoscaler.TargetCPUUtilizationPercentage != nil && *autoscaler.TargetCPUUtilizationPercentage < 1 {
  45. allErrs = append(allErrs, field.Invalid(fldPath.Child("targetCPUUtilizationPercentage"), autoscaler.TargetCPUUtilizationPercentage, "must be greater than 0"))
  46. }
  47. if refErrs := ValidateCrossVersionObjectReference(autoscaler.ScaleTargetRef, fldPath.Child("scaleTargetRef")); len(refErrs) > 0 {
  48. allErrs = append(allErrs, refErrs...)
  49. }
  50. return allErrs
  51. }
  52. func ValidateCrossVersionObjectReference(ref autoscaling.CrossVersionObjectReference, fldPath *field.Path) field.ErrorList {
  53. allErrs := field.ErrorList{}
  54. if len(ref.Kind) == 0 {
  55. allErrs = append(allErrs, field.Required(fldPath.Child("kind"), ""))
  56. } else {
  57. for _, msg := range apivalidation.IsValidPathSegmentName(ref.Kind) {
  58. allErrs = append(allErrs, field.Invalid(fldPath.Child("kind"), ref.Kind, msg))
  59. }
  60. }
  61. if len(ref.Name) == 0 {
  62. allErrs = append(allErrs, field.Required(fldPath.Child("name"), ""))
  63. } else {
  64. for _, msg := range apivalidation.IsValidPathSegmentName(ref.Name) {
  65. allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), ref.Name, msg))
  66. }
  67. }
  68. return allErrs
  69. }
  70. func validateHorizontalPodAutoscalerAnnotations(annotations map[string]string, fldPath *field.Path) field.ErrorList {
  71. allErrs := field.ErrorList{}
  72. if annotationValue, found := annotations[podautoscaler.HpaCustomMetricsTargetAnnotationName]; found {
  73. // Try to parse the annotation
  74. var targetList extensions.CustomMetricTargetList
  75. if err := json.Unmarshal([]byte(annotationValue), &targetList); err != nil {
  76. allErrs = append(allErrs, field.Invalid(fldPath.Child("annotations"), annotations, "failed to parse custom metrics target annotation"))
  77. } else {
  78. if len(targetList.Items) == 0 {
  79. allErrs = append(allErrs, field.Required(fldPath.Child("annotations", "items"), "custom metrics target must not be empty"))
  80. }
  81. for _, target := range targetList.Items {
  82. if target.Name == "" {
  83. allErrs = append(allErrs, field.Required(fldPath.Child("annotations", "items", "name"), "missing custom metric target name"))
  84. }
  85. if target.TargetValue.MilliValue() <= 0 {
  86. allErrs = append(allErrs, field.Invalid(fldPath.Child("annotations", "items", "value"), target.TargetValue, "custom metric target value must be greater than 0"))
  87. }
  88. }
  89. }
  90. }
  91. return allErrs
  92. }
  93. func ValidateHorizontalPodAutoscaler(autoscaler *autoscaling.HorizontalPodAutoscaler) field.ErrorList {
  94. allErrs := apivalidation.ValidateObjectMeta(&autoscaler.ObjectMeta, true, ValidateHorizontalPodAutoscalerName, field.NewPath("metadata"))
  95. allErrs = append(allErrs, validateHorizontalPodAutoscalerSpec(autoscaler.Spec, field.NewPath("spec"))...)
  96. allErrs = append(allErrs, validateHorizontalPodAutoscalerAnnotations(autoscaler.Annotations, field.NewPath("metadata"))...)
  97. return allErrs
  98. }
  99. func ValidateHorizontalPodAutoscalerUpdate(newAutoscaler, oldAutoscaler *autoscaling.HorizontalPodAutoscaler) field.ErrorList {
  100. allErrs := apivalidation.ValidateObjectMetaUpdate(&newAutoscaler.ObjectMeta, &oldAutoscaler.ObjectMeta, field.NewPath("metadata"))
  101. allErrs = append(allErrs, validateHorizontalPodAutoscalerSpec(newAutoscaler.Spec, field.NewPath("spec"))...)
  102. return allErrs
  103. }
  104. func ValidateHorizontalPodAutoscalerStatusUpdate(newAutoscaler, oldAutoscaler *autoscaling.HorizontalPodAutoscaler) field.ErrorList {
  105. allErrs := apivalidation.ValidateObjectMetaUpdate(&newAutoscaler.ObjectMeta, &oldAutoscaler.ObjectMeta, field.NewPath("metadata"))
  106. status := newAutoscaler.Status
  107. allErrs = append(allErrs, apivalidation.ValidateNonnegativeField(int64(status.CurrentReplicas), field.NewPath("status", "currentReplicas"))...)
  108. allErrs = append(allErrs, apivalidation.ValidateNonnegativeField(int64(status.DesiredReplicas), field.NewPath("status", "desiredReplicasa"))...)
  109. return allErrs
  110. }