types.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 autoscaling
  14. import (
  15. "k8s.io/apimachinery/pkg/api/resource"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. api "k8s.io/kubernetes/pkg/apis/core"
  18. )
  19. // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
  20. // Scale represents a scaling request for a resource.
  21. type Scale struct {
  22. metav1.TypeMeta
  23. // Standard object metadata; More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata.
  24. // +optional
  25. metav1.ObjectMeta
  26. // defines the behavior of the scale. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status.
  27. // +optional
  28. Spec ScaleSpec
  29. // current status of the scale. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status. Read-only.
  30. // +optional
  31. Status ScaleStatus
  32. }
  33. // ScaleSpec describes the attributes of a scale subresource.
  34. type ScaleSpec struct {
  35. // desired number of instances for the scaled object.
  36. // +optional
  37. Replicas int32
  38. }
  39. // ScaleStatus represents the current status of a scale subresource.
  40. type ScaleStatus struct {
  41. // actual number of observed instances of the scaled object.
  42. Replicas int32
  43. // label query over pods that should match the replicas count. This is same
  44. // as the label selector but in the string format to avoid introspection
  45. // by clients. The string will be in the same format as the query-param syntax.
  46. // More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
  47. // +optional
  48. Selector string
  49. }
  50. // CrossVersionObjectReference contains enough information to let you identify the referred resource.
  51. type CrossVersionObjectReference struct {
  52. // Kind of the referent; More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds"
  53. Kind string
  54. // Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
  55. Name string
  56. // API version of the referent
  57. // +optional
  58. APIVersion string
  59. }
  60. // HorizontalPodAutoscalerSpec describes the desired functionality of the HorizontalPodAutoscaler.
  61. type HorizontalPodAutoscalerSpec struct {
  62. // ScaleTargetRef points to the target resource to scale, and is used to the pods for which metrics
  63. // should be collected, as well as to actually change the replica count.
  64. ScaleTargetRef CrossVersionObjectReference
  65. // minReplicas is the lower limit for the number of replicas to which the autoscaler
  66. // can scale down. It defaults to 1 pod. minReplicas is allowed to be 0 if the
  67. // alpha feature gate HPAScaleToZero is enabled and at least one Object or External
  68. // metric is configured. Scaling is active as long as at least one metric value is
  69. // available.
  70. // +optional
  71. MinReplicas *int32
  72. // MaxReplicas is the upper limit for the number of replicas to which the autoscaler can scale up.
  73. // It cannot be less that minReplicas.
  74. MaxReplicas int32
  75. // Metrics contains the specifications for which to use to calculate the
  76. // desired replica count (the maximum replica count across all metrics will
  77. // be used). The desired replica count is calculated multiplying the
  78. // ratio between the target value and the current value by the current
  79. // number of pods. Ergo, metrics used must decrease as the pod count is
  80. // increased, and vice-versa. See the individual metric source types for
  81. // more information about how each type of metric must respond.
  82. // +optional
  83. Metrics []MetricSpec
  84. // behavior configures the scaling behavior of the target
  85. // in both Up and Down directions (scaleUp and scaleDown fields respectively).
  86. // If not set, the default HPAScalingRules for scale up and scale down are used.
  87. // +optional
  88. Behavior *HorizontalPodAutoscalerBehavior
  89. }
  90. // HorizontalPodAutoscalerBehavior configures a scaling behavior for Up and Down direction
  91. // (scaleUp and scaleDown fields respectively).
  92. type HorizontalPodAutoscalerBehavior struct {
  93. // scaleUp is scaling policy for scaling Up.
  94. // If not set, the default value is the higher of:
  95. // * increase no more than 4 pods per 60 seconds
  96. // * double the number of pods per 60 seconds
  97. // No stabilization is used.
  98. // +optional
  99. ScaleUp *HPAScalingRules
  100. // scaleDown is scaling policy for scaling Down.
  101. // If not set, the default value is to allow to scale down to minReplicas pods, with a
  102. // 300 second stabilization window (i.e., the highest recommendation for
  103. // the last 300sec is used).
  104. // +optional
  105. ScaleDown *HPAScalingRules
  106. }
  107. // ScalingPolicySelect is used to specify which policy should be used while scaling in a certain direction
  108. type ScalingPolicySelect string
  109. const (
  110. // MaxPolicySelect selects the policy with the highest possible change.
  111. MaxPolicySelect ScalingPolicySelect = "Max"
  112. // MinPolicySelect selects the policy with the lowest possible change.
  113. MinPolicySelect ScalingPolicySelect = "Min"
  114. // DisabledPolicySelect disables the scaling in this direction.
  115. DisabledPolicySelect ScalingPolicySelect = "Disabled"
  116. )
  117. // HPAScalingRules configures the scaling behavior for one direction.
  118. // These Rules are applied after calculating DesiredReplicas from metrics for the HPA.
  119. // They can limit the scaling velocity by specifying scaling policies.
  120. // They can prevent flapping by specifying the stabilization window, so that the
  121. // number of replicas is not set instantly, instead, the safest value from the stabilization
  122. // window is chosen.
  123. type HPAScalingRules struct {
  124. // StabilizationWindowSeconds is the number of seconds for which past recommendations should be
  125. // considered while scaling up or scaling down.
  126. // StabilizationWindowSeconds must be greater than or equal to zero and less than or equal to 3600 (one hour).
  127. // If not set, use the default values:
  128. // - For scale up: 0 (i.e. no stabilization is done).
  129. // - For scale down: 300 (i.e. the stabilization window is 300 seconds long).
  130. // +optional
  131. StabilizationWindowSeconds *int32
  132. // selectPolicy is used to specify which policy should be used.
  133. // If not set, the default value MaxPolicySelect is used.
  134. // +optional
  135. SelectPolicy *ScalingPolicySelect
  136. // policies is a list of potential scaling polices which can used during scaling.
  137. // At least one policy must be specified, otherwise the HPAScalingRules will be discarded as invalid
  138. // +optional
  139. Policies []HPAScalingPolicy
  140. }
  141. // HPAScalingPolicyType is the type of the policy which could be used while making scaling decisions.
  142. type HPAScalingPolicyType string
  143. const (
  144. // PodsScalingPolicy is a policy used to specify a change in absolute number of pods.
  145. PodsScalingPolicy HPAScalingPolicyType = "Pods"
  146. // PercentScalingPolicy is a policy used to specify a relative amount of change with respect to
  147. // the current number of pods.
  148. PercentScalingPolicy HPAScalingPolicyType = "Percent"
  149. )
  150. // HPAScalingPolicy is a single policy which must hold true for a specified past interval.
  151. type HPAScalingPolicy struct {
  152. // Type is used to specify the scaling policy.
  153. Type HPAScalingPolicyType
  154. // Value contains the amount of change which is permitted by the policy.
  155. // It must be greater than zero
  156. Value int32
  157. // PeriodSeconds specifies the window of time for which the policy should hold true.
  158. // PeriodSeconds must be greater than zero and less than or equal to 1800 (30 min).
  159. PeriodSeconds int32
  160. }
  161. // MetricSourceType indicates the type of metric.
  162. type MetricSourceType string
  163. const (
  164. // ObjectMetricSourceType is a metric describing a kubernetes object
  165. // (for example, hits-per-second on an Ingress object).
  166. ObjectMetricSourceType MetricSourceType = "Object"
  167. // PodsMetricSourceType is a metric describing each pod in the current scale
  168. // target (for example, transactions-processed-per-second). The values
  169. // will be averaged together before being compared to the target value.
  170. PodsMetricSourceType MetricSourceType = "Pods"
  171. // ResourceMetricSourceType is a resource metric known to Kubernetes, as
  172. // specified in requests and limits, describing each pod in the current
  173. // scale target (e.g. CPU or memory). Such metrics are built in to
  174. // Kubernetes, and have special scaling options on top of those available
  175. // to normal per-pod metrics (the "pods" source).
  176. ResourceMetricSourceType MetricSourceType = "Resource"
  177. // ExternalMetricSourceType is a global metric that is not associated
  178. // with any Kubernetes object. It allows autoscaling based on information
  179. // coming from components running outside of cluster
  180. // (for example length of queue in cloud messaging service, or
  181. // QPS from loadbalancer running outside of cluster).
  182. ExternalMetricSourceType MetricSourceType = "External"
  183. )
  184. // MetricSpec specifies how to scale based on a single metric
  185. // (only `type` and one other matching field should be set at once).
  186. type MetricSpec struct {
  187. // Type is the type of metric source. It should be one of "Object",
  188. // "Pods" or "Resource", each mapping to a matching field in the object.
  189. Type MetricSourceType
  190. // Object refers to a metric describing a single kubernetes object
  191. // (for example, hits-per-second on an Ingress object).
  192. // +optional
  193. Object *ObjectMetricSource
  194. // Pods refers to a metric describing each pod in the current scale target
  195. // (for example, transactions-processed-per-second). The values will be
  196. // averaged together before being compared to the target value.
  197. // +optional
  198. Pods *PodsMetricSource
  199. // Resource refers to a resource metric (such as those specified in
  200. // requests and limits) known to Kubernetes describing each pod in the
  201. // current scale target (e.g. CPU or memory). Such metrics are built in to
  202. // Kubernetes, and have special scaling options on top of those available
  203. // to normal per-pod metrics using the "pods" source.
  204. // +optional
  205. Resource *ResourceMetricSource
  206. // External refers to a global metric that is not associated
  207. // with any Kubernetes object. It allows autoscaling based on information
  208. // coming from components running outside of cluster
  209. // (for example length of queue in cloud messaging service, or
  210. // QPS from loadbalancer running outside of cluster).
  211. // +optional
  212. External *ExternalMetricSource
  213. }
  214. // ObjectMetricSource indicates how to scale on a metric describing a
  215. // kubernetes object (for example, hits-per-second on an Ingress object).
  216. type ObjectMetricSource struct {
  217. DescribedObject CrossVersionObjectReference
  218. Target MetricTarget
  219. Metric MetricIdentifier
  220. }
  221. // PodsMetricSource indicates how to scale on a metric describing each pod in
  222. // the current scale target (for example, transactions-processed-per-second).
  223. // The values will be averaged together before being compared to the target
  224. // value.
  225. type PodsMetricSource struct {
  226. // metric identifies the target metric by name and selector
  227. Metric MetricIdentifier
  228. // target specifies the target value for the given metric
  229. Target MetricTarget
  230. }
  231. // ResourceMetricSource indicates how to scale on a resource metric known to
  232. // Kubernetes, as specified in requests and limits, describing each pod in the
  233. // current scale target (e.g. CPU or memory). The values will be averaged
  234. // together before being compared to the target. Such metrics are built in to
  235. // Kubernetes, and have special scaling options on top of those available to
  236. // normal per-pod metrics using the "pods" source. Only one "target" type
  237. // should be set.
  238. type ResourceMetricSource struct {
  239. // Name is the name of the resource in question.
  240. Name api.ResourceName
  241. // Target specifies the target value for the given metric
  242. Target MetricTarget
  243. }
  244. // ExternalMetricSource indicates how to scale on a metric not associated with
  245. // any Kubernetes object (for example length of queue in cloud
  246. // messaging service, or QPS from loadbalancer running outside of cluster).
  247. type ExternalMetricSource struct {
  248. // Metric identifies the target metric by name and selector
  249. Metric MetricIdentifier
  250. // Target specifies the target value for the given metric
  251. Target MetricTarget
  252. }
  253. // MetricIdentifier defines the name and optionally selector for a metric
  254. type MetricIdentifier struct {
  255. // Name is the name of the given metric
  256. Name string
  257. // Selector is the selector for the given metric
  258. // it is the string-encoded form of a standard kubernetes label selector
  259. // +optional
  260. Selector *metav1.LabelSelector
  261. }
  262. // MetricTarget defines the target value, average value, or average utilization of a specific metric
  263. type MetricTarget struct {
  264. // Type represents whether the metric type is Utilization, Value, or AverageValue
  265. Type MetricTargetType
  266. // Value is the target value of the metric (as a quantity).
  267. Value *resource.Quantity
  268. // TargetAverageValue is the target value of the average of the
  269. // metric across all relevant pods (as a quantity)
  270. AverageValue *resource.Quantity
  271. // AverageUtilization is the target value of the average of the
  272. // resource metric across all relevant pods, represented as a percentage of
  273. // the requested value of the resource for the pods.
  274. // Currently only valid for Resource metric source type
  275. AverageUtilization *int32
  276. }
  277. // MetricTargetType specifies the type of metric being targeted, and should be either
  278. // "Value", "AverageValue", or "Utilization"
  279. type MetricTargetType string
  280. const (
  281. // UtilizationMetricType is a possible value for MetricTarget.Type.
  282. UtilizationMetricType MetricTargetType = "Utilization"
  283. // ValueMetricType is a possible value for MetricTarget.Type.
  284. ValueMetricType MetricTargetType = "Value"
  285. // AverageValueMetricType is a possible value for MetricTarget.Type.
  286. AverageValueMetricType MetricTargetType = "AverageValue"
  287. )
  288. // HorizontalPodAutoscalerStatus describes the current status of a horizontal pod autoscaler.
  289. type HorizontalPodAutoscalerStatus struct {
  290. // ObservedGeneration is the most recent generation observed by this autoscaler.
  291. // +optional
  292. ObservedGeneration *int64
  293. // LastScaleTime is the last time the HorizontalPodAutoscaler scaled the number of pods,
  294. // used by the autoscaler to control how often the number of pods is changed.
  295. // +optional
  296. LastScaleTime *metav1.Time
  297. // CurrentReplicas is current number of replicas of pods managed by this autoscaler,
  298. // as last seen by the autoscaler.
  299. CurrentReplicas int32
  300. // DesiredReplicas is the desired number of replicas of pods managed by this autoscaler,
  301. // as last calculated by the autoscaler.
  302. DesiredReplicas int32
  303. // CurrentMetrics is the last read state of the metrics used by this autoscaler.
  304. // +optional
  305. CurrentMetrics []MetricStatus
  306. // Conditions is the set of conditions required for this autoscaler to scale its target,
  307. // and indicates whether or not those conditions are met.
  308. Conditions []HorizontalPodAutoscalerCondition
  309. }
  310. // ConditionStatus indicates the status of a condition (true, false, or unknown).
  311. type ConditionStatus string
  312. // These are valid condition statuses. "ConditionTrue" means a resource is in the condition;
  313. // "ConditionFalse" means a resource is not in the condition; "ConditionUnknown" means kubernetes
  314. // can't decide if a resource is in the condition or not. In the future, we could add other
  315. // intermediate conditions, e.g. ConditionDegraded.
  316. const (
  317. ConditionTrue ConditionStatus = "True"
  318. ConditionFalse ConditionStatus = "False"
  319. ConditionUnknown ConditionStatus = "Unknown"
  320. )
  321. // HorizontalPodAutoscalerConditionType are the valid conditions of
  322. // a HorizontalPodAutoscaler.
  323. type HorizontalPodAutoscalerConditionType string
  324. const (
  325. // ScalingActive indicates that the HPA controller is able to scale if necessary:
  326. // it's correctly configured, can fetch the desired metrics, and isn't disabled.
  327. ScalingActive HorizontalPodAutoscalerConditionType = "ScalingActive"
  328. // AbleToScale indicates a lack of transient issues which prevent scaling from occurring,
  329. // such as being in a backoff window, or being unable to access/update the target scale.
  330. AbleToScale HorizontalPodAutoscalerConditionType = "AbleToScale"
  331. // ScalingLimited indicates that the calculated scale based on metrics would be above or
  332. // below the range for the HPA, and has thus been capped.
  333. ScalingLimited HorizontalPodAutoscalerConditionType = "ScalingLimited"
  334. )
  335. // HorizontalPodAutoscalerCondition describes the state of
  336. // a HorizontalPodAutoscaler at a certain point.
  337. type HorizontalPodAutoscalerCondition struct {
  338. // Type describes the current condition
  339. Type HorizontalPodAutoscalerConditionType
  340. // Status is the status of the condition (True, False, Unknown)
  341. Status ConditionStatus
  342. // LastTransitionTime is the last time the condition transitioned from
  343. // one status to another
  344. // +optional
  345. LastTransitionTime metav1.Time
  346. // Reason is the reason for the condition's last transition.
  347. // +optional
  348. Reason string
  349. // Message is a human-readable explanation containing details about
  350. // the transition
  351. // +optional
  352. Message string
  353. }
  354. // MetricStatus describes the last-read state of a single metric.
  355. type MetricStatus struct {
  356. // Type is the type of metric source. It will be one of "Object",
  357. // "Pods" or "Resource", each corresponds to a matching field in the object.
  358. Type MetricSourceType
  359. // Object refers to a metric describing a single kubernetes object
  360. // (for example, hits-per-second on an Ingress object).
  361. // +optional
  362. Object *ObjectMetricStatus
  363. // Pods refers to a metric describing each pod in the current scale target
  364. // (for example, transactions-processed-per-second). The values will be
  365. // averaged together before being compared to the target value.
  366. // +optional
  367. Pods *PodsMetricStatus
  368. // Resource refers to a resource metric (such as those specified in
  369. // requests and limits) known to Kubernetes describing each pod in the
  370. // current scale target (e.g. CPU or memory). Such metrics are built in to
  371. // Kubernetes, and have special scaling options on top of those available
  372. // to normal per-pod metrics using the "pods" source.
  373. // +optional
  374. Resource *ResourceMetricStatus
  375. // External refers to a global metric that is not associated
  376. // with any Kubernetes object. It allows autoscaling based on information
  377. // coming from components running outside of cluster
  378. // (for example length of queue in cloud messaging service, or
  379. // QPS from loadbalancer running outside of cluster).
  380. // +optional
  381. External *ExternalMetricStatus
  382. }
  383. // ObjectMetricStatus indicates the current value of a metric describing a
  384. // kubernetes object (for example, hits-per-second on an Ingress object).
  385. type ObjectMetricStatus struct {
  386. Metric MetricIdentifier
  387. Current MetricValueStatus
  388. DescribedObject CrossVersionObjectReference
  389. }
  390. // PodsMetricStatus indicates the current value of a metric describing each pod in
  391. // the current scale target (for example, transactions-processed-per-second).
  392. type PodsMetricStatus struct {
  393. Metric MetricIdentifier
  394. Current MetricValueStatus
  395. }
  396. // ResourceMetricStatus indicates the current value of a resource metric known to
  397. // Kubernetes, as specified in requests and limits, describing each pod in the
  398. // current scale target (e.g. CPU or memory). Such metrics are built in to
  399. // Kubernetes, and have special scaling options on top of those available to
  400. // normal per-pod metrics using the "pods" source.
  401. type ResourceMetricStatus struct {
  402. // Name is the name of the resource in question.
  403. Name api.ResourceName
  404. Current MetricValueStatus
  405. }
  406. // ExternalMetricStatus indicates the current value of a global metric
  407. // not associated with any Kubernetes object.
  408. type ExternalMetricStatus struct {
  409. Metric MetricIdentifier
  410. Current MetricValueStatus
  411. }
  412. // MetricValueStatus indicates the current value of a metric.
  413. type MetricValueStatus struct {
  414. Value *resource.Quantity
  415. AverageValue *resource.Quantity
  416. AverageUtilization *int32
  417. }
  418. // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
  419. // HorizontalPodAutoscaler is the configuration for a horizontal pod
  420. // autoscaler, which automatically manages the replica count of any resource
  421. // implementing the scale subresource based on the metrics specified.
  422. type HorizontalPodAutoscaler struct {
  423. metav1.TypeMeta
  424. // Metadata is the standard object metadata.
  425. // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
  426. // +optional
  427. metav1.ObjectMeta
  428. // Spec is the specification for the behaviour of the autoscaler.
  429. // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status.
  430. // +optional
  431. Spec HorizontalPodAutoscalerSpec
  432. // Status is the current information about the autoscaler.
  433. // +optional
  434. Status HorizontalPodAutoscalerStatus
  435. }
  436. // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
  437. // HorizontalPodAutoscalerList is a list of horizontal pod autoscaler objects.
  438. type HorizontalPodAutoscalerList struct {
  439. metav1.TypeMeta
  440. // Metadata is the standard list metadata.
  441. // +optional
  442. metav1.ListMeta
  443. // Items is the list of horizontal pod autoscaler objects.
  444. Items []HorizontalPodAutoscaler
  445. }