-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (111 loc) · 4.94 KB
/
main.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
/*
* Copyright (C) 2019 Heinrich-Heine-Universitaet Duesseldorf, Institute of Computer Science, Department Operating Systems
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package main
import (
"flag"
"fmt"
log "github.com/Sirupsen/logrus"
v1 "github.com/grieshaber/generic-autoscaler-controller/pkg/apis/autoscalingrule/v1"
"github.com/grieshaber/generic-autoscaler-controller/pkg/autoscaler"
"github.com/grieshaber/generic-autoscaler-controller/pkg/autoscalerv2"
"github.com/grieshaber/generic-autoscaler-controller/pkg/client/clientset/versioned"
"github.com/grieshaber/generic-autoscaler-controller/pkg/client/informers/externalversions"
"github.com/grieshaber/generic-autoscaler-controller/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"time"
)
var rules = make(map[string]*v1.AutoscalingRule)
func init() {
log.SetLevel(log.DebugLevel)
}
func getConfig() *rest.Config {
config, err := rest.InClusterConfig()
if err != nil {
log.Panic("Could not retrieve config", err)
}
return config
}
func getKubernetesClientset(config *rest.Config) *kubernetes.Clientset {
// creates the clientset for scalable application
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Panic("Could not retrieve clientset for scalable application", err)
}
return clientset
}
func getRulesClientset(config *rest.Config) *versioned.Clientset {
// creates the clientset for autoscaling rules crd
clientset, err := versioned.NewForConfig(config)
if err != nil {
log.Panic("Could not retrieve clientset for autoscaling rules", err)
}
return clientset
}
func createRulesInformer(rulesClientset *versioned.Clientset, namespace string) cache.SharedIndexInformer {
factory := externalversions.NewSharedInformerFactoryWithOptions(rulesClientset, 0, externalversions.WithNamespace(namespace))
informer := factory.Bsinfo().V1().AutoscalingRules().Informer()
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: onAdd,
DeleteFunc: onDelete,
})
return informer
}
func main() {
rulesNamespace := flag.String("rulesNamespace", metav1.NamespaceAll, "Namespace to look for autoscaling rules")
targetNamespace := flag.String("targetNamespace", metav1.NamespaceAll, "Namespace, the target is deployed in")
targetName := flag.String("targetName", "workload-sim-dummy", "Name of the target")
targetKind := flag.String("targetKind", "Deployment", "Kind of the target")
minReplicas := flag.Int("minReplicas", 1, "Minimum number of replicas")
maxReplicas := flag.Int("maxReplicas", 10, "Maximum number of replicas")
calmdownInts := flag.Int64("calmdownInts", 3, "Number of calmdown intervals")
checkInterval := flag.Int("checkInterval", 5, "Period between intervals in s")
usev2 := flag.Bool("usev2", true, "Use advanced rules")
flag.Parse()
fmt.Printf("Starting the GenericAutoscalerController with following Parameters: \n\trulesNamespace: %v"+
"\n\ttargetNamespace: %v \n\ttargetName: %v \n\ttargetKind: %v\n\tminReplicas: %v\n\tmaxReplicas: %v"+
"\n\tcalmdownInts: %v\n\tcheckInterval: %v\n\tusev2: %v", *rulesNamespace, *targetNamespace, *targetName, *targetKind, *minReplicas,
*maxReplicas, *calmdownInts, *checkInterval, *usev2)
target := *util.NewTarget(*targetNamespace, *targetName, *targetKind)
log.Info("Start GA-Controller..")
stopChan := make(chan struct{})
defer close(stopChan)
config := getConfig()
clientset := getKubernetesClientset(config)
rulesClientset := getRulesClientset(config)
log.Debug("Create informer to keep track of autoscaling rules..")
informer := createRulesInformer(rulesClientset, *rulesNamespace)
go informer.Run(stopChan)
log.Info("Infomer started.")
log.Debug("Start autoscaler..")
if *usev2 {
scaler := autoscalerv2.New(clientset, time.Duration(*checkInterval)*time.Second, target, *calmdownInts, rules, *minReplicas, *maxReplicas)
go scaler.Run()
} else {
scaler := autoscaler.New(clientset, time.Duration(*checkInterval)*time.Second, target, *calmdownInts, rules, *minReplicas, *maxReplicas)
go scaler.Run()
}
<-stopChan
log.Info("Stopped application!")
}
func onAdd(obj interface{}) {
rule := obj.(*v1.AutoscalingRule)
rules[rule.Name] = rule
log.Infof("Rule added: %s", rule.Name)
}
func onDelete(obj interface{}) {
rule := obj.(*v1.AutoscalingRule)
delete(rules, rule.Name)
log.Infof("Rule %s deleted", rule.Name)
}