-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmagefile.go
521 lines (477 loc) · 14.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//go:build mage
// +build mage
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"gopkg.in/yaml.v2"
)
var (
wrapperCmd = "bin/konvoy-image-wrapper"
baseURL = "https://downloads.d2iq.com/dkp"
containerdURL = "https://packages.d2iq.com/dkp/containerd"
nvidiaURL = "https://download.nvidia.com/XFree86/Linux-x86_64"
pipPackagesFile = "pip-packages.tar.gz"
overrideDirName = "overrides"
fipsSuffix = "_fips"
tgzExt = ".tar.gz"
perm os.FileMode = 0o700
artifactsDir = "artifacts"
)
var (
basic = "basic"
fips = "fips"
fipsKernel = "fips-kernel"
nvidia = "nvidia"
offline = "offline"
offlineFIPS = "offline-fips"
offlineNvidia = "offline-nvidia"
rhck = "rhck"
validOS = []string{
"redhat 8.8",
"redhat 8.10",
"oracle 8.9",
"oracle 9.4",
"flatcar",
"ubuntu 20.04",
"ubuntu 22.04",
"rocky 9.0",
"rocky 9.1",
"rocky 9",
}
validBuildConfig = []string{
basic,
fipsKernel,
fips,
nvidia,
offline,
offlineFIPS,
offlineNvidia,
rhck,
}
aws = "aws"
azure = "azure"
ova = "ova"
gcp = "gcp"
validInfra = []string{
aws,
azure,
ova,
gcp,
}
oldImageFormatVersion = 24
)
var (
dryRunFlag = "--dry-run"
g4dnInstance = "g4dn.2xlarge"
azureGPUInstance = "Standard_NC6s_v3"
azureBuildInstance = "Standard_B2ms"
)
func BuildWrapper() error {
return sh.RunV("make", "build-wrapper")
}
// Runs E2e for images.
func RunE2e(buildOS, buildConfig, buildInfra string, dryRun bool) error {
mg.Deps(BuildWrapper)
err := validateBuildOS(buildOS)
if err != nil {
return err
}
err = validateBuildConfig(buildConfig)
if err != nil {
return err
}
err = validateBuildInfra(buildInfra)
if err != nil {
return err
}
buildPath := getBuildPath(buildOS, buildInfra)
featureOverrides := getOverridesFromBuildConfig(buildConfig)
overrideFlagForCmd := make([]string, 0, len(featureOverrides))
for _, override := range featureOverrides {
fullOverride := fmt.Sprintf("--overrides=%s/%s", overrideDirName, override)
overrideFlagForCmd = append(overrideFlagForCmd, fullOverride)
}
// we need these extra overrides always for ova
if buildConfig == offline || buildConfig == offlineNvidia || buildConfig == offlineFIPS {
infraOverride := getInfraOverride(buildInfra)
fullOverride := fmt.Sprintf("--overrides=%s", infraOverride)
overrideFlagForCmd = append(overrideFlagForCmd, fullOverride)
fmt.Printf("making infraOverride %s \n", infraOverride)
// TODO: @faiq - move this to mage
if err := sh.RunV("make", infraOverride); err != nil {
return fmt.Errorf("failed to create offline infra with override %s %v", infraOverride, err)
}
defer func() {
// TODO: @faiq - move this to mage
if buildInfra == aws {
if err := sh.RunV("make", "infra.aws.destroy"); err != nil {
fmt.Printf("failed to delete offline infra %v\n", err)
}
}
}()
if err := downloadAirgappedArtifacts(buildOS, buildConfig); err != nil {
return fmt.Errorf("failed to fetch airgap artifacts: %w", err)
}
}
if (buildConfig == basic || buildConfig == fips) && buildInfra == ova {
basicOverride := "packer-ova-basic-override.yaml"
basicOverrideFlag := fmt.Sprintf("--overrides=%s", basicOverride)
overrideFlagForCmd = append(overrideFlagForCmd, basicOverrideFlag)
fmt.Printf("making basic override %s \n", basicOverride)
// TODO: @faiq - move this to mage
if err := sh.RunV("make", basicOverride); err != nil {
return fmt.Errorf("failed to override for basic ova %s %v", basicOverride, err)
}
}
args := []string{"build"}
buildCmd := buildInfra
if buildCmd == ova {
buildCmd = "vsphere"
}
args = append(args, buildCmd)
args = append(args, buildPath)
// skip creating image
if dryRun {
overrideFlagForCmd = append(
overrideFlagForCmd,
fmt.Sprintf("--overrides=%s/%s", overrideDirName, "runtags.yaml"))
args = append(args, dryRunFlag)
} else {
releaseOverrideFile, err := getReleaseOverride(buildConfig)
if err != nil {
return fmt.Errorf("failed to create release override file: %w", err)
}
overrideFlagForCmd = append(
overrideFlagForCmd,
fmt.Sprintf("--overrides=%s/%s", overrideDirName, releaseOverrideFile))
}
args = append(args, overrideFlagForCmd...)
vmMachine := getVMForBuild(buildInfra, buildConfig)
if vmMachine != "" {
args = append(args, fmt.Sprintf("--instance-type=%s", vmMachine))
}
// extra args for gcp
if buildInfra == gcp {
args = append(args,
fmt.Sprintf("--project-id=%s", os.Getenv("GCP_PROJECT")),
fmt.Sprintf("--network=%s", os.Getenv("GCP_NETWORK")),
fmt.Sprintf("--image-storage-locations=%s", os.Getenv("GCP_IMAGE_LOCATIONS")))
}
fmt.Printf("Running %s with args %v\n", wrapperCmd, args)
return sh.RunV(wrapperCmd, args...)
}
// Clean up after yourself.
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll("artifacts")
}
func validateBuildOS(buildOS string) error {
if buildOS == "" {
return fmt.Errorf("no buildOS found using %s", buildOS)
}
found := false
for _, valid := range validOS {
if buildOS == valid {
found = true
break
}
}
if !found {
return fmt.Errorf("buildOS %s is invalid must be one of %v", buildOS, validOS)
}
return nil
}
func validateBuildConfig(buildConfig string) error {
if buildConfig == "" {
return fmt.Errorf("no buildConfig found using %s", buildConfig)
}
found := false
for _, valid := range validBuildConfig {
if buildConfig == valid {
found = true
break
}
}
if !found {
return fmt.Errorf("buildConfig %s is invalid must be one of %v", buildConfig, validBuildConfig)
}
return nil
}
func validateBuildInfra(buildInfra string) error {
if buildInfra == "" {
return fmt.Errorf("no buildInfra found using %s", buildInfra)
}
found := false
for _, valid := range validInfra {
if buildInfra == valid {
found = true
break
}
}
if !found {
return fmt.Errorf("buildInfra %s is invalid must be one of %v", buildInfra, validInfra)
}
return nil
}
func getBuildPath(buildOS, buildInfra string) string {
formattedOS := strings.TrimLeft(buildOS, " ")
formattedOS = strings.TrimPrefix(formattedOS, " ")
formattedOS = strings.Replace(formattedOS, " ", "-", 1)
formattedOS = strings.Replace(formattedOS, ".", "", 1)
formattedOS = strings.Replace(formattedOS, "redhat", "rhel", 1)
fileForOS := fmt.Sprintf("%s.yaml", formattedOS)
infraDirForImage := buildInfra
// we change aws for ami
if infraDirForImage == "aws" {
infraDirForImage = "ami"
}
return path.Join("images", infraDirForImage, fileForOS)
}
func getOverridesFromBuildConfig(buildConfig string) []string {
switch buildConfig {
case basic:
return nil
case fipsKernel:
return []string{"fips-configure.yaml", "fips.yaml"}
case fips:
return []string{"fips.yaml"}
case nvidia:
return []string{"nvidia.yaml"}
case offline:
return []string{"offline.yaml"}
case offlineFIPS:
return []string{"offline-fips.yaml", "fips.yaml"}
case offlineNvidia:
return []string{"offline.yaml", "offline-nvidia.yaml"}
case rhck:
return []string{"rhck.yaml"}
}
return nil
}
// getReleaseOverride creates temporary release override file
// The release override file contains metadata about build name that
// get appended to the final image artifact.
// The konvoy e2e tests uses the released image by
// locating the image with kubernetes version and build metadata in the image name
func getReleaseOverride(buildConfig string) (string, error) {
currentDir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("error finding current directory: %w", err)
}
buildNameExtra := "-release"
switch buildConfig {
case fips, offlineFIPS:
// "-fips-release"
buildNameExtra = fmt.Sprintf("-fips%s", buildNameExtra)
case nvidia, offlineNvidia:
// "-nvidia-release"
buildNameExtra = fmt.Sprintf("-nvidia%s", buildNameExtra)
case fipsKernel:
buildNameExtra = fmt.Sprintf("-fips-kernel%s", buildNameExtra)
}
releaseFile := "release.yaml"
releaseOverride := filepath.Join(currentDir, overrideDirName, releaseFile)
content := []byte(fmt.Sprintf("---\nbuild_name_extra: %s\n", buildNameExtra))
if err := os.WriteFile(releaseOverride, content, 0o644); err != nil {
return "", fmt.Errorf("error creating release override file: %w", err)
}
return releaseFile, nil
}
func getInfraOverride(buildInfra string) string {
baseOfflineTemplate := "packer-%s-offline-override.yaml"
switch buildInfra {
case aws:
return fmt.Sprintf(baseOfflineTemplate, aws)
case ova:
return fmt.Sprintf(baseOfflineTemplate, ova)
}
return ""
}
func fetchNvidiaRunFile() error {
config, err := getAnsibleDefaults()
if err != nil {
return fmt.Errorf("failed to parse ansible defaults: %w", err)
}
nvidiaRunfileVersion, ok := config["nvidia_driver_version"].(string)
if !ok {
return fmt.Errorf("could not parse nvidia_driver_version version")
}
u, err := url.Parse(nvidiaURL)
if err != nil {
return fmt.Errorf("failed to parse URL %w", err)
}
runFile := fmt.Sprintf("NVIDIA-Linux-x86_64-%s.run", nvidiaRunfileVersion)
u.Path = path.Join(u.Path, nvidiaRunfileVersion, runFile)
destPath := path.Join("artifacts", runFile)
return downloadArtifact(u, destPath)
}
func getVMForBuild(buildInfra, buildConfig string) string {
switch buildInfra {
case aws:
switch buildConfig {
case nvidia, offlineNvidia:
return g4dnInstance
default:
return ""
}
case azure:
switch buildConfig {
case nvidia:
return azureGPUInstance
default:
return azureBuildInstance
}
default:
return ""
}
}
func downloadAirgappedArtifacts(buildOS, buildConfig string) error {
ansibleDefaults, err := getAnsibleDefaults()
if err != nil {
return fmt.Errorf("failed to parse ansible default vars: %w", err)
}
kubeVersion, ok := ansibleDefaults["kubernetes_version"].(string)
if !ok {
return fmt.Errorf("unable to parse kubernetes_version from ansible defaults")
}
containerdVersion, ok := ansibleDefaults["containerd_version"].(string)
if !ok {
return fmt.Errorf("unable to parse containerd_version from ansible defaults")
}
// Fetch artifacts
isFips := buildConfig == offlineFIPS
if err := fetchImageBundle(kubeVersion, artifactsDir, isFips); err != nil {
return fmt.Errorf("failed to fetch Image bundle %w", err)
}
if err := fetchContainerd(buildOS, artifactsDir, containerdVersion, isFips); err != nil {
return fmt.Errorf("failed to fetch containerd %w", err)
}
if err := fetchPipPackages(artifactsDir); err != nil {
return fmt.Errorf("failed to fetch pip packages %w", err)
}
isGPU := buildConfig == offlineNvidia
if isGPU {
if err := fetchNvidiaRunFile(); err != nil {
return fmt.Errorf("failed to fetch nvidiaRunFile %w", err)
}
}
if err := createOSBundle(buildOS, kubeVersion, artifactsDir, isFips, isGPU); err != nil {
return fmt.Errorf("failed to fetch OS bundle %w", err)
}
return nil
}
func createOSBundle(osName, kubernetesVersion, downloadDir string, fips, gpu bool) error {
osInfo := strings.Replace(osName, " ", "-", 1)
args := []string{
"create-package-bundle", fmt.Sprintf("--os=%s", osInfo),
fmt.Sprintf("--output-directory=%s", artifactsDir),
}
if fips {
args = append(args, "--fips=true")
}
if osName == "redhat 8.8" || osName == "redhat 8.6" {
args = append(args, "--enable-eus-repos=true")
}
if gpu {
args = append(args, "--fetch-kernel-headers=true")
}
return sh.RunV(wrapperCmd, args...)
}
func fetchImageBundle(kubernetesVersion, downloadDir string, fips bool) error {
imageBundleName := fmt.Sprintf("kubernetes-images-%s-d2iq.1", kubernetesVersion)
if fips {
imageBundleName += "-fips"
}
imageBundleName += ".tar"
srcURL, err := url.Parse(baseURL)
if err != nil {
return fmt.Errorf("failed to parse url %w", err)
}
srcURL.Path = path.Join(srcURL.Path, "airgapped", "kubernetes-images", imageBundleName)
destPath := filepath.Join(downloadDir, "images", imageBundleName)
return downloadArtifact(srcURL, destPath)
}
func fetchPipPackages(downloadDir string) error {
srcURL, err := url.Parse(baseURL)
if err != nil {
return fmt.Errorf("failed to parse url %w", err)
}
srcURL.Path = path.Join(srcURL.Path, "airgapped", "pip-packages", pipPackagesFile)
destPath := filepath.Join(downloadDir, pipPackagesFile)
return downloadArtifact(srcURL, destPath)
}
func fetchContainerd(osName, downloadDir, containerdVersion string, fips bool) error {
osInfo := strings.Split(osName, " ")
// TODO: improve this
osMajorMinor := strings.Split(osInfo[1], ".")
osMajor := osMajorMinor[0]
osMinor := osMajorMinor[1]
osDist := osInfo[0]
osDist = strings.Replace(osDist, "redhat", "rhel", 1)
osDist = strings.Replace(osDist, "oracle", "ol", 1)
containerdFile := fmt.Sprintf("containerd-%s-d2iq.1-%s-%s.%s-x86_64", containerdVersion, osDist, osMajor, osMinor)
if fips {
containerdFile += "_fips"
}
containerdFile += tgzExt
srcURL, err := url.Parse(containerdURL)
if err != nil {
return fmt.Errorf("failed to parse URL %w", err)
}
srcURL.Path = path.Join(srcURL.Path, containerdFile)
destPath := filepath.Join(downloadDir, containerdFile)
return downloadArtifact(srcURL, destPath)
}
func downloadArtifact(srcURL *url.URL, destPath string) error {
fmt.Println("Downloading", srcURL.String(), "to", destPath)
resp, err := http.DefaultClient.Do(&http.Request{
Method: http.MethodGet,
URL: srcURL,
})
if err != nil {
return fmt.Errorf("failed to download %s: %w", srcURL.String(), err)
}
defer resp.Body.Close()
destDir := filepath.Dir(destPath)
_, statErr := os.Stat(destDir)
if os.IsNotExist(statErr) {
if err = os.MkdirAll(destDir, perm); err != nil {
return fmt.Errorf("error creating download directory %s: %w", destDir, err)
}
} else if statErr != nil {
//nolint:wrapcheck //error has all context needed
return err
}
out, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("failed to create file %w", err)
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
//nolint:wrapcheck // error has all context needed
return err
}
func getAnsibleDefaults() (map[string]interface{}, error) {
pwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get process's working dir: %w", err)
}
bytes, err := os.ReadFile(path.Join(pwd, "ansible", "group_vars", "all", "defaults.yaml"))
if err != nil {
return nil, fmt.Errorf("failed to read ansible defaults file %w", err)
}
var config map[string]interface{}
if err = yaml.Unmarshal(bytes, &config); err != nil {
return nil, fmt.Errorf("failed to unmarshal ansible defaults %w", err)
}
return config, nil
}