-
Notifications
You must be signed in to change notification settings - Fork 10
/
hpa.go
101 lines (87 loc) · 2.58 KB
/
hpa.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
// Copyright 2018-2020 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package provider
import (
"reflect"
"strings"
log "github.com/sirupsen/logrus"
"github.com/wavefronthq/wavefront-kubernetes-adapter/pkg/config"
"k8s.io/api/autoscaling/v2beta1"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
const (
metricAnnotationPrefix = "wavefront.com.external.metric"
)
type hpaListener struct {
kubeClient kubernetes.Interface
addFunc RuleHandlerFunc
deleteFunc RuleHandlerFunc
}
func StartHPAListener(client kubernetes.Interface, addFunc, deleteFunc RuleHandlerFunc) {
listener := &hpaListener{
kubeClient: client,
addFunc: addFunc,
deleteFunc: deleteFunc,
}
go listener.listen()
}
func (l *hpaListener) listen() {
log.Info("listening for HPA instances")
rc := l.kubeClient.AutoscalingV2beta1().RESTClient()
lw := cache.NewListWatchFromClient(rc, "horizontalpodautoscalers", v1.NamespaceAll, fields.Everything())
inf := cache.NewSharedInformer(lw, &v2beta1.HorizontalPodAutoscaler{}, 0)
inf.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
hpa := obj.(*v2beta1.HorizontalPodAutoscaler)
rules := rulesFromAnnotations(hpa.Annotations)
if len(rules) > 0 {
l.addFunc(rules)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
oldHPA := oldObj.(*v2beta1.HorizontalPodAutoscaler)
newHPA := newObj.(*v2beta1.HorizontalPodAutoscaler)
// HPA objects are updated frequently when status changes
// validate if annotations have changed
if reflect.DeepEqual(oldHPA.Annotations, newHPA.Annotations) {
log.Debugf("annotations have not changed for %s", newHPA.Name)
return
}
oldRules := rulesFromAnnotations(oldHPA.Annotations)
if len(oldRules) > 0 {
l.deleteFunc(oldRules)
}
newRules := rulesFromAnnotations(newHPA.Annotations)
if len(newRules) > 0 {
l.addFunc(newRules)
}
},
DeleteFunc: func(obj interface{}) {
hpa := obj.(*v2beta1.HorizontalPodAutoscaler)
rules := rulesFromAnnotations(hpa.Annotations)
if len(rules) > 0 {
l.deleteFunc(rules)
}
},
})
go inf.Run(wait.NeverStop)
}
func rulesFromAnnotations(annotations map[string]string) []config.MetricRule {
plen := len(metricAnnotationPrefix)
var rules []config.MetricRule
for k, v := range annotations {
if strings.HasPrefix(k, metricAnnotationPrefix) {
if len(k) > plen+1 {
rules = append(rules, config.MetricRule{
Name: k[plen+1:],
Query: v,
})
}
}
}
return rules
}