forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aksengine.go
1359 lines (1214 loc) · 49.6 KB
/
aksengine.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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"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"
)
const (
winZipTemplate = "win-zip-%s.zip"
k8sNodeTarballTemplate = "kubernetes-node-linux-amd64-%s.tar.gz"
azureBlobContainerURLTemplate = "https://%s.blob.core.windows.net/%s"
azureKubemarkTestPrefix = "[for azure kubemark test]"
)
var (
aksResourceName = flag.String("aksengine-resource-name", "", "Azure Resource Name")
aksResourceGroupName = flag.String("aksengine-resourcegroup-name", "", "Azure Resource Group Name")
aksLocation = flag.String("aksengine-location", "", "Azure AKS location")
aksMasterVMSize = flag.String("aksengine-mastervmsize", "", "Azure Master VM size")
aksAgentVMSize = flag.String("aksengine-agentvmsize", "", "Azure Agent VM size")
aksAdminUsername = flag.String("aksengine-admin-username", "", "Admin username")
aksAdminPassword = flag.String("aksengine-admin-password", "", "Admin password")
aksAgentPoolCount = flag.Int("aksengine-agentpoolcount", 0, "Azure Agent Pool Count")
aksTemplateURL = flag.String("aksengine-template-url", "", "Azure Template URL.")
aksDNSPrefix = flag.String("aksengine-dnsprefix", "", "Azure K8s Master DNS Prefix")
aksEngineURL = flag.String("aksengine-download-url", "", "Download URL for AKS engine")
aksEngineMD5 = flag.String("aksengine-md5-sum", "", "Checksum for aks engine download")
aksSSHPublicKeyPath = flag.String("aksengine-public-key", "", "Path to SSH Public Key")
aksSSHPrivateKeyPath = flag.String("aksengine-private-key", "", "Path to SSH Private Key")
aksWinBinaries = flag.Bool("aksengine-win-binaries", false, "Set to True if you want kubetest to build a custom zip with windows binaries for aks-engine")
aksCcm = flag.Bool("aksengine-ccm", false, "Set to True if you want kubetest to build a custom cloud controller manager for aks-engine")
aksCnm = flag.Bool("aksengine-cnm", false, "Set to True if you want kubetest to build a custom cloud node manager for aks-engine. Require --aksengine-ccm to be true")
aksCredentialsFile = flag.String("aksengine-creds", "", "Path to credential file for Azure")
aksOrchestratorRelease = flag.String("aksengine-orchestratorRelease", "", "Orchestrator Profile for aks-engine")
aksWinZipBuildScript = flag.String("aksengine-winZipBuildScript", "https://raw.githubusercontent.com/Azure/aks-engine/master/scripts/build-windows-k8s.sh", "Build script to create custom zip containing win binaries for aks-engine")
aksNetworkPlugin = flag.String("aksengine-networkPlugin", "azure", "Network pluging to use with aks-engine")
aksAzureEnv = flag.String("aksengine-azure-env", "AzurePublicCloud", "The target Azure cloud")
aksIdentitySystem = flag.String("aksengine-identity-system", "azure_ad", "identity system (default:`azure_ad`, `adfs`)")
aksCustomCloudURL = flag.String("aksengine-custom-cloud-url", "", "management portal URL to use in custom Azure cloud (i.e Azure Stack etc)")
aksDeployCustomK8s = flag.Bool("aksengine-deploy-custom-k8s", false, "Set to True if you want to deploy custom-built k8s via aks-engine")
aksCheckParams = flag.Bool("aksengine-check-params", true, "Set to True if you want to validate your input parameters")
aksDumpClusterLogs = flag.Bool("aksengine-dump-cluster-logs", true, "Set to True if you want to dump cluster logs")
aksNodeProblemDetector = flag.Bool("aksengine-node-problem-detector", false, "Set to True if you want to enable node problem detector addon")
runExternalE2EGinkgoTest = flag.Bool("run-external-e2e-ginkgo-test", false, "Set to True if you want external e2e ginkgo tests for the CSI driver")
testCcm = flag.Bool("test-ccm", false, "Set to True if you want kubetest to run e2e tests for ccm")
testAzureFileCSIDriver = flag.Bool("test-azure-file-csi-driver", false, "Set to True if you want kubetest to run e2e tests for Azure File CSI driver")
testAzureDiskCSIDriver = flag.Bool("test-azure-disk-csi-driver", false, "Set to True if you want kubetest to run e2e tests for Azure Disk CSI driver")
testBlobCSIDriver = flag.Bool("test-blob-csi-driver", false, "Set to True if you want kubetest to run e2e tests for Azure Blob Storage CSI driver")
testSecretStoreCSIDriver = flag.Bool("test-secrets-store-csi-driver", false, "Set to True if you want kubetest to run e2e tests for Secrets Store CSI driver")
testSMBCSIDriver = flag.Bool("test-csi-driver-smb", false, "Set to True if you want kubetest to run e2e tests for SMB CSI driver")
testNFSCSIDriver = flag.Bool("test-csi-driver-nfs", false, "Set to True if you want kubetest to run e2e tests for NFS CSI driver")
// Commonly used variables
k8sVersion = getImageVersion(util.K8s("kubernetes"))
cloudProviderAzureVersion = getImageVersion(util.K8sSigs("cloud-provider-azure"))
imageRegistry = os.Getenv("REGISTRY")
k8sNodeTarballDir = util.K8s("kubernetes", "_output", "release-tars") // contains custom-built kubelet and kubectl
// kubemark scale tests
buildWithKubemark = flag.Bool("build-with-kubemark", false, fmt.Sprintf("%s Enable building clusters with kubemark", azureKubemarkTestPrefix))
kubemarkBuildScriptURL = flag.String("kubemark-build-script-url", "", fmt.Sprintf("%s URL to the building script of kubemark and kubemark-external cluster", azureKubemarkTestPrefix))
kubemarkClusterTemplateURL = flag.String("kubemark-cluster-template-url", "", fmt.Sprintf("%s URL to the aks-engine template of kubemark cluster", azureKubemarkTestPrefix))
externalClusterTemplateURL = flag.String("external-cluster-template-url", "", fmt.Sprintf("%s URL to the aks-engine template of kubemark external cluster", azureKubemarkTestPrefix))
hollowNodesDeploymentURL = flag.String("hollow-nodes-deployment-url", "", fmt.Sprintf("%s URL to the deployment configuration file of hollow nodes", azureKubemarkTestPrefix))
clusterLoader2BinURL = flag.String("clusterloader2-bin-url", "", fmt.Sprintf("%s URL to the binary of clusterloader2", azureKubemarkTestPrefix))
kubemarkLocation = flag.String("kubemark-location", "southcentralus", fmt.Sprintf("%s The location where the kubemark and external clusters run", azureKubemarkTestPrefix))
kubemarkSize = flag.String("kubemark-size", "100", fmt.Sprintf("%s The number of hollow nodes in kubemark cluster", azureKubemarkTestPrefix))
)
const (
// AzureStackCloud is a const string reference identifier for Azure Stack cloud
AzureStackCloud = "AzureStackCloud"
// ADFSIdentitySystem is a const for ADFS identifier on Azure Stack cloud
ADFSIdentitySystem = "adfs"
)
const (
ccmImageName = "azure-cloud-controller-manager"
cnmImageName = "azure-cloud-node-manager"
cnmAddonName = "cloud-node-manager"
nodeProblemDetectorAddonName = "node-problem-detector"
hyperkubeImageName = "hyperkube-amd64"
kubeAPIServerImageName = "kube-apiserver-amd64"
kubeControllerManagerImageName = "kube-controller-manager-amd64"
kubeSchedulerImageName = "kube-scheduler-amd64"
kubeProxyImageName = "kube-proxy-amd64"
)
const (
vmTypeVMSS = "vmss"
vmTypeStandard = "standard"
availabilityProfileVMSS = "VirtualMachineScaleSets"
)
type aksDeploymentMethod int
const (
// https://github.com/Azure/aks-engine/blob/master/docs/topics/kubernetes-developers.md#kubernetes-116-or-earlier
customHyperkube aksDeploymentMethod = iota
// https://github.com/Azure/aks-engine/blob/master/docs/topics/kubernetes-developers.md#kubernetes-117
customK8sComponents
noop
)
type Creds struct {
ClientID string
ClientSecret string
TenantID string
SubscriptionID string
StorageAccountName string
StorageAccountKey string
}
type Config struct {
Creds Creds
}
type aksEngineDeployer 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
sshPrivateKeyPath string
adminUsername string
adminPassword string
masterVMSize string
agentVMSize string
customHyperkubeImage string
aksCustomWinBinariesURL string
aksEngineBinaryPath string
customCcmImage string // custom cloud controller manager (ccm) image
customCnmImage string // custom cloud node manager (cnm) image
customKubeAPIServerImage string
customKubeControllerManagerImage string
customKubeProxyImage string
customKubeSchedulerImage string
customKubeBinaryURL string
azureEnvironment string
azureIdentitySystem string
azureCustomCloudURL string
agentPoolCount int
k8sVersion string
networkPlugin string
azureClient *AzureClient
aksDeploymentMethod aksDeploymentMethod
useManagedIdentity bool
identityName string
azureBlobContainerURL string
}
// IsAzureStackCloud return true if the cloud is AzureStack
func (c *aksEngineDeployer) isAzureStackCloud() bool {
return c.azureCustomCloudURL != "" && strings.EqualFold(c.azureEnvironment, AzureStackCloud)
}
// SetCustomCloudProfileEnvironment retrieves the endpoints from Azure Stack metadata endpoint and sets the values for azure.Environment
func (c *aksEngineDeployer) SetCustomCloudProfileEnvironment() error {
var environmentJSON string
if c.isAzureStackCloud() {
env := azure.Environment{}
env.Name = c.azureEnvironment
azsFQDNSuffix := strings.Replace(c.azureCustomCloudURL, fmt.Sprintf("https://portal.%s.", c.location), "", -1)
azsFQDNSuffix = strings.TrimSuffix(azsFQDNSuffix, "/")
env.ResourceManagerEndpoint = fmt.Sprintf("https://management.%s.%s/", c.location, azsFQDNSuffix)
metadataURL := fmt.Sprintf("%s/metadata/endpoints?api-version=1.0", strings.TrimSuffix(env.ResourceManagerEndpoint, "/"))
// Retrieve the metadata
httpClient := &http.Client{
Timeout: 30 * time.Second,
}
endpointsresp, err := httpClient.Get(metadataURL)
if err != nil {
return fmt.Errorf("%s . apimodel invalid: failed to retrieve Azure Stack endpoints from %s", err, metadataURL)
}
defer endpointsresp.Body.Close()
if endpointsresp.StatusCode != 200 {
return fmt.Errorf("%s . apimodel invalid: failed to retrieve Azure Stack endpoints from %s", err, metadataURL)
}
body, err := io.ReadAll(endpointsresp.Body)
if err != nil {
return fmt.Errorf("%s . apimodel invalid: failed to read the response from %s", err, metadataURL)
}
endpoints := AzureStackMetadataEndpoints{}
err = json.Unmarshal(body, &endpoints)
if err != nil {
return fmt.Errorf("%s . apimodel invalid: failed to parse the response from %s", err, metadataURL)
}
if endpoints.GraphEndpoint == "" || endpoints.Authentication == nil || endpoints.Authentication.LoginEndpoint == "" || len(endpoints.Authentication.Audiences) == 0 || endpoints.Authentication.Audiences[0] == "" {
return fmt.Errorf("%s . apimodel invalid: invalid response from %s", err, metadataURL)
}
env.GraphEndpoint = endpoints.GraphEndpoint
env.ServiceManagementEndpoint = endpoints.Authentication.Audiences[0]
env.GalleryEndpoint = endpoints.GalleryEndpoint
env.ActiveDirectoryEndpoint = endpoints.Authentication.LoginEndpoint
if strings.EqualFold(c.azureIdentitySystem, ADFSIdentitySystem) {
env.ActiveDirectoryEndpoint = strings.TrimSuffix(env.ActiveDirectoryEndpoint, "/")
env.ActiveDirectoryEndpoint = strings.TrimSuffix(env.ActiveDirectoryEndpoint, ADFSIdentitySystem)
}
env.ManagementPortalURL = endpoints.PortalEndpoint
env.ResourceManagerVMDNSSuffix = fmt.Sprintf("cloudapp.%s", azsFQDNSuffix)
env.StorageEndpointSuffix = fmt.Sprintf("%s.%s", c.location, azsFQDNSuffix)
env.KeyVaultDNSSuffix = fmt.Sprintf("vault.%s.%s", c.location, azsFQDNSuffix)
bytes, err := json.Marshal(env)
if err != nil {
return fmt.Errorf("Could not serialize Environment object - %s", err.Error())
}
environmentJSON = string(bytes)
// Create and update the file.
tmpFile, err := os.CreateTemp("", "azurestackcloud.json")
tmpFileName := tmpFile.Name()
if err != nil {
return err
}
// Build content for the file
if err = os.WriteFile(tmpFileName, []byte(environmentJSON), os.ModeAppend); err != nil {
return err
}
os.Setenv("AZURE_ENVIRONMENT_FILEPATH", tmpFileName)
}
return nil
}
func validateAzureStackCloudProfile() error {
if *aksLocation == "" {
return fmt.Errorf("no location specified for Azure Stack")
}
if *aksCustomCloudURL == "" {
return fmt.Errorf("no custom cloud portal URL specified for Azure Stack")
}
if !strings.HasPrefix(*aksCustomCloudURL, fmt.Sprintf("https://portal.%s.", *aksLocation)) {
return fmt.Errorf("custom cloud portal URL needs to start with https://portal.%s. ", *aksLocation)
}
return nil
}
func randomAKSEngineLocation() string {
var AzureLocations = []string{
"canadacentral",
"centralus",
"eastus",
"eastus2",
"francecentral",
"northcentralus",
"northeurope",
"southcentralus",
"uksouth",
"westeurope",
"westus2",
}
return AzureLocations[rand.Intn(len(AzureLocations))]
}
func checkParams() error {
if !*aksCheckParams {
log.Print("Skipping checkParams")
return nil
}
// Validate flags
if strings.EqualFold(*aksAzureEnv, AzureStackCloud) {
if err := validateAzureStackCloudProfile(); err != nil {
return err
}
} else if *aksLocation == "" {
*aksLocation = randomAKSEngineLocation()
}
if *aksCredentialsFile == "" {
return fmt.Errorf("no credentials file path specified")
}
if *aksResourceName == "" {
*aksResourceName = "kubetest-" + strings.ToLower(randString(8))
}
if *aksResourceGroupName == "" {
*aksResourceGroupName = *aksResourceName
}
if *aksDNSPrefix == "" {
*aksDNSPrefix = *aksResourceName
}
if *aksSSHPublicKeyPath == "" {
*aksSSHPublicKeyPath = os.Getenv("HOME") + "/.ssh/id_rsa.pub"
}
if *aksSSHPrivateKeyPath == "" {
*aksSSHPrivateKeyPath = os.Getenv("HOME") + "/.ssh/id_rsa"
}
if !*buildWithKubemark && *aksTemplateURL == "" {
return fmt.Errorf("no ApiModel URL specified, *buildWithKubemark=%v\n", *buildWithKubemark)
}
if *aksCnm && !*aksCcm {
return fmt.Errorf("--aksengine-cnm cannot be true without --aksengine-ccm also being true")
}
return nil
}
func newAKSEngine() (*aksEngineDeployer, error) {
if err := checkParams(); err != nil {
return nil, fmt.Errorf("error creating Azure K8S cluster: %w", err)
}
sshKey, err := os.ReadFile(*aksSSHPublicKeyPath)
if err != nil {
if os.IsNotExist(err) {
sshKey = []byte{}
} else {
return nil, fmt.Errorf("error reading SSH Key %v %w", *aksSSHPublicKeyPath, err)
}
}
outputDir, err := os.MkdirTemp(os.Getenv("HOME"), "tmp")
if err != nil {
return nil, fmt.Errorf("error creating tempdir: %w", err)
}
c := aksEngineDeployer{
ctx: context.Background(),
apiModelPath: *aksTemplateURL,
name: *aksResourceName,
dnsPrefix: *aksDNSPrefix,
location: *aksLocation,
resourceGroup: *aksResourceGroupName,
outputDir: outputDir,
sshPublicKey: string(sshKey),
sshPrivateKeyPath: *aksSSHPrivateKeyPath,
credentials: &Creds{},
masterVMSize: *aksMasterVMSize,
agentVMSize: *aksAgentVMSize,
adminUsername: *aksAdminUsername,
adminPassword: *aksAdminPassword,
agentPoolCount: *aksAgentPoolCount,
k8sVersion: *aksOrchestratorRelease,
networkPlugin: *aksNetworkPlugin,
azureEnvironment: *aksAzureEnv,
azureIdentitySystem: *aksIdentitySystem,
azureCustomCloudURL: *aksCustomCloudURL,
customHyperkubeImage: getDockerImage(hyperkubeImageName),
customCcmImage: getDockerImage(ccmImageName),
customCnmImage: getDockerImage(cnmImageName),
customKubeAPIServerImage: getDockerImage(kubeAPIServerImageName),
customKubeControllerManagerImage: getDockerImage(kubeControllerManagerImageName),
customKubeProxyImage: getDockerImage(kubeProxyImageName),
customKubeSchedulerImage: getDockerImage(kubeSchedulerImageName),
aksEngineBinaryPath: "aks-engine", // use the one in path by default
aksDeploymentMethod: getAKSDeploymentMethod(*aksOrchestratorRelease),
useManagedIdentity: false,
identityName: "",
}
creds, err := getAzCredentials()
if err != nil {
return nil, fmt.Errorf("failed to get azure credentials: %w", err)
}
c.credentials = creds
c.azureBlobContainerURL = fmt.Sprintf(azureBlobContainerURLTemplate, c.credentials.StorageAccountName, os.Getenv("AZ_STORAGE_CONTAINER_NAME"))
c.customKubeBinaryURL = fmt.Sprintf("%s/%s", c.azureBlobContainerURL, fmt.Sprintf(k8sNodeTarballTemplate, k8sVersion))
c.aksCustomWinBinariesURL = fmt.Sprintf("%s/%s", c.azureBlobContainerURL, fmt.Sprintf(winZipTemplate, k8sVersion))
err = c.SetCustomCloudProfileEnvironment()
if err != nil {
return nil, fmt.Errorf("failed to create custom cloud profile file: %w", err)
}
err = c.getAzureClient(c.ctx)
if err != nil {
return nil, fmt.Errorf("failed to generate ARM client: %w", 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
}
if err := c.dockerLogin(); err != nil {
return nil, err
}
return &c, nil
}
func getAKSDeploymentMethod(k8sRelease string) aksDeploymentMethod {
if !*aksDeployCustomK8s {
return noop
}
// k8sRelease should be in the format of X.XX
s := strings.Split(k8sRelease, ".")
if len(s) != 2 {
return noop
}
minor, err := strconv.Atoi(s[1])
if err != nil {
return noop
}
// Deploy custom-built individual k8s components because
// there is no hyperkube support in aks-engine for 1.17+
if minor >= 17 {
return customK8sComponents
}
return customHyperkube
}
func (c *aksEngineDeployer) populateAPIModelTemplate() error {
var err error
v := AKSEngineAPIModel{}
if c.apiModelPath != "" {
// template already exists, read it
template, err := os.ReadFile(path.Join(c.outputDir, "kubernetes.json"))
if err != nil {
return fmt.Errorf("error reading ApiModel template file: %v.", err)
}
dec := json.NewDecoder(bytes.NewReader(template))
// Enforce strict JSON
dec.DisallowUnknownFields()
if err := dec.Decode(&v); err != nil {
return fmt.Errorf("error unmarshaling ApiModel template file: %w", err)
}
} else {
return fmt.Errorf("No template file specified %w", err)
}
// replace APIModel template properties from flags
if c.location != "" {
v.Location = c.location
}
if c.name != "" {
v.Name = c.name
}
if v.Properties.OrchestratorProfile == nil {
v.Properties.OrchestratorProfile = &OrchestratorProfile{}
}
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,
}}
if !toBool(v.Properties.OrchestratorProfile.KubernetesConfig.UseManagedIdentity) {
// prevent the nil pointer panic
v.Properties.ServicePrincipalProfile = &ServicePrincipalProfile{
ClientID: c.credentials.ClientID,
Secret: c.credentials.ClientSecret,
}
} else {
c.useManagedIdentity = true
if v.Properties.OrchestratorProfile.KubernetesConfig.UserAssignedID != "" {
c.identityName = v.Properties.OrchestratorProfile.KubernetesConfig.UserAssignedID
} else {
c.identityName = c.resourceGroup + "-id"
v.Properties.OrchestratorProfile.KubernetesConfig.UserAssignedID = c.identityName
}
}
if *aksWinBinaries {
v.Properties.OrchestratorProfile.KubernetesConfig.CustomWindowsPackageURL = c.aksCustomWinBinariesURL
}
if *aksCcm {
useCloudControllerManager := true
v.Properties.OrchestratorProfile.KubernetesConfig.UseCloudControllerManager = &useCloudControllerManager
v.Properties.OrchestratorProfile.KubernetesConfig.CustomCcmImage = c.customCcmImage
}
if *aksCnm {
cnmAddon := KubernetesAddon{
Name: cnmAddonName,
Enabled: boolPointer(true),
Containers: []KubernetesContainerSpec{
{
Name: cnmAddonName,
Image: c.customCnmImage,
},
},
}
appendAddonToAPIModel(&v, cnmAddon)
}
if *aksNodeProblemDetector {
nodeProblemDetectorAddon := KubernetesAddon{
Name: nodeProblemDetectorAddonName,
Enabled: boolPointer(true),
}
appendAddonToAPIModel(&v, nodeProblemDetectorAddon)
}
// Populate PrivateAzureRegistryServer field if we are using ACR and custom-built k8s components
if strings.Contains(imageRegistry, "azurecr") && c.aksDeploymentMethod != noop {
v.Properties.OrchestratorProfile.KubernetesConfig.PrivateAzureRegistryServer = imageRegistry
}
switch c.aksDeploymentMethod {
case customHyperkube:
v.Properties.OrchestratorProfile.KubernetesConfig.CustomKubeAPIServerImage = ""
v.Properties.OrchestratorProfile.KubernetesConfig.CustomKubeControllerManagerImage = ""
v.Properties.OrchestratorProfile.KubernetesConfig.CustomKubeProxyImage = ""
v.Properties.OrchestratorProfile.KubernetesConfig.CustomKubeSchedulerImage = ""
v.Properties.OrchestratorProfile.KubernetesConfig.CustomKubeBinaryURL = ""
v.Properties.OrchestratorProfile.KubernetesConfig.CustomHyperkubeImage = c.customHyperkubeImage
case customK8sComponents:
v.Properties.OrchestratorProfile.KubernetesConfig.CustomKubeAPIServerImage = c.customKubeAPIServerImage
v.Properties.OrchestratorProfile.KubernetesConfig.CustomKubeControllerManagerImage = c.customKubeControllerManagerImage
v.Properties.OrchestratorProfile.KubernetesConfig.CustomKubeProxyImage = c.customKubeProxyImage
v.Properties.OrchestratorProfile.KubernetesConfig.CustomKubeSchedulerImage = c.customKubeSchedulerImage
v.Properties.OrchestratorProfile.KubernetesConfig.CustomKubeBinaryURL = c.customKubeBinaryURL
v.Properties.OrchestratorProfile.KubernetesConfig.CustomHyperkubeImage = ""
}
if c.isAzureStackCloud() {
v.Properties.CustomCloudProfile.PortalURL = c.azureCustomCloudURL
}
if len(v.Properties.AgentPoolProfiles) > 0 {
// Default to VirtualMachineScaleSets if AvailabilityProfile is empty
isVMSS := v.Properties.AgentPoolProfiles[0].AvailabilityProfile == "" || v.Properties.AgentPoolProfiles[0].AvailabilityProfile == availabilityProfileVMSS
if err := populateAzureCloudConfig(isVMSS, *c.credentials, c.azureEnvironment, c.resourceGroup, c.location, c.outputDir); err != nil {
return err
}
}
// disable runUnattendedUpgradesOnBootstrap to avoid health check during node reboot
v.Properties.LinuxProfile.RunUnattendedUpgradesOnBootstrap = false
apiModel, _ := json.MarshalIndent(v, "", " ")
c.apiModelPath = path.Join(c.outputDir, "kubernetes.json")
err = os.WriteFile(c.apiModelPath, apiModel, 0644)
if err != nil {
return fmt.Errorf("cannot write apimodel to file: %w", err)
}
return nil
}
func appendAddonToAPIModel(v *AKSEngineAPIModel, addon KubernetesAddon) {
// Update the addon if it already exists in the API model
for i := range v.Properties.OrchestratorProfile.KubernetesConfig.Addons {
a := &v.Properties.OrchestratorProfile.KubernetesConfig.Addons[i]
if a.Name == addon.Name {
a = &addon
return
}
}
v.Properties.OrchestratorProfile.KubernetesConfig.Addons = append(v.Properties.OrchestratorProfile.KubernetesConfig.Addons, addon)
}
func (c *aksEngineDeployer) getAKSEngine(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, *aksEngineURL)
if err := httpRead(*aksEngineURL, f); err == nil {
break
}
err = fmt.Errorf("url=%s failed get %v: %v.", *aksEngineURL, downloadPath, err)
if i == retry-1 {
return err
}
log.Println(err)
sleep(time.Duration(i) * time.Second)
}
f.Close()
if *aksEngineMD5 != "" {
o, err := control.Output(exec.Command("md5sum", f.Name()))
if err != nil {
return err
}
if strings.Split(string(o), " ")[0] != *aksEngineMD5 {
return fmt.Errorf("wrong md5 sum for aks-engine.")
}
}
// adding aks-engine binary to the kubernetes dir modifies the tree
// and dirties the tree. This makes it difficult to diff the CI signal
// so moving the binary to /tmp folder
wd := os.TempDir()
log.Printf("Extracting tar file %v into directory %v .", f.Name(), wd)
if err = control.FinishRunning(exec.Command("tar", "-xzf", f.Name(), "--strip", "1", "-C", wd)); err != nil {
return err
}
c.aksEngineBinaryPath = path.Join(wd, "aks-engine")
return nil
}
func (c *aksEngineDeployer) generateARMTemplates() error {
cmd := exec.Command(c.aksEngineBinaryPath, "generate", c.apiModelPath, "--output-directory", c.outputDir)
cmd.Dir = os.TempDir()
if err := control.FinishRunning(cmd); err != nil {
return fmt.Errorf("failed to generate ARM templates: %v.", err)
}
return nil
}
func (c *aksEngineDeployer) loadARMTemplates() error {
var err error
template, err := os.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 %w", err)
}
parameters, err := os.ReadFile(path.Join(c.outputDir, "azuredeploy.parameters.json"))
if err != nil {
return fmt.Errorf("error reading ARM parameters file: %w", err)
}
c.parametersJSON = make(map[string]interface{})
err = json.Unmarshal(parameters, &c.parametersJSON)
if err != nil {
return fmt.Errorf("error unmarshall parameters %w", err)
}
c.parametersJSON = c.parametersJSON["parameters"].(map[string]interface{})
return nil
}
func (c *aksEngineDeployer) getAzureClient(ctx context.Context) error {
// instantiate Azure Resource Manager Client
env, err := azure.EnvironmentFromName(c.azureEnvironment)
if err != nil {
return err
}
var client *AzureClient
if c.isAzureStackCloud() && strings.EqualFold(c.azureIdentitySystem, ADFSIdentitySystem) {
if client, err = getAzureClient(env,
c.credentials.SubscriptionID,
c.credentials.ClientID,
c.azureIdentitySystem,
c.credentials.ClientSecret); err != nil {
return fmt.Errorf("error trying to get ADFS Azure Client: %w", err)
}
} else {
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: %w", err)
}
}
c.azureClient = client
return nil
}
func (c *aksEngineDeployer) createCluster() error {
var err error
kubecfgDir, _ := os.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: %w", 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: %w", 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: %w", err)
}
if c.useManagedIdentity && c.identityName != "" {
log.Printf("Assigning 'Owner' role to %s in %s", c.identityName, c.resourceGroup)
if err := c.azureClient.AssignOwnerRoleToIdentity(c.ctx, c.resourceGroup, c.identityName); err != nil {
return err
}
}
return nil
}
func (c *aksEngineDeployer) dockerLogin() error {
cwd, _ := os.Getwd()
log.Printf("CWD %v", cwd)
username := ""
pwd := ""
server := ""
var err error
if !strings.Contains(imageRegistry, "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 := os.ReadFile(passwordFile)
if err != nil {
return fmt.Errorf("error reading docker password file %v: %w", 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 = imageRegistry
}
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: %w", err)
}
log.Println("Docker login success.")
return nil
}
func dockerPush(images ...string) error {
for _, image := range images {
log.Printf("Pushing docker image %s", image)
cmd := exec.Command("docker", "push", image)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to push %s: %w", image, err)
}
}
return nil
}
func getDockerImage(imageName string) string {
imageVersion := k8sVersion
switch imageName {
case ccmImageName, cnmImageName:
imageVersion = cloudProviderAzureVersion
}
return fmt.Sprintf("%s/%s:%s", imageRegistry, imageName, imageVersion)
}
func areAllDockerImagesExist(images ...string) bool {
for _, image := range images {
cmd := exec.Command("docker", "pull", image)
if err := cmd.Run(); err != nil {
log.Printf("%s does not exist", image)
return false
}
log.Printf("Reusing %s", image)
}
return true
}
func isURLExist(url string) bool {
cmd := exec.Command("curl", "-s", "-o", "/dev/null", "-s", "-f", url)
if err := cmd.Run(); err != nil {
log.Printf("%s does not exist", url)
return false
}
log.Printf("Reusing %s", url)
return true
}
// getImageVersion returns the image version based on the project's latest git commit
func getImageVersion(projectPath string) string {
cmd := exec.Command("git", "describe", "--tags", "--always", "--dirty")
cmd.Dir = projectPath
output, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(output))
}
func (c *aksEngineDeployer) buildAzureCloudComponents() error {
log.Println("Building cloud controller manager and cloud node manager.")
// Set environment variables for building cloud components' images
if err := os.Setenv("IMAGE_REGISTRY", imageRegistry); err != nil {
return err
}
if err := os.Setenv("IMAGE_TAG", cloudProviderAzureVersion); err != nil {
return err
}
cmd := exec.Command("make", "-C", util.K8sSigs("cloud-provider-azure"), "image", "push")
cmd.Stdout = io.Discard
if err := control.FinishRunning(cmd); err != nil {
return err
}
log.Printf("Custom cloud controller manager image: %s", c.customCcmImage)
if *aksCnm {
c.customCnmImage = getDockerImage(cnmImageName)
log.Printf("Custom cloud node manager image: %s", c.customCnmImage)
}
return nil
}
func (c *aksEngineDeployer) buildHyperkube() error {
var pushCmd *exec.Cmd
os.Setenv("VERSION", k8sVersion)
log.Println("Building hyperkube.")
if _, err := os.Stat(util.K8s("kubernetes", "cmd", "hyperkube")); err == nil {
// cmd/hyperkube binary still exists in repo
cmd := exec.Command("make", "-C", util.K8s("kubernetes"), "WHAT=cmd/hyperkube")
cmd.Stdout = io.Discard
if err := control.FinishRunning(cmd); err != nil {
return err
}
hyperkubeBin := util.K8s("kubernetes", "_output", "bin", "hyperkube")
pushCmd = exec.Command("make", "-C", util.K8s("kubernetes", "cluster", "images", "hyperkube"), "push", fmt.Sprintf("HYPERKUBE_BIN=%s", hyperkubeBin))
} else if os.IsNotExist(err) {
pushCmd = exec.Command("make", "-C", util.K8s("kubernetes", "cluster", "images", "hyperkube"), "push")
}
log.Println("Pushing hyperkube.")
pushCmd.Stdout = io.Discard
if err := control.FinishRunning(pushCmd); err != nil {
return err
}
log.Printf("Custom hyperkube image: %s", c.customHyperkubeImage)
return nil
}
func (c *aksEngineDeployer) uploadToAzureStorage(filePath string) (string, error) {
credential, err := azblob.NewSharedKeyCredential(c.credentials.StorageAccountName, c.credentials.StorageAccountKey)
if err != nil {
return "", fmt.Errorf("new shared key credential: %w", err)
}
p := azblob.NewPipeline(credential, azblob.PipelineOptions{})
URL, _ := url.Parse(c.azureBlobContainerURL)
containerURL := azblob.NewContainerURL(*URL, p)
file, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("failed to open file %v . Error %w", filePath, err)
}
defer file.Close()
blobURL := containerURL.NewBlockBlobURL(filepath.Base(file.Name()))
blobURLString := blobURL.URL()
if _, err = azblob.UploadFileToBlockBlob(context.Background(), file, blobURL, azblob.UploadToBlockBlobOptions{}); err != nil {
// 'BlobHasBeenModified' conflict happens when two concurrent jobs are trying to upload files with the same name
// Simply ignore the error and return the blob URL since at least one job will successfully upload the file to Azure storage
if strings.Contains(err.Error(), "BlobHasBeenModified") {
return blobURLString.String(), nil
}
return "", err
}
log.Printf("Uploaded %s to %s", filePath, blobURLString.String())
return blobURLString.String(), 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 *aksEngineDeployer) buildWinZip() error {
zipName := fmt.Sprintf(winZipTemplate, k8sVersion)
buildFolder := path.Join(os.Getenv("HOME"), "winbuild")
zipPath := path.Join(os.Getenv("HOME"), zipName)
log.Printf("Building %s", zipName)
buildScriptPath, err := getZipBuildScript(*aksWinZipBuildScript, 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 = io.Discard
if err := control.FinishRunning(cmd); err != nil {
return err
}
log.Printf("Uploading %s", zipPath)
if c.aksCustomWinBinariesURL, err = c.uploadToAzureStorage(zipPath); err != nil {
return err
}
return nil
}
func (c *aksEngineDeployer) Up() error {
if *buildWithKubemark {