|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + . "github.com/onsi/ginkgo" |
| 21 | + . "github.com/onsi/gomega" |
| 22 | + |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/utils/pointer" |
| 26 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3" |
| 27 | + "sigs.k8s.io/cluster-api/util/conditions" |
| 28 | + "sigs.k8s.io/cluster-api/util/patch" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + |
| 31 | + infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/api/v1alpha3" |
| 32 | +) |
| 33 | + |
| 34 | +var _ = Describe("VsphereMachineReconciler", func() { |
| 35 | + |
| 36 | + var ( |
| 37 | + capiCluster *clusterv1.Cluster |
| 38 | + capiMachine *clusterv1.Machine |
| 39 | + |
| 40 | + infraCluster *infrav1.VSphereCluster |
| 41 | + infraMachine *infrav1.VSphereMachine |
| 42 | + |
| 43 | + testNs *corev1.Namespace |
| 44 | + key client.ObjectKey |
| 45 | + ) |
| 46 | + |
| 47 | + isPresentAndFalseWithReason := func(getter conditions.Getter, condition clusterv1.ConditionType, reason string) bool { |
| 48 | + ExpectWithOffset(1, testEnv.Get(ctx, key, getter)).To(Succeed()) |
| 49 | + if !conditions.Has(getter, condition) { |
| 50 | + return false |
| 51 | + } |
| 52 | + objectCondition := conditions.Get(getter, condition) |
| 53 | + return objectCondition.Status == corev1.ConditionFalse && |
| 54 | + objectCondition.Reason == reason |
| 55 | + } |
| 56 | + |
| 57 | + BeforeEach(func() { |
| 58 | + var err error |
| 59 | + testNs, err = testEnv.CreateNamespace(ctx, "vsphere-machine-reconciler") |
| 60 | + Expect(err).NotTo(HaveOccurred()) |
| 61 | + |
| 62 | + capiCluster = &clusterv1.Cluster{ |
| 63 | + ObjectMeta: metav1.ObjectMeta{ |
| 64 | + GenerateName: "test1-", |
| 65 | + Namespace: testNs.Name, |
| 66 | + }, |
| 67 | + Spec: clusterv1.ClusterSpec{ |
| 68 | + InfrastructureRef: &corev1.ObjectReference{ |
| 69 | + APIVersion: "infrastructure.cluster.x-k8s.io/v1alpha3", |
| 70 | + Kind: "VSphereCluster", |
| 71 | + Name: "vsphere-test1", |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + Expect(testEnv.Create(ctx, capiCluster)).To(Succeed()) |
| 76 | + |
| 77 | + infraCluster = &infrav1.VSphereCluster{ |
| 78 | + ObjectMeta: metav1.ObjectMeta{ |
| 79 | + Name: "vsphere-test1", |
| 80 | + Namespace: testNs.Name, |
| 81 | + OwnerReferences: []metav1.OwnerReference{ |
| 82 | + { |
| 83 | + APIVersion: "cluster.x-k8s.io/v1alpha3", |
| 84 | + Kind: "Cluster", |
| 85 | + Name: capiCluster.Name, |
| 86 | + UID: "blah", |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + Spec: infrav1.VSphereClusterSpec{}, |
| 91 | + } |
| 92 | + Expect(testEnv.Create(ctx, infraCluster)).To(Succeed()) |
| 93 | + |
| 94 | + capiMachine = &clusterv1.Machine{ |
| 95 | + ObjectMeta: metav1.ObjectMeta{ |
| 96 | + GenerateName: "machine-created-", |
| 97 | + Namespace: testNs.Name, |
| 98 | + Finalizers: []string{clusterv1.MachineFinalizer}, |
| 99 | + Labels: map[string]string{ |
| 100 | + clusterv1.ClusterLabelName: capiCluster.Name, |
| 101 | + }, |
| 102 | + }, |
| 103 | + Spec: clusterv1.MachineSpec{ |
| 104 | + ClusterName: capiCluster.Name, |
| 105 | + InfrastructureRef: corev1.ObjectReference{ |
| 106 | + APIVersion: "infrastructure.cluster.x-k8s.io/v1alpha3", |
| 107 | + Kind: "VSphereMachine", |
| 108 | + Name: "vsphere-machine-1", |
| 109 | + }, |
| 110 | + }, |
| 111 | + } |
| 112 | + Expect(testEnv.Create(ctx, capiMachine)).To(Succeed()) |
| 113 | + |
| 114 | + infraMachine = &infrav1.VSphereMachine{ |
| 115 | + ObjectMeta: metav1.ObjectMeta{ |
| 116 | + Name: "vsphere-machine-1", |
| 117 | + Namespace: testNs.Name, |
| 118 | + Labels: map[string]string{ |
| 119 | + clusterv1.ClusterLabelName: capiCluster.Name, |
| 120 | + clusterv1.MachineControlPlaneLabelName: "", |
| 121 | + }, |
| 122 | + OwnerReferences: []metav1.OwnerReference{ |
| 123 | + { |
| 124 | + APIVersion: clusterv1.GroupVersion.String(), |
| 125 | + Kind: "Machine", |
| 126 | + Name: capiMachine.Name, |
| 127 | + UID: "blah", |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + Spec: infrav1.VSphereMachineSpec{ |
| 132 | + VirtualMachineCloneSpec: infrav1.VirtualMachineCloneSpec{ |
| 133 | + Template: "ubuntu-k9s-1.19", |
| 134 | + Network: infrav1.NetworkSpec{ |
| 135 | + Devices: []infrav1.NetworkDeviceSpec{ |
| 136 | + {NetworkName: "network-1", DHCP4: true}, |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + } |
| 142 | + Expect(testEnv.Create(ctx, infraMachine)).To(Succeed()) |
| 143 | + |
| 144 | + key = client.ObjectKey{Namespace: testNs.Name, Name: infraMachine.Name} |
| 145 | + }) |
| 146 | + |
| 147 | + AfterEach(func() { |
| 148 | + Expect(testEnv.Cleanup(ctx, testNs, capiCluster, infraCluster, capiMachine, infraMachine)).To(Succeed()) |
| 149 | + }) |
| 150 | + |
| 151 | + It("waits for cluster status to be ready", func() { |
| 152 | + Eventually(func() bool { |
| 153 | + // this is to make sure that the VSphereMachine is created before the next check for the |
| 154 | + // presence of conditions on the VSphereMachine proceeds. |
| 155 | + if err := testEnv.Get(ctx, key, infraMachine); err != nil { |
| 156 | + return false |
| 157 | + } |
| 158 | + return isPresentAndFalseWithReason(infraMachine, infrav1.VMProvisionedCondition, infrav1.WaitingForClusterInfrastructureReason) |
| 159 | + }, timeout).Should(BeTrue()) |
| 160 | + |
| 161 | + By("setting the cluster infrastructure to be ready") |
| 162 | + Eventually(func() error { |
| 163 | + ph, err := patch.NewHelper(capiCluster, testEnv) |
| 164 | + Expect(err).ShouldNot(HaveOccurred()) |
| 165 | + capiCluster.Status.InfrastructureReady = true |
| 166 | + return ph.Patch(ctx, capiCluster, patch.WithStatusObservedGeneration{}) |
| 167 | + }, timeout).Should(BeNil()) |
| 168 | + |
| 169 | + Eventually(func() bool { |
| 170 | + return isPresentAndFalseWithReason(infraMachine, infrav1.VMProvisionedCondition, infrav1.WaitingForClusterInfrastructureReason) |
| 171 | + }, timeout).Should(BeFalse()) |
| 172 | + }) |
| 173 | + |
| 174 | + Context("With Cluster Infrastructure status ready", func() { |
| 175 | + BeforeEach(func() { |
| 176 | + ph, err := patch.NewHelper(capiCluster, testEnv) |
| 177 | + Expect(err).ShouldNot(HaveOccurred()) |
| 178 | + capiCluster.Status.InfrastructureReady = true |
| 179 | + Expect(ph.Patch(ctx, capiCluster, patch.WithStatusObservedGeneration{})).To(Succeed()) |
| 180 | + }) |
| 181 | + |
| 182 | + It("moves to VSphere VM creation", func() { |
| 183 | + Eventually(func() bool { |
| 184 | + vms := infrav1.VSphereVMList{} |
| 185 | + Expect(testEnv.List(ctx, &vms, client.InNamespace(testNs.Name), client.MatchingLabels{ |
| 186 | + clusterv1.ClusterLabelName: capiCluster.Name, |
| 187 | + })).To(Succeed()) |
| 188 | + return isPresentAndFalseWithReason(infraMachine, infrav1.VMProvisionedCondition, infrav1.WaitingForBootstrapDataReason) && |
| 189 | + len(vms.Items) == 0 |
| 190 | + }, timeout).Should(BeTrue()) |
| 191 | + |
| 192 | + By("setting the bootstrap data") |
| 193 | + Eventually(func() error { |
| 194 | + ph, err := patch.NewHelper(capiMachine, testEnv) |
| 195 | + Expect(err).ShouldNot(HaveOccurred()) |
| 196 | + capiMachine.Spec.Bootstrap = clusterv1.Bootstrap{ |
| 197 | + DataSecretName: pointer.StringPtr("some-secret"), |
| 198 | + } |
| 199 | + return ph.Patch(ctx, capiMachine, patch.WithStatusObservedGeneration{}) |
| 200 | + }, timeout).Should(BeNil()) |
| 201 | + |
| 202 | + Eventually(func() int { |
| 203 | + vms := infrav1.VSphereVMList{} |
| 204 | + Expect(testEnv.List(ctx, &vms)).To(Succeed()) |
| 205 | + return len(vms.Items) |
| 206 | + }, timeout).Should(BeNumerically(">", 0)) |
| 207 | + }) |
| 208 | + }) |
| 209 | +}) |
0 commit comments