forked from Azure/aks-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_test.go
5496 lines (5223 loc) · 173 KB
/
validate_test.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 (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package vlabs
import (
"fmt"
"os"
"strings"
"testing"
"github.com/Azure/aks-engine/pkg/api/common"
"github.com/Azure/aks-engine/pkg/helpers"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/to"
"github.com/blang/semver"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const (
ValidKubernetesNodeStatusUpdateFrequency = "10s"
ValidKubernetesCtrlMgrNodeMonitorGracePeriod = "40s"
ValidKubernetesCtrlMgrPodEvictionTimeout = "5m0s"
ValidKubernetesCtrlMgrRouteReconciliationPeriod = "10s"
ValidKubernetesCloudProviderBackoff = false
ValidKubernetesCloudProviderBackoffRetries = 6
ValidKubernetesCloudProviderBackoffJitter = 1
ValidKubernetesCloudProviderBackoffDuration = 5
ValidKubernetesCloudProviderBackoffExponent = 1.5
ValidKubernetesCloudProviderRateLimit = false
ValidKubernetesCloudProviderRateLimitQPS = 3
ValidKubernetesCloudProviderRateLimitBucket = 10
)
var falseVal = false
var trueVal = true
func Test_OrchestratorProfile_Validate(t *testing.T) {
tests := map[string]struct {
properties *Properties
expectedError string
isUpdate bool
}{
"should error when KubernetesConfig populated for non-Kubernetes OrchestratorType": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "DCOS",
KubernetesConfig: &KubernetesConfig{
ClusterSubnet: "10.0.0.0/16",
},
},
},
expectedError: "KubernetesConfig can be specified only when OrchestratorType is Kubernetes",
},
"should error when KubernetesConfig has invalid etcd version": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
KubernetesConfig: &KubernetesConfig{
EtcdVersion: "1.0.0",
},
},
},
expectedError: "Invalid etcd version \"1.0.0\", please use one of the following versions: [2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.1.0 3.1.1 3.1.2 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.1.10 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.23 3.2.24 3.2.25 3.2.26 3.3.0 3.3.1 3.3.8 3.3.9 3.3.10 3.3.13 3.3.15 3.3.18 3.3.19 3.3.22]",
},
"should error when KubernetesConfig has invalid containerd version for containerd runtime": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
KubernetesConfig: &KubernetesConfig{
ContainerRuntime: Containerd,
ContainerdVersion: "1.0.0",
},
},
},
expectedError: "Invalid containerd version \"1.0.0\", please use one of the following versions: [1.3.2]",
},
"should error when KubernetesConfig has containerdVersion value for docker container runtime": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
KubernetesConfig: &KubernetesConfig{
ContainerRuntime: Docker,
ContainerdVersion: "1.0.0",
},
},
},
expectedError: fmt.Sprintf("containerdVersion is only valid in a non-docker context, use %s containerRuntime value instead if you wish to provide a containerdVersion", Containerd),
},
"should error when KubernetesConfig has containerdVersion value for default (empty string) container runtime": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
KubernetesConfig: &KubernetesConfig{
ContainerdVersion: "1.0.0",
},
},
},
expectedError: fmt.Sprintf("containerdVersion is only valid in a non-docker context, use %s containerRuntime value instead if you wish to provide a containerdVersion", Containerd),
},
"should error when KubernetesConfig has enableAggregatedAPIs enabled and enableRBAC disabled": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
OrchestratorVersion: "1.15.11",
KubernetesConfig: &KubernetesConfig{
EnableAggregatedAPIs: true,
EnableRbac: &falseVal,
},
},
},
expectedError: "enableAggregatedAPIs requires the enableRbac feature as a prerequisite",
},
"should error when KubernetesConfig has enableRBAC disabled for >= 1.15": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
OrchestratorVersion: common.GetLatestPatchVersion("1.15", common.GetAllSupportedKubernetesVersions(false, false, false)),
KubernetesConfig: &KubernetesConfig{
EnableRbac: &falseVal,
},
},
},
expectedError: fmt.Sprintf("RBAC support is required for Kubernetes version 1.15.0 or greater; unable to build Kubernetes v%s cluster with enableRbac=false", common.GetLatestPatchVersion("1.15", common.GetAllSupportedKubernetesVersions(false, false, false))),
},
"should error when KubernetesConfig has enableDataEncryptionAtRest enabled with invalid version": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
OrchestratorVersion: "1.6.9",
KubernetesConfig: &KubernetesConfig{
EnableDataEncryptionAtRest: &trueVal,
},
},
},
expectedError: "enableDataEncryptionAtRest is only available in Kubernetes version 1.7.0 or greater; unable to validate for Kubernetes version 1.6.9",
},
"should error when KubernetesConfig has enableDataEncryptionAtRest enabled with invalid encryption key": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
OrchestratorVersion: "1.15.11",
KubernetesConfig: &KubernetesConfig{
EnableDataEncryptionAtRest: &trueVal,
EtcdEncryptionKey: "fakeEncryptionKey",
},
},
},
expectedError: "etcdEncryptionKey must be base64 encoded. Please provide a valid base64 encoded value or leave the etcdEncryptionKey empty to auto-generate the value",
},
"should error when KubernetesConfig has enableEncryptionWithExternalKms enabled with invalid version": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
OrchestratorVersion: "1.6.9",
KubernetesConfig: &KubernetesConfig{
EnableEncryptionWithExternalKms: &trueVal,
},
},
},
expectedError: "enableEncryptionWithExternalKms is only available in Kubernetes version 1.10.0 or greater; unable to validate for Kubernetes version 1.6.9",
},
"should error when KubernetesConfig has Standard loadBalancerSku with invalid version": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
OrchestratorVersion: "1.6.9",
KubernetesConfig: &KubernetesConfig{
LoadBalancerSku: StandardLoadBalancerSku,
},
},
},
expectedError: "loadBalancerSku is only available in Kubernetes version 1.11.0 or greater; unable to validate for Kubernetes version 1.6.9",
},
"should error when KubernetesConfig has Basic loadBalancerSku with loadBalancerOutboundIPs config": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
OrchestratorVersion: "1.18.8",
KubernetesConfig: &KubernetesConfig{
LoadBalancerSku: BasicLoadBalancerSku,
ExcludeMasterFromStandardLB: to.BoolPtr(true),
LoadBalancerOutboundIPs: to.IntPtr(3),
},
},
},
expectedError: "kubernetesConfig.loadBalancerOutboundIPs configuration only supported for Standard loadBalancerSku=Standard",
},
"should error when too many loadBalancerOutboundIPs are configured": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
OrchestratorVersion: "1.18.8",
KubernetesConfig: &KubernetesConfig{
LoadBalancerOutboundIPs: to.IntPtr(17),
},
},
},
expectedError: fmt.Sprintf("kubernetesConfig.loadBalancerOutboundIPs was set to %d, the maximum allowed is %d", 17, common.MaxLoadBalancerOutboundIPs),
},
"should error when KubernetesConfig has enablePodSecurity enabled with invalid settings": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
OrchestratorVersion: "1.15.11",
KubernetesConfig: &KubernetesConfig{
EnablePodSecurityPolicy: &trueVal,
},
},
},
expectedError: "enablePodSecurityPolicy requires the enableRbac feature as a prerequisite",
},
"should not error with empty object": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
DcosConfig: &DcosConfig{},
},
},
},
"should error when DcosConfig orchestrator has invalid configuration": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "DCOS",
OrchestratorVersion: "1.12.0",
},
},
expectedError: "the following OrchestratorProfile configuration is not supported: OrchestratorType: DCOS, OrchestratorRelease: , OrchestratorVersion: 1.12.0. Please check supported Release or Version for this build of aks-engine",
},
"should error when DcosConfig orchestrator configuration has invalid static IP": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "DCOS",
DcosConfig: &DcosConfig{
BootstrapProfile: &BootstrapProfile{
StaticIP: "0.0.0.0.0.0",
},
},
},
},
expectedError: "DcosConfig.BootstrapProfile.StaticIP '0.0.0.0.0.0' is an invalid IP address",
},
"should error when DcosConfig populated for non-Kubernetes OrchestratorType 1": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
DcosConfig: &DcosConfig{
DcosWindowsBootstrapURL: "http://www.microsoft.com",
},
},
},
expectedError: "DcosConfig can be specified only when OrchestratorType is DCOS",
},
"should error when DcosConfig populated for non-Kubernetes OrchestratorType 2": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
DcosConfig: &DcosConfig{
DcosWindowsBootstrapURL: "http://www.microsoft.com",
DcosBootstrapURL: "http://www.microsoft.com",
},
},
},
expectedError: "DcosConfig can be specified only when OrchestratorType is DCOS",
},
"kubernetes should have failed on old patch version": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
OrchestratorVersion: "1.6.0",
},
},
expectedError: fmt.Sprint("the following OrchestratorProfile configuration is not supported: OrchestratorType: \"Kubernetes\", OrchestratorRelease: \"\", OrchestratorVersion: \"1.6.0\". Please use one of the following versions: ", common.GetAllSupportedKubernetesVersions(false, false, false)),
},
"kubernetes should not fail on old patch version if update": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
OrchestratorVersion: "1.6.0",
},
},
isUpdate: true,
},
"kubernetes should not have failed on version with v prefix": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
OrchestratorVersion: "v1.15.11",
},
},
},
"should error when maximumLoadBalancerRuleCount populated with a negative integer": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
KubernetesConfig: &KubernetesConfig{
MaximumLoadBalancerRuleCount: -1,
},
},
},
expectedError: "maximumLoadBalancerRuleCount shouldn't be less than 0",
},
"should error when outboundRuleIdleTimeoutInMinutes populated is out of valid range": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
KubernetesConfig: &KubernetesConfig{
LoadBalancerSku: StandardLoadBalancerSku,
ExcludeMasterFromStandardLB: to.BoolPtr(true),
OutboundRuleIdleTimeoutInMinutes: 3,
},
},
},
expectedError: "outboundRuleIdleTimeoutInMinutes shouldn't be less than 4 or greater than 120",
},
"should error when EtcdStorageLimitGB is too low": {
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: "Kubernetes",
KubernetesConfig: &KubernetesConfig{
EtcdStorageLimitGB: 1,
},
},
},
expectedError: "EtcdStorageLimitGB value of 1 is too small, the minimum allowed is 2",
},
}
for testName, test := range tests {
test := test
t.Run(testName, func(t *testing.T) {
t.Parallel()
err := test.properties.ValidateOrchestratorProfile(test.isUpdate)
if test.expectedError == "" && err == nil {
return
}
if test.expectedError == "" && err != nil {
t.Errorf("%s expected no error but received: %s", testName, err.Error())
return
}
if test.expectedError != "" && err == nil {
t.Errorf("%s expected error: %s, but received no error", testName, test.expectedError)
return
}
if !strings.Contains(err.Error(), test.expectedError) {
t.Errorf("%s expected error: %s but received: %s", testName, test.expectedError, err.Error())
}
})
}
}
func ExampleProperties_validateOrchestratorProfile() {
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
DisableTimestamp: true,
})
cs := getK8sDefaultContainerService(true)
cs.Properties.OrchestratorProfile.KubernetesConfig = &KubernetesConfig{
EtcdStorageLimitGB: 9,
}
if err := cs.Properties.ValidateOrchestratorProfile(false); err != nil {
log.Error(err)
}
// Output:
// level=warning msg="EtcdStorageLimitGB of 9 is larger than the recommended maximum of 8"
}
func Test_KubernetesConfig_Validate(t *testing.T) {
// Tests that should pass across all versions
for _, k8sVersion := range common.GetAllSupportedKubernetesVersions(true, false, false) {
c := KubernetesConfig{}
if err := c.Validate(k8sVersion, false, false, false); err != nil {
t.Errorf("should not error on empty KubernetesConfig: %v, version %s", err, k8sVersion)
}
c = KubernetesConfig{
ClusterSubnet: "10.120.0.0/16",
DockerBridgeSubnet: "10.120.1.0/16",
MaxPods: 42,
CloudProviderBackoff: to.BoolPtr(ValidKubernetesCloudProviderBackoff),
CloudProviderBackoffRetries: ValidKubernetesCloudProviderBackoffRetries,
CloudProviderBackoffJitter: ValidKubernetesCloudProviderBackoffJitter,
CloudProviderBackoffDuration: ValidKubernetesCloudProviderBackoffDuration,
CloudProviderBackoffExponent: ValidKubernetesCloudProviderBackoffExponent,
CloudProviderRateLimit: to.BoolPtr(ValidKubernetesCloudProviderRateLimit),
CloudProviderRateLimitQPS: ValidKubernetesCloudProviderRateLimitQPS,
CloudProviderRateLimitBucket: ValidKubernetesCloudProviderRateLimitBucket,
KubeletConfig: map[string]string{
"--node-status-update-frequency": ValidKubernetesNodeStatusUpdateFrequency,
},
ControllerManagerConfig: map[string]string{
"--node-monitor-grace-period": ValidKubernetesCtrlMgrNodeMonitorGracePeriod,
"--pod-eviction-timeout": ValidKubernetesCtrlMgrPodEvictionTimeout,
"--route-reconciliation-period": ValidKubernetesCtrlMgrRouteReconciliationPeriod,
},
}
if err := c.Validate(k8sVersion, false, false, false); err != nil {
t.Errorf("should not error on a KubernetesConfig with valid param values: %v", err)
}
c = KubernetesConfig{
ClusterSubnet: "10.16.x.0/invalid",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error on invalid ClusterSubnet")
}
c = KubernetesConfig{
DockerBridgeSubnet: "10.120.1.0/invalid",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error on invalid DockerBridgeSubnet")
}
c = KubernetesConfig{
KubeletConfig: map[string]string{
"--non-masquerade-cidr": "10.120.1.0/24",
},
}
if err := c.Validate(k8sVersion, false, false, false); err != nil {
t.Error("should not error on valid --non-masquerade-cidr")
}
// Re-implement these tests if we re-introduce --ip-maquerade-cidr
/*c = KubernetesConfig{
KubeletConfig: map[string]string{
"--non-masquerade-cidr": "10.120.1.0/invalid",
},
}
if err := c.Validate(k8sVersion, false); err == nil {
t.Error("should error on invalid --non-masquerade-cidr")
}*/
c = KubernetesConfig{
MaxPods: KubernetesMinMaxPods - 1,
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error on invalid MaxPods")
}
c = KubernetesConfig{
KubeletConfig: map[string]string{
"--node-status-update-frequency": "invalid",
},
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error on invalid --node-status-update-frequency kubelet config")
}
c = KubernetesConfig{
ControllerManagerConfig: map[string]string{
"--node-monitor-grace-period": "invalid",
},
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error on invalid --node-monitor-grace-period")
}
c = KubernetesConfig{
ControllerManagerConfig: map[string]string{
"--node-monitor-grace-period": "30s",
},
KubeletConfig: map[string]string{
"--node-status-update-frequency": "10s",
},
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when --node-monitor-grace-period is not sufficiently larger than --node-status-update-frequency kubelet config")
}
c = KubernetesConfig{
ControllerManagerConfig: map[string]string{
"--pod-eviction-timeout": "invalid",
},
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error on invalid --pod-eviction-timeout")
}
c = KubernetesConfig{
ControllerManagerConfig: map[string]string{
"--route-reconciliation-period": "invalid",
},
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error on invalid --route-reconciliation-period")
}
c = KubernetesConfig{
DNSServiceIP: "192.168.0.10",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when DNSServiceIP but not ServiceCidr")
}
c = KubernetesConfig{
ServiceCidr: "192.168.0.10/24",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when ServiceCidr but not DNSServiceIP")
}
c = KubernetesConfig{
DNSServiceIP: "invalid",
ServiceCidr: "192.168.0.0/24",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when DNSServiceIP is invalid")
}
c = KubernetesConfig{
DNSServiceIP: "192.168.1.10",
ServiceCidr: "192.168.0.0/not-a-len",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when ServiceCidr is invalid")
}
c = KubernetesConfig{
DNSServiceIP: "192.168.1.10",
ServiceCidr: "192.168.0.0/24",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when DNSServiceIP is outside of ServiceCidr")
}
c = KubernetesConfig{
DNSServiceIP: "172.99.255.255",
ServiceCidr: "172.99.0.1/16",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when DNSServiceIP is broadcast address of ServiceCidr")
}
c = KubernetesConfig{
DNSServiceIP: "172.99.0.1",
ServiceCidr: "172.99.0.1/16",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when DNSServiceIP is first IP of ServiceCidr")
}
c = KubernetesConfig{
DNSServiceIP: "172.99.255.10",
ServiceCidr: "172.99.0.1/16",
}
if err := c.Validate(k8sVersion, false, false, false); err != nil {
t.Error("should not error when DNSServiceIP and ServiceCidr are valid")
}
c = KubernetesConfig{
ClusterSubnet: "192.168.0.1/24",
NetworkPlugin: "azure",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when ClusterSubnet has a mask of 24 bits or higher")
}
c = KubernetesConfig{
ClusterSubnet: "192.168.0.1/24",
NetworkPlugin: "azure",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when ClusterSubnet has a mask of 24 bits or higher")
}
c = KubernetesConfig{
ProxyMode: KubeProxyMode("invalid"),
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when ProxyMode has an invalid string value")
}
for _, validProxyModeValue := range []KubeProxyMode{KubeProxyModeIPTables, KubeProxyModeIPVS} {
c = KubernetesConfig{
ProxyMode: validProxyModeValue,
}
if err := c.Validate(k8sVersion, false, false, false); err != nil {
t.Error("should error when ProxyMode has a valid string value")
}
c = KubernetesConfig{
ProxyMode: validProxyModeValue,
}
if err := c.Validate(k8sVersion, false, false, false); err != nil {
t.Error("should error when ProxyMode has a valid string value")
}
}
}
// Tests that apply to 1.6 and later releases
for _, k8sVersion := range common.GetAllSupportedKubernetesVersions(false, false, false) {
c := KubernetesConfig{
CloudProviderBackoff: to.BoolPtr(true),
CloudProviderRateLimit: to.BoolPtr(true),
}
if err := c.Validate(k8sVersion, false, false, false); err != nil {
t.Error("should not error when basic backoff and rate limiting are set to true with no options")
}
}
// Tests that apply to 1.8 and later releases
for _, k8sVersion := range common.GetVersionsGt(common.GetAllSupportedKubernetesVersions(true, false, false), "1.8.0", true, true) {
c := KubernetesConfig{
UseCloudControllerManager: to.BoolPtr(true),
}
if err := c.Validate(k8sVersion, false, false, false); err != nil {
t.Error("should not error because UseCloudControllerManager is available since v1.8")
}
}
// Tests that apply to dualstack with 1.16 and later releases
for _, k8sVersion := range common.GetVersionsGt(common.GetAllSupportedKubernetesVersions(false, false, false), "1.16.0", true, true) {
c := KubernetesConfig{
NetworkPlugin: "kubenet",
ClusterSubnet: "10.244.0.0/16,ace:cab:deca::/8",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when more than 1 cluster subnet provided with ipv6dualstack feature disabled")
}
c = KubernetesConfig{
NetworkPlugin: "kubenet",
ClusterSubnet: "10.244.0.0/16,ace:cab:deca::/8,fec0::/7",
}
if err := c.Validate(k8sVersion, false, true, false); err == nil {
t.Error("should error when more than 2 cluster subnets provided")
}
c = KubernetesConfig{
NetworkPlugin: "kubenet",
ClusterSubnet: "10.244.0.0/16,ace:cab:deca::/8",
ProxyMode: "iptables",
}
if err := c.Validate(k8sVersion, false, true, false); err == nil && !common.IsKubernetesVersionGe(k8sVersion, "1.18.0") {
t.Errorf("should error with ipv6 dual stack feature enabled as iptables mode not supported in %s", k8sVersion)
}
c = KubernetesConfig{
ServiceCidr: "10.0.0.0/16,fe80:20d::/112",
}
if err := c.Validate(k8sVersion, false, false, false); err == nil {
t.Error("should error when more than 1 service cidr provided with ipv6dualstack feature disabled")
}
c = KubernetesConfig{
NetworkPlugin: "kubenet",
ClusterSubnet: "10.244.0.0/16,ace:cab:deca::/8",
ProxyMode: "ipvs",
ServiceCidr: "10.0.0.0/16,fe80:20d::/112,fec0::/7",
DNSServiceIP: "10.0.0.10",
}
if err := c.Validate(k8sVersion, false, true, false); err == nil {
t.Error("should error when more than 2 service cidr provided with ipv6dualstack feature enabled")
}
c = KubernetesConfig{
NetworkPlugin: "kubenet",
ClusterSubnet: "10.244.0.0/16,ace:cab:deca::/8",
ProxyMode: "ipvs",
ServiceCidr: "10.0.0.0/16,2001:db8::/129",
DNSServiceIP: "10.0.0.10",
}
if err := c.Validate(k8sVersion, false, true, false); err == nil {
t.Error("should error when secondary cidr is invalid with ipv6dualstack feature enabled")
}
c = KubernetesConfig{
NetworkPlugin: "kubenet",
ClusterSubnet: "10.244.0.0/16,ace:cab:deca::/8",
ProxyMode: "ipvs",
ServiceCidr: "10.0.0.0/16,fe80:20d::/112",
DNSServiceIP: "10.0.0.10",
}
if err := c.Validate(k8sVersion, false, true, false); err != nil {
t.Error("shouldn't have errored with ipv6 dual stack feature enabled")
}
// validate config with azure cni dual stack and network policy enabled.
c = KubernetesConfig{
NetworkPlugin: "azure",
ClusterSubnet: "10.240.0.0/12,fe80:20d::/112",
NetworkPolicy: "azure",
}
if err := c.Validate(k8sVersion, false, true, false); err == nil {
t.Errorf("should error when network policy defined for azure cni dual stack: %v", err)
}
//validate azure cni dual stack enabled scenario
c = KubernetesConfig{
NetworkPlugin: "azure",
ClusterSubnet: "10.240.0.0/16,ace:cab:deca::/8",
ProxyMode: "ipvs",
ServiceCidr: "10.0.0.0/16,fe80:20d::/112",
DNSServiceIP: "10.0.0.10",
}
if err := c.Validate(k8sVersion, false, true, false); err != nil {
t.Errorf("shouldn't have errored with azure cni ipv6 dual stack feature enabled: %v", err)
}
}
// Tests that apply to single stack IPv6 with 1.18 and later releases
for _, k8sVersion := range common.GetVersionsGt(common.GetAllSupportedKubernetesVersions(false, false, false), "1.18.0", true, true) {
c := KubernetesConfig{
NetworkPlugin: "azure",
}
if err := c.Validate(k8sVersion, false, false, true); err == nil {
t.Error("should error when network plugin is not kubenet for single stack IPv6")
}
if err := c.Validate(k8sVersion, false, true, true); err == nil {
t.Error("should error when dual stack and single stack IPv6 enabled simultaneously")
}
}
}
func Test_Properties_ValidateCustomKubeComponent(t *testing.T) {
p := &Properties{}
p.OrchestratorProfile = &OrchestratorProfile{}
p.OrchestratorProfile.OrchestratorType = Kubernetes
p.OrchestratorProfile.KubernetesConfig = &KubernetesConfig{}
p.OrchestratorProfile.OrchestratorVersion = "1.17.0"
p.OrchestratorProfile.KubernetesConfig.CustomHyperkubeImage = "example.azurecr.io/hyperkube-amd64:tag"
err := p.validateCustomKubeComponent()
expectedMsg := "customHyperkubeImage has no effect in Kubernetes version 1.17.0 or above"
if err.Error() != expectedMsg {
t.Errorf("expected error message : %s to be thrown, but got : %s", expectedMsg, err.Error())
}
p.OrchestratorProfile.KubernetesConfig.CustomHyperkubeImage = ""
p.OrchestratorProfile.KubernetesConfig.CustomKubeAPIServerImage = "example.azurecr.io/kube-apiserver-amd64:tag"
p.OrchestratorProfile.KubernetesConfig.CustomKubeControllerManagerImage = "example.azurecr.io/kube-controller-manager-amd64:tag"
p.OrchestratorProfile.KubernetesConfig.CustomKubeProxyImage = "example.azurecr.io/kube-proxy-amd64:tag"
p.OrchestratorProfile.KubernetesConfig.CustomKubeSchedulerImage = "example.azurecr.io/kube-scheduler-amd64:tag"
p.OrchestratorProfile.KubernetesConfig.CustomKubeBinaryURL = "https://example.blob.core.windows.net/kubernetes-node-linux-amd64.tar.gz"
err = p.validateCustomKubeComponent()
if err != nil {
t.Errorf("should not error because custom kube components can be used in 1.17, got error : %s", err.Error())
}
p.OrchestratorProfile.OrchestratorVersion = "1.16.0"
err = p.validateCustomKubeComponent()
expectedMsg = "customKubeAPIServerImage, customKubeControllerManagerImage, customKubeSchedulerImage or customKubeBinaryURL have no effect in Kubernetes version 1.16 or earlier"
if err.Error() != expectedMsg {
t.Errorf("expected error message : %s to be thrown, but got : %s", expectedMsg, err.Error())
}
p.OrchestratorProfile.OrchestratorVersion = "1.15.0"
p.OrchestratorProfile.KubernetesConfig.CustomHyperkubeImage = "example.azurecr.io/hyperkube-amd64:tag"
p.OrchestratorProfile.KubernetesConfig.CustomKubeAPIServerImage = ""
p.OrchestratorProfile.KubernetesConfig.CustomKubeControllerManagerImage = ""
p.OrchestratorProfile.KubernetesConfig.CustomKubeProxyImage = "example.azurecr.io/kube-proxy-amd64:tag"
p.OrchestratorProfile.KubernetesConfig.CustomKubeSchedulerImage = ""
p.OrchestratorProfile.KubernetesConfig.CustomKubeBinaryURL = ""
err = p.validateCustomKubeComponent()
expectedMsg = "customKubeProxyImage has no effect in Kubernetes version 1.15 or earlier"
if err.Error() != expectedMsg {
t.Errorf("expected error message : %s to be thrown, but got : %s", expectedMsg, err.Error())
}
p.OrchestratorProfile.OrchestratorVersion = "1.16.0"
err = p.validateCustomKubeComponent()
if err != nil {
t.Errorf("should not error because custom kube-proxy and hyperkube components can be used in 1.16, got error : %s", err.Error())
}
}
func Test_Properties_ValidateDistro(t *testing.T) {
p := &Properties{}
p.OrchestratorProfile = &OrchestratorProfile{}
p.OrchestratorProfile.OrchestratorType = Kubernetes
p.MasterProfile = &MasterProfile{
DNSPrefix: "foo",
}
// Should not error on valid distros in non-update scenarios
for _, distro := range DistroValues {
p.MasterProfile.Distro = distro
p.AgentPoolProfiles = []*AgentPoolProfile{
{
Name: "pool1",
Distro: distro,
},
}
if err := p.validateMasterProfile(false); err != nil {
t.Errorf(
"should not error on distro=\"%s\"",
distro,
)
}
if err := p.validateAgentPoolProfiles(false); err != nil {
t.Errorf(
"should not error on distro=\"%s\"",
distro,
)
}
}
// Should not error on valid distros in update scenarios
for _, distro := range DistroValues {
p.MasterProfile.Distro = distro
p.AgentPoolProfiles = []*AgentPoolProfile{
{
Name: "pool1",
Distro: distro,
},
}
if err := p.validateMasterProfile(true); err != nil {
t.Errorf(
"should not error on distro=\"%s\"",
distro,
)
}
if err := p.validateAgentPoolProfiles(true); err != nil {
t.Errorf(
"should not error on distro=\"%s\"",
distro,
)
}
}
// Should error for invalid distros on non-update scenarios
bogusDistroValues := []Distro{AKSDockerEngine, AKS1604Deprecated, AKS1804Deprecated, "bogon"}
for _, distro := range bogusDistroValues {
p.MasterProfile.Distro = distro
p.AgentPoolProfiles = []*AgentPoolProfile{
{
Name: "pool1",
Distro: distro,
},
}
if err := p.validateMasterProfile(false); err == nil {
t.Errorf(
"should error on distro=\"%s\"",
distro,
)
}
if err := p.validateAgentPoolProfiles(false); err == nil {
t.Errorf(
"should error on distro=\"%s\"",
distro,
)
}
}
// Should not error for deprecated distro on update scenarios
oldDistros := []Distro{AKSDockerEngine, AKS1604Deprecated, AKS1804Deprecated}
for _, distro := range oldDistros {
p.MasterProfile.Distro = distro
p.AgentPoolProfiles = []*AgentPoolProfile{
{
Name: "pool1",
Distro: distro,
},
}
if err := p.validateMasterProfile(true); err != nil {
t.Errorf(
"should error on distro=\"%s\"",
distro,
)
}
if err := p.validateAgentPoolProfiles(true); err != nil {
t.Errorf(
"should error on distro=\"%s\"",
distro,
)
}
}
}
func Test_Properties_ValidateNetworkPolicy(t *testing.T) {
p := &Properties{}
p.OrchestratorProfile = &OrchestratorProfile{}
p.OrchestratorProfile.OrchestratorType = Kubernetes
k8sVersion := "1.8.0"
for _, policy := range NetworkPolicyValues {
p.OrchestratorProfile.KubernetesConfig = &KubernetesConfig{}
p.OrchestratorProfile.KubernetesConfig.NetworkPolicy = policy
if err := p.OrchestratorProfile.KubernetesConfig.validateNetworkPolicy(k8sVersion, false); err != nil {
t.Errorf(
"should not error on networkPolicy=\"%s\" on k8sVersion=\"%s\"",
policy,
k8sVersion,
)
}
}
p.OrchestratorProfile.KubernetesConfig.NetworkPolicy = "not-existing"
if err := p.OrchestratorProfile.KubernetesConfig.validateNetworkPolicy(k8sVersion, false); err == nil {
t.Errorf(
"should error on invalid networkPolicy",
)
}
k8sVersion = "1.7.9"
p.OrchestratorProfile.KubernetesConfig.NetworkPolicy = "azure"
p.OrchestratorProfile.KubernetesConfig.NetworkPlugin = "azure"
if err := p.OrchestratorProfile.KubernetesConfig.validateNetworkPolicy(k8sVersion, false); err == nil {
t.Errorf(
"should error on azure networkPolicy + azure networkPlugin with k8s version < 1.8.0",
)
}
p.OrchestratorProfile.KubernetesConfig.NetworkPolicy = "calico"
if err := p.OrchestratorProfile.KubernetesConfig.validateNetworkPolicy(k8sVersion, true); err == nil {
t.Errorf(
"should error on calico for windows clusters",
)
}
p.OrchestratorProfile.KubernetesConfig.NetworkPolicy = NetworkPolicyCilium
if err := p.OrchestratorProfile.KubernetesConfig.validateNetworkPolicy(k8sVersion, true); err == nil {
t.Errorf(
"should error on cilium for windows clusters",
)
}
p.OrchestratorProfile.KubernetesConfig.NetworkPolicy = NetworkPolicyAntrea
if err := p.OrchestratorProfile.KubernetesConfig.validateNetworkPolicy(k8sVersion, true); err == nil {
t.Errorf(
"should error on antrea for windows clusters",
)
}
}
func ExampleKubernetesConfig_validateNetworkPlugin() {
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
DisableTimestamp: true,
})
cs := getK8sDefaultContainerService(true)
cs.Properties.OrchestratorProfile.KubernetesConfig = &KubernetesConfig{}
cs.Properties.OrchestratorProfile.KubernetesConfig.NetworkPlugin = NetworkPluginKubenet
if err := cs.Properties.OrchestratorProfile.KubernetesConfig.validateNetworkPlugin(true); err != nil {
fmt.Printf("error in ValidateNetworkPlugin: %s", err)
}
// Output:
// level=warning msg="Windows + Kubenet is for development and testing only, not recommended for production"
}
func Test_Properties_ValidateNetworkPlugin(t *testing.T) {
p := &Properties{}
p.OrchestratorProfile = &OrchestratorProfile{}
p.OrchestratorProfile.OrchestratorType = Kubernetes
for _, policy := range NetworkPluginValues {
p.OrchestratorProfile.KubernetesConfig = &KubernetesConfig{}
p.OrchestratorProfile.KubernetesConfig.NetworkPlugin = policy
if err := p.OrchestratorProfile.KubernetesConfig.validateNetworkPlugin(false); err != nil {
t.Errorf(
"should not error on networkPolicy=\"%s\"",
policy,
)
}
}
p.OrchestratorProfile.KubernetesConfig.NetworkPlugin = "not-existing"
if err := p.OrchestratorProfile.KubernetesConfig.validateNetworkPlugin(false); err == nil {
t.Errorf(
"should error on invalid networkPlugin",
)
}
p.OrchestratorProfile.KubernetesConfig.NetworkPlugin = NetworkPluginAntrea
if err := p.OrchestratorProfile.KubernetesConfig.validateNetworkPlugin(true); err == nil {
t.Errorf(
"should error on antrea for windows clusters",
)
}
}
func Test_Properties_ValidateNetworkPluginPlusPolicy(t *testing.T) {
p := &Properties{}
p.OrchestratorProfile = &OrchestratorProfile{}
p.OrchestratorProfile.OrchestratorType = Kubernetes
for _, config := range networkPluginPlusPolicyAllowed {