forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure.go
771 lines (698 loc) · 24.5 KB
/
azure.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
"github.com/pelletier/go-toml"
"k8s.io/test-infra/kubetest/e2e"
"k8s.io/test-infra/kubetest/process"
"k8s.io/test-infra/kubetest/util"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/satori/go.uuid"
)
var (
// azure specific flags
acsResourceName = flag.String("acsengine-resource-name", "", "Azure Resource Name")
acsResourceGroupName = flag.String("acsengine-resourcegroup-name", "", "Azure Resource Group Name")
acsLocation = flag.String("acsengine-location", "", "Azure ACS location")
acsMasterVmSize = flag.String("acsengine-mastervmsize", "", "Azure Master VM size")
acsAgentVmSize = flag.String("acsengine-agentvmsize", "", "Azure Agent VM size")
acsAdminUsername = flag.String("acsengine-admin-username", "", "Admin username")
acsAdminPassword = flag.String("acsengine-admin-password", "", "Admin password")
acsAgentPoolCount = flag.Int("acsengine-agentpoolcount", 0, "Azure Agent Pool Count")
acsTemplateURL = flag.String("acsengine-template-url", "", "Azure Template URL.")
acsDnsPrefix = flag.String("acsengine-dnsprefix", "", "Azure K8s Master DNS Prefix")
acsEngineURL = flag.String("acsengine-download-url", "", "Download URL for ACS engine")
acsEngineMD5 = flag.String("acsengine-md5-sum", "", "Checksum for acs engine download")
acsSSHPublicKeyPath = flag.String("acsengine-public-key", "", "Path to SSH Public Key")
acsWinBinaries = flag.Bool("acsengine-win-binaries", false, "Set to True if you want kubetest to build a custom zip with windows binaries for aks-engine")
acsHyperKube = flag.Bool("acsengine-hyperkube", false, "Set to True if you want kubetest to build a custom hyperkube for aks-engine")
acsCcm = flag.Bool("acsengine-ccm", false, "Set to True if you want kubetest to build a custom cloud controller manager for aks-engine")
acsCredentialsFile = flag.String("acsengine-creds", "", "Path to credential file for Azure")
acsOrchestratorRelease = flag.String("acsengine-orchestratorRelease", "", "Orchestrator Profile for acs-engine")
acsWinZipBuildScript = flag.String("acsengine-winZipBuildScript", "https://raw.githubusercontent.com/Azure/acs-engine/master/scripts/build-windows-k8s.sh", "Build script to create custom zip containing win binaries for acs-engine")
acsNetworkPlugin = flag.String("acsengine-networkPlugin", "azure", "Network pluging to use with acs-engine")
testCcm = flag.Bool("test-ccm", false, "Set to True if you want kubetest to run e2e tests for ccm")
)
type Creds struct {
ClientID string
ClientSecret string
TenantID string
SubscriptionID string
StorageAccountName string
StorageAccountKey string
}
type Config struct {
Creds Creds
}
type Cluster struct {
ctx context.Context
credentials *Creds
location string
resourceGroup string
name string
apiModelPath string
dnsPrefix string
templateJSON map[string]interface{}
parametersJSON map[string]interface{}
outputDir string
sshPublicKey string
adminUsername string
adminPassword string
masterVMSize string
agentVMSize string
acsCustomHyperKubeURL string
acsCustomWinBinariesURL string
acsEngineBinaryPath string
acsCustomCcmURL string
agentPoolCount int
k8sVersion string
networkPlugin string
azureClient *AzureClient
}
func (c *Cluster) getAzCredentials() error {
content, err := ioutil.ReadFile(*acsCredentialsFile)
log.Printf("Reading credentials file %v", *acsCredentialsFile)
if err != nil {
return fmt.Errorf("error reading credentials file %v %v", *acsCredentialsFile, err)
}
config := Config{}
err = toml.Unmarshal(content, &config)
c.credentials = &config.Creds
if err != nil {
return fmt.Errorf("error parsing credentials file %v %v", *acsCredentialsFile, err)
}
return nil
}
func randomAcsEngineLocation() string {
var AzureLocations = []string{
"westeurope",
"westus2",
"eastus2",
"southcentralus",
}
return AzureLocations[rand.Intn(len(AzureLocations))]
}
func checkParams() error {
if *acsLocation == "" {
*acsLocation = randomAcsEngineLocation()
}
if *acsCredentialsFile == "" {
return fmt.Errorf("no credentials file path specified")
}
if *acsResourceName == "" {
*acsResourceName = "kubetest-" + uuid.NewV1().String()
}
if *acsResourceGroupName == "" {
*acsResourceGroupName = *acsResourceName
}
if *acsDnsPrefix == "" {
*acsDnsPrefix = *acsResourceName
}
if *acsSSHPublicKeyPath == "" {
*acsSSHPublicKeyPath = os.Getenv("HOME") + "/.ssh/id_rsa.pub"
}
if *acsTemplateURL == "" {
return fmt.Errorf("no ApiModel URL specified.")
}
return nil
}
func newAcsEngine() (*Cluster, error) {
if err := checkParams(); err != nil {
return nil, fmt.Errorf("error creating Azure K8S cluster: %v", err)
}
tempdir, _ := ioutil.TempDir(os.Getenv("HOME"), "acs")
sshKey, err := ioutil.ReadFile(*acsSSHPublicKeyPath)
if err != nil {
return nil, fmt.Errorf("error reading SSH Key %v %v", *acsSSHPublicKeyPath, err)
}
c := Cluster{
ctx: context.Background(),
apiModelPath: *acsTemplateURL,
name: *acsResourceName,
dnsPrefix: *acsDnsPrefix,
location: *acsLocation,
resourceGroup: *acsResourceGroupName,
outputDir: tempdir,
sshPublicKey: fmt.Sprintf("%s", sshKey),
credentials: &Creds{},
masterVMSize: *acsMasterVmSize,
agentVMSize: *acsAgentVmSize,
adminUsername: *acsAdminUsername,
adminPassword: *acsAdminPassword,
agentPoolCount: *acsAgentPoolCount,
k8sVersion: *acsOrchestratorRelease,
networkPlugin: *acsNetworkPlugin,
acsCustomHyperKubeURL: "",
acsCustomWinBinariesURL: "",
acsCustomCcmURL: "",
acsEngineBinaryPath: "aks-engine", // use the one in path by default
}
c.getAzCredentials()
err = c.getARMClient(c.ctx)
if err != nil {
return nil, fmt.Errorf("failed to generate ARM client: %v", err)
}
// like kops and gke set KUBERNETES_CONFORMANCE_TEST so the auth is picked up
// from kubectl instead of bash inference.
if err := os.Setenv("KUBERNETES_CONFORMANCE_TEST", "yes"); err != nil {
return nil, err
}
return &c, nil
}
func (c *Cluster) populateApiModelTemplate() error {
var err error
v := AcsEngineAPIModel{}
if c.apiModelPath != "" {
// template already exists, read it
template, err := ioutil.ReadFile(path.Join(c.outputDir, "kubernetes.json"))
if err != nil {
return fmt.Errorf("error reading ApiModel template file: %v.", err)
}
err = json.Unmarshal(template, &v)
if err != nil {
return fmt.Errorf("error unmarshaling ApiModel template file: %v", err)
}
} else {
return fmt.Errorf("No template file specified %v", err)
}
// set default distro so we do not use prebuilt os image
v.Properties.MasterProfile.Distro = "ubuntu"
for _, agentPool := range v.Properties.AgentPoolProfiles {
agentPool.Distro = "ubuntu"
}
// replace APIModel template properties from flags
if c.location != "" {
v.Location = c.location
}
if c.name != "" {
v.Name = c.name
}
if c.k8sVersion != "" {
v.Properties.OrchestratorProfile.OrchestratorRelease = c.k8sVersion
}
if v.Properties.OrchestratorProfile.KubernetesConfig == nil {
v.Properties.OrchestratorProfile.KubernetesConfig = &KubernetesConfig{}
}
// to support aks-engine validation logic `networkPolicy 'none' is not supported with networkPlugin 'azure'`
if v.Properties.OrchestratorProfile.KubernetesConfig.NetworkPolicy != "none" && v.Properties.OrchestratorProfile.KubernetesConfig.NetworkPlugin == "" {
// default NetworkPlugin to Azure if not provided
v.Properties.OrchestratorProfile.KubernetesConfig.NetworkPlugin = c.networkPlugin
}
if c.dnsPrefix != "" {
v.Properties.MasterProfile.DNSPrefix = c.dnsPrefix
}
if c.masterVMSize != "" {
v.Properties.MasterProfile.VMSize = c.masterVMSize
}
if c.agentVMSize != "" {
for _, agentPool := range v.Properties.AgentPoolProfiles {
agentPool.VMSize = c.agentVMSize
}
}
if c.agentPoolCount != 0 {
for _, agentPool := range v.Properties.AgentPoolProfiles {
agentPool.Count = c.agentPoolCount
}
}
if c.adminUsername != "" {
v.Properties.LinuxProfile.AdminUsername = c.adminUsername
if v.Properties.WindowsProfile != nil {
v.Properties.WindowsProfile.AdminUsername = c.adminUsername
}
}
if c.adminPassword != "" {
if v.Properties.WindowsProfile != nil {
v.Properties.WindowsProfile.AdminPassword = c.adminPassword
}
}
v.Properties.LinuxProfile.SSHKeys.PublicKeys = []PublicKey{{
KeyData: c.sshPublicKey,
}}
v.Properties.ServicePrincipalProfile.ClientID = c.credentials.ClientID
v.Properties.ServicePrincipalProfile.Secret = c.credentials.ClientSecret
if c.acsCustomHyperKubeURL != "" {
v.Properties.OrchestratorProfile.KubernetesConfig.CustomHyperkubeImage = c.acsCustomHyperKubeURL
if strings.Contains(os.Getenv("REGISTRY"), "azurecr") {
v.Properties.OrchestratorProfile.KubernetesConfig.PrivateAzureRegistryServer = os.Getenv("REGISTRY")
}
}
if c.acsCustomWinBinariesURL != "" {
v.Properties.OrchestratorProfile.KubernetesConfig.CustomWindowsPackageURL = c.acsCustomWinBinariesURL
}
if c.acsCustomCcmURL != "" {
useCloudControllerManager := true
v.Properties.OrchestratorProfile.KubernetesConfig.UseCloudControllerManager = &useCloudControllerManager
v.Properties.OrchestratorProfile.KubernetesConfig.CustomCcmImage = c.acsCustomCcmURL
}
apiModel, _ := json.MarshalIndent(v, "", " ")
c.apiModelPath = path.Join(c.outputDir, "kubernetes.json")
err = ioutil.WriteFile(c.apiModelPath, apiModel, 0644)
if err != nil {
return fmt.Errorf("cannot write apimodel to file: %v", err)
}
return nil
}
func (c *Cluster) getAcsEngine(retry int) error {
downloadPath := path.Join(os.Getenv("HOME"), "aks-engine.tar.gz")
f, err := os.Create(downloadPath)
if err != nil {
return err
}
defer f.Close()
for i := 0; i < retry; i++ {
log.Printf("downloading %v from %v.", downloadPath, *acsEngineURL)
if err := httpRead(*acsEngineURL, f); err == nil {
break
}
err = fmt.Errorf("url=%s failed get %v: %v.", *acsEngineURL, downloadPath, err)
if i == retry-1 {
return err
}
log.Println(err)
sleep(time.Duration(i) * time.Second)
}
f.Close()
if *acsEngineMD5 != "" {
o, err := control.Output(exec.Command("md5sum", f.Name()))
if err != nil {
return err
}
if strings.Split(string(o), " ")[0] != *acsEngineMD5 {
return fmt.Errorf("wrong md5 sum for acs-engine.")
}
}
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to get current directory: %v .", err)
}
log.Printf("Extracting tar file %v into directory %v .", f.Name(), cwd)
if err = control.FinishRunning(exec.Command("tar", "-xzf", f.Name(), "--strip", "1")); err != nil {
return err
}
c.acsEngineBinaryPath = path.Join(cwd, "aks-engine")
return nil
}
func (c Cluster) generateARMTemplates() error {
if err := control.FinishRunning(exec.Command(c.acsEngineBinaryPath, "generate", c.apiModelPath, "--output-directory", c.outputDir)); err != nil {
return fmt.Errorf("failed to generate ARM templates: %v.", err)
}
return nil
}
func (c *Cluster) loadARMTemplates() error {
var err error
template, err := ioutil.ReadFile(path.Join(c.outputDir, "azuredeploy.json"))
if err != nil {
return fmt.Errorf("error reading ARM template file: %v.", err)
}
c.templateJSON = make(map[string]interface{})
err = json.Unmarshal(template, &c.templateJSON)
if err != nil {
return fmt.Errorf("error unmarshall template %v", err.Error())
}
parameters, err := ioutil.ReadFile(path.Join(c.outputDir, "azuredeploy.parameters.json"))
if err != nil {
return fmt.Errorf("error reading ARM parameters file: %v", err)
}
c.parametersJSON = make(map[string]interface{})
err = json.Unmarshal(parameters, &c.parametersJSON)
if err != nil {
return fmt.Errorf("error unmarshall parameters %v", err.Error())
}
c.parametersJSON = c.parametersJSON["parameters"].(map[string]interface{})
return nil
}
func (c *Cluster) getARMClient(ctx context.Context) error {
// instantiate Azure Resource Manager Client
env, err := azure.EnvironmentFromName("AzurePublicCloud")
var client *AzureClient
if client, err = getAzureClient(env,
c.credentials.SubscriptionID,
c.credentials.ClientID,
c.credentials.TenantID,
c.credentials.ClientSecret); err != nil {
return fmt.Errorf("error trying to get Azure Client: %v", err)
}
c.azureClient = client
return nil
}
func (c *Cluster) createCluster() error {
var err error
kubecfgDir, _ := ioutil.ReadDir(path.Join(c.outputDir, "kubeconfig"))
kubecfg := path.Join(c.outputDir, "kubeconfig", kubecfgDir[0].Name())
log.Printf("Setting kubeconfig env variable: kubeconfig path: %v.", kubecfg)
os.Setenv("KUBECONFIG", kubecfg)
log.Printf("Creating resource group: %v.", c.resourceGroup)
log.Printf("Creating Azure resource group: %v for cluster deployment.", c.resourceGroup)
_, err = c.azureClient.EnsureResourceGroup(c.ctx, c.resourceGroup, c.location, nil)
if err != nil {
return fmt.Errorf("could not ensure resource group: %v", err)
}
log.Printf("Validating deployment ARM templates.")
if _, err := c.azureClient.ValidateDeployment(
c.ctx, c.resourceGroup, c.name, &c.templateJSON, &c.parametersJSON,
); err != nil {
return fmt.Errorf("ARM template invalid: %v", err)
}
log.Printf("Deploying cluster %v in resource group %v.", c.name, c.resourceGroup)
if _, err := c.azureClient.DeployTemplate(
c.ctx, c.resourceGroup, c.name, &c.templateJSON, &c.parametersJSON,
); err != nil {
return fmt.Errorf("cannot deploy: %v", err)
}
return nil
}
func (c *Cluster) dockerLogin() error {
cwd, _ := os.Getwd()
log.Printf("CWD %v", cwd)
cmd := &exec.Cmd{}
username := ""
pwd := ""
server := ""
var err error
if !strings.Contains(os.Getenv("REGISTRY"), "azurecr.io") {
// if REGISTRY is not ACR, then use docker cred
log.Println("Attempting Docker login with docker cred.")
username = os.Getenv("DOCKER_USERNAME")
passwordFile := os.Getenv("DOCKER_PASSWORD_FILE")
password, err := ioutil.ReadFile(passwordFile)
if err != nil {
return fmt.Errorf("error reading docker passoword file %v: %v", passwordFile, err)
}
pwd = strings.TrimSuffix(string(password), "\n")
} else {
// if REGISTRY is ACR, then use azure credential
log.Println("Attempting Docker login with azure cred.")
username = c.credentials.ClientID
pwd = c.credentials.ClientSecret
server = os.Getenv("REGISTRY")
}
cmd = exec.Command("docker", "login", fmt.Sprintf("--username=%s", username), fmt.Sprintf("--password=%s", pwd), server)
if err = cmd.Run(); err != nil {
return fmt.Errorf("failed Docker login with error: %v", err)
}
log.Println("Docker login success.")
return nil
}
func dockerLogout() error {
log.Println("Docker logout.")
cmd := exec.Command("docker", "logout")
return cmd.Run()
}
func (c *Cluster) buildCcm() error {
image := fmt.Sprintf("%v/azure-cloud-controller-manager:%v-%v", os.Getenv("REGISTRY"), os.Getenv("BUILD_ID"), uuid.NewV1().String()[:8])
if err := c.dockerLogin(); err != nil {
return err
}
log.Println("Building ccm.")
projectPath := util.K8s("cloud-provider-azure")
log.Printf("projectPath %v", projectPath)
cmd := exec.Command("docker", "build", "-t", image, ".")
cmd.Dir = projectPath
if err := control.FinishRunning(cmd); err != nil {
return err
}
cmd = exec.Command("docker", "push", image)
cmd.Stdout = ioutil.Discard
if err := control.FinishRunning(cmd); err != nil {
return err
}
c.acsCustomCcmURL = image
if err := dockerLogout(); err != nil {
log.Println("Docker logout failed.")
return err
}
log.Printf("Custom cloud controller manager URL: %v .", c.acsCustomCcmURL)
return nil
}
func (c *Cluster) buildHyperKube() error {
os.Setenv("VERSION", fmt.Sprintf("azure-e2e-%v-%v", os.Getenv("BUILD_ID"), uuid.NewV1().String()[:8]))
if err := c.dockerLogin(); err != nil {
return err
}
log.Println("Building and pushing hyperkube.")
pushHyperkube := util.K8s("kubernetes", "hack", "dev-push-hyperkube.sh")
cmd := exec.Command(pushHyperkube)
// dev-push-hyperkube will produce a lot of output to stdout. We should capture the output here.
cmd.Stdout = ioutil.Discard
if err := control.FinishRunning(cmd); err != nil {
return err
}
c.acsCustomHyperKubeURL = fmt.Sprintf("%s/hyperkube-amd64:%s", os.Getenv("REGISTRY"), os.Getenv("VERSION"))
if err := dockerLogout(); err != nil {
log.Println("Docker logout failed.")
return err
}
log.Printf("Custom hyperkube URL: %v .", c.acsCustomHyperKubeURL)
return nil
}
func (c *Cluster) uploadZip(zipPath string) error {
credential, err := azblob.NewSharedKeyCredential(c.credentials.StorageAccountName, c.credentials.StorageAccountKey)
if err != nil {
return fmt.Errorf("new shared key credential: %v", err)
}
p := azblob.NewPipeline(credential, azblob.PipelineOptions{})
var containerName string = os.Getenv("AZ_STORAGE_CONTAINER_NAME")
URL, _ := url.Parse(
fmt.Sprintf("https://%s.blob.core.windows.net/%s", c.credentials.StorageAccountName, containerName))
containerURL := azblob.NewContainerURL(*URL, p)
file, err := os.Open(zipPath)
if err != nil {
return fmt.Errorf("failed to open file %v . Error %v", zipPath, err)
}
blobURL := containerURL.NewBlockBlobURL(filepath.Base(file.Name()))
_, err1 := azblob.UploadFileToBlockBlob(context.Background(), file, blobURL, azblob.UploadToBlockBlobOptions{})
file.Close()
if err1 != nil {
return err1
}
blobURLString := blobURL.URL()
c.acsCustomWinBinariesURL = blobURLString.String()
log.Printf("Custom win binaries url: %v", c.acsCustomWinBinariesURL)
return nil
}
func getApiModelTemplate(url string, downloadPath string, retry int) (string, error) {
f, err := os.Create(downloadPath)
if err != nil {
return "", err
}
defer f.Close()
for i := 0; i < retry; i++ {
log.Printf("downloading %v from %v.", downloadPath, url)
if err := httpRead(url, f); err == nil {
break
}
err = fmt.Errorf("url=%s failed get %v: %v.", url, downloadPath, err)
if i == retry-1 {
return "", err
}
log.Println(err)
sleep(time.Duration(i) * time.Second)
}
f.Chmod(0644)
return downloadPath, nil
}
func getZipBuildScript(buildScriptURL string, retry int) (string, error) {
downloadPath := path.Join(os.Getenv("HOME"), "build-win-zip.sh")
f, err := os.Create(downloadPath)
if err != nil {
return "", err
}
defer f.Close()
for i := 0; i < retry; i++ {
log.Printf("downloading %v from %v.", downloadPath, buildScriptURL)
if err := httpRead(buildScriptURL, f); err == nil {
break
}
err = fmt.Errorf("url=%s failed get %v: %v.", buildScriptURL, downloadPath, err)
if i == retry-1 {
return "", err
}
log.Println(err)
sleep(time.Duration(i) * time.Second)
}
f.Chmod(0744)
return downloadPath, nil
}
func (c *Cluster) buildWinZip() error {
zipName := fmt.Sprintf("%s%s.zip", os.Getenv("BUILD_ID"), uuid.NewV1().String()[:8])
buildFolder := path.Join(os.Getenv("HOME"), "winbuild")
zipPath := path.Join(os.Getenv("HOME"), zipName)
log.Printf("Building %s", zipName)
buildScriptPath, err := getZipBuildScript(*acsWinZipBuildScript, 2)
if err != nil {
return err
}
// the build script for the windows binaries will produce a lot of output. Capture it here.
cmd := exec.Command(buildScriptPath, "-u", zipName, "-z", buildFolder)
cmd.Stdout = ioutil.Discard
if err := control.FinishRunning(cmd); err != nil {
return err
}
log.Printf("Uploading %s", zipPath)
if err := c.uploadZip(zipPath); err != nil {
return err
}
return nil
}
func (c Cluster) Up() error {
var err error
if *acsCcm == true {
err = c.buildCcm()
if err != nil {
return fmt.Errorf("error building cloud controller manager %v", err)
}
}
if *acsHyperKube == true {
err = c.buildHyperKube()
if err != nil {
return fmt.Errorf("error building hyperkube %v", err)
}
}
if *acsWinBinaries == true {
err = c.buildWinZip()
if err != nil {
return fmt.Errorf("error building windowsZipFile %v", err)
}
}
if c.apiModelPath != "" {
templateFile, err := getApiModelTemplate(c.apiModelPath, path.Join(c.outputDir, "kubernetes.json"), 2)
if err != nil {
return fmt.Errorf("error downloading ApiModel template: %v with error %v", c.apiModelPath, err)
}
c.apiModelPath = templateFile
}
err = c.populateApiModelTemplate()
if err != nil {
return fmt.Errorf("failed to populate acs-engine apimodel template: %v", err)
}
if *acsEngineURL != "" {
err = c.getAcsEngine(2)
if err != nil {
return fmt.Errorf("failed to get ACS Engine binary: %v", err)
}
}
err = c.generateARMTemplates()
if err != nil {
return fmt.Errorf("failed to generate ARM templates: %v", err)
}
err = c.loadARMTemplates()
if err != nil {
return fmt.Errorf("error loading ARM templates: %v", err)
}
err = c.createCluster()
if err != nil {
return fmt.Errorf("error creating cluster: %v", err)
}
return nil
}
func (c Cluster) Down() error {
log.Printf("Deleting resource group: %v.", c.resourceGroup)
return c.azureClient.DeleteResourceGroup(c.ctx, c.resourceGroup)
}
func (c Cluster) DumpClusterLogs(localPath, gcsPath string) error {
return nil
}
func (c Cluster) GetClusterCreated(clusterName string) (time.Time, error) {
return time.Time{}, errors.New("not implemented")
}
func (c Cluster) TestSetup() error {
// set env vars required by the ccm e2e tests
if *testCcm == true {
if err := os.Setenv("K8S_AZURE_TENANTID", c.credentials.TenantID); err != nil {
return err
}
if err := os.Setenv("K8S_AZURE_SUBSID", c.credentials.SubscriptionID); err != nil {
return err
}
if err := os.Setenv("K8S_AZURE_SPID", c.credentials.ClientID); err != nil {
return err
}
if err := os.Setenv("K8S_AZURE_SPSEC", c.credentials.ClientSecret); err != nil {
return err
}
if err := os.Setenv("K8S_AZURE_LOCATION", c.location); err != nil {
return err
}
}
// Download repo-list that defines repositories for Windows test images.
downloadUrl, ok := os.LookupEnv("KUBE_TEST_REPO_LIST_DOWNLOAD_LOCATION")
if !ok {
// Env value for downloadUrl is not set, nothing to do
log.Printf("KUBE_TEST_REPO_LIST_DOWNLOAD_LOCATION not set. Using default test image repos.")
return nil
}
downloadPath := path.Join(os.Getenv("HOME"), "repo-list")
f, err := os.Create(downloadPath)
if err != nil {
return err
}
defer f.Close()
log.Printf("downloading %v from %v.", downloadPath, downloadUrl)
err = httpRead(downloadUrl, f)
if err != nil {
return fmt.Errorf("url=%s failed get %v: %v.", downloadUrl, downloadPath, err)
}
f.Chmod(0744)
if err := os.Setenv("KUBE_TEST_REPO_LIST", downloadPath); err != nil {
return err
}
return nil
}
func (c Cluster) IsUp() error {
return isUp(c)
}
func (_ Cluster) KubectlCommand() (*exec.Cmd, error) { return nil, nil }
// BuildTester returns a standard ginkgo-script tester or a custom one if testCcm is enabled
func (c *Cluster) BuildTester(o *e2e.BuildTesterOptions) (e2e.Tester, error) {
if *testCcm != true {
return &GinkgoScriptTester{}, nil
}
log.Printf("running go tests directly")
return &GinkgoCustomTester{}, nil
}
// GinkgoCustomTester implements Tester by calling a custom ginkgo script
type GinkgoCustomTester struct {
}
// Run executes custom ginkgo script
func (t *GinkgoCustomTester) Run(control *process.Control, testArgs []string) error {
artifactsDir, ok := os.LookupEnv("ARTIFACTS")
if !ok {
artifactsDir = filepath.Join(os.Getenv("WORKSPACE"), "_artifacts")
}
log.Printf("artifactsDir %v", artifactsDir)
// set CCM_JUNIT_REPORT_DIR for ccm e2e test to use the same dir
if err := os.Setenv("CCM_JUNIT_REPORT_DIR", artifactsDir); err != nil {
return err
}
cmd := exec.Command("make", "test-ccm-e2e")
projectPath := util.K8s("cloud-provider-azure")
cmd.Dir = projectPath
testErr := control.FinishRunning(cmd)
return testErr
}