forked from kedacore/http-add-on
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
470 lines (413 loc) · 11.4 KB
/
magefile.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//go:build mage
// +build mage
package main
import (
"context"
"fmt"
"github.com/kedacore/http-add-on/pkg/build"
"github.com/kedacore/http-add-on/pkg/env"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Note for Mac M1 users building Docker images:
// If you want to build images for Linux (like, for example, an
// AKS/GKE/EKS, DOKS cluster), you need to use docker's buildx driver
// to do so. This would be the command to build the interceptor for
// 64 Bit Amd and ARM platforms on Linux, for example:
//
// docker buildx build --platform linux/amd64,linux/arm64 --push -t testingkeda.azurecr.io/interceptor:testing -f interceptor/Dockerfile .
//
// See
// https://blog.jaimyn.dev/how-to-build-multi-architecture-docker-images-on-an-m1-mac/
// for more details.
// Global consts
const (
DEFAULT_NAMESPACE string = "kedahttp"
ACR_REGISTRY_NAME = "KEDAHTTP_ACR_REGISTRY"
SCALER_IMAGE_ENV_VAR = "KEDAHTTP_SCALER_IMAGE"
INTERCEPTOR_IMAGE_ENV_VAR = "KEDAHTTP_INTERCEPTOR_IMAGE"
OPERATOR_IMAGE_ENV_VAR = "KEDAHTTP_OPERATOR_IMAGE"
NAMESPACE_ENV_VAR = "KEDAHTTP_NAMESPACE"
)
type Scaler mg.Namespace
// Generate Go build of the scaler binary
func (Scaler) Build(ctx context.Context) error {
fmt.Println("Running scaler binary build")
buildFunc, err := build.GoBuild()
if err != nil {
return err
}
out, err := buildFunc("bin/scaler", "./scaler")
if err != nil {
return err
}
fmt.Println("Finished building scaler")
fmt.Println("Command Output: ", out)
return nil
}
// Run tests on the Scaler
func (Scaler) Test(ctx context.Context) error {
fmt.Println("Running scaler tests")
testOutput, err := sh.Output("go", "test", "./scaler/...")
fmt.Println(testOutput)
if err != nil {
return err
}
return nil
}
func (Scaler) DockerBuild(ctx context.Context) error {
img, err := build.GetImageName(SCALER_IMAGE_ENV_VAR)
if err != nil {
return err
}
return build.DockerBuild(img, "scaler/Dockerfile", ".")
}
// Build the scaler docker image using ACR tasks.
//
// This command reads the value of the following environment variables:
//
// - KEDAHTTP_ACR_REGISTRY - for the value of the --registry flag
// - KEDAHTTP_SCALER_IMAGE -- for the value of the --image flag
//
// it returns an error if either of the env vars are not set or they are and
// the build fails.
func (Scaler) DockerBuildACR(ctx context.Context) error {
registry, err := env.Get(ACR_REGISTRY_NAME)
if err != nil {
return err
}
image, err := build.GetImageName(SCALER_IMAGE_ENV_VAR)
if err != nil {
return err
}
return build.DockerBuildACR(registry, image, "scaler/Dockerfile", ".")
}
func (Scaler) DockerPush(ctx context.Context) error {
image, err := build.GetImageName(SCALER_IMAGE_ENV_VAR)
if err != nil {
return err
}
return build.DockerPush(image)
}
type Operator mg.Namespace
// Generate Go build of the operator binary
func (Operator) Build(ctx context.Context) error {
fmt.Println("Running operator binary build")
buildFunc, err := build.GoBuild()
if err != nil {
return err
}
out, err := buildFunc("bin/operator", "./operator")
if err != nil {
return err
}
fmt.Println("Finished building operator")
fmt.Println("Command Output: ", out)
return nil
}
// Run operator tests
func (Operator) Test(ctx context.Context) error {
fmt.Println("Running operator tests")
testOutput, err := sh.Output("go", "test", "./operator/...")
if err != nil {
return err
}
fmt.Println(testOutput)
return nil
}
func (Operator) DockerBuild(ctx context.Context) error {
img, err := build.GetImageName(OPERATOR_IMAGE_ENV_VAR)
if err != nil {
return err
}
return build.DockerBuild(img, "operator/Dockerfile", ".")
}
// Build the operator docker image using ACR tasks.
//
// This command reads the value of the following environment variables:
//
// - KEDAHTTP_ACR_REGISTRY - for the value of the --registry flag
// - KEDAHTTP_INTERCEPTOR_IMAGE -- for the value of the --image flag
//
// it returns an error if either of the env vars are not set or they are and
// the build fails.
func (Operator) DockerBuildACR(ctx context.Context) error {
registry, err := env.Get(ACR_REGISTRY_NAME)
if err != nil {
return err
}
image, err := build.GetImageName(OPERATOR_IMAGE_ENV_VAR)
if err != nil {
return err
}
return build.DockerBuildACR(registry, image, "operator/Dockerfile", ".")
}
func (Operator) DockerPush(ctx context.Context) error {
image, err := build.GetImageName(OPERATOR_IMAGE_ENV_VAR)
if err != nil {
return err
}
return build.DockerPush(image)
}
type Interceptor mg.Namespace
// Generate Go build of the interceptor binary
func (Interceptor) Build(ctx context.Context) error {
fmt.Println("Running interceptor binary build")
buildFunc, err := build.GoBuild()
if err != nil {
return err
}
out, err := buildFunc("bin/interceptor", "./interceptor")
if err != nil {
return err
}
fmt.Println("Finished building interceptor")
fmt.Println("Command Output: ", out)
return nil
}
// Run interceptor tests
func (Interceptor) Test(ctx context.Context) error {
fmt.Println("Running interceptor tests")
testOutput, err := sh.Output("go", "test", "./interceptor/...")
if err != nil {
return err
}
fmt.Println(testOutput)
return nil
}
// DockerBuild builds the interceptor docker image. It looks for the
// KEDAHTTP_INTERCEPTOR_IMAGE environment variable and builds the image with
// that as the name
func (Interceptor) DockerBuild(ctx context.Context) error {
image, err := build.GetImageName(INTERCEPTOR_IMAGE_ENV_VAR)
if err != nil {
return err
}
return build.DockerBuild(image, "interceptor/Dockerfile", ".")
}
// Build the interceptor docker image using ACR tasks.
//
// This command reads the value of the following environment variables:
//
// - KEDAHTTP_ACR_REGISTRY - for the value of the --registry flag
// - KEDAHTTP_INTERCEPTOR_IMAGE -- for the value of the --image flag
//
// it returns an error if either of the env vars are not set or they are and
// the build fails.
func (Interceptor) DockerBuildACR(ctx context.Context) error {
registry, err := env.Get(ACR_REGISTRY_NAME)
if err != nil {
return err
}
image, err := build.GetImageName(INTERCEPTOR_IMAGE_ENV_VAR)
if err != nil {
return err
}
return build.DockerBuildACR(registry, image, "interceptor/Dockerfile", ".")
}
func (Interceptor) DockerPush(ctx context.Context) error {
image, err := build.GetImageName(INTERCEPTOR_IMAGE_ENV_VAR)
if err != nil {
return err
}
return build.DockerPush(image)
}
// Build all binaries
func Build() {
fmt.Println("Building all binaries")
scaler := Scaler{}
interceptor := Interceptor{}
operator := Operator{}
mg.Deps(scaler.Build, operator.Build, interceptor.Build)
}
// Run tests on all the components in this project
func Test() error {
return sh.RunV("go", "test", "-timeout=20s", "./...")
}
// --- Docker --- //
// DockerBuild builds the operator, scaler and interceptor images in parallel
func DockerBuild(ctx context.Context) error {
scaler, operator, interceptor := Scaler{}, Interceptor{}, Operator{}
mg.Deps(scaler.DockerBuild, operator.DockerBuild, interceptor.DockerBuild)
return nil
}
// DockerBuildACR builds the operator, scaler and interceptor images in parallel,
// all using ACR tasks
func DockerBuildACR(ctx context.Context) error {
scaler, operator, interceptor := Scaler{}, Interceptor{}, Operator{}
mg.Deps(scaler.DockerBuildACR, operator.DockerBuildACR, interceptor.DockerBuildACR)
return nil
}
// Pushes a given image name to a given repository
func DockerPush(ctx context.Context) error {
scaler, operator, interceptor := Scaler{}, Interceptor{}, Operator{}
mg.Deps(scaler.DockerPush, operator.DockerPush, interceptor.DockerPush)
return nil
}
// --- Helm --- //
// Upgrades or installs the Add-on onto the current cluster.
// Issuing "mage helmupgradeoperator kedahttp kedacore" will download
// "kedacore/http-add-on-operator:{SHA}" image and install along with the
// interceptor and scaler images on the same SHA
func UpgradeOperator(ctx context.Context) error {
namespace, err := env.Get(NAMESPACE_ENV_VAR)
if err != nil {
namespace = DEFAULT_NAMESPACE
}
operatorImg, err := build.GetImageName(OPERATOR_IMAGE_ENV_VAR)
if err != nil {
return err
}
scalerImg, err := build.GetImageName(SCALER_IMAGE_ENV_VAR)
if err != nil {
return err
}
interceptorImg, err := build.GetImageName(INTERCEPTOR_IMAGE_ENV_VAR)
if err != nil {
return err
}
if err := sh.RunV(
"helm",
"upgrade",
"kedahttp",
"./charts/keda-http-operator",
"--install",
"--namespace",
namespace,
"--create-namespace",
"--set",
fmt.Sprintf("images.operator=%s", operatorImg),
"--set",
fmt.Sprintf("images.scaler=%s", scalerImg),
"--set",
fmt.Sprintf("images.interceptor=%s", interceptorImg),
); err != nil {
return err
}
return nil
}
// Deletes the operator release
func DeleteOperator(ctx context.Context) error {
namespace, err := env.Get(NAMESPACE_ENV_VAR)
if err != nil {
namespace = DEFAULT_NAMESPACE
}
if err := sh.RunV("helm", "delete", "-n", namespace, "kedahttp"); err != nil {
return err
}
return nil
}
// Installs or upgrades KEDA in the given namespace
func InstallKeda(ctx context.Context) error {
namespace, err := env.Get(NAMESPACE_ENV_VAR)
if err != nil {
namespace = DEFAULT_NAMESPACE
}
if err := sh.RunV(
"helm",
"upgrade",
"kedacore/keda",
"--install",
"--namespace",
namespace,
"--create-namespace",
"--set",
fmt.Sprintf("watchNamespace=%s", namespace),
); err != nil {
return err
}
return nil
}
// Deletes the installed release of KEDA in the given namespaces
func DeleteKeda(ctx context.Context) error {
namespace, err := env.Get(NAMESPACE_ENV_VAR)
if err != nil {
namespace = DEFAULT_NAMESPACE
}
if err := sh.RunV(
"helm",
"delete",
"-n",
namespace,
"keda",
); err != nil {
return err
}
return nil
}
func InstallXKCD(ctx context.Context) error {
namespace, err := env.Get(NAMESPACE_ENV_VAR)
if err != nil {
namespace = DEFAULT_NAMESPACE
}
if err := sh.RunV(
"helm",
"upgrade",
"xkcd",
"./examples/xkcd",
"--install",
"--namespace",
namespace,
"--create-namespace",
); err != nil {
return err
}
return nil
}
// --- Operator tasks --- //
// Generates the operator
func (Operator) Generate() error {
if err := sh.RunV("mage", "-d", "operator", "all"); err != nil {
return err
}
return nil
}
// Rebuilds all manifests for the operator
func (Operator) BuildManifests() error {
if err := sh.RunV("mage", "-d", "operator", "manifests"); err != nil {
return err
}
return nil
}
// --- Misc --- //
// Generates protofiles for external scaler
func (Scaler) GenerateProto() error {
if err := sh.RunV(
"protoc",
"--go_out",
".",
"--go_opt",
"paths=source_relative",
"--go-grpc_out",
".",
"--go-grpc_opt",
"paths=source_relative",
"proto/scaler.proto",
); err != nil {
return err
}
return nil
}
// Create a new example HTTPScaledObject
func NewHTTPSO(ctx context.Context, namespace string) error {
return sh.RunWithV(
make(map[string]string),
"kubectl", "create", "-f", "examples/httpscaledobject.yaml", "-n", namespace,
)
}
func ShowHTTPSO(ctx context.Context, namespace string) error {
return sh.RunWithV(
make(map[string]string),
"kubectl", "get", "httpscaledobject", "-n", namespace,
)
}
// Delete the example HTTPScaledObject created from NewHTTPSO
func DeleteHTTPSO(ctx context.Context, namespace string) error {
return sh.RunWithV(
make(map[string]string),
"kubectl", "delete", "httpscaledobject", "xkcd", "-n", namespace,
)
}
func TestE2E(ctx context.Context) error {
return sh.RunV("go", "test", "-test.v", "./e2e...")
}