/
test_autoscaler.go
231 lines (214 loc) · 7.59 KB
/
test_autoscaler.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
package testingframework
import (
"context"
"fmt"
"time"
comm "github.com/berops/claudie/internal/command"
"github.com/berops/claudie/internal/kubectl"
"github.com/berops/claudie/internal/utils"
"github.com/berops/claudie/proto/pb"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
)
const (
// Deployment which should NOT trigger scale up
scaleUpDeploymentIgnored = `apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-ignore
labels:
app: nginx
spec:
replicas: 4
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
resources:
requests:
memory: 8000Mi`
// Deployment which should trigger scale up
scaleUpDeployment = `apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-success
labels:
app: nginx
spec:
replicas: 4
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
resources:
requests:
memory: 500Mi`
// Time in which Autoscaler should trigger scale up
scaleUpTimeout = 3
// Time in which Autoscaler should trigger scale down
scaleDownTimeout = 15
)
// testAutoscaler tests the Autoscaler deployment.
func testAutoscaler(ctx context.Context, config *pb.Config) error {
autoscaledClusters := getAutoscaledClusters(config)
if len(autoscaledClusters) == 0 {
// No clusters are currently autoscaled.
return testLonghornDeployment(ctx, config)
}
c, cc := clientConnection()
defer func() {
err := cc.Close()
if err != nil {
log.Err(err).Msgf("error while closing client connection")
}
}()
var clusterGroup errgroup.Group
for _, cluster := range autoscaledClusters {
func(cluster *pb.K8Scluster) {
clusterGroup.Go(
func() error {
log.Info().Msgf("Deploying pods which should be ignored by autoscaler for cluster %s", cluster.ClusterInfo.Name)
return applyDeployment(cluster, scaleUpDeploymentIgnored)
})
}(cluster)
}
if err := clusterGroup.Wait(); err != nil {
return fmt.Errorf("failed to deploy scale up deployment which should be ignored: %w", err)
}
// Wait before checking for changes
log.Info().Msgf("Waiting %d minutes to see if autoscaler starts the scale up", scaleUpTimeout)
time.Sleep(scaleUpTimeout * time.Minute)
// Check if build has been started, if yes, error.
if res, err := c.GetConfigFromDB(context.Background(), &pb.GetConfigFromDBRequest{Id: config.Id, Type: pb.IdType_HASH}); err == nil {
if !checksumsEqual(res.Config.DsChecksum, res.Config.CsChecksum) {
return fmt.Errorf("some cluster/s in config %s have been scaled up, when they should not", config.Name)
} else {
log.Info().Msgf("Config %s has successfully passed autoscaling test [1/3]", config.Name)
}
} else {
return fmt.Errorf("error while retrieving config %s from DB : %w", config.Name, err)
}
// Apply scale up deployment.
for _, cluster := range autoscaledClusters {
func(cluster *pb.K8Scluster) {
clusterGroup.Go(
func() error {
log.Info().Msgf("Deploying pods which should trigger scale up by autoscaler for cluster %s", cluster.ClusterInfo.Name)
return applyDeployment(cluster, scaleUpDeployment)
})
}(cluster)
}
if err := clusterGroup.Wait(); err != nil {
return fmt.Errorf("failed to deploy scale up deployment : %w", err)
}
// Wait before checking for changes.
log.Info().Msgf("Waiting %d minutes to see if autoscaler starts the scale up", scaleUpTimeout)
time.Sleep(scaleUpTimeout * time.Minute)
// Check if build has been started, if no, error (Scale up).
if res, err := c.GetConfigFromDB(context.Background(), &pb.GetConfigFromDBRequest{Id: config.Id, Type: pb.IdType_HASH}); err == nil {
if checksumsEqual(res.Config.DsChecksum, res.Config.CsChecksum) {
return fmt.Errorf("some cluster/s in config %s have not been scaled up, when they should have", config.Name)
} else {
log.Info().Msgf("Config %s has successfully passed autoscaling test [2/3]", config.Name)
}
} else {
return fmt.Errorf("error while retrieving config %s from DB : %w", config.Name, err)
}
// Wait until build is finished.
if err := configChecker(ctx, c, "autoscaling", "scale-up-test", idInfo{id: config.Id, idType: pb.IdType_HASH}); err != nil {
return err
}
// Test longhorn.
// Get new config from DB with updated counts.
if res, err := c.GetConfigFromDB(context.Background(), &pb.GetConfigFromDBRequest{Id: config.Id, Type: pb.IdType_HASH}); err != nil {
return err
} else {
if err := testLonghornDeployment(ctx, res.Config); err != nil {
return err
}
}
for _, cluster := range autoscaledClusters {
func(cluster *pb.K8Scluster) {
clusterGroup.Go(
func() error {
log.Info().Msgf("Removing pods which should trigger scale down by autoscaler for cluster %s", cluster.ClusterInfo.Name)
return removeDeployment(cluster, scaleUpDeployment)
})
}(cluster)
}
if err := clusterGroup.Wait(); err != nil {
return fmt.Errorf("failed to remove scale up deployment : %w", err)
}
// Wait before checking for changes.
log.Info().Msgf("Waiting %d minutes to let autoscaler start the scale down", scaleDownTimeout)
time.Sleep(scaleDownTimeout * time.Minute)
// Check if build has been started, if not, error (Scale down).
if res, err := c.GetConfigFromDB(context.Background(), &pb.GetConfigFromDBRequest{Id: config.Id, Type: pb.IdType_HASH}); err == nil {
if checksumsEqual(res.Config.DsChecksum, res.Config.CsChecksum) {
return fmt.Errorf("some cluster/s in config %s have not been scaled down, when they should have", config.Name)
} else {
log.Info().Msgf("Config %s has successfully passed autoscaling test [3/3]", config.Name)
}
} else {
return fmt.Errorf("error while retrieving config %s from DB : %w", config.Name, err)
}
// Wait until build is finished.
if err := configChecker(ctx, c, "autoscaling", "scale-down-test", idInfo{id: config.Id, idType: pb.IdType_HASH}); err != nil {
return err
}
// Test longhorn.
// Get new config from DB with updated counts.
if res, err := c.GetConfigFromDB(context.Background(), &pb.GetConfigFromDBRequest{Id: config.Id, Type: pb.IdType_HASH}); err != nil {
return err
} else {
if err := testLonghornDeployment(ctx, res.Config); err != nil {
return err
}
}
return nil
}
// applyDeployment applies specified deployment into specified cluster.
func applyDeployment(c *pb.K8Scluster, deployment string) error {
kc := kubectl.Kubectl{Kubeconfig: c.Kubeconfig, MaxKubectlRetries: 5}
if log.Logger.GetLevel() == zerolog.DebugLevel {
prefix := utils.GetClusterID(c.ClusterInfo)
kc.Stdout = comm.GetStdOut(prefix)
kc.Stderr = comm.GetStdErr(prefix)
}
if err := kc.KubectlApplyString(deployment, "-n", "default"); err != nil {
return fmt.Errorf("failed to apply deployment on cluster %s : %w", c.ClusterInfo.Name, err)
}
return nil
}
// removeDeployment deletes specified deployment from specified cluster.
func removeDeployment(c *pb.K8Scluster, deployment string) error {
kc := kubectl.Kubectl{Kubeconfig: c.Kubeconfig, MaxKubectlRetries: 5}
if log.Logger.GetLevel() == zerolog.DebugLevel {
prefix := utils.GetClusterID(c.ClusterInfo)
kc.Stdout = comm.GetStdOut(prefix)
kc.Stderr = comm.GetStdErr(prefix)
}
if err := kc.KubectlDeleteString(deployment, "-n", "default"); err != nil {
return fmt.Errorf("failed to remove deployment on cluster %s : %w", c.ClusterInfo.Name, err)
}
return nil
}