validation.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. "strings"
  16. apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
  17. pathvalidation "k8s.io/apimachinery/pkg/api/validation/path"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. "k8s.io/apimachinery/pkg/util/validation/field"
  20. "k8s.io/kubernetes/pkg/apis/autoscaling"
  21. apivalidation "k8s.io/kubernetes/pkg/apis/core/validation"
  22. )
  23. func ValidateScale(scale *autoscaling.Scale) field.ErrorList {
  24. allErrs := field.ErrorList{}
  25. allErrs = append(allErrs, apivalidation.ValidateObjectMeta(&scale.ObjectMeta, true, apimachineryvalidation.NameIsDNSSubdomain, field.NewPath("metadata"))...)
  26. if scale.Spec.Replicas < 0 {
  27. allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "replicas"), scale.Spec.Replicas, "must be greater than or equal to 0"))
  28. }
  29. return allErrs
  30. }
  31. // ValidateHorizontalPodAutoscaler can be used to check whether the given autoscaler name is valid.
  32. // Prefix indicates this name will be used as part of generation, in which case trailing dashes are allowed.
  33. var ValidateHorizontalPodAutoscalerName = apivalidation.ValidateReplicationControllerName
  34. func validateHorizontalPodAutoscalerSpec(autoscaler autoscaling.HorizontalPodAutoscalerSpec, fldPath *field.Path) field.ErrorList {
  35. allErrs := field.ErrorList{}
  36. if autoscaler.MinReplicas != nil && *autoscaler.MinReplicas < 1 {
  37. allErrs = append(allErrs, field.Invalid(fldPath.Child("minReplicas"), *autoscaler.MinReplicas, "must be greater than 0"))
  38. }
  39. if autoscaler.MaxReplicas < 1 {
  40. allErrs = append(allErrs, field.Invalid(fldPath.Child("maxReplicas"), autoscaler.MaxReplicas, "must be greater than 0"))
  41. }
  42. if autoscaler.MinReplicas != nil && autoscaler.MaxReplicas < *autoscaler.MinReplicas {
  43. allErrs = append(allErrs, field.Invalid(fldPath.Child("maxReplicas"), autoscaler.MaxReplicas, "must be greater than or equal to `minReplicas`"))
  44. }
  45. if refErrs := ValidateCrossVersionObjectReference(autoscaler.ScaleTargetRef, fldPath.Child("scaleTargetRef")); len(refErrs) > 0 {
  46. allErrs = append(allErrs, refErrs...)
  47. }
  48. if refErrs := validateMetrics(autoscaler.Metrics, fldPath.Child("metrics")); len(refErrs) > 0 {
  49. allErrs = append(allErrs, refErrs...)
  50. }
  51. return allErrs
  52. }
  53. func ValidateCrossVersionObjectReference(ref autoscaling.CrossVersionObjectReference, fldPath *field.Path) field.ErrorList {
  54. allErrs := field.ErrorList{}
  55. if len(ref.Kind) == 0 {
  56. allErrs = append(allErrs, field.Required(fldPath.Child("kind"), ""))
  57. } else {
  58. for _, msg := range pathvalidation.IsValidPathSegmentName(ref.Kind) {
  59. allErrs = append(allErrs, field.Invalid(fldPath.Child("kind"), ref.Kind, msg))
  60. }
  61. }
  62. if len(ref.Name) == 0 {
  63. allErrs = append(allErrs, field.Required(fldPath.Child("name"), ""))
  64. } else {
  65. for _, msg := range pathvalidation.IsValidPathSegmentName(ref.Name) {
  66. allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), ref.Name, msg))
  67. }
  68. }
  69. return allErrs
  70. }
  71. func ValidateHorizontalPodAutoscaler(autoscaler *autoscaling.HorizontalPodAutoscaler) field.ErrorList {
  72. allErrs := apivalidation.ValidateObjectMeta(&autoscaler.ObjectMeta, true, ValidateHorizontalPodAutoscalerName, field.NewPath("metadata"))
  73. allErrs = append(allErrs, validateHorizontalPodAutoscalerSpec(autoscaler.Spec, field.NewPath("spec"))...)
  74. return allErrs
  75. }
  76. func ValidateHorizontalPodAutoscalerUpdate(newAutoscaler, oldAutoscaler *autoscaling.HorizontalPodAutoscaler) field.ErrorList {
  77. allErrs := apivalidation.ValidateObjectMetaUpdate(&newAutoscaler.ObjectMeta, &oldAutoscaler.ObjectMeta, field.NewPath("metadata"))
  78. allErrs = append(allErrs, validateHorizontalPodAutoscalerSpec(newAutoscaler.Spec, field.NewPath("spec"))...)
  79. return allErrs
  80. }
  81. func ValidateHorizontalPodAutoscalerStatusUpdate(newAutoscaler, oldAutoscaler *autoscaling.HorizontalPodAutoscaler) field.ErrorList {
  82. allErrs := apivalidation.ValidateObjectMetaUpdate(&newAutoscaler.ObjectMeta, &oldAutoscaler.ObjectMeta, field.NewPath("metadata"))
  83. status := newAutoscaler.Status
  84. allErrs = append(allErrs, apivalidation.ValidateNonnegativeField(int64(status.CurrentReplicas), field.NewPath("status", "currentReplicas"))...)
  85. allErrs = append(allErrs, apivalidation.ValidateNonnegativeField(int64(status.DesiredReplicas), field.NewPath("status", "desiredReplicasa"))...)
  86. return allErrs
  87. }
  88. func validateMetrics(metrics []autoscaling.MetricSpec, fldPath *field.Path) field.ErrorList {
  89. allErrs := field.ErrorList{}
  90. for i, metricSpec := range metrics {
  91. idxPath := fldPath.Index(i)
  92. if targetErrs := validateMetricSpec(metricSpec, idxPath); len(targetErrs) > 0 {
  93. allErrs = append(allErrs, targetErrs...)
  94. }
  95. }
  96. return allErrs
  97. }
  98. var validMetricSourceTypes = sets.NewString(string(autoscaling.ObjectMetricSourceType), string(autoscaling.PodsMetricSourceType), string(autoscaling.ResourceMetricSourceType), string(autoscaling.ExternalMetricSourceType))
  99. var validMetricSourceTypesList = validMetricSourceTypes.List()
  100. func validateMetricSpec(spec autoscaling.MetricSpec, fldPath *field.Path) field.ErrorList {
  101. allErrs := field.ErrorList{}
  102. if len(string(spec.Type)) == 0 {
  103. allErrs = append(allErrs, field.Required(fldPath.Child("type"), "must specify a metric source type"))
  104. }
  105. if !validMetricSourceTypes.Has(string(spec.Type)) {
  106. allErrs = append(allErrs, field.NotSupported(fldPath.Child("type"), spec.Type, validMetricSourceTypesList))
  107. }
  108. typesPresent := sets.NewString()
  109. if spec.Object != nil {
  110. typesPresent.Insert("object")
  111. if typesPresent.Len() == 1 {
  112. allErrs = append(allErrs, validateObjectSource(spec.Object, fldPath.Child("object"))...)
  113. }
  114. }
  115. if spec.External != nil {
  116. typesPresent.Insert("external")
  117. if typesPresent.Len() == 1 {
  118. allErrs = append(allErrs, validateExternalSource(spec.External, fldPath.Child("external"))...)
  119. }
  120. }
  121. if spec.Pods != nil {
  122. typesPresent.Insert("pods")
  123. if typesPresent.Len() == 1 {
  124. allErrs = append(allErrs, validatePodsSource(spec.Pods, fldPath.Child("pods"))...)
  125. }
  126. }
  127. if spec.Resource != nil {
  128. typesPresent.Insert("resource")
  129. if typesPresent.Len() == 1 {
  130. allErrs = append(allErrs, validateResourceSource(spec.Resource, fldPath.Child("resource"))...)
  131. }
  132. }
  133. expectedField := strings.ToLower(string(spec.Type))
  134. if !typesPresent.Has(expectedField) {
  135. allErrs = append(allErrs, field.Required(fldPath.Child(expectedField), "must populate information for the given metric source"))
  136. }
  137. if typesPresent.Len() != 1 {
  138. typesPresent.Delete(expectedField)
  139. for typ := range typesPresent {
  140. allErrs = append(allErrs, field.Forbidden(fldPath.Child(typ), "must populate the given metric source only"))
  141. }
  142. }
  143. return allErrs
  144. }
  145. func validateObjectSource(src *autoscaling.ObjectMetricSource, fldPath *field.Path) field.ErrorList {
  146. allErrs := field.ErrorList{}
  147. allErrs = append(allErrs, ValidateCrossVersionObjectReference(src.DescribedObject, fldPath.Child("describedObject"))...)
  148. allErrs = append(allErrs, validateMetricIdentifier(src.Metric, fldPath.Child("metric"))...)
  149. if &src.Target == nil {
  150. allErrs = append(allErrs, field.Required(fldPath.Child("target"), "must specify a metric target"))
  151. } else {
  152. allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)
  153. }
  154. if src.Target.Value == nil && src.Target.AverageValue == nil {
  155. allErrs = append(allErrs, field.Required(fldPath.Child("target").Child("averageValue"), "must set either a target value or averageValue"))
  156. }
  157. return allErrs
  158. }
  159. func validateExternalSource(src *autoscaling.ExternalMetricSource, fldPath *field.Path) field.ErrorList {
  160. allErrs := field.ErrorList{}
  161. allErrs = append(allErrs, validateMetricIdentifier(src.Metric, fldPath.Child("metric"))...)
  162. if &src.Target == nil {
  163. allErrs = append(allErrs, field.Required(fldPath.Child("target"), "must specify a metric target"))
  164. } else {
  165. allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)
  166. }
  167. if src.Target.Value == nil && src.Target.AverageValue == nil {
  168. allErrs = append(allErrs, field.Required(fldPath.Child("target").Child("averageValue"), "must set either a target value for metric or a per-pod target"))
  169. }
  170. if src.Target.Value != nil && src.Target.AverageValue != nil {
  171. allErrs = append(allErrs, field.Forbidden(fldPath.Child("target").Child("value"), "may not set both a target value for metric and a per-pod target"))
  172. }
  173. return allErrs
  174. }
  175. func validatePodsSource(src *autoscaling.PodsMetricSource, fldPath *field.Path) field.ErrorList {
  176. allErrs := field.ErrorList{}
  177. allErrs = append(allErrs, validateMetricIdentifier(src.Metric, fldPath.Child("metric"))...)
  178. if &src.Target == nil {
  179. allErrs = append(allErrs, field.Required(fldPath.Child("target"), "must specify a metric target"))
  180. } else {
  181. allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)
  182. }
  183. if src.Target.AverageValue == nil {
  184. allErrs = append(allErrs, field.Required(fldPath.Child("target").Child("averageValue"), "must specify a positive target averageValue"))
  185. }
  186. return allErrs
  187. }
  188. func validateResourceSource(src *autoscaling.ResourceMetricSource, fldPath *field.Path) field.ErrorList {
  189. allErrs := field.ErrorList{}
  190. if len(src.Name) == 0 {
  191. allErrs = append(allErrs, field.Required(fldPath.Child("name"), "must specify a resource name"))
  192. }
  193. if &src.Target == nil {
  194. allErrs = append(allErrs, field.Required(fldPath.Child("target"), "must specify a metric target"))
  195. } else {
  196. allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)
  197. }
  198. if src.Target.AverageUtilization == nil && src.Target.AverageValue == nil {
  199. allErrs = append(allErrs, field.Required(fldPath.Child("target").Child("averageUtilization"), "must set either a target raw value or a target utilization"))
  200. }
  201. if src.Target.AverageUtilization != nil && src.Target.AverageValue != nil {
  202. allErrs = append(allErrs, field.Forbidden(fldPath.Child("target").Child("averageValue"), "may not set both a target raw value and a target utilization"))
  203. }
  204. return allErrs
  205. }
  206. func validateMetricTarget(mt autoscaling.MetricTarget, fldPath *field.Path) field.ErrorList {
  207. allErrs := field.ErrorList{}
  208. if len(mt.Type) == 0 {
  209. allErrs = append(allErrs, field.Required(fldPath.Child("type"), "must specify a metric target type"))
  210. }
  211. if mt.Type != autoscaling.UtilizationMetricType &&
  212. mt.Type != autoscaling.ValueMetricType &&
  213. mt.Type != autoscaling.AverageValueMetricType {
  214. allErrs = append(allErrs, field.Invalid(fldPath.Child("type"), mt.Type, "must be either Utilization, Value, or AverageValue"))
  215. }
  216. if mt.Value != nil && mt.Value.Sign() != 1 {
  217. allErrs = append(allErrs, field.Invalid(fldPath.Child("value"), mt.Value, "must be positive"))
  218. }
  219. if mt.AverageValue != nil && mt.AverageValue.Sign() != 1 {
  220. allErrs = append(allErrs, field.Invalid(fldPath.Child("averageValue"), mt.AverageValue, "must be positive"))
  221. }
  222. if mt.AverageUtilization != nil && *mt.AverageUtilization < 1 {
  223. allErrs = append(allErrs, field.Invalid(fldPath.Child("averageUtilization"), mt.AverageUtilization, "must be greater than 0"))
  224. }
  225. return allErrs
  226. }
  227. func validateMetricIdentifier(id autoscaling.MetricIdentifier, fldPath *field.Path) field.ErrorList {
  228. allErrs := field.ErrorList{}
  229. if len(id.Name) == 0 {
  230. allErrs = append(allErrs, field.Required(fldPath.Child("name"), "must specify a metric name"))
  231. } else {
  232. for _, msg := range pathvalidation.IsValidPathSegmentName(id.Name) {
  233. allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), id.Name, msg))
  234. }
  235. }
  236. return allErrs
  237. }