-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaler.go
94 lines (78 loc) · 2.6 KB
/
scaler.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
package scaler
import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/ryanmt/cluster-resource-autoscaler/check"
"github.com/ryanmt/cluster-resource-autoscaler/kubeapi"
"github.com/ryanmt/cluster-resource-autoscaler/logging"
autoscalingv1 "k8s.io/api/autoscaling/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
cacheddiscovery "k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/scale"
)
var ctx context.Context
var logger logr.Logger
var scaler scale.ScalesGetter
func Init(initCtx context.Context) {
var err error
logger = logging.FromContextOrDiscard(initCtx)
ctx = initCtx
config := kubeapi.Config
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
panic(err.Error())
}
cachedDiscoveryClient := cacheddiscovery.NewMemCacheClient(discoveryClient)
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(cachedDiscoveryClient)
restMapper.Reset()
scaleKindResolver := scale.NewDiscoveryScaleKindResolver(discoveryClient)
scaler, err = scale.NewForConfig(config, restMapper, dynamic.LegacyAPIPathResolverFunc, scaleKindResolver)
if err != nil {
panic(err.Error())
}
}
func GetReplicas(target check.ScalingTarget) (int32, error) {
gr := lookupGroupResource(target)
currentScale, err := scaler.Scales(target.Namespace).Get(ctx, gr, target.Name, metav1.GetOptions{})
if err != nil {
return 0, err
}
logger.V(2).Info("Current scale", "scale", currentScale.Status.Replicas, "target", target.Key())
return currentScale.Status.Replicas, nil
}
func UpdateReplicas(target check.ScalingTarget, desiredReplicas int32) (prevReplicas int32, err error) {
gr := lookupGroupResource(target)
s := &autoscalingv1.Scale{
ObjectMeta: metav1.ObjectMeta{
Name: target.Name,
Namespace: target.Namespace,
},
Spec: autoscalingv1.ScaleSpec{
Replicas: desiredReplicas,
},
}
newScale, err := scaler.Scales(target.Namespace).Update(ctx, gr, s, metav1.UpdateOptions{})
if err != nil {
logger.Error(err, "WTH", "newScale", newScale, "gr", gr)
return newScale.Spec.Replicas, err
}
logger.V(2).Info("Scaling complete", "scale", newScale)
return newScale.Status.Replicas, nil
}
func lookupGroupResource(target check.ScalingTarget) schema.GroupResource {
var group string = "apps"
// switch target.Kind {
// case "deployment":
// group = "apps"
// case "replicaset":
// group = "apps"
// case "statefulset":
// group = "apps"
// }
return schema.ParseGroupResource(fmt.Sprintf("%s.%s", target.Kind, group))
}