blob: 55b2a0d6b68d34f2cabbcbcabaddc42bada3bbf8 [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 v1
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// specification of a horizontal pod autoscaler.
37type HorizontalPodAutoscalerSpec struct {
38 // reference to scaled resource; horizontal pod autoscaler will learn the current resource consumption
39 // and will set the desired number of pods by using its Scale subresource.
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 // upper limit for the number of pods that can be set by the autoscaler; cannot be smaller than MinReplicas.
49 MaxReplicas int32 `json:"maxReplicas" protobuf:"varint,3,opt,name=maxReplicas"`
50 // target average CPU utilization (represented as a percentage of requested CPU) over all the pods;
51 // if not specified the default autoscaling policy will be used.
52 // +optional
53 TargetCPUUtilizationPercentage *int32 `json:"targetCPUUtilizationPercentage,omitempty" protobuf:"varint,4,opt,name=targetCPUUtilizationPercentage"`
54}
55
56// current status of a horizontal pod autoscaler
57type HorizontalPodAutoscalerStatus struct {
58 // most recent generation observed by this autoscaler.
59 // +optional
60 ObservedGeneration *int64 `json:"observedGeneration,omitempty" protobuf:"varint,1,opt,name=observedGeneration"`
61
62 // last time the HorizontalPodAutoscaler scaled the number of pods;
63 // used by the autoscaler to control how often the number of pods is changed.
64 // +optional
65 LastScaleTime *metav1.Time `json:"lastScaleTime,omitempty" protobuf:"bytes,2,opt,name=lastScaleTime"`
66
67 // current number of replicas of pods managed by this autoscaler.
68 CurrentReplicas int32 `json:"currentReplicas" protobuf:"varint,3,opt,name=currentReplicas"`
69
70 // desired number of replicas of pods managed by this autoscaler.
71 DesiredReplicas int32 `json:"desiredReplicas" protobuf:"varint,4,opt,name=desiredReplicas"`
72
73 // current average CPU utilization over all pods, represented as a percentage of requested CPU,
74 // e.g. 70 means that an average pod is using now 70% of its requested CPU.
75 // +optional
76 CurrentCPUUtilizationPercentage *int32 `json:"currentCPUUtilizationPercentage,omitempty" protobuf:"varint,5,opt,name=currentCPUUtilizationPercentage"`
77}
78
79// +genclient
80// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
81
82// configuration of a horizontal pod autoscaler.
83type HorizontalPodAutoscaler struct {
84 metav1.TypeMeta `json:",inline"`
85 // Standard object metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
86 // +optional
87 metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
88
89 // behaviour of autoscaler. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status.
90 // +optional
91 Spec HorizontalPodAutoscalerSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
92
93 // current information about the autoscaler.
94 // +optional
95 Status HorizontalPodAutoscalerStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
96}
97
98// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
99
100// list of horizontal pod autoscaler objects.
101type HorizontalPodAutoscalerList struct {
102 metav1.TypeMeta `json:",inline"`
103 // Standard list metadata.
104 // +optional
105 metav1.ListMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
106
107 // list of horizontal pod autoscaler objects.
108 Items []HorizontalPodAutoscaler `json:"items" protobuf:"bytes,2,rep,name=items"`
109}
110
111// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
112
113// Scale represents a scaling request for a resource.
114type Scale struct {
115 metav1.TypeMeta `json:",inline"`
116 // Standard object metadata; More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata.
117 // +optional
118 metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
119
120 // defines the behavior of the scale. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status.
121 // +optional
122 Spec ScaleSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
123
124 // current status of the scale. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status. Read-only.
125 // +optional
126 Status ScaleStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
127}
128
129// ScaleSpec describes the attributes of a scale subresource.
130type ScaleSpec struct {
131 // desired number of instances for the scaled object.
132 // +optional
133 Replicas int32 `json:"replicas,omitempty" protobuf:"varint,1,opt,name=replicas"`
134}
135
136// ScaleStatus represents the current status of a scale subresource.
137type ScaleStatus struct {
138 // actual number of observed instances of the scaled object.
139 Replicas int32 `json:"replicas" protobuf:"varint,1,opt,name=replicas"`
140
141 // label query over pods that should match the replicas count. This is same
142 // as the label selector but in the string format to avoid introspection
143 // by clients. The string will be in the same format as the query-param syntax.
144 // More info about label selectors: http://kubernetes.io/docs/user-guide/labels#label-selectors
145 // +optional
146 Selector string `json:"selector,omitempty" protobuf:"bytes,2,opt,name=selector"`
147}
148
149// the types below are used in the alpha metrics annotation
150
151// MetricSourceType indicates the type of metric.
152type MetricSourceType string
153
154const (
155 // ObjectMetricSourceType is a metric describing a kubernetes object
156 // (for example, hits-per-second on an Ingress object).
157 ObjectMetricSourceType MetricSourceType = "Object"
158 // PodsMetricSourceType is a metric describing each pod in the current scale
159 // target (for example, transactions-processed-per-second). The values
160 // will be averaged together before being compared to the target value.
161 PodsMetricSourceType MetricSourceType = "Pods"
162 // ResourceMetricSourceType is a resource metric known to Kubernetes, as
163 // specified in requests and limits, describing each pod in the current
164 // scale target (e.g. CPU or memory). Such metrics are built in to
165 // Kubernetes, and have special scaling options on top of those available
166 // to normal per-pod metrics (the "pods" source).
167 ResourceMetricSourceType MetricSourceType = "Resource"
168 // ExternalMetricSourceType is a global metric that is not associated
169 // with any Kubernetes object. It allows autoscaling based on information
170 // coming from components running outside of cluster
171 // (for example length of queue in cloud messaging service, or
172 // QPS from loadbalancer running outside of cluster).
173 ExternalMetricSourceType MetricSourceType = "External"
174)
175
176// MetricSpec specifies how to scale based on a single metric
177// (only `type` and one other matching field should be set at once).
178type MetricSpec struct {
179 // type is the type of metric source. It should be one of "Object",
180 // "Pods" or "Resource", each mapping to a matching field in the object.
181 Type MetricSourceType `json:"type" protobuf:"bytes,1,name=type"`
182
183 // object refers to a metric describing a single kubernetes object
184 // (for example, hits-per-second on an Ingress object).
185 // +optional
186 Object *ObjectMetricSource `json:"object,omitempty" protobuf:"bytes,2,opt,name=object"`
187 // pods refers to a metric describing each pod in the current scale target
188 // (for example, transactions-processed-per-second). The values will be
189 // averaged together before being compared to the target value.
190 // +optional
191 Pods *PodsMetricSource `json:"pods,omitempty" protobuf:"bytes,3,opt,name=pods"`
192 // resource refers to a resource metric (such as those specified in
193 // requests and limits) known to Kubernetes describing each pod in the
194 // current scale target (e.g. CPU or memory). Such metrics are built in to
195 // Kubernetes, and have special scaling options on top of those available
196 // to normal per-pod metrics using the "pods" source.
197 // +optional
198 Resource *ResourceMetricSource `json:"resource,omitempty" protobuf:"bytes,4,opt,name=resource"`
199 // external refers to a global metric that is not associated
200 // with any Kubernetes object. It allows autoscaling based on information
201 // coming from components running outside of cluster
202 // (for example length of queue in cloud messaging service, or
203 // QPS from loadbalancer running outside of cluster).
204 // +optional
205 External *ExternalMetricSource `json:"external,omitempty" protobuf:"bytes,5,opt,name=external"`
206}
207
208// ObjectMetricSource indicates how to scale on a metric describing a
209// kubernetes object (for example, hits-per-second on an Ingress object).
210type ObjectMetricSource struct {
211 // target is the described Kubernetes object.
212 Target CrossVersionObjectReference `json:"target" protobuf:"bytes,1,name=target"`
213
214 // metricName is the name of the metric in question.
215 MetricName string `json:"metricName" protobuf:"bytes,2,name=metricName"`
216 // targetValue is the target value of the metric (as a quantity).
217 TargetValue resource.Quantity `json:"targetValue" protobuf:"bytes,3,name=targetValue"`
218
219 // selector is the string-encoded form of a standard kubernetes label selector for the given metric.
220 // When set, it is passed as an additional parameter to the metrics server for more specific metrics scoping
221 // When unset, just the metricName will be used to gather metrics.
222 // +optional
223 Selector *metav1.LabelSelector `json:"selector,omitempty" protobuf:"bytes,4,name=selector"`
224 // averageValue is the target value of the average of the
225 // metric across all relevant pods (as a quantity)
226 // +optional
227 AverageValue *resource.Quantity `json:"averageValue,omitempty" protobuf:"bytes,5,name=averageValue"`
228}
229
230// PodsMetricSource indicates how to scale on a metric describing each pod in
231// the current scale target (for example, transactions-processed-per-second).
232// The values will be averaged together before being compared to the target
233// value.
234type PodsMetricSource struct {
235 // metricName is the name of the metric in question
236 MetricName string `json:"metricName" protobuf:"bytes,1,name=metricName"`
237 // targetAverageValue is the target value of the average of the
238 // metric across all relevant pods (as a quantity)
239 TargetAverageValue resource.Quantity `json:"targetAverageValue" protobuf:"bytes,2,name=targetAverageValue"`
240
241 // selector is the string-encoded form of a standard kubernetes label selector for the given metric
242 // When set, it is passed as an additional parameter to the metrics server for more specific metrics scoping
243 // When unset, just the metricName will be used to gather metrics.
244 // +optional
245 Selector *metav1.LabelSelector `json:"selector,omitempty" protobuf:"bytes,3,name=selector"`
246}
247
248// ResourceMetricSource indicates how to scale on a resource metric known to
249// Kubernetes, as specified in requests and limits, describing each pod in the
250// current scale target (e.g. CPU or memory). The values will be averaged
251// together before being compared to the target. Such metrics are built in to
252// Kubernetes, and have special scaling options on top of those available to
253// normal per-pod metrics using the "pods" source. Only one "target" type
254// should be set.
255type ResourceMetricSource struct {
256 // name is the name of the resource in question.
257 Name v1.ResourceName `json:"name" protobuf:"bytes,1,name=name"`
258 // targetAverageUtilization is the target value of the average of the
259 // resource metric across all relevant pods, represented as a percentage of
260 // the requested value of the resource for the pods.
261 // +optional
262 TargetAverageUtilization *int32 `json:"targetAverageUtilization,omitempty" protobuf:"varint,2,opt,name=targetAverageUtilization"`
263 // targetAverageValue is the target value of the average of the
264 // resource metric across all relevant pods, as a raw value (instead of as
265 // a percentage of the request), similar to the "pods" metric source type.
266 // +optional
267 TargetAverageValue *resource.Quantity `json:"targetAverageValue,omitempty" protobuf:"bytes,3,opt,name=targetAverageValue"`
268}
269
270// ExternalMetricSource indicates how to scale on a metric not associated with
271// any Kubernetes object (for example length of queue in cloud
272// messaging service, or QPS from loadbalancer running outside of cluster).
273type ExternalMetricSource struct {
274 // metricName is the name of the metric in question.
275 MetricName string `json:"metricName" protobuf:"bytes,1,name=metricName"`
276 // metricSelector is used to identify a specific time series
277 // within a given metric.
278 // +optional
279 MetricSelector *metav1.LabelSelector `json:"metricSelector,omitempty" protobuf:"bytes,2,opt,name=metricSelector"`
280 // targetValue is the target value of the metric (as a quantity).
281 // Mutually exclusive with TargetAverageValue.
282 // +optional
283 TargetValue *resource.Quantity `json:"targetValue,omitempty" protobuf:"bytes,3,opt,name=targetValue"`
284 // targetAverageValue is the target per-pod value of global metric (as a quantity).
285 // Mutually exclusive with TargetValue.
286 // +optional
287 TargetAverageValue *resource.Quantity `json:"targetAverageValue,omitempty" protobuf:"bytes,4,opt,name=targetAverageValue"`
288}
289
290// MetricStatus describes the last-read state of a single metric.
291type MetricStatus struct {
292 // type is the type of metric source. It will be one of "Object",
293 // "Pods" or "Resource", each corresponds to a matching field in the object.
294 Type MetricSourceType `json:"type" protobuf:"bytes,1,name=type"`
295
296 // object refers to a metric describing a single kubernetes object
297 // (for example, hits-per-second on an Ingress object).
298 // +optional
299 Object *ObjectMetricStatus `json:"object,omitempty" protobuf:"bytes,2,opt,name=object"`
300 // pods refers to a metric describing each pod in the current scale target
301 // (for example, transactions-processed-per-second). The values will be
302 // averaged together before being compared to the target value.
303 // +optional
304 Pods *PodsMetricStatus `json:"pods,omitempty" protobuf:"bytes,3,opt,name=pods"`
305 // resource refers to a resource metric (such as those specified in
306 // requests and limits) known to Kubernetes describing each pod in the
307 // current scale target (e.g. CPU or memory). Such metrics are built in to
308 // Kubernetes, and have special scaling options on top of those available
309 // to normal per-pod metrics using the "pods" source.
310 // +optional
311 Resource *ResourceMetricStatus `json:"resource,omitempty" protobuf:"bytes,4,opt,name=resource"`
312 // external refers to a global metric that is not associated
313 // with any Kubernetes object. It allows autoscaling based on information
314 // coming from components running outside of cluster
315 // (for example length of queue in cloud messaging service, or
316 // QPS from loadbalancer running outside of cluster).
317 // +optional
318 External *ExternalMetricStatus `json:"external,omitempty" protobuf:"bytes,5,opt,name=external"`
319}
320
321// HorizontalPodAutoscalerConditionType are the valid conditions of
322// a HorizontalPodAutoscaler.
323type HorizontalPodAutoscalerConditionType string
324
325const (
326 // ScalingActive indicates that the HPA controller is able to scale if necessary:
327 // it's correctly configured, can fetch the desired metrics, and isn't disabled.
328 ScalingActive HorizontalPodAutoscalerConditionType = "ScalingActive"
329 // AbleToScale indicates a lack of transient issues which prevent scaling from occurring,
330 // such as being in a backoff window, or being unable to access/update the target scale.
331 AbleToScale HorizontalPodAutoscalerConditionType = "AbleToScale"
332 // ScalingLimited indicates that the calculated scale based on metrics would be above or
333 // below the range for the HPA, and has thus been capped.
334 ScalingLimited HorizontalPodAutoscalerConditionType = "ScalingLimited"
335)
336
337// HorizontalPodAutoscalerCondition describes the state of
338// a HorizontalPodAutoscaler at a certain point.
339type HorizontalPodAutoscalerCondition struct {
340 // type describes the current condition
341 Type HorizontalPodAutoscalerConditionType `json:"type" protobuf:"bytes,1,name=type"`
342 // status is the status of the condition (True, False, Unknown)
343 Status v1.ConditionStatus `json:"status" protobuf:"bytes,2,name=status"`
344 // lastTransitionTime is the last time the condition transitioned from
345 // one status to another
346 // +optional
347 LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty" protobuf:"bytes,3,opt,name=lastTransitionTime"`
348 // reason is the reason for the condition's last transition.
349 // +optional
350 Reason string `json:"reason,omitempty" protobuf:"bytes,4,opt,name=reason"`
351 // message is a human-readable explanation containing details about
352 // the transition
353 // +optional
354 Message string `json:"message,omitempty" protobuf:"bytes,5,opt,name=message"`
355}
356
357// ObjectMetricStatus indicates the current value of a metric describing a
358// kubernetes object (for example, hits-per-second on an Ingress object).
359type ObjectMetricStatus struct {
360 // target is the described Kubernetes object.
361 Target CrossVersionObjectReference `json:"target" protobuf:"bytes,1,name=target"`
362
363 // metricName is the name of the metric in question.
364 MetricName string `json:"metricName" protobuf:"bytes,2,name=metricName"`
365 // currentValue is the current value of the metric (as a quantity).
366 CurrentValue resource.Quantity `json:"currentValue" protobuf:"bytes,3,name=currentValue"`
367
368 // selector is the string-encoded form of a standard kubernetes label selector for the given metric
369 // When set in the ObjectMetricSource, it is passed as an additional parameter to the metrics server for more specific metrics scoping.
370 // When unset, just the metricName will be used to gather metrics.
371 // +optional
372 Selector *metav1.LabelSelector `json:"selector,omitempty" protobuf:"bytes,4,name=selector"`
373 // averageValue is the current value of the average of the
374 // metric across all relevant pods (as a quantity)
375 // +optional
376 AverageValue *resource.Quantity `json:"averageValue,omitempty" protobuf:"bytes,5,name=averageValue"`
377}
378
379// PodsMetricStatus indicates the current value of a metric describing each pod in
380// the current scale target (for example, transactions-processed-per-second).
381type PodsMetricStatus struct {
382 // metricName is the name of the metric in question
383 MetricName string `json:"metricName" protobuf:"bytes,1,name=metricName"`
384 // currentAverageValue is the current value of the average of the
385 // metric across all relevant pods (as a quantity)
386 CurrentAverageValue resource.Quantity `json:"currentAverageValue" protobuf:"bytes,2,name=currentAverageValue"`
387
388 // selector is the string-encoded form of a standard kubernetes label selector for the given metric
389 // When set in the PodsMetricSource, it is passed as an additional parameter to the metrics server for more specific metrics scoping.
390 // When unset, just the metricName will be used to gather metrics.
391 // +optional
392 Selector *metav1.LabelSelector `json:"selector,omitempty" protobuf:"bytes,3,name=selector"`
393}
394
395// ResourceMetricStatus indicates the current value of a resource metric known to
396// Kubernetes, as specified in requests and limits, describing each pod in the
397// current scale target (e.g. CPU or memory). Such metrics are built in to
398// Kubernetes, and have special scaling options on top of those available to
399// normal per-pod metrics using the "pods" source.
400type ResourceMetricStatus struct {
401 // name is the name of the resource in question.
402 Name v1.ResourceName `json:"name" protobuf:"bytes,1,name=name"`
403 // currentAverageUtilization is the current value of the average of the
404 // resource metric across all relevant pods, represented as a percentage of
405 // the requested value of the resource for the pods. It will only be
406 // present if `targetAverageValue` was set in the corresponding metric
407 // specification.
408 // +optional
409 CurrentAverageUtilization *int32 `json:"currentAverageUtilization,omitempty" protobuf:"bytes,2,opt,name=currentAverageUtilization"`
410 // currentAverageValue is the current value of the average of the
411 // resource metric across all relevant pods, as a raw value (instead of as
412 // a percentage of the request), similar to the "pods" metric source type.
413 // It will always be set, regardless of the corresponding metric specification.
414 CurrentAverageValue resource.Quantity `json:"currentAverageValue" protobuf:"bytes,3,name=currentAverageValue"`
415}
416
417// ExternalMetricStatus indicates the current value of a global metric
418// not associated with any Kubernetes object.
419type ExternalMetricStatus struct {
420 // metricName is the name of a metric used for autoscaling in
421 // metric system.
422 MetricName string `json:"metricName" protobuf:"bytes,1,name=metricName"`
423 // metricSelector is used to identify a specific time series
424 // within a given metric.
425 // +optional
426 MetricSelector *metav1.LabelSelector `json:"metricSelector,omitempty" protobuf:"bytes,2,opt,name=metricSelector"`
427 // currentValue is the current value of the metric (as a quantity)
428 CurrentValue resource.Quantity `json:"currentValue" protobuf:"bytes,3,name=currentValue"`
429 // currentAverageValue is the current value of metric averaged over autoscaled pods.
430 // +optional
431 CurrentAverageValue *resource.Quantity `json:"currentAverageValue,omitempty" protobuf:"bytes,4,opt,name=currentAverageValue"`
432}