/
kubernetes_scaler_service.go
149 lines (130 loc) · 4.59 KB
/
kubernetes_scaler_service.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
package compose
import (
"context"
"encoding/json"
"fmt"
"os"
"crypto/sha1"
"encoding/hex"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"github.com/obecto/perper/scaler/pkg/fabric"
)
type KubernetesScalerService interface {
Start(context.Context) error
}
type KubernetesScalerOptions struct {
Namespace string
}
type kubernetesScalerService struct {
options KubernetesScalerOptions
fabric fabric.FabricService
client dynamic.Interface
}
var agentGRV = schema.GroupVersionResource{Group: "kudo.dev", Version: "v1beta1", Resource: "instances"}
var agentInstancesField = []string{"spec", "parameters", "instances"}
var agentOrigInstancesField = []string{"spec", "parameters", "origInstances"}
var agentLabel = "perper.obecto.com/agent"
var instanceLabel = "perper.obecto.com/instance"
var podGRV = schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
func NewKubernetesScalerService(kubernetesClient dynamic.Interface, fabricService fabric.FabricService, options KubernetesScalerOptions) KubernetesScalerService {
return &kubernetesScalerService{
options: options,
fabric: fabricService,
client: kubernetesClient,
}
}
func HashValues(values []fabric.Instance) []string {
var r []string
for _, v := range values {
h := sha1.New()
h.Write([]byte(v))
s := hex.EncodeToString(h.Sum(nil))
r = append(r, s)
}
return r
}
func (c *kubernetesScalerService) Start(ctx context.Context) error {
fmt.Fprintf(os.Stdout, "Starting scaler\n")
for {
select {
case <-ctx.Done():
return nil
case allInstances := <-c.fabric.Instances():
for agent, orgInstances := range allInstances {
agentCopy := string(agent)
fmt.Fprintf(os.Stdout, "Got agent: %s\n", agentCopy)
orgInstancesCopy, err := json.Marshal(orgInstances)
if err != nil {
return err
}
instances := HashValues(orgInstances)
instancesCopy, err := json.Marshal(instances)
if err != nil {
return err
}
resourceIntreface := c.client.Resource(agentGRV).Namespace(c.options.Namespace)
labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{agentLabel: agentCopy}}
result, err := resourceIntreface.List(ctx, metav1.ListOptions{LabelSelector: metav1.FormatLabelSelector(&labelSelector), Limit: 1})
if err != nil {
return err
}
if len(result.Items) == 0 {
fmt.Fprintf(os.Stderr, "Unable to locate agent %s\n", agentCopy)
return nil
}
for _, item := range result.Items {
currentInstancesStr, _, err := unstructured.NestedString(item.Object, agentInstancesField...)
fmt.Fprintf(os.Stdout, "Agent instance: %s with instances: %s to be updated with %s\n", agentCopy, currentInstancesStr, string(instancesCopy))
if len(currentInstancesStr) > 0 {
var currentInstances []string
if err := json.Unmarshal([]byte(currentInstancesStr), ¤tInstances); err != nil {
return err
}
for _, currentInstance := range currentInstances {
found := false
for _, newInstance := range instances {
fmt.Fprintf(os.Stdout, "Compare: %s to with %s\n", currentInstance, string(newInstance))
if currentInstance == string(newInstance) {
found = true
break
}
}
if !found {
podIntreface := c.client.Resource(podGRV).Namespace(c.options.Namespace)
podSelector := metav1.LabelSelector{MatchLabels: map[string]string{instanceLabel: currentInstance}}
fmt.Fprintf(os.Stdout, "Query pods with label %s: %s...\n", instanceLabel, currentInstance)
pods, err := podIntreface.List(ctx, metav1.ListOptions{LabelSelector: metav1.FormatLabelSelector(&podSelector), Limit: 1})
if err != nil {
fmt.Fprintf(os.Stdout, "No pods found - %s", currentInstance)
return err
}
for _, pod := range pods.Items {
fmt.Fprintf(os.Stdout, "Attempt to delete pod %s...\n", pod.GetName())
err = podIntreface.Delete(ctx, pod.GetName(), metav1.DeleteOptions{})
if err != nil {
return err
}
}
}
}
}
err = unstructured.SetNestedField(item.Object, string(instancesCopy), agentInstancesField...)
if err != nil {
return err
}
err = unstructured.SetNestedField(item.Object, string(orgInstancesCopy), agentOrigInstancesField...)
if err != nil {
return err
}
_, err = resourceIntreface.Update(ctx, &item, metav1.UpdateOptions{})
if err != nil {
return err
}
}
}
}
}
}