This repository has been archived by the owner on Dec 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
315 lines (269 loc) · 8.28 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
Copyright 2021 The Custom Pod Autoscaler 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.
*/
// Horizontal Pod Autoscaler provides executable Horizontal Pod Autoscaler logic, which
// can be built into a Custom Pod Autoscaler.
// The Horizontal Pod Autoscaler has two modes, metric gathering and evaluation.
// Metric mode gathers metrics, taking in a resource to get the metrics for and outputting
// these metrics as serialised JSON.
// Evaluation mode makes decisions on how many replicas a resource should have, taking in
// metrics and outputting evaluation decisions as seralised JSON.
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"
autoscalingv1 "k8s.io/api/autoscaling/v1"
cpaevaluate "github.com/jthomperoo/custom-pod-autoscaler/v2/evaluate"
cpametric "github.com/jthomperoo/custom-pod-autoscaler/v2/metric"
"github.com/jthomperoo/k8shorizmetrics"
"github.com/jthomperoo/k8shorizmetrics/metrics"
"github.com/jthomperoo/k8shorizmetrics/metricsclient"
"github.com/jthomperoo/k8shorizmetrics/podsclient"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
k8sscale "k8s.io/client-go/scale"
)
const (
defaultTolerance = float64(0.1)
// 5 minute CPU initialization period
defaultCPUInitializationPeriod = 300
// 30 second initial readiness delay
defaultInitialReadinessDelay = 30
)
// EvaluateSpec represents the information fed to the evaluator
type EvaluateSpec struct {
Metrics []*cpametric.ResourceMetric `json:"metrics"`
UnstructuredResource unstructured.Unstructured `json:"resource"`
RunType string `json:"runType"`
}
// MetricSpec represents the information fed to the metric gatherer
type MetricSpec struct {
UnstructuredResource unstructured.Unstructured `json:"resource"`
RunType string `json:"runType"`
}
func main() {
stdin, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
modePtr := flag.String("mode", "no_mode", "command mode, either metric or evaluate")
flag.Parse()
switch *modePtr {
case "metric":
gather(bytes.NewReader(stdin))
case "evaluate":
evaluate(bytes.NewReader(stdin))
default:
log.Fatalf("Unknown command mode: %s", *modePtr)
os.Exit(1)
}
}
func gather(stdin io.Reader) {
var spec MetricSpec
err := yaml.NewYAMLOrJSONDecoder(stdin, 10).Decode(&spec)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
metricSpecsValue, exists := os.LookupEnv("metrics")
if !exists {
log.Fatal("Metric specs not supplied")
os.Exit(1)
}
// Read in metric specs to evaluate
var metricSpecs []autoscalingv2.MetricSpec
err = yaml.NewYAMLOrJSONDecoder(strings.NewReader(metricSpecsValue), 10).Decode(&metricSpecs)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
if len(metricSpecs) == 0 {
log.Fatal("Metric specs not supplied")
os.Exit(1)
}
// Get initial readiness delay, can be set as a configuration variable
initialReadinessDelaySecs, err := parseInt64EnvVar("initialReadinessDelay", defaultInitialReadinessDelay)
if err != nil {
log.Fatalf("Invalid initial readiness delay provided: %e\n", err)
os.Exit(1)
}
// Get CPU initialization period, can be set as a configuration variable
cpuInitializationPeriodSecs, err := parseInt64EnvVar("cpuInitializationPeriod", defaultCPUInitializationPeriod)
if err != nil {
log.Fatalf("Invalid CPU initialization period provided: %e\n", err)
os.Exit(1)
}
clusterConfig, clientset, err := getKubernetesClients()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
scale, err := getScaleSubResource(clientset, &spec.UnstructuredResource)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
metricsclient := metricsclient.NewClient(clusterConfig, clientset.Discovery())
podsclient := &podsclient.OnDemandPodLister{
Clientset: clientset,
}
cpuInitializationPeriod := time.Duration(cpuInitializationPeriodSecs) * time.Second
initialReadinessDelay := time.Duration(initialReadinessDelaySecs) * time.Second
// Create metric gatherer, with required clients and configuration
gatherer := k8shorizmetrics.NewGatherer(metricsclient, podsclient, cpuInitializationPeriod, initialReadinessDelay)
selector, err := labels.Parse(scale.Status.Selector)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
// Get metrics for deployment
metrics, err := gatherer.Gather(metricSpecs, scale.GetNamespace(), selector)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
// Marshal metrics into JSON
jsonMetrics, err := json.Marshal(metrics)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
// Write serialised metrics to stdout
fmt.Print(string(jsonMetrics))
}
func evaluate(stdin io.Reader) {
var spec EvaluateSpec
err := yaml.NewYAMLOrJSONDecoder(stdin, 10).Decode(&spec)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
// Get tolerance, can be set as a configuration variable
var tolerance float64
toleranceValue, exists := os.LookupEnv("tolerance")
if !exists {
// use default
tolerance = defaultTolerance
} else {
// try to parse provided value
tolerance, err = strconv.ParseFloat(toleranceValue, 64)
if err != nil {
log.Fatalf("Invalid tolerance provided: %e\n", err)
os.Exit(1)
}
}
_, clientset, err := getKubernetesClients()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
scale, err := getScaleSubResource(clientset, &spec.UnstructuredResource)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
var combinedMetrics []*metrics.Metric
err = yaml.NewYAMLOrJSONDecoder(strings.NewReader(spec.Metrics[0].Value), 10).Decode(&combinedMetrics)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
evaluator := k8shorizmetrics.NewEvaluator(tolerance)
targetReplicas, err := evaluator.Evaluate(combinedMetrics, scale.Spec.Replicas)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
// Marshal evaluation into JSON
jsonEvaluation, err := json.Marshal(cpaevaluate.Evaluation{
TargetReplicas: targetReplicas,
})
if err != nil {
log.Fatal(err)
os.Exit(1)
}
// Write serialised evaluation to stdout
fmt.Print(string(jsonEvaluation))
}
func getScaleSubResource(clientset *kubernetes.Clientset, resource *unstructured.Unstructured) (*autoscalingv1.Scale, error) {
groupResources, err := restmapper.GetAPIGroupResources(clientset.Discovery())
if err != nil {
return nil, err
}
scaleClient := k8sscale.New(
clientset.RESTClient(),
restmapper.NewDiscoveryRESTMapper(groupResources),
dynamic.LegacyAPIPathResolverFunc,
k8sscale.NewDiscoveryScaleKindResolver(
clientset.Discovery(),
),
)
resourceGV, err := schema.ParseGroupVersion(resource.GetAPIVersion())
if err != nil {
return nil, err
}
targetGR := schema.GroupResource{
Group: resourceGV.Group,
Resource: resource.GetKind(),
}
scale, err := scaleClient.Scales(resource.GetNamespace()).Get(context.Background(), targetGR, resource.GetName(), metav1.GetOptions{})
if err != nil {
return nil, err
}
return scale, nil
}
func getKubernetesClients() (*rest.Config, *kubernetes.Clientset, error) {
clusterConfig, err := rest.InClusterConfig()
if err != nil {
return nil, nil, err
}
clientset, err := kubernetes.NewForConfig(clusterConfig)
if err != nil {
return nil, nil, err
}
return clusterConfig, clientset, nil
}
func parseInt64EnvVar(name string, defaultValue int64) (int64, error) {
var envVar int64
var err error
envVarVal, exists := os.LookupEnv(name)
if !exists {
// use default
envVar = defaultValue
} else {
// try to parse provided value
envVar, err = strconv.ParseInt(envVarVal, 10, 64)
if err != nil {
return 0, err
}
}
return envVar, nil
}