-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
192 lines (162 loc) · 6.84 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
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
/*
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.
*/
package main
import (
"flag"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
log "github.com/golang/glog"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/autoscaler/addon-resizer/nanny"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const noValue = "MISSING"
var (
// Flags to define the resource requirements.
baseCPU = flag.String("cpu", noValue, "The base CPU resource requirement.")
cpuPerNode = flag.String("extra-cpu", "0", "The amount of CPU to add per node.")
baseMemory = flag.String("memory", noValue, "The base memory resource requirement.")
memoryPerNode = flag.String("extra-memory", "0Mi", "The amount of memory to add per node.")
baseStorage = flag.String("storage", noValue, "The base storage resource requirement.")
storagePerNode = flag.String("extra-storage", "0Gi", "The amount of storage to add per node.")
scaleDownDelay = flag.Duration("scale-down-delay", time.Duration(0), "The time to wait after the addon-resizer start or last scaling operation before the scale down can be performed.")
scaleUpDelay = flag.Duration("scale-up-delay", time.Duration(0), "The time to wait after the addon-resizer start or last scaling operation before the scale up can be performed.")
recommendationOffset = flag.Int("recommendation-offset", 10, "A number from range 0-100. When the dependent's resources are rewritten, they are set to the closer end of the range defined by this percentage threshold.")
acceptanceOffset = flag.Int("acceptance-offset", 20, "A number from range 0-100. The dependent's resources are rewritten when they deviate from expected by a percentage that is higher than this threshold. Can't be lower than recommendation-offset.")
// Flags to identify the container to nanny.
podNamespace = flag.String("namespace", os.Getenv("MY_POD_NAMESPACE"), "The namespace of the ward. This defaults to the nanny pod's own namespace.")
deployment = flag.String("deployment", "", "The name of the deployment being monitored. This is required.")
podName = flag.String("pod", os.Getenv("MY_POD_NAME"), "The name of the pod to watch. This defaults to the nanny's own pod.")
containerName = flag.String("container", "pod-nanny", "The name of the container to watch. This defaults to the nanny itself.")
// Flags to control runtime behavior.
pollPeriodMillis = flag.Int("poll-period", 10000, "The time, in milliseconds, to poll the dependent container.")
)
func checkPercentageFlagBounds(flagName string, flagValue int) {
if flagValue < 0 || flagValue > 100 {
log.Fatalf("%s flag must be between 0 and 100 inclusively, was %d.", flagName, flagValue)
}
}
// GetClientOrDie returns a k8s clientset to the request from inside of cluster
func GetClientOrDie() kubernetes.Interface {
config, err := rest.InClusterConfig()
if err != nil {
log.Fatalf("Can not get kubernetes config: %v", err)
}
config.UserAgent = userAgent()
return kubernetes.NewForConfigOrDie(config)
}
func buildOutOfClusterConfig() (*rest.Config, error) {
kubeconfigPath := os.Getenv("KUBECONFIG")
if kubeconfigPath == "" {
kubeconfigPath = os.Getenv("HOME") + "/.kube/config"
}
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
if err != nil {
return nil, err
}
config.UserAgent = userAgent()
return config, nil
}
func userAgent() string {
command := ""
if len(os.Args) > 0 && len(os.Args[0]) > 0 {
command = filepath.Base(os.Args[0])
}
if len(command) == 0 {
command = "addon-resizer"
}
return command + "/" + nanny.AddonResizerVersion
}
// GetClientOutOfClusterOrDie returns a k8s clientset to the request from outside of cluster
func GetClientOutOfClusterOrDie() kubernetes.Interface {
config, err := buildOutOfClusterConfig()
if err != nil {
log.Fatalf("Can not get kubernetes config: %v", err)
}
clientset := kubernetes.NewForConfigOrDie(config)
return clientset
}
func main() {
// Add standard go flags to the flag set, to enable e.g. setting glog flags.
flag.Parse()
// Perform further validation of flags.
if *deployment == "" {
log.Fatal("Must specify a deployment.")
}
checkPercentageFlagBounds("recommendation-offset", *recommendationOffset)
checkPercentageFlagBounds("acceptance-offset", *acceptanceOffset)
pollPeriod := time.Duration(int64(*pollPeriodMillis) * int64(time.Millisecond))
log.Infof("Version: %s", nanny.AddonResizerVersion)
log.Infof("Poll period: %+v", pollPeriod)
log.Infof("Watching namespace: %s, pod: %s, container: %s.", *podNamespace, *podName, *containerName)
log.Infof("cpu: %s, extra_cpu: %s, memory: %s, extra_memory: %s, storage: %s, extra_storage: %s", *baseCPU, *cpuPerNode, *baseMemory, *memoryPerNode, *baseStorage, *storagePerNode)
log.Infof("Accepted range +/-%d%%", *acceptanceOffset)
log.Infof("Recommended range +/-%d%%", *recommendationOffset)
var kubeClient kubernetes.Interface
_, err := rest.InClusterConfig()
if err != nil {
kubeClient = GetClientOutOfClusterOrDie()
} else {
kubeClient = GetClientOrDie()
}
k8s := nanny.NewKubernetesClient(kubeClient, *podNamespace, *deployment, *podName, *containerName)
var resources []nanny.Resource
// Monitor only the resources specified.
if *baseCPU != noValue {
resources = append(resources, nanny.Resource{
Base: resource.MustParse(*baseCPU),
ExtraPerNode: resource.MustParse(*cpuPerNode),
Name: "cpu",
})
}
if *baseMemory != noValue {
resources = append(resources, nanny.Resource{
Base: resource.MustParse(*baseMemory),
ExtraPerNode: resource.MustParse(*memoryPerNode),
Name: "memory",
})
}
if *baseStorage != noValue {
resources = append(resources, nanny.Resource{
Base: resource.MustParse(*baseStorage),
ExtraPerNode: resource.MustParse(*memoryPerNode),
Name: "storage",
})
}
log.Infof("Resources: %+v", resources)
// handle termination info
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
go func() {
<-ch
log.Infof("Received termination, signaling shutdown")
k8s.Stop()
os.Exit(0)
}()
// Begin nannying.
nanny.PollAPIServer(
k8s,
nanny.Estimator{
AcceptanceOffset: int64(*acceptanceOffset),
RecommendationOffset: int64(*recommendationOffset),
Resources: resources,
},
pollPeriod,
*scaleDownDelay,
*scaleUpDelay)
}