-
Notifications
You must be signed in to change notification settings - Fork 65
/
cloud_test.go
743 lines (664 loc) · 28 KB
/
cloud_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
package cloud
import (
"fmt"
"strings"
"testing"
. "github.com/onsi/gomega"
configv1 "github.com/openshift/api/config/v1"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/openshift/cluster-cloud-controller-manager-operator/pkg/cloud/common"
"github.com/openshift/cluster-cloud-controller-manager-operator/pkg/config"
"github.com/openshift/cluster-cloud-controller-manager-operator/pkg/util/testingutils"
)
const (
cloudControllerManagerDefaultPort = 10258
cloudNodeManagerDefaultPort = 10263
)
func getDummyPlatformStatus(platformType configv1.PlatformType, isAzureStack bool) *configv1.PlatformStatus {
platformStatus := configv1.PlatformStatus{
Type: platformType,
}
if isAzureStack {
platformStatus.Azure = &configv1.AzurePlatformStatus{
CloudName: configv1.AzureStackCloud,
}
}
return &platformStatus
}
type testPlatform struct {
platformStatus *configv1.PlatformStatus
}
func (tp *testPlatform) getOperatorConfig() config.OperatorConfig {
return config.OperatorConfig{
ManagedNamespace: "openshift-cloud-controller-manager",
ImagesReference: config.ImagesReference{
CloudControllerManagerOperator: "registry.ci.openshift.org/openshift:cluster-cloud-controller-manager-operator",
CloudControllerManagerAWS: "registry.ci.openshift.org/openshift:aws-cloud-controller-manager",
CloudControllerManagerAzure: "quay.io/openshift/origin-azure-cloud-controller-manager",
CloudNodeManagerAzure: "quay.io/openshift/origin-azure-cloud-node-manager",
CloudControllerManagerGCP: "registry.ci.openshift.org/openshift:gcp-cloud-controller-manager",
CloudControllerManagerIBM: "registry.ci.openshift.org/openshift:ibm-cloud-controller-manager",
CloudControllerManagerOpenStack: "registry.ci.openshift.org/openshift:openstack-cloud-controller-manager",
CloudControllerManagerVSphere: "registry.ci.openshift.org/openshift:vsphere-cloud-controller-manager",
CloudControllerManagerPowerVS: "quay.io/openshift/origin-powervs-cloud-controller-manager",
CloudControllerManagerNutanix: "quay.io/openshift/origin-nutanix-cloud-controller-manager",
},
PlatformStatus: tp.platformStatus,
InfrastructureName: "my-cool-cluster-777",
}
}
type testPlatformsMap map[string]testPlatform
func getPlatforms() testPlatformsMap {
return testPlatformsMap{
string(configv1.AWSPlatformType): {getDummyPlatformStatus(configv1.AWSPlatformType, false)},
string(configv1.AzurePlatformType): {getDummyPlatformStatus(configv1.AzurePlatformType, false)},
"AzureStackHub": {getDummyPlatformStatus(configv1.AzurePlatformType, true)},
string(configv1.BareMetalPlatformType): {getDummyPlatformStatus(configv1.BareMetalPlatformType, false)},
string(configv1.GCPPlatformType): {getDummyPlatformStatus(configv1.GCPPlatformType, false)},
string(configv1.IBMCloudPlatformType): {getDummyPlatformStatus(configv1.IBMCloudPlatformType, false)},
string(configv1.KubevirtPlatformType): {getDummyPlatformStatus(configv1.KubevirtPlatformType, false)},
string(configv1.LibvirtPlatformType): {getDummyPlatformStatus(configv1.LibvirtPlatformType, false)},
string(configv1.NonePlatformType): {getDummyPlatformStatus(configv1.NonePlatformType, false)},
string(configv1.NutanixPlatformType): {getDummyPlatformStatus(configv1.NutanixPlatformType, false)},
string(configv1.OpenStackPlatformType): {getDummyPlatformStatus(configv1.OpenStackPlatformType, false)},
string(configv1.OvirtPlatformType): {getDummyPlatformStatus(configv1.OvirtPlatformType, false)},
string(configv1.PowerVSPlatformType): {getDummyPlatformStatus(configv1.PowerVSPlatformType, false)},
string(configv1.VSpherePlatformType): {getDummyPlatformStatus(configv1.VSpherePlatformType, false)},
}
}
func TestGetResources(t *testing.T) {
platformsMap := getPlatforms()
t.Log("disabling klog logging")
testingutils.TurnOffKlog()
defer func() {
t.Log("enabling klog logging")
testingutils.TurnOnKlog()
}()
tc := []struct {
name string
testPlatform testPlatform
expectedResourceCount int
singleReplica bool
expectedResourcesKindName []string
}{{
name: "AWS resources returned as expected",
testPlatform: platformsMap[string(configv1.AWSPlatformType)],
expectedResourceCount: 2,
expectedResourcesKindName: []string{
"Deployment/aws-cloud-controller-manager",
"PodDisruptionBudget/aws-cloud-controller-manager",
},
}, {
name: "AWS resources returned as expected with single node cluster",
testPlatform: platformsMap[string(configv1.AWSPlatformType)],
expectedResourceCount: 1,
singleReplica: true,
expectedResourcesKindName: []string{"Deployment/aws-cloud-controller-manager"},
}, {
name: "OpenStack resources returned as expected",
testPlatform: platformsMap[string(configv1.OpenStackPlatformType)],
expectedResourceCount: 2,
expectedResourcesKindName: []string{
"Deployment/openstack-cloud-controller-manager",
"PodDisruptionBudget/openstack-cloud-controller-manager",
},
}, {
name: "OpenStack resources returned as expected with signle node cluster",
testPlatform: platformsMap[string(configv1.OpenStackPlatformType)],
expectedResourceCount: 1,
singleReplica: true,
expectedResourcesKindName: []string{
"Deployment/openstack-cloud-controller-manager",
},
}, {
name: "GCP resources returned as expected",
testPlatform: platformsMap[string(configv1.GCPPlatformType)],
expectedResourceCount: 4,
expectedResourcesKindName: []string{
"Deployment/gcp-cloud-controller-manager",
"PodDisruptionBudget/gcp-cloud-controller-manager",
"ClusterRole/gcp-cloud-controller-manager",
"ClusterRoleBinding/gcp-cloud-controller-manager:cloud-provider",
},
}, {
name: "GCP resources returned as expected with single node cluster",
testPlatform: platformsMap[string(configv1.GCPPlatformType)],
expectedResourceCount: 3,
singleReplica: true,
expectedResourcesKindName: []string{
"Deployment/gcp-cloud-controller-manager",
"ClusterRole/gcp-cloud-controller-manager",
"ClusterRoleBinding/gcp-cloud-controller-manager:cloud-provider",
},
}, {
name: "Azure resources returned as expected",
testPlatform: platformsMap[string(configv1.AzurePlatformType)],
expectedResourceCount: 5,
expectedResourcesKindName: []string{
"Deployment/azure-cloud-controller-manager",
"DaemonSet/azure-cloud-node-manager",
"ClusterRole/azure-cloud-controller-manager",
"ClusterRoleBinding/cloud-controller-manager:azure-cloud-controller-manager",
"PodDisruptionBudget/azure-cloud-controller-manager",
},
}, {
name: "Azure resources returned as expected with single node cluster",
testPlatform: platformsMap[string(configv1.AzurePlatformType)],
expectedResourceCount: 4,
singleReplica: true,
expectedResourcesKindName: []string{
"Deployment/azure-cloud-controller-manager",
"DaemonSet/azure-cloud-node-manager",
"ClusterRole/azure-cloud-controller-manager",
"ClusterRoleBinding/cloud-controller-manager:azure-cloud-controller-manager",
},
}, {
name: "Azure Stack resources returned as expected",
testPlatform: platformsMap["AzureStackHub"],
expectedResourceCount: 3,
expectedResourcesKindName: []string{
"Deployment/azure-cloud-controller-manager",
"DaemonSet/azure-cloud-node-manager",
"PodDisruptionBudget/azure-cloud-controller-manager",
},
}, {
name: "Azure Stack resources returned as expected with single node",
testPlatform: platformsMap["AzureStackHub"],
expectedResourceCount: 2,
singleReplica: true,
expectedResourcesKindName: []string{
"Deployment/azure-cloud-controller-manager",
"DaemonSet/azure-cloud-node-manager",
},
}, {
name: "VSphere resources returned as expected",
testPlatform: platformsMap[string(configv1.VSpherePlatformType)],
expectedResourceCount: 8,
expectedResourcesKindName: []string{
"Deployment/vsphere-cloud-controller-manager",
"PodDisruptionBudget/vsphere-cloud-controller-manager",
"Role/vsphere-cloud-controller-manager",
"RoleBinding/vsphere-cloud-controller-manager:vsphere-cloud-controller-manager",
"RoleBinding/vsphere-cloud-controller-manager:cloud-controller-manager",
"ClusterRole/vsphere-cloud-controller-manager",
"ClusterRoleBinding/vsphere-cloud-controller-manager:vsphere-cloud-controller-manager",
"ClusterRoleBinding/vsphere-cloud-controller-manager:cloud-controller-manager",
},
}, {
name: "VSphere resources returned as expected with single node",
testPlatform: platformsMap[string(configv1.VSpherePlatformType)],
expectedResourceCount: 7,
singleReplica: true,
expectedResourcesKindName: []string{
"Deployment/vsphere-cloud-controller-manager",
"Role/vsphere-cloud-controller-manager",
"RoleBinding/vsphere-cloud-controller-manager:vsphere-cloud-controller-manager",
"RoleBinding/vsphere-cloud-controller-manager:cloud-controller-manager",
"ClusterRole/vsphere-cloud-controller-manager",
"ClusterRoleBinding/vsphere-cloud-controller-manager:vsphere-cloud-controller-manager",
"ClusterRoleBinding/vsphere-cloud-controller-manager:cloud-controller-manager",
},
}, {
name: "OVirt resources are empty, as the platform is not yet supported",
testPlatform: platformsMap[string(configv1.OvirtPlatformType)],
}, {
name: "IBMCloud resources",
testPlatform: platformsMap[string(configv1.IBMCloudPlatformType)],
expectedResourceCount: 2,
expectedResourcesKindName: []string{
"Deployment/ibm-cloud-controller-manager",
"PodDisruptionBudget/ibmcloud-cloud-controller-manager",
},
}, {
name: "IBMCloud resources with single node cluster",
testPlatform: platformsMap[string(configv1.IBMCloudPlatformType)],
expectedResourceCount: 1,
singleReplica: true,
expectedResourcesKindName: []string{"Deployment/ibm-cloud-controller-manager"},
}, {
name: "PowerVS resources",
testPlatform: platformsMap[string(configv1.PowerVSPlatformType)],
expectedResourceCount: 2,
singleReplica: false,
expectedResourcesKindName: []string{
"Deployment/powervs-cloud-controller-manager",
"PodDisruptionBudget/powervs-cloud-controller-manager",
},
}, {
name: "PowerVS resources with single node cluster",
testPlatform: platformsMap[string(configv1.PowerVSPlatformType)],
expectedResourceCount: 1,
singleReplica: true,
expectedResourcesKindName: []string{"Deployment/powervs-cloud-controller-manager"},
}, {
name: "Libvirt resources are empty",
testPlatform: platformsMap[string(configv1.LibvirtPlatformType)],
}, {
name: "Kubevirt resources are empty",
testPlatform: platformsMap[string(configv1.KubevirtPlatformType)],
}, {
name: "BareMetal resources are empty",
testPlatform: platformsMap[string(configv1.BareMetalPlatformType)],
}, {
name: "None platform resources are empty",
testPlatform: platformsMap[string(configv1.NonePlatformType)],
}}
for _, tc := range tc {
t.Run(tc.name, func(t *testing.T) {
operatorConfig := tc.testPlatform.getOperatorConfig()
operatorConfig.IsSingleReplica = tc.singleReplica
resources, err := GetResources(operatorConfig)
assert.NoError(t, err)
assert.Equal(t, tc.expectedResourceCount, len(resources))
otherResourcesArray, err := GetResources(operatorConfig)
assert.NoError(t, err)
assert.EqualValues(t, otherResourcesArray, resources)
if tc.expectedResourceCount > 0 {
assert.NotZero(t, tc.expectedResourcesKindName, "expectedResourcesKindName for this testcase should be specified")
for _, resource := range resources {
resourceKind := resource.GetObjectKind().GroupVersionKind().Kind
resourceKindName := fmt.Sprintf("%s/%s", resourceKind, resource.GetName())
assert.Contains(t, tc.expectedResourcesKindName, resourceKindName)
}
}
// Edit and repeat procedure to ensure modification in place is not present
if len(resources) > 0 {
resources[0].SetName("different")
newResources, err := GetResources(operatorConfig)
assert.NoError(t, err)
assert.Equal(t, len(otherResourcesArray), len(newResources))
assert.EqualValues(t, otherResourcesArray, newResources)
assert.NotEqualValues(t, resources, newResources)
}
})
}
}
func TestRenderedResources(t *testing.T) {
/*
This test runs a number of different checks against the podSpecs produced by
the different platform resources.
*/
platforms := getPlatforms()
for platformName, platform := range platforms {
t.Run(platformName, func(t *testing.T) {
resources, err := GetResources(platform.getOperatorConfig())
assert.NoError(t, err)
for _, resource := range resources {
var podSpec corev1.PodSpec
switch obj := resource.(type) {
case *corev1.Pod:
podSpec = obj.Spec
case *appsv1.Deployment:
podSpec = obj.Spec.Template.Spec
case *appsv1.DaemonSet:
podSpec = obj.Spec.Template.Spec
default:
// Nothing to check for non pod producing types
continue
}
checkResourceRunsBeforeCNI(t, platformName, podSpec)
checkLeaderElection(t, podSpec)
checkCloudControllerManagerFlags(t, podSpec)
checkTrustedCAMounted(t, podSpec)
checkUseServiceAccountCredentials(t, podSpec)
}
})
}
}
func checkResourceRunsBeforeCNI(t *testing.T, platformName string, podSpec corev1.PodSpec) {
/*
As CNI relies on CMM to initialist the Node IP addresses. We must ensure
that CCM pods can run before the CNO has been deployed and before the CNI
initialises the Node.
To achieve this, we must tolerate the not-ready taint, use host
networking and use the internal API Load Balancer instead of the API Service.
*/
checkResourceTolerations(t, podSpec)
checkHostNetwork(t, podSpec)
checkPorts(t, podSpec)
checkVolumes(t, platformName, podSpec)
checkContainerCommand(t, podSpec)
checkContainerTerminationMessagePolicy(t, podSpec)
}
func checkResourceTolerations(t *testing.T, podSpec corev1.PodSpec) {
uninitializedTaint := corev1.Toleration{
Key: "node.cloudprovider.kubernetes.io/uninitialized",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoSchedule,
}
notReadyTaint := corev1.Toleration{
Key: "node.kubernetes.io/not-ready",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoSchedule,
}
noScheduleTaint := corev1.Toleration{
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoSchedule,
}
tolerations := podSpec.Tolerations
g := NewWithT(t)
g.Expect(tolerations).To(SatisfyAny(
SatisfyAll(
ContainElement(uninitializedTaint),
ContainElement(notReadyTaint),
),
ContainElement(noScheduleTaint),
), "PodSpec must either contain the uninitialized and not-ready tolerations, or tolerate any NoSchedule taint")
}
func checkHostNetwork(t *testing.T, podSpec corev1.PodSpec) {
assert.Equal(t, podSpec.HostNetwork, true, "PodSpec should set HostNetwork true")
}
// This test is to ensure that the guidelines set out in https://github.com/openshift/enhancements/blob/master/dev-guide/host-port-registry.md
// are correctly adhered to.
func checkPorts(t *testing.T, podSpec corev1.PodSpec) {
var foundValidPort bool
for _, container := range podSpec.Containers {
for _, port := range container.Ports {
switch port.ContainerPort {
case cloudControllerManagerDefaultPort, cloudNodeManagerDefaultPort:
foundValidPort = true
default:
t.Errorf("Unknown Container Port %d: All ports on Host Network processes must be registered before use", port.ContainerPort)
}
}
}
if !foundValidPort {
t.Errorf("Container Ports must specify any used ports. CloudControllerManager should use port %d, CloudNodeManager should use port %d.", cloudControllerManagerDefaultPort, cloudNodeManagerDefaultPort)
}
}
func checkVolumes(t *testing.T, platformName string, podSpec corev1.PodSpec) {
directory := corev1.HostPathDirectory
var (
hostVolume corev1.Volume
hostVolumeMount corev1.VolumeMount
)
switch platformName {
case "Azure":
// Azure CCM and node manager use an init-container to merge provided credentials
// with the cloud conf either from host /etc/kubernetes (node-manager) or from
// the accm configmap (cloud-controller-manager).
// For this reason, Azure mounts the merged-cloud-config volume where the generated
// cloud conf has been created.
hostVolume = corev1.Volume{
Name: "merged-cloud-config",
VolumeSource: corev1.VolumeSource{
EmptyDir: nil,
},
}
assert.Contains(t, podSpec.Volumes, hostVolume, "PodSpec Volumes should contain merged-cloud-config empty dir volume")
for _, container := range podSpec.Containers {
hostVolumeMount = corev1.VolumeMount{
Name: "merged-cloud-config",
ReadOnly: true,
}
switch container.Name {
case "cloud-controller-manager":
hostVolumeMount.MountPath = "/etc/kubernetes-cloud-config"
case "cloud-node-manager":
hostVolumeMount.MountPath = "/etc/kubernetes"
}
assert.Contains(t, container.VolumeMounts, hostVolumeMount, "Container VolumeMounts should contain merged-cloud-config volume mount")
}
default:
hostVolume = corev1.Volume{
Name: "host-etc-kube",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/kubernetes",
Type: &directory,
},
},
}
hostVolumeMount = corev1.VolumeMount{
MountPath: "/etc/kubernetes",
Name: "host-etc-kube",
ReadOnly: true,
}
assert.Contains(t, podSpec.Volumes, hostVolume, "PodSpec Volumes should contain host-etc-kube host path volume")
for _, container := range podSpec.Containers {
assert.Contains(t, container.VolumeMounts, hostVolumeMount, "Container VolumeMounts should contain host-etc-kube volume mount")
}
}
}
func checkContainerCommand(t *testing.T, podSpec corev1.PodSpec) {
binBash := "/bin/bash"
dashC := "-c"
// This script should be present on every node.
// https://github.com/openshift/machine-config-operator/pull/2232
// The script sets the API server URL environment variables that
// the client SDK detects automatically.
setAPIEnv := `#!/bin/bash
set -o allexport
if [[ -f /etc/kubernetes/apiserver-url.env ]]; then
source /etc/kubernetes/apiserver-url.env
fi
exec `
for _, container := range podSpec.Containers {
command := container.Command
assert.Len(t, command, 3, "Container Command should have 3 elements")
assert.Len(t, container.Args, 0, "Container Args should have no elements, inline the args into the Container Command")
assert.Equal(t, command[0], binBash, "Container Command first element should equal %q", binBash)
assert.Equal(t, command[1], dashC, "Container Command second element should equal %q", dashC)
assert.True(t, strings.HasPrefix(command[2], setAPIEnv), "Container Command third (%q) element should start with %q", command[2], setAPIEnv)
}
}
func checkContainerTerminationMessagePolicy(t *testing.T, podSpec corev1.PodSpec) {
for _, container := range append(podSpec.Containers, podSpec.InitContainers...) {
assert.Equal(t, container.TerminationMessagePolicy, corev1.TerminationMessageFallbackToLogsOnError, "Container TerminationMessagePolicy should be set to %q", corev1.TerminationMessageFallbackToLogsOnError)
}
}
func checkLeaderElection(t *testing.T, podSpec corev1.PodSpec) {
const (
leaderElect = "--leader-elect=true"
leaderElectLeaseDuration = "--leader-elect-lease-duration=137s"
leaderElectRenewDeadline = "--leader-elect-renew-deadline=107s"
leaderElectRetryPeriod = "--leader-elect-retry-period=26s"
leaderElectResourceNamesapce = "--leader-elect-resource-namespace=openshift-cloud-controller-manager"
)
for _, container := range podSpec.Containers {
if container.Name != "cloud-controller-manager" {
// Only the cloud-controller-manager container needs leader election
continue
}
command := container.Command
assert.Len(t, command, 3, "Container Command should have 3 elements")
for _, flag := range []string{leaderElect, leaderElectLeaseDuration, leaderElectRenewDeadline, leaderElectRetryPeriod, leaderElectResourceNamesapce} {
assert.Contains(t, command[2], flag, "Container Command third (%q) element should contain flag %q", command[2], flag)
}
}
}
func checkCloudControllerManagerFlags(t *testing.T, podSpec corev1.PodSpec) {
const (
// This flag will disable the cloud route controller.
// The route controller is responsible for setting up inter pod networking
// using cloud networks, but this isn't required when you have an overlay
// network as is used within OpenShift.
configureCloudRoutes = "--configure-cloud-routes=false"
)
for _, container := range podSpec.Containers {
if container.Name != "cloud-controller-manager" {
// Only the cloud-controller-manager container needs these flags checking
continue
}
command := container.Command
assert.Len(t, command, 3, "Container Command should have 3 elements")
for _, flag := range []string{configureCloudRoutes} {
assert.Contains(t, command[2], flag, "Container Command third (%q) element should contain flag %q", command[2], flag)
}
}
}
func TestDeploymentPodAntiAffinity(t *testing.T) {
platforms := getPlatforms()
for platformName, platform := range platforms {
t.Run(platformName, func(t *testing.T) {
resources, err := GetResources(platform.getOperatorConfig())
assert.NoError(t, err)
for _, resource := range resources {
switch obj := resource.(type) {
case *appsv1.Deployment:
checkPodAntiAffinity(t, obj.Spec.Template.Spec, obj.ObjectMeta)
default:
// Nothing to check for non
}
}
})
}
}
func checkPodAntiAffinity(t *testing.T, podSpec corev1.PodSpec, podMeta metav1.ObjectMeta) {
assert.NotNil(t, podSpec.Affinity)
podAntiAffinity := &corev1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
{
TopologyKey: "kubernetes.io/hostname",
LabelSelector: &metav1.LabelSelector{
MatchLabels: podMeta.Labels,
},
},
},
}
assert.EqualValues(t, podAntiAffinity, podSpec.Affinity.PodAntiAffinity)
}
func TestDeploymentStrategy(t *testing.T) {
/*
This test is designed to check that when a Pod is created by the CCCMO,
we can update the pod when running on an SNO cluster.
Because host ports are used by the pods we create, we must release the
port before creating the new pod
*/
platforms := getPlatforms()
for platformName, platform := range platforms {
t.Run(platformName, func(t *testing.T) {
resources, err := GetResources(platform.getOperatorConfig())
assert.NoError(t, err)
for _, resource := range resources {
switch obj := resource.(type) {
case *appsv1.Deployment:
checkDeploymentStrategy(t, obj.Spec.Strategy)
default:
// Nothing to check for non
}
}
})
}
}
func checkDeploymentStrategy(t *testing.T, strategy appsv1.DeploymentStrategy) {
if strategy.Type != appsv1.RecreateDeploymentStrategyType {
t.Errorf("Deployment should set strategy type to \"Recreate\"")
}
}
func checkTrustedCAMounted(t *testing.T, podSpec corev1.PodSpec) {
trustedCAVolume := corev1.Volume{
Name: "trusted-ca",
VolumeSource: corev1.VolumeSource{ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: "ccm-trusted-ca"},
Items: []corev1.KeyToPath{{Key: "ca-bundle.crt", Path: "tls-ca-bundle.pem"}},
}},
}
trustedCAVolumeMount := corev1.VolumeMount{
MountPath: "/etc/pki/ca-trust/extracted/pem",
Name: "trusted-ca",
ReadOnly: true,
}
assert.Contains(t, podSpec.Volumes, trustedCAVolume, "PodSpec %s volumes should contain trusted-ca volume")
for _, c := range podSpec.Containers {
assert.Contains(t, c.VolumeMounts, trustedCAVolumeMount, "Container VolumeMounts should contain trusted ca volume mount")
}
}
func checkUseServiceAccountCredentials(t *testing.T, podSpec corev1.PodSpec) {
const (
useServiceAccountCredentials = "--use-service-account-credentials=true"
)
for _, container := range podSpec.Containers {
if container.Name != "cloud-controller-manager" {
// Only the cloud-controller-manager container needs leader election
continue
}
command := container.Command
assert.Len(t, command, 3, "Container Command should have 3 elements")
assert.Contains(t, command[2], useServiceAccountCredentials, "Container Command third (%q) element should contain flag %q", command[2], useServiceAccountCredentials)
}
}
func TestSelectorLabels(t *testing.T) {
/*
This test checks consistency between all Deployment pod template labels and selector labels
*/
platforms := getPlatforms()
for platformName, platform := range platforms {
t.Run(platformName, func(t *testing.T) {
resources, err := GetResources(platform.getOperatorConfig())
assert.NoError(t, err)
for _, resource := range resources {
switch obj := resource.(type) {
case *appsv1.Deployment:
checkDeployementSelectorLabels(t, obj, platform.platformStatus.Type)
case *appsv1.DaemonSet:
checkDaemonSetSelectorLabels(t, obj, platform.platformStatus.Type)
default:
// Nothing to check for
}
}
})
}
}
func checkDeployementSelectorLabels(t *testing.T, deployment *appsv1.Deployment, platformType configv1.PlatformType) {
assert.Contains(t, deployment.Labels, "k8s-app")
assert.Contains(t, deployment.Spec.Template.Labels, "k8s-app")
assert.Contains(t, deployment.Spec.Selector.MatchLabels, "k8s-app")
assert.Contains(t, deployment.Labels, common.CloudControllerManagerProviderLabel)
assert.Contains(t, deployment.Spec.Template.Labels, common.CloudControllerManagerProviderLabel)
assert.Contains(t, deployment.Spec.Selector.MatchLabels, common.CloudControllerManagerProviderLabel)
assert.Equal(t, string(platformType), deployment.Labels[common.CloudControllerManagerProviderLabel])
assert.Equal(t, string(platformType), deployment.Spec.Template.Labels[common.CloudControllerManagerProviderLabel])
assert.Equal(t, string(platformType), deployment.Spec.Selector.MatchLabels[common.CloudControllerManagerProviderLabel])
}
func checkDaemonSetSelectorLabels(t *testing.T, ds *appsv1.DaemonSet, platformType configv1.PlatformType) {
assert.Contains(t, ds.Labels, "k8s-app")
assert.Contains(t, ds.Spec.Template.Labels, "k8s-app")
assert.Contains(t, ds.Spec.Selector.MatchLabels, "k8s-app")
assert.Contains(t, ds.Labels, common.CloudNodeManagerCloudProviderLabel)
assert.Contains(t, ds.Spec.Template.Labels, common.CloudNodeManagerCloudProviderLabel)
assert.Contains(t, ds.Spec.Selector.MatchLabels, common.CloudNodeManagerCloudProviderLabel)
assert.Equal(t, string(platformType), ds.Labels[common.CloudNodeManagerCloudProviderLabel])
assert.Equal(t, string(platformType), ds.Spec.Template.Labels[common.CloudNodeManagerCloudProviderLabel])
assert.Equal(t, string(platformType), ds.Spec.Selector.MatchLabels[common.CloudNodeManagerCloudProviderLabel])
}
func TestReplicas(t *testing.T) {
platforms := getPlatforms()
for platformName, platform := range platforms {
t.Run(platformName, func(t *testing.T) {
resources, err := GetResources(platform.getOperatorConfig())
assert.NoError(t, err)
for _, resource := range resources {
switch obj := resource.(type) {
case *appsv1.Deployment:
assert.Equal(t, derefReplicas(obj.Spec.Replicas), 2)
default:
// Nothing to check for
}
}
})
t.Run(fmt.Sprintf("%s single node", platformName), func(t *testing.T) {
cfg := platform.getOperatorConfig()
cfg.IsSingleReplica = true
resources, err := GetResources(cfg)
assert.NoError(t, err)
for _, resource := range resources {
switch obj := resource.(type) {
case *appsv1.Deployment:
assert.Equal(t, derefReplicas(obj.Spec.Replicas), 1)
default:
// Nothing to check for
}
}
})
}
}
func derefReplicas(num *int32) int {
if num != nil {
return int(*num)
}
return 1
}