validation.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. "fmt"
  16. "strings"
  17. apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
  18. pathvalidation "k8s.io/apimachinery/pkg/api/validation/path"
  19. "k8s.io/apimachinery/pkg/util/sets"
  20. "k8s.io/apimachinery/pkg/util/validation/field"
  21. utilfeature "k8s.io/apiserver/pkg/util/feature"
  22. "k8s.io/kubernetes/pkg/apis/autoscaling"
  23. apivalidation "k8s.io/kubernetes/pkg/apis/core/validation"
  24. "k8s.io/kubernetes/pkg/features"
  25. )
  26. const (
  27. // MaxPeriodSeconds is the largest allowed scaling policy period (in seconds)
  28. MaxPeriodSeconds int32 = 1800
  29. // MaxStabilizationWindowSeconds is the largest allowed stabilization window (in seconds)
  30. MaxStabilizationWindowSeconds int32 = 3600
  31. )
  32. // ValidateScale validates a Scale and returns an ErrorList with any errors.
  33. func ValidateScale(scale *autoscaling.Scale) field.ErrorList {
  34. allErrs := field.ErrorList{}
  35. allErrs = append(allErrs, apivalidation.ValidateObjectMeta(&scale.ObjectMeta, true, apimachineryvalidation.NameIsDNSSubdomain, field.NewPath("metadata"))...)
  36. if scale.Spec.Replicas < 0 {
  37. allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "replicas"), scale.Spec.Replicas, "must be greater than or equal to 0"))
  38. }
  39. return allErrs
  40. }
  41. // ValidateHorizontalPodAutoscalerName can be used to check whether the given autoscaler name is valid.
  42. // Prefix indicates this name will be used as part of generation, in which case trailing dashes are allowed.
  43. var ValidateHorizontalPodAutoscalerName = apivalidation.ValidateReplicationControllerName
  44. func validateHorizontalPodAutoscalerSpec(autoscaler autoscaling.HorizontalPodAutoscalerSpec, fldPath *field.Path, minReplicasLowerBound int32) field.ErrorList {
  45. allErrs := field.ErrorList{}
  46. if autoscaler.MinReplicas != nil && *autoscaler.MinReplicas < minReplicasLowerBound {
  47. allErrs = append(allErrs, field.Invalid(fldPath.Child("minReplicas"), *autoscaler.MinReplicas,
  48. fmt.Sprintf("must be greater than or equal to %d", minReplicasLowerBound)))
  49. }
  50. if autoscaler.MaxReplicas < 1 {
  51. allErrs = append(allErrs, field.Invalid(fldPath.Child("maxReplicas"), autoscaler.MaxReplicas, "must be greater than 0"))
  52. }
  53. if autoscaler.MinReplicas != nil && autoscaler.MaxReplicas < *autoscaler.MinReplicas {
  54. allErrs = append(allErrs, field.Invalid(fldPath.Child("maxReplicas"), autoscaler.MaxReplicas, "must be greater than or equal to `minReplicas`"))
  55. }
  56. if refErrs := ValidateCrossVersionObjectReference(autoscaler.ScaleTargetRef, fldPath.Child("scaleTargetRef")); len(refErrs) > 0 {
  57. allErrs = append(allErrs, refErrs...)
  58. }
  59. if refErrs := validateMetrics(autoscaler.Metrics, fldPath.Child("metrics"), autoscaler.MinReplicas); len(refErrs) > 0 {
  60. allErrs = append(allErrs, refErrs...)
  61. }
  62. if refErrs := validateBehavior(autoscaler.Behavior, fldPath.Child("behavior")); len(refErrs) > 0 {
  63. allErrs = append(allErrs, refErrs...)
  64. }
  65. return allErrs
  66. }
  67. // ValidateCrossVersionObjectReference validates a CrossVersionObjectReference and returns an
  68. // ErrorList with any errors.
  69. func ValidateCrossVersionObjectReference(ref autoscaling.CrossVersionObjectReference, fldPath *field.Path) field.ErrorList {
  70. allErrs := field.ErrorList{}
  71. if len(ref.Kind) == 0 {
  72. allErrs = append(allErrs, field.Required(fldPath.Child("kind"), ""))
  73. } else {
  74. for _, msg := range pathvalidation.IsValidPathSegmentName(ref.Kind) {
  75. allErrs = append(allErrs, field.Invalid(fldPath.Child("kind"), ref.Kind, msg))
  76. }
  77. }
  78. if len(ref.Name) == 0 {
  79. allErrs = append(allErrs, field.Required(fldPath.Child("name"), ""))
  80. } else {
  81. for _, msg := range pathvalidation.IsValidPathSegmentName(ref.Name) {
  82. allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), ref.Name, msg))
  83. }
  84. }
  85. return allErrs
  86. }
  87. // ValidateHorizontalPodAutoscaler validates a HorizontalPodAutoscaler and returns an
  88. // ErrorList with any errors.
  89. func ValidateHorizontalPodAutoscaler(autoscaler *autoscaling.HorizontalPodAutoscaler) field.ErrorList {
  90. allErrs := apivalidation.ValidateObjectMeta(&autoscaler.ObjectMeta, true, ValidateHorizontalPodAutoscalerName, field.NewPath("metadata"))
  91. // MinReplicasLowerBound represents a minimum value for minReplicas
  92. // 0 when HPA scale-to-zero feature is enabled
  93. var minReplicasLowerBound int32
  94. if utilfeature.DefaultFeatureGate.Enabled(features.HPAScaleToZero) {
  95. minReplicasLowerBound = 0
  96. } else {
  97. minReplicasLowerBound = 1
  98. }
  99. allErrs = append(allErrs, validateHorizontalPodAutoscalerSpec(autoscaler.Spec, field.NewPath("spec"), minReplicasLowerBound)...)
  100. return allErrs
  101. }
  102. // ValidateHorizontalPodAutoscalerUpdate validates an update to a HorizontalPodAutoscaler and returns an
  103. // ErrorList with any errors.
  104. func ValidateHorizontalPodAutoscalerUpdate(newAutoscaler, oldAutoscaler *autoscaling.HorizontalPodAutoscaler) field.ErrorList {
  105. allErrs := apivalidation.ValidateObjectMetaUpdate(&newAutoscaler.ObjectMeta, &oldAutoscaler.ObjectMeta, field.NewPath("metadata"))
  106. // minReplicasLowerBound represents a minimum value for minReplicas
  107. // 0 when HPA scale-to-zero feature is enabled or HPA object already has minReplicas=0
  108. var minReplicasLowerBound int32
  109. if utilfeature.DefaultFeatureGate.Enabled(features.HPAScaleToZero) || (oldAutoscaler.Spec.MinReplicas != nil && *oldAutoscaler.Spec.MinReplicas == 0) {
  110. minReplicasLowerBound = 0
  111. } else {
  112. minReplicasLowerBound = 1
  113. }
  114. allErrs = append(allErrs, validateHorizontalPodAutoscalerSpec(newAutoscaler.Spec, field.NewPath("spec"), minReplicasLowerBound)...)
  115. return allErrs
  116. }
  117. // ValidateHorizontalPodAutoscalerStatusUpdate validates an update to status on a HorizontalPodAutoscaler and
  118. // returns an ErrorList with any errors.
  119. func ValidateHorizontalPodAutoscalerStatusUpdate(newAutoscaler, oldAutoscaler *autoscaling.HorizontalPodAutoscaler) field.ErrorList {
  120. allErrs := apivalidation.ValidateObjectMetaUpdate(&newAutoscaler.ObjectMeta, &oldAutoscaler.ObjectMeta, field.NewPath("metadata"))
  121. status := newAutoscaler.Status
  122. allErrs = append(allErrs, apivalidation.ValidateNonnegativeField(int64(status.CurrentReplicas), field.NewPath("status", "currentReplicas"))...)
  123. allErrs = append(allErrs, apivalidation.ValidateNonnegativeField(int64(status.DesiredReplicas), field.NewPath("status", "desiredReplicas"))...)
  124. return allErrs
  125. }
  126. func validateMetrics(metrics []autoscaling.MetricSpec, fldPath *field.Path, minReplicas *int32) field.ErrorList {
  127. allErrs := field.ErrorList{}
  128. hasObjectMetrics := false
  129. hasExternalMetrics := false
  130. for i, metricSpec := range metrics {
  131. idxPath := fldPath.Index(i)
  132. if targetErrs := validateMetricSpec(metricSpec, idxPath); len(targetErrs) > 0 {
  133. allErrs = append(allErrs, targetErrs...)
  134. }
  135. if metricSpec.Type == autoscaling.ObjectMetricSourceType {
  136. hasObjectMetrics = true
  137. }
  138. if metricSpec.Type == autoscaling.ExternalMetricSourceType {
  139. hasExternalMetrics = true
  140. }
  141. }
  142. if minReplicas != nil && *minReplicas == 0 {
  143. if !hasObjectMetrics && !hasExternalMetrics {
  144. allErrs = append(allErrs, field.Forbidden(fldPath, "must specify at least one Object or External metric to support scaling to zero replicas"))
  145. }
  146. }
  147. return allErrs
  148. }
  149. func validateBehavior(behavior *autoscaling.HorizontalPodAutoscalerBehavior, fldPath *field.Path) field.ErrorList {
  150. allErrs := field.ErrorList{}
  151. if behavior != nil {
  152. if scaleUpErrs := validateScalingRules(behavior.ScaleUp, fldPath.Child("scaleUp")); len(scaleUpErrs) > 0 {
  153. allErrs = append(allErrs, scaleUpErrs...)
  154. }
  155. if scaleDownErrs := validateScalingRules(behavior.ScaleDown, fldPath.Child("scaleDown")); len(scaleDownErrs) > 0 {
  156. allErrs = append(allErrs, scaleDownErrs...)
  157. }
  158. }
  159. return allErrs
  160. }
  161. var validSelectPolicyTypes = sets.NewString(string(autoscaling.MaxPolicySelect), string(autoscaling.MinPolicySelect), string(autoscaling.DisabledPolicySelect))
  162. var validSelectPolicyTypesList = validSelectPolicyTypes.List()
  163. func validateScalingRules(rules *autoscaling.HPAScalingRules, fldPath *field.Path) field.ErrorList {
  164. allErrs := field.ErrorList{}
  165. if rules != nil {
  166. if rules.StabilizationWindowSeconds != nil && *rules.StabilizationWindowSeconds < 0 {
  167. allErrs = append(allErrs, field.Invalid(fldPath.Child("stabilizationWindowSeconds"), rules.StabilizationWindowSeconds, "must be greater than or equal to zero"))
  168. }
  169. if rules.StabilizationWindowSeconds != nil && *rules.StabilizationWindowSeconds > MaxStabilizationWindowSeconds {
  170. allErrs = append(allErrs, field.Invalid(fldPath.Child("stabilizationWindowSeconds"), rules.StabilizationWindowSeconds,
  171. fmt.Sprintf("must be less than or equal to %v", MaxStabilizationWindowSeconds)))
  172. }
  173. if rules.SelectPolicy != nil && !validSelectPolicyTypes.Has(string(*rules.SelectPolicy)) {
  174. allErrs = append(allErrs, field.NotSupported(fldPath.Child("selectPolicy"), rules.SelectPolicy, validSelectPolicyTypesList))
  175. }
  176. policiesPath := fldPath.Child("policies")
  177. if len(rules.Policies) == 0 {
  178. allErrs = append(allErrs, field.Required(policiesPath, "must specify at least one Policy"))
  179. }
  180. for i, policy := range rules.Policies {
  181. idxPath := policiesPath.Index(i)
  182. if policyErrs := validateScalingPolicy(policy, idxPath); len(policyErrs) > 0 {
  183. allErrs = append(allErrs, policyErrs...)
  184. }
  185. }
  186. }
  187. return allErrs
  188. }
  189. var validPolicyTypes = sets.NewString(string(autoscaling.PodsScalingPolicy), string(autoscaling.PercentScalingPolicy))
  190. var validPolicyTypesList = validPolicyTypes.List()
  191. func validateScalingPolicy(policy autoscaling.HPAScalingPolicy, fldPath *field.Path) field.ErrorList {
  192. allErrs := field.ErrorList{}
  193. if policy.Type != autoscaling.PodsScalingPolicy && policy.Type != autoscaling.PercentScalingPolicy {
  194. allErrs = append(allErrs, field.NotSupported(fldPath.Child("type"), policy.Type, validPolicyTypesList))
  195. }
  196. if policy.Value <= 0 {
  197. allErrs = append(allErrs, field.Invalid(fldPath.Child("value"), policy.Value, "must be greater than zero"))
  198. }
  199. if policy.PeriodSeconds <= 0 {
  200. allErrs = append(allErrs, field.Invalid(fldPath.Child("periodSeconds"), policy.PeriodSeconds, "must be greater than zero"))
  201. }
  202. if policy.PeriodSeconds > MaxPeriodSeconds {
  203. allErrs = append(allErrs, field.Invalid(fldPath.Child("periodSeconds"), policy.PeriodSeconds,
  204. fmt.Sprintf("must be less than or equal to %v", MaxPeriodSeconds)))
  205. }
  206. return allErrs
  207. }
  208. var validMetricSourceTypes = sets.NewString(string(autoscaling.ObjectMetricSourceType), string(autoscaling.PodsMetricSourceType), string(autoscaling.ResourceMetricSourceType), string(autoscaling.ExternalMetricSourceType))
  209. var validMetricSourceTypesList = validMetricSourceTypes.List()
  210. func validateMetricSpec(spec autoscaling.MetricSpec, fldPath *field.Path) field.ErrorList {
  211. allErrs := field.ErrorList{}
  212. if len(string(spec.Type)) == 0 {
  213. allErrs = append(allErrs, field.Required(fldPath.Child("type"), "must specify a metric source type"))
  214. }
  215. if !validMetricSourceTypes.Has(string(spec.Type)) {
  216. allErrs = append(allErrs, field.NotSupported(fldPath.Child("type"), spec.Type, validMetricSourceTypesList))
  217. }
  218. typesPresent := sets.NewString()
  219. if spec.Object != nil {
  220. typesPresent.Insert("object")
  221. if typesPresent.Len() == 1 {
  222. allErrs = append(allErrs, validateObjectSource(spec.Object, fldPath.Child("object"))...)
  223. }
  224. }
  225. if spec.External != nil {
  226. typesPresent.Insert("external")
  227. if typesPresent.Len() == 1 {
  228. allErrs = append(allErrs, validateExternalSource(spec.External, fldPath.Child("external"))...)
  229. }
  230. }
  231. if spec.Pods != nil {
  232. typesPresent.Insert("pods")
  233. if typesPresent.Len() == 1 {
  234. allErrs = append(allErrs, validatePodsSource(spec.Pods, fldPath.Child("pods"))...)
  235. }
  236. }
  237. if spec.Resource != nil {
  238. typesPresent.Insert("resource")
  239. if typesPresent.Len() == 1 {
  240. allErrs = append(allErrs, validateResourceSource(spec.Resource, fldPath.Child("resource"))...)
  241. }
  242. }
  243. expectedField := strings.ToLower(string(spec.Type))
  244. if !typesPresent.Has(expectedField) {
  245. allErrs = append(allErrs, field.Required(fldPath.Child(expectedField), "must populate information for the given metric source"))
  246. }
  247. if typesPresent.Len() != 1 {
  248. typesPresent.Delete(expectedField)
  249. for typ := range typesPresent {
  250. allErrs = append(allErrs, field.Forbidden(fldPath.Child(typ), "must populate the given metric source only"))
  251. }
  252. }
  253. return allErrs
  254. }
  255. func validateObjectSource(src *autoscaling.ObjectMetricSource, fldPath *field.Path) field.ErrorList {
  256. allErrs := field.ErrorList{}
  257. allErrs = append(allErrs, ValidateCrossVersionObjectReference(src.DescribedObject, fldPath.Child("describedObject"))...)
  258. allErrs = append(allErrs, validateMetricIdentifier(src.Metric, fldPath.Child("metric"))...)
  259. allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)
  260. if src.Target.Value == nil && src.Target.AverageValue == nil {
  261. allErrs = append(allErrs, field.Required(fldPath.Child("target").Child("averageValue"), "must set either a target value or averageValue"))
  262. }
  263. return allErrs
  264. }
  265. func validateExternalSource(src *autoscaling.ExternalMetricSource, fldPath *field.Path) field.ErrorList {
  266. allErrs := field.ErrorList{}
  267. allErrs = append(allErrs, validateMetricIdentifier(src.Metric, fldPath.Child("metric"))...)
  268. allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)
  269. if src.Target.Value == nil && src.Target.AverageValue == nil {
  270. allErrs = append(allErrs, field.Required(fldPath.Child("target").Child("averageValue"), "must set either a target value for metric or a per-pod target"))
  271. }
  272. if src.Target.Value != nil && src.Target.AverageValue != nil {
  273. allErrs = append(allErrs, field.Forbidden(fldPath.Child("target").Child("value"), "may not set both a target value for metric and a per-pod target"))
  274. }
  275. return allErrs
  276. }
  277. func validatePodsSource(src *autoscaling.PodsMetricSource, fldPath *field.Path) field.ErrorList {
  278. allErrs := field.ErrorList{}
  279. allErrs = append(allErrs, validateMetricIdentifier(src.Metric, fldPath.Child("metric"))...)
  280. allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)
  281. if src.Target.AverageValue == nil {
  282. allErrs = append(allErrs, field.Required(fldPath.Child("target").Child("averageValue"), "must specify a positive target averageValue"))
  283. }
  284. return allErrs
  285. }
  286. func validateResourceSource(src *autoscaling.ResourceMetricSource, fldPath *field.Path) field.ErrorList {
  287. allErrs := field.ErrorList{}
  288. if len(src.Name) == 0 {
  289. allErrs = append(allErrs, field.Required(fldPath.Child("name"), "must specify a resource name"))
  290. }
  291. allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)
  292. if src.Target.AverageUtilization == nil && src.Target.AverageValue == nil {
  293. allErrs = append(allErrs, field.Required(fldPath.Child("target").Child("averageUtilization"), "must set either a target raw value or a target utilization"))
  294. }
  295. if src.Target.AverageUtilization != nil && src.Target.AverageValue != nil {
  296. allErrs = append(allErrs, field.Forbidden(fldPath.Child("target").Child("averageValue"), "may not set both a target raw value and a target utilization"))
  297. }
  298. return allErrs
  299. }
  300. func validateMetricTarget(mt autoscaling.MetricTarget, fldPath *field.Path) field.ErrorList {
  301. allErrs := field.ErrorList{}
  302. if len(mt.Type) == 0 {
  303. allErrs = append(allErrs, field.Required(fldPath.Child("type"), "must specify a metric target type"))
  304. }
  305. if mt.Type != autoscaling.UtilizationMetricType &&
  306. mt.Type != autoscaling.ValueMetricType &&
  307. mt.Type != autoscaling.AverageValueMetricType {
  308. allErrs = append(allErrs, field.Invalid(fldPath.Child("type"), mt.Type, "must be either Utilization, Value, or AverageValue"))
  309. }
  310. if mt.Value != nil && mt.Value.Sign() != 1 {
  311. allErrs = append(allErrs, field.Invalid(fldPath.Child("value"), mt.Value, "must be positive"))
  312. }
  313. if mt.AverageValue != nil && mt.AverageValue.Sign() != 1 {
  314. allErrs = append(allErrs, field.Invalid(fldPath.Child("averageValue"), mt.AverageValue, "must be positive"))
  315. }
  316. if mt.AverageUtilization != nil && *mt.AverageUtilization < 1 {
  317. allErrs = append(allErrs, field.Invalid(fldPath.Child("averageUtilization"), mt.AverageUtilization, "must be greater than 0"))
  318. }
  319. return allErrs
  320. }
  321. func validateMetricIdentifier(id autoscaling.MetricIdentifier, fldPath *field.Path) field.ErrorList {
  322. allErrs := field.ErrorList{}
  323. if len(id.Name) == 0 {
  324. allErrs = append(allErrs, field.Required(fldPath.Child("name"), "must specify a metric name"))
  325. } else {
  326. for _, msg := range pathvalidation.IsValidPathSegmentName(id.Name) {
  327. allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), id.Name, msg))
  328. }
  329. }
  330. return allErrs
  331. }