blob: d76e879678a65456a3779604290ac129dc17c067 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package v2beta1
18
19import (
20 v1 "k8s.io/api/core/v1"
21 "k8s.io/apimachinery/pkg/api/resource"
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23)
24
25// CrossVersionObjectReference contains enough information to let you identify the referred resource.
26type CrossVersionObjectReference struct {
27 // Kind of the referent; More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds"
28 Kind string `json:"kind" protobuf:"bytes,1,opt,name=kind"`
29 // Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
30 Name string `json:"name" protobuf:"bytes,2,opt,name=name"`
31 // API version of the referent
32 // +optional
33 APIVersion string `json:"apiVersion,omitempty" protobuf:"bytes,3,opt,name=apiVersion"`
34}
35
36// HorizontalPodAutoscalerSpec describes the desired functionality of the HorizontalPodAutoscaler.
37type HorizontalPodAutoscalerSpec struct {
38 // scaleTargetRef points to the target resource to scale, and is used to the pods for which metrics
39 // should be collected, as well as to actually change the replica count.
40 ScaleTargetRef CrossVersionObjectReference `json:"scaleTargetRef" protobuf:"bytes,1,opt,name=scaleTargetRef"`
41 // minReplicas is the lower limit for the number of replicas to which the autoscaler
42 // can scale down. It defaults to 1 pod. minReplicas is allowed to be 0 if the
43 // alpha feature gate HPAScaleToZero is enabled and at least one Object or External
44 // metric is configured. Scaling is active as long as at least one metric value is
45 // available.
46 // +optional
47 MinReplicas *int32 `json:"minReplicas,omitempty" protobuf:"varint,2,opt,name=minReplicas"`
48 // maxReplicas is the upper limit for the number of replicas to which the autoscaler can scale up.
49 // It cannot be less that minReplicas.
50 MaxReplicas int32 `json:"maxReplicas" protobuf:"varint,3,opt,name=maxReplicas"`
51 // metrics contains the specifications for which to use to calculate the
52 // desired replica count (the maximum replica count across all metrics will
53 // be used). The desired replica count is calculated multiplying the
54 // ratio between the target value and the current value by the current
55 // number of pods. Ergo, metrics used must decrease as the pod count is
56 // increased, and vice-versa. See the individual metric source types for
57 // more information about how each type of metric must respond.
58 // +optional
59 Metrics []MetricSpec `json:"metrics,omitempty" protobuf:"bytes,4,rep,name=metrics"`
60}
61
62// MetricSourceType indicates the type of metric.
63type MetricSourceType string
64
65const (
66 // ObjectMetricSourceType is a metric describing a kubernetes object
67 // (for example, hits-per-second on an Ingress object).
68 ObjectMetricSourceType MetricSourceType = "Object"
69 // PodsMetricSourceType is a metric describing each pod in the current scale
70 // target (for example, transactions-processed-per-second). The values
71 // will be averaged together before being compared to the target value.
72 PodsMetricSourceType MetricSourceType = "Pods"
73 // ResourceMetricSourceType is a resource metric known to Kubernetes, as
74 // specified in requests and limits, describing each pod in the current
75 // scale target (e.g. CPU or memory). Such metrics are built in to
76 // Kubernetes, and have special scaling options on top of those available
77 // to normal per-pod metrics (the "pods" source).
78 ResourceMetricSourceType MetricSourceType = "Resource"
79 // ExternalMetricSourceType is a global metric that is not associated
80 // with any Kubernetes object. It allows autoscaling based on information
81 // coming from components running outside of cluster
82 // (for example length of queue in cloud messaging service, or
83 // QPS from loadbalancer running outside of cluster).
84 ExternalMetricSourceType MetricSourceType = "External"
85)
86
87// MetricSpec specifies how to scale based on a single metric
88// (only `type` and one other matching field should be set at once).
89type MetricSpec struct {
90 // type is the type of metric source. It should be one of "Object",
91 // "Pods" or "Resource", each mapping to a matching field in the object.
92 Type MetricSourceType `json:"type" protobuf:"bytes,1,name=type"`
93
94 // object refers to a metric describing a single kubernetes object
95 // (for example, hits-per-second on an Ingress object).
96 // +optional
97 Object *ObjectMetricSource `json:"object,omitempty" protobuf:"bytes,2,opt,name=object"`
98 // pods refers to a metric describing each pod in the current scale target
99 // (for example, transactions-processed-per-second). The values will be
100 // averaged together before being compared to the target value.
101 // +optional
102 Pods *PodsMetricSource `json:"pods,omitempty" protobuf:"bytes,3,opt,name=pods"`
103 // resource refers to a resource metric (such as those specified in
104 // requests and limits) known to Kubernetes describing each pod in the
105 // current scale target (e.g. CPU or memory). Such metrics are built in to
106 // Kubernetes, and have special scaling options on top of those available
107 // to normal per-pod metrics using the "pods" source.
108 // +optional
109 Resource *ResourceMetricSource `json:"resource,omitempty" protobuf:"bytes,4,opt,name=resource"`
110 // external refers to a global metric that is not associated
111 // with any Kubernetes object. It allows autoscaling based on information
112 // coming from components running outside of cluster
113 // (for example length of queue in cloud messaging service, or
114 // QPS from loadbalancer running outside of cluster).
115 // +optional
116 External *ExternalMetricSource `json:"external,omitempty" protobuf:"bytes,5,opt,name=external"`
117}
118
119// ObjectMetricSource indicates how to scale on a metric describing a
120// kubernetes object (for example, hits-per-second on an Ingress object).
121type ObjectMetricSource struct {
122 // target is the described Kubernetes object.
123 Target CrossVersionObjectReference `json:"target" protobuf:"bytes,1,name=target"`
124
125 // metricName is the name of the metric in question.
126 MetricName string `json:"metricName" protobuf:"bytes,2,name=metricName"`
127 // targetValue is the target value of the metric (as a quantity).
128 TargetValue resource.Quantity `json:"targetValue" protobuf:"bytes,3,name=targetValue"`
129
130 // selector is the string-encoded form of a standard kubernetes label selector for the given metric
131 // When set, it is passed as an additional parameter to the metrics server for more specific metrics scoping
132 // When unset, just the metricName will be used to gather metrics.
133 // +optional
134 Selector *metav1.LabelSelector `json:"selector,omitempty" protobuf:"bytes,4,name=selector"`
135 // averageValue is the target value of the average of the
136 // metric across all relevant pods (as a quantity)
137 // +optional
138 AverageValue *resource.Quantity `json:"averageValue,omitempty" protobuf:"bytes,5,name=averageValue"`
139}
140
141// PodsMetricSource indicates how to scale on a metric describing each pod in
142// the current scale target (for example, transactions-processed-per-second).
143// The values will be averaged together before being compared to the target
144// value.
145type PodsMetricSource struct {
146 // metricName is the name of the metric in question
147 MetricName string `json:"metricName" protobuf:"bytes,1,name=metricName"`
148 // targetAverageValue is the target value of the average of the
149 // metric across all relevant pods (as a quantity)
150 TargetAverageValue resource.Quantity `json:"targetAverageValue" protobuf:"bytes,2,name=targetAverageValue"`
151
152 // selector is the string-encoded form of a standard kubernetes label selector for the given metric
153 // When set, it is passed as an additional parameter to the metrics server for more specific metrics scoping
154 // When unset, just the metricName will be used to gather metrics.
155 // +optional
156 Selector *metav1.LabelSelector `json:"selector,omitempty" protobuf:"bytes,3,name=selector"`
157}
158
159// ResourceMetricSource indicates how to scale on a resource metric known to
160// Kubernetes, as specified in requests and limits, describing each pod in the
161// current scale target (e.g. CPU or memory). The values will be averaged
162// together before being compared to the target. Such metrics are built in to
163// Kubernetes, and have special scaling options on top of those available to
164// normal per-pod metrics using the "pods" source. Only one "target" type
165// should be set.
166type ResourceMetricSource struct {
167 // name is the name of the resource in question.
168 Name v1.ResourceName `json:"name" protobuf:"bytes,1,name=name"`
169 // targetAverageUtilization is the target value of the average of the
170 // resource metric across all relevant pods, represented as a percentage of
171 // the requested value of the resource for the pods.
172 // +optional
173 TargetAverageUtilization *int32 `json:"targetAverageUtilization,omitempty" protobuf:"varint,2,opt,name=targetAverageUtilization"`
174 // targetAverageValue is the target value of the average of the
175 // resource metric across all relevant pods, as a raw value (instead of as
176 // a percentage of the request), similar to the "pods" metric source type.
177 // +optional
178 TargetAverageValue *resource.Quantity `json:"targetAverageValue,omitempty" protobuf:"bytes,3,opt,name=targetAverageValue"`
179}
180
181// ExternalMetricSource indicates how to scale on a metric not associated with
182// any Kubernetes object (for example length of queue in cloud
183// messaging service, or QPS from loadbalancer running outside of cluster).
184// Exactly one "target" type should be set.
185type ExternalMetricSource struct {
186 // metricName is the name of the metric in question.
187 MetricName string `json:"metricName" protobuf:"bytes,1,name=metricName"`
188 // metricSelector is used to identify a specific time series
189 // within a given metric.
190 // +optional
191 MetricSelector *metav1.LabelSelector `json:"metricSelector,omitempty" protobuf:"bytes,2,opt,name=metricSelector"`
192 // targetValue is the target value of the metric (as a quantity).
193 // Mutually exclusive with TargetAverageValue.
194 // +optional
195 TargetValue *resource.Quantity `json:"targetValue,omitempty" protobuf:"bytes,3,opt,name=targetValue"`
196 // targetAverageValue is the target per-pod value of global metric (as a quantity).
197 // Mutually exclusive with TargetValue.
198 // +optional
199 TargetAverageValue *resource.Quantity `json:"targetAverageValue,omitempty" protobuf:"bytes,4,opt,name=targetAverageValue"`
200}
201
202// HorizontalPodAutoscalerStatus describes the current status of a horizontal pod autoscaler.
203type HorizontalPodAutoscalerStatus struct {
204 // observedGeneration is the most recent generation observed by this autoscaler.
205 // +optional
206 ObservedGeneration *int64 `json:"observedGeneration,omitempty" protobuf:"varint,1,opt,name=observedGeneration"`
207
208 // lastScaleTime is the last time the HorizontalPodAutoscaler scaled the number of pods,
209 // used by the autoscaler to control how often the number of pods is changed.
210 // +optional
211 LastScaleTime *metav1.Time `json:"lastScaleTime,omitempty" protobuf:"bytes,2,opt,name=lastScaleTime"`
212
213 // currentReplicas is current number of replicas of pods managed by this autoscaler,
214 // as last seen by the autoscaler.
215 CurrentReplicas int32 `json:"currentReplicas" protobuf:"varint,3,opt,name=currentReplicas"`
216
217 // desiredReplicas is the desired number of replicas of pods managed by this autoscaler,
218 // as last calculated by the autoscaler.
219 DesiredReplicas int32 `json:"desiredReplicas" protobuf:"varint,4,opt,name=desiredReplicas"`
220
221 // currentMetrics is the last read state of the metrics used by this autoscaler.
222 // +optional
223 CurrentMetrics []MetricStatus `json:"currentMetrics" protobuf:"bytes,5,rep,name=currentMetrics"`
224
225 // conditions is the set of conditions required for this autoscaler to scale its target,
226 // and indicates whether or not those conditions are met.
227 Conditions []HorizontalPodAutoscalerCondition `json:"conditions" protobuf:"bytes,6,rep,name=conditions"`
228}
229
230// HorizontalPodAutoscalerConditionType are the valid conditions of
231// a HorizontalPodAutoscaler.
232type HorizontalPodAutoscalerConditionType string
233
234const (
235 // ScalingActive indicates that the HPA controller is able to scale if necessary:
236 // it's correctly configured, can fetch the desired metrics, and isn't disabled.
237 ScalingActive HorizontalPodAutoscalerConditionType = "ScalingActive"
238 // AbleToScale indicates a lack of transient issues which prevent scaling from occurring,
239 // such as being in a backoff window, or being unable to access/update the target scale.
240 AbleToScale HorizontalPodAutoscalerConditionType = "AbleToScale"
241 // ScalingLimited indicates that the calculated scale based on metrics would be above or
242 // below the range for the HPA, and has thus been capped.
243 ScalingLimited HorizontalPodAutoscalerConditionType = "ScalingLimited"
244)
245
246// HorizontalPodAutoscalerCondition describes the state of
247// a HorizontalPodAutoscaler at a certain point.
248type HorizontalPodAutoscalerCondition struct {
249 // type describes the current condition
250 Type HorizontalPodAutoscalerConditionType `json:"type" protobuf:"bytes,1,name=type"`
251 // status is the status of the condition (True, False, Unknown)
252 Status v1.ConditionStatus `json:"status" protobuf:"bytes,2,name=status"`
253 // lastTransitionTime is the last time the condition transitioned from
254 // one status to another
255 // +optional
256 LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty" protobuf:"bytes,3,opt,name=lastTransitionTime"`
257 // reason is the reason for the condition's last transition.
258 // +optional
259 Reason string `json:"reason,omitempty" protobuf:"bytes,4,opt,name=reason"`
260 // message is a human-readable explanation containing details about
261 // the transition
262 // +optional
263 Message string `json:"message,omitempty" protobuf:"bytes,5,opt,name=message"`
264}
265
266// MetricStatus describes the last-read state of a single metric.
267type MetricStatus struct {
268 // type is the type of metric source. It will be one of "Object",
269 // "Pods" or "Resource", each corresponds to a matching field in the object.
270 Type MetricSourceType `json:"type" protobuf:"bytes,1,name=type"`
271
272 // object refers to a metric describing a single kubernetes object
273 // (for example, hits-per-second on an Ingress object).
274 // +optional
275 Object *ObjectMetricStatus `json:"object,omitempty" protobuf:"bytes,2,opt,name=object"`
276 // pods refers to a metric describing each pod in the current scale target
277 // (for example, transactions-processed-per-second). The values will be
278 // averaged together before being compared to the target value.
279 // +optional
280 Pods *PodsMetricStatus `json:"pods,omitempty" protobuf:"bytes,3,opt,name=pods"`
281 // resource refers to a resource metric (such as those specified in
282 // requests and limits) known to Kubernetes describing each pod in the
283 // current scale target (e.g. CPU or memory). Such metrics are built in to
284 // Kubernetes, and have special scaling options on top of those available
285 // to normal per-pod metrics using the "pods" source.
286 // +optional
287 Resource *ResourceMetricStatus `json:"resource,omitempty" protobuf:"bytes,4,opt,name=resource"`
288 // external refers to a global metric that is not associated
289 // with any Kubernetes object. It allows autoscaling based on information
290 // coming from components running outside of cluster
291 // (for example length of queue in cloud messaging service, or
292 // QPS from loadbalancer running outside of cluster).
293 // +optional
294 External *ExternalMetricStatus `json:"external,omitempty" protobuf:"bytes,5,opt,name=external"`
295}
296
297// ObjectMetricStatus indicates the current value of a metric describing a
298// kubernetes object (for example, hits-per-second on an Ingress object).
299type ObjectMetricStatus struct {
300 // target is the described Kubernetes object.
301 Target CrossVersionObjectReference `json:"target" protobuf:"bytes,1,name=target"`
302
303 // metricName is the name of the metric in question.
304 MetricName string `json:"metricName" protobuf:"bytes,2,name=metricName"`
305 // currentValue is the current value of the metric (as a quantity).
306 CurrentValue resource.Quantity `json:"currentValue" protobuf:"bytes,3,name=currentValue"`
307
308 // selector is the string-encoded form of a standard kubernetes label selector for the given metric
309 // When set in the ObjectMetricSource, it is passed as an additional parameter to the metrics server for more specific metrics scoping.
310 // When unset, just the metricName will be used to gather metrics.
311 // +optional
312 Selector *metav1.LabelSelector `json:"selector,omitempty" protobuf:"bytes,4,name=selector"`
313 // averageValue is the current value of the average of the
314 // metric across all relevant pods (as a quantity)
315 // +optional
316 AverageValue *resource.Quantity `json:"averageValue,omitempty" protobuf:"bytes,5,name=averageValue"`
317}
318
319// PodsMetricStatus indicates the current value of a metric describing each pod in
320// the current scale target (for example, transactions-processed-per-second).
321type PodsMetricStatus struct {
322 // metricName is the name of the metric in question
323 MetricName string `json:"metricName" protobuf:"bytes,1,name=metricName"`
324 // currentAverageValue is the current value of the average of the
325 // metric across all relevant pods (as a quantity)
326 CurrentAverageValue resource.Quantity `json:"currentAverageValue" protobuf:"bytes,2,name=currentAverageValue"`
327
328 // selector is the string-encoded form of a standard kubernetes label selector for the given metric
329 // When set in the PodsMetricSource, it is passed as an additional parameter to the metrics server for more specific metrics scoping.
330 // When unset, just the metricName will be used to gather metrics.
331 // +optional
332 Selector *metav1.LabelSelector `json:"selector,omitempty" protobuf:"bytes,3,name=selector"`
333}
334
335// ResourceMetricStatus indicates the current value of a resource metric known to
336// Kubernetes, as specified in requests and limits, describing each pod in the
337// current scale target (e.g. CPU or memory). Such metrics are built in to
338// Kubernetes, and have special scaling options on top of those available to
339// normal per-pod metrics using the "pods" source.
340type ResourceMetricStatus struct {
341 // name is the name of the resource in question.
342 Name v1.ResourceName `json:"name" protobuf:"bytes,1,name=name"`
343 // currentAverageUtilization is the current value of the average of the
344 // resource metric across all relevant pods, represented as a percentage of
345 // the requested value of the resource for the pods. It will only be
346 // present if `targetAverageValue` was set in the corresponding metric
347 // specification.
348 // +optional
349 CurrentAverageUtilization *int32 `json:"currentAverageUtilization,omitempty" protobuf:"bytes,2,opt,name=currentAverageUtilization"`
350 // currentAverageValue is the current value of the average of the
351 // resource metric across all relevant pods, as a raw value (instead of as
352 // a percentage of the request), similar to the "pods" metric source type.
353 // It will always be set, regardless of the corresponding metric specification.
354 CurrentAverageValue resource.Quantity `json:"currentAverageValue" protobuf:"bytes,3,name=currentAverageValue"`
355}
356
357// ExternalMetricStatus indicates the current value of a global metric
358// not associated with any Kubernetes object.
359type ExternalMetricStatus struct {
360 // metricName is the name of a metric used for autoscaling in
361 // metric system.
362 MetricName string `json:"metricName" protobuf:"bytes,1,name=metricName"`
363 // metricSelector is used to identify a specific time series
364 // within a given metric.
365 // +optional
366 MetricSelector *metav1.LabelSelector `json:"metricSelector,omitempty" protobuf:"bytes,2,opt,name=metricSelector"`
367 // currentValue is the current value of the metric (as a quantity)
368 CurrentValue resource.Quantity `json:"currentValue" protobuf:"bytes,3,name=currentValue"`
369 // currentAverageValue is the current value of metric averaged over autoscaled pods.
370 // +optional
371 CurrentAverageValue *resource.Quantity `json:"currentAverageValue,omitempty" protobuf:"bytes,4,opt,name=currentAverageValue"`
372}
373
374// +genclient
375// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
376// +k8s:prerelease-lifecycle-gen:introduced=1.8
377// +k8s:prerelease-lifecycle-gen:deprecated=1.22
378// +k8s:prerelease-lifecycle-gen:replacement=autoscaling,v2beta2,HorizontalPodAutoscaler
379
380// HorizontalPodAutoscaler is the configuration for a horizontal pod
381// autoscaler, which automatically manages the replica count of any resource
382// implementing the scale subresource based on the metrics specified.
383type HorizontalPodAutoscaler struct {
384 metav1.TypeMeta `json:",inline"`
385 // metadata is the standard object metadata.
386 // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
387 // +optional
388 metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
389
390 // spec is the specification for the behaviour of the autoscaler.
391 // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status.
392 // +optional
393 Spec HorizontalPodAutoscalerSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
394
395 // status is the current information about the autoscaler.
396 // +optional
397 Status HorizontalPodAutoscalerStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
398}
399
400// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
401// +k8s:prerelease-lifecycle-gen:introduced=1.8
402// +k8s:prerelease-lifecycle-gen:deprecated=1.22
403// +k8s:prerelease-lifecycle-gen:replacement=autoscaling,v2beta2,HorizontalPodAutoscalerList
404
405// HorizontalPodAutoscaler is a list of horizontal pod autoscaler objects.
406type HorizontalPodAutoscalerList struct {
407 metav1.TypeMeta `json:",inline"`
408 // metadata is the standard list metadata.
409 // +optional
410 metav1.ListMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
411
412 // items is the list of horizontal pod autoscaler objects.
413 Items []HorizontalPodAutoscaler `json:"items" protobuf:"bytes,2,rep,name=items"`
414}