-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgather.go
284 lines (253 loc) · 11.4 KB
/
gather.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Modifications Copyright 2024 The K8sHorizMetrics Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Modified to split up evaluations and metric gathering to work with the
Custom Pod Autoscaler framework.
Original source:
https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/podautoscaler/horizontal.go
https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/podautoscaler/replica_calculator.go
*/
package k8shorizmetrics
import (
"fmt"
"time"
"github.com/jthomperoo/k8shorizmetrics/v4/internal/external"
"github.com/jthomperoo/k8shorizmetrics/v4/internal/object"
"github.com/jthomperoo/k8shorizmetrics/v4/internal/pods"
"github.com/jthomperoo/k8shorizmetrics/v4/internal/podutil"
"github.com/jthomperoo/k8shorizmetrics/v4/internal/resource"
"github.com/jthomperoo/k8shorizmetrics/v4/metrics"
externalmetrics "github.com/jthomperoo/k8shorizmetrics/v4/metrics/external"
objectmetrics "github.com/jthomperoo/k8shorizmetrics/v4/metrics/object"
podsmetrics "github.com/jthomperoo/k8shorizmetrics/v4/metrics/pods"
resourcemetrics "github.com/jthomperoo/k8shorizmetrics/v4/metrics/resource"
metricsclient "github.com/jthomperoo/k8shorizmetrics/v4/metricsclient"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
corelisters "k8s.io/client-go/listers/core/v1"
k8sscale "k8s.io/client-go/scale"
)
// GathererMultiMetricError occurs when gathering multiple metrics, if any metric fails to be gathered this error will
// be returned which contains all of the individual errors in the 'Errors' slice, if some metrics were gathered
// successfully the error will have the 'Partial' property set to true.
type GathererMultiMetricError struct {
Partial bool
Errors []error
}
func (e *GathererMultiMetricError) Error() string {
return fmt.Sprintf("gatherer multi metric error: %d errors, first error is %s", len(e.Errors), e.Errors[0])
}
// ExternalGatherer allows retrieval of external metrics.
type ExternalGatherer interface {
Gather(metricName, namespace string, metricSelector *metav1.LabelSelector, podSelector labels.Selector) (*externalmetrics.Metric, error)
GatherPerPod(metricName, namespace string, metricSelector *metav1.LabelSelector) (*externalmetrics.Metric, error)
}
// ObjectGatherer allows retrieval of object metrics.
type ObjectGatherer interface {
Gather(metricName string, namespace string, objectRef *autoscalingv2.CrossVersionObjectReference, podSelector labels.Selector, metricSelector labels.Selector) (*objectmetrics.Metric, error)
GatherPerPod(metricName string, namespace string, objectRef *autoscalingv2.CrossVersionObjectReference, metricSelector labels.Selector) (*objectmetrics.Metric, error)
}
// PodsGatherer allows retrieval of pods metrics.
type PodsGatherer interface {
Gather(metricName string, namespace string, podSelector labels.Selector, metricSelector labels.Selector) (*podsmetrics.Metric, error)
}
// ResourceGatherer allows retrieval of resource metrics.
type ResourceGatherer interface {
Gather(resourceName corev1.ResourceName, namespace string, podSelector labels.Selector,
cpuInitializationPeriod time.Duration, delayOfInitialReadinessStatus time.Duration) (*resourcemetrics.Metric, error)
GatherRaw(resourceName corev1.ResourceName, namespace string, podSelector labels.Selector,
cpuInitializationPeriod time.Duration, delayOfInitialReadinessStatus time.Duration) (*resourcemetrics.Metric, error)
}
// Gatherer provides functionality for retrieving metrics on supplied metric specs.
type Gatherer struct {
Resource ResourceGatherer
Pods PodsGatherer
Object ObjectGatherer
External ExternalGatherer
ScaleClient k8sscale.ScalesGetter
CPUInitializationPeriod time.Duration
DelayOfInitialReadinessStatus time.Duration
}
// NewGatherer sets up a new Metric Gatherer
func NewGatherer(
metricsclient metricsclient.Client,
podlister corelisters.PodLister,
cpuInitializationPeriod time.Duration,
delayOfInitialReadinessStatus time.Duration) *Gatherer {
// Set up pod ready counter
podReadyCounter := &podutil.PodReadyCount{
PodLister: podlister,
}
return &Gatherer{
Resource: &resource.Gather{
MetricsClient: metricsclient,
PodLister: podlister,
},
Pods: &pods.Gather{
MetricsClient: metricsclient,
PodLister: podlister,
},
Object: &object.Gather{
MetricsClient: metricsclient,
PodReadyCounter: podReadyCounter,
},
External: &external.Gather{
MetricsClient: metricsclient,
PodReadyCounter: podReadyCounter,
},
CPUInitializationPeriod: cpuInitializationPeriod,
DelayOfInitialReadinessStatus: delayOfInitialReadinessStatus,
}
}
// Gather returns all of the metrics gathered based on the metric specs provided.
// If an error occurs gathering any metric this will return a GatherMultiMetricError. If a partial error occurs,
// meaning some metrics were gathered successfully and others failed, the 'Partial' property of this error will be
// set to true.
func (c *Gatherer) Gather(specs []autoscalingv2.MetricSpec, namespace string, podSelector labels.Selector) ([]*metrics.Metric, error) {
return c.GatherWithOptions(specs, namespace, podSelector, c.CPUInitializationPeriod, c.DelayOfInitialReadinessStatus)
}
// GatherWithOptions returns all of the metrics gathered based on the metric specs provided with options.
// If an error occurs gathering any metric this will return a GatherMultiMetricError. If a partial error occurs,
// meaning some metrics were gathered successfully and others failed, the 'Partial' property of this error will be
// set to true.
func (c *Gatherer) GatherWithOptions(specs []autoscalingv2.MetricSpec, namespace string, podSelector labels.Selector,
cpuInitializationPeriod time.Duration, delayOfInitialReadinessStatus time.Duration) ([]*metrics.Metric, error) {
combinedMetrics := []*metrics.Metric{}
gatherErrors := []error{}
for _, spec := range specs {
metric, err := c.GatherSingleMetricWithOptions(spec, namespace, podSelector, cpuInitializationPeriod, delayOfInitialReadinessStatus)
if err != nil {
gatherErrors = append(gatherErrors, err)
continue
}
combinedMetrics = append(combinedMetrics, metric)
}
if len(gatherErrors) > 0 {
partial := len(gatherErrors) < len(specs)
if partial {
return combinedMetrics, &GathererMultiMetricError{
Partial: partial,
Errors: gatherErrors,
}
}
return nil, &GathererMultiMetricError{
Partial: partial,
Errors: gatherErrors,
}
}
return combinedMetrics, nil
}
// GatherSingleMetric returns the metric gathered based on a single metric spec.
func (c *Gatherer) GatherSingleMetric(spec autoscalingv2.MetricSpec, namespace string, podSelector labels.Selector) (*metrics.Metric, error) {
return c.GatherSingleMetricWithOptions(spec, namespace, podSelector, c.CPUInitializationPeriod, c.DelayOfInitialReadinessStatus)
}
// GatherSingleMetricWithOptions returns the metric gathered based on a single metric spec with options.
func (c *Gatherer) GatherSingleMetricWithOptions(spec autoscalingv2.MetricSpec, namespace string, podSelector labels.Selector,
cpuInitializationPeriod time.Duration, delayOfInitialReadinessStatus time.Duration) (*metrics.Metric, error) {
switch spec.Type {
case autoscalingv2.ObjectMetricSourceType:
metricSelector, err := metav1.LabelSelectorAsSelector(spec.Object.Metric.Selector)
if err != nil {
return nil, fmt.Errorf("failed to get object metric: %w", err)
}
switch spec.Object.Target.Type {
case autoscalingv2.ValueMetricType:
objectMetric, err := c.Object.Gather(spec.Object.Metric.Name, namespace, &spec.Object.DescribedObject, podSelector, metricSelector)
if err != nil {
return nil, fmt.Errorf("failed to get object metric: %w", err)
}
return &metrics.Metric{
Spec: spec,
Object: objectMetric,
}, nil
case autoscalingv2.AverageValueMetricType:
objectMetric, err := c.Object.GatherPerPod(spec.Object.Metric.Name, namespace, &spec.Object.DescribedObject, metricSelector)
if err != nil {
return nil, fmt.Errorf("failed to get object metric: %w", err)
}
return &metrics.Metric{
Spec: spec,
Object: objectMetric,
}, nil
default:
return nil, fmt.Errorf("invalid object metric source: must be either value or average value")
}
case autoscalingv2.PodsMetricSourceType:
metricSelector, err := metav1.LabelSelectorAsSelector(spec.Pods.Metric.Selector)
if err != nil {
return nil, fmt.Errorf("failed to get pods metric: %w", err)
}
if spec.Pods.Target.Type != autoscalingv2.AverageValueMetricType {
return nil, fmt.Errorf("invalid pods metric source: must be average value")
}
podsMetric, err := c.Pods.Gather(spec.Pods.Metric.Name, namespace, podSelector, metricSelector)
if err != nil {
return nil, fmt.Errorf("failed to get pods metric: %w", err)
}
return &metrics.Metric{
Spec: spec,
Pods: podsMetric,
}, nil
case autoscalingv2.ResourceMetricSourceType:
switch spec.Resource.Target.Type {
case autoscalingv2.AverageValueMetricType:
resourceMetric, err := c.Resource.GatherRaw(spec.Resource.Name, namespace, podSelector, cpuInitializationPeriod, delayOfInitialReadinessStatus)
if err != nil {
return nil, fmt.Errorf("failed to get resource metric: %w", err)
}
return &metrics.Metric{
Spec: spec,
Resource: resourceMetric,
}, nil
case autoscalingv2.UtilizationMetricType:
resourceMetric, err := c.Resource.Gather(spec.Resource.Name, namespace, podSelector, cpuInitializationPeriod, delayOfInitialReadinessStatus)
if err != nil {
return nil, fmt.Errorf("failed to get resource metric: %w", err)
}
return &metrics.Metric{
Spec: spec,
Resource: resourceMetric,
}, nil
default:
return nil, fmt.Errorf("invalid resource metric source: must be either average value or average utilization")
}
case autoscalingv2.ExternalMetricSourceType:
switch spec.External.Target.Type {
case autoscalingv2.ValueMetricType:
externalMetric, err := c.External.Gather(spec.External.Metric.Name, namespace, spec.External.Metric.Selector, podSelector)
if err != nil {
return nil, fmt.Errorf("failed to get external metric: %w", err)
}
return &metrics.Metric{
Spec: spec,
External: externalMetric,
}, nil
case autoscalingv2.AverageValueMetricType:
externalMetric, err := c.External.GatherPerPod(spec.External.Metric.Name, namespace, spec.External.Metric.Selector)
if err != nil {
return nil, fmt.Errorf("failed to get external metric: %w", err)
}
return &metrics.Metric{
Spec: spec,
External: externalMetric,
}, nil
default:
return nil, fmt.Errorf("invalid external metric source: must be either value or average value")
}
default:
return nil, fmt.Errorf("unknown metric source type %q", string(spec.Type))
}
}