/
hpas.go
278 lines (245 loc) · 8.75 KB
/
hpas.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
package internal
import (
"context"
"fmt"
"reflect"
"regexp"
"strings"
"sync"
"sync/atomic"
"github.com/davecgh/go-spew/spew"
"github.com/signalfx/golib/datapoint"
"github.com/signalfx/golib/sfxclient"
autoscaling "k8s.io/api/autoscaling/v2"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/klog"
)
type HPACallback func(ctx context.Context, ms []HPAMetric)
type HPADiscoverer struct {
client kubernetes.Interface
mapper apimeta.RESTMapper
metricConfig map[types.UID][]HPAMetric
updateCallback HPACallback
deleteCallback HPACallback
sync.Mutex
totalHPAsWithMetrics int64
totalMetricsTracked int64
}
func NewHPADiscoverer(client kubernetes.Interface, updateCallback HPACallback, deleteCallback HPACallback, mapper apimeta.RESTMapper) *HPADiscoverer {
if updateCallback == nil {
panic("updateCallack should not be nil")
}
if deleteCallback == nil {
panic("deleteCallack should not be nil")
}
return &HPADiscoverer{
client: client,
metricConfig: make(map[types.UID][]HPAMetric),
updateCallback: updateCallback,
deleteCallback: deleteCallback,
mapper: mapper,
}
}
func (d *HPADiscoverer) Discover(ctx context.Context) {
watchList := cache.NewListWatchFromClient(d.client.AutoscalingV2().RESTClient(), "horizontalpodautoscalers", "", fields.Everything())
_, controller := cache.NewInformer(
watchList,
&autoscaling.HorizontalPodAutoscaler{},
0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
metrics := d.updateHPA(ctx, obj.(*autoscaling.HorizontalPodAutoscaler))
d.updateCallback(ctx, metrics)
if len(metrics) > 0 {
d.totalHPAsWithMetrics++
d.totalMetricsTracked += int64(len(metrics))
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
oldHPA := oldObj.(*autoscaling.HorizontalPodAutoscaler)
newHPA := newObj.(*autoscaling.HorizontalPodAutoscaler)
// The Status field in HPAs changes pretty frequently, so only
// process changes if the spec (where metrics are defined) or
// the annotations (which is where config for this adapter
// lives), is different from the old version.
if reflect.DeepEqual(oldHPA.Spec, newHPA.Spec) && reflect.DeepEqual(oldHPA.Annotations, newHPA.Annotations) {
return
}
oldMetrics := d.metricConfig[newHPA.UID]
d.deleteCallback(ctx, oldMetrics)
newMetrics := d.updateHPA(ctx, newHPA)
d.updateCallback(ctx, newMetrics)
if len(oldMetrics) != len(newMetrics) {
d.totalMetricsTracked += int64(len(newMetrics) - len(oldMetrics))
if len(newMetrics) == 0 {
d.totalHPAsWithMetrics--
}
}
},
DeleteFunc: func(obj interface{}) {
hpa := obj.(*autoscaling.HorizontalPodAutoscaler)
klog.V(2).Infof("HPA was deleted: %v / %v", hpa.Namespace, hpa.Name)
metrics := d.metricConfig[hpa.UID]
delete(d.metricConfig, hpa.UID)
if len(metrics) > 0 {
d.totalHPAsWithMetrics--
d.totalMetricsTracked -= int64(len(metrics))
}
d.deleteCallback(ctx, metrics)
},
})
go controller.Run(ctx.Done())
}
func (d *HPADiscoverer) updateHPA(ctx context.Context, hpa *autoscaling.HorizontalPodAutoscaler) []HPAMetric {
klog.V(2).Infof("HPA was updated: %v / %v", hpa.Namespace, hpa.Name)
metrics := d.extractConfig(ctx, hpa)
klog.V(2).Infof("Extracted from %v / %v HPA: %v", hpa.Namespace, hpa.Name, spew.Sdump(metrics))
d.metricConfig[hpa.UID] = metrics
return metrics
}
var externalRE = regexp.MustCompile(`signalfx.com.external.metric/(?P<metric_name>.+)`)
const customMetricsAnnotation = "signalfx.com.custom.metrics"
func (d *HPADiscoverer) extractConfig(ctx context.Context, hpa *autoscaling.HorizontalPodAutoscaler) []HPAMetric {
metrics := map[string]HPAMetric{}
if whitelist, ok := hpa.Annotations[customMetricsAnnotation]; ok {
whitelistSet := map[string]bool{}
if whitelist != "" {
for _, name := range strings.Split(whitelist, ",") {
whitelistSet[name] = true
}
}
d.populateHPACustomMetrics(ctx, metrics, hpa, whitelistSet)
}
externals := map[string]bool{}
for k, v := range hpa.Annotations {
if !strings.HasPrefix(k, "signalfx.com") {
continue
}
if groups := externalRE.FindStringSubmatch(k); len(groups) > 0 {
name := groups[1]
ex := metrics[name]
ex.Program = strings.TrimSpace(v)
metrics[name] = ex
externals[name] = true
continue
}
if k != customMetricsAnnotation {
klog.Errorf("hpa annotation has invalid metrics key: %s=%s", k, v)
}
}
if len(externals) > 0 {
d.populateHPAExternalMetrics(metrics, hpa, externals)
}
var metricsSlice []HPAMetric
for _, m := range metrics {
metricsSlice = append(metricsSlice, m)
}
return metricsSlice
}
func (d *HPADiscoverer) populateHPACustomMetrics(ctx context.Context, metrics map[string]HPAMetric, hpa *autoscaling.HorizontalPodAutoscaler, filterSet map[string]bool) {
podSelector, err := getPodLabelSelector(ctx, d.client, hpa)
if err != nil {
klog.Errorf("Could not fetch pod selectors from HPA %s/%s target: %v", hpa.Namespace, hpa.Name, err)
}
for _, metric := range hpa.Spec.Metrics {
if metric.Object != nil {
name := metric.Object.Metric.Name
if len(filterSet) > 0 && !filterSet[name] {
continue
}
m := metrics[name]
m.Metric = name
m.Namespace = hpa.Namespace
groupKind := schema.GroupKind{
Kind: metric.Object.DescribedObject.Kind,
}
mapping, err := d.mapper.RESTMapping(groupKind, metric.Object.DescribedObject.APIVersion)
if err != nil {
klog.Errorf("Could not get mapping from %v to resource: %v", groupKind, err)
}
m.GroupResource.Group = mapping.Resource.Group
m.GroupResource.Resource = mapping.Resource.Resource
m.HPAResource = hpa.Spec.ScaleTargetRef
m.TargetName = metric.Object.DescribedObject.Name
if metric.Object.Metric.Selector != nil {
// The requirements come from K8s so they should be pre-validated
m.MetricSelector, _ = metav1.LabelSelectorAsSelector(metric.Object.Metric.Selector)
}
metrics[name] = m
continue
}
if metric.Pods != nil {
name := metric.Pods.Metric.Name
if len(filterSet) > 0 && !filterSet[name] {
continue
}
m := metrics[name]
m.Metric = name
m.Namespace = hpa.Namespace
m.GroupResource.Resource = "pods"
if metric.Pods.Metric.Selector != nil {
// The selector comes from K8s so they should be pre-validated
m.MetricSelector, _ = metav1.LabelSelectorAsSelector(metric.Pods.Metric.Selector)
}
m.HPAResource = hpa.Spec.ScaleTargetRef
m.PodSelector = podSelector
metrics[name] = m
continue
}
}
}
func (d *HPADiscoverer) populateHPAExternalMetrics(metrics map[string]HPAMetric, hpa *autoscaling.HorizontalPodAutoscaler, filterSet map[string]bool) {
for _, metric := range hpa.Spec.Metrics {
if metric.External != nil {
name := metric.External.Metric.Name
if !filterSet[name] {
continue
}
m := metrics[name]
m.Metric = name
m.Namespace = hpa.Namespace
m.HPAResource = hpa.Spec.ScaleTargetRef
if metric.External.Metric.Selector != nil {
klog.Errorf("External metric selector (%v) is used only for identifying the metric, will be ignored in SignalFlow", metric.External.Metric.Selector)
// The requirements come from K8s so they should be pre-validated
m.MetricSelector, _ = metav1.LabelSelectorAsSelector(metric.External.Metric.Selector)
}
metrics[name] = m
continue
}
}
}
// Copied and modified from https://github.com/zalando-incubator/kube-metrics-adapter/blob/9950851cad3a77ab575c78f88ddabf3df5e35039/pkg/collector/pod_collector.go
func getPodLabelSelector(ctx context.Context, client kubernetes.Interface, hpa *autoscaling.HorizontalPodAutoscaler) (labels.Selector, error) {
var selector *metav1.LabelSelector
switch hpa.Spec.ScaleTargetRef.Kind {
case "Deployment":
deployment, err := client.AppsV1().Deployments(hpa.Namespace).Get(ctx, hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
selector = deployment.Spec.Selector
case "StatefulSet":
sts, err := client.AppsV1().StatefulSets(hpa.Namespace).Get(ctx, hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
selector = sts.Spec.Selector
default:
return nil, fmt.Errorf("unable to get pod label selector for scale target ref '%s'", hpa.Spec.ScaleTargetRef.Kind)
}
return metav1.LabelSelectorAsSelector(selector)
}
func (d *HPADiscoverer) InternalMetrics() []*datapoint.Datapoint {
return []*datapoint.Datapoint{
sfxclient.Gauge("hpas_with_metrics", nil, atomic.LoadInt64(&d.totalHPAsWithMetrics)),
sfxclient.Gauge("metrics_tracked", nil, atomic.LoadInt64(&d.totalMetricsTracked)),
}
}