|
| 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 e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "strings" |
| 25 | + "time" |
| 26 | + |
| 27 | + . "github.com/onsi/ginkgo" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | + corev1 "k8s.io/api/core/v1" |
| 30 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 31 | + "k8s.io/utils/pointer" |
| 32 | + |
| 33 | + runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1" |
| 34 | + "sigs.k8s.io/cluster-api/test/framework" |
| 35 | + "sigs.k8s.io/cluster-api/test/framework/clusterctl" |
| 36 | + "sigs.k8s.io/cluster-api/util" |
| 37 | +) |
| 38 | + |
| 39 | +// clusterUpgradeWithRuntimeSDKSpecInput is the input for clusterUpgradeWithRuntimeSDKSpec. |
| 40 | +type clusterUpgradeWithRuntimeSDKSpecInput struct { |
| 41 | + E2EConfig *clusterctl.E2EConfig |
| 42 | + ClusterctlConfigPath string |
| 43 | + BootstrapClusterProxy framework.ClusterProxy |
| 44 | + ArtifactFolder string |
| 45 | + SkipCleanup bool |
| 46 | + |
| 47 | + // ControlPlaneMachineCount is used in `config cluster` to configure the count of the control plane machines used in the test. |
| 48 | + // Default is 1. |
| 49 | + ControlPlaneMachineCount *int64 |
| 50 | + |
| 51 | + // WorkerMachineCount is used in `config cluster` to configure the count of the worker machines used in the test. |
| 52 | + // NOTE: If the WORKER_MACHINE_COUNT var is used multiple times in the cluster template, the absolute count of |
| 53 | + // worker machines is a multiple of WorkerMachineCount. |
| 54 | + // Default is 2. |
| 55 | + WorkerMachineCount *int64 |
| 56 | + |
| 57 | + // Flavor to use when creating the cluster for testing, "upgrades" is used if not specified. |
| 58 | + Flavor *string |
| 59 | +} |
| 60 | + |
| 61 | +// clusterUpgradeWithRuntimeSDKSpec implements a spec that upgrades a cluster and runs the Kubernetes conformance suite. |
| 62 | +// Upgrading a cluster refers to upgrading the control-plane and worker nodes (managed by MD and machine pools). |
| 63 | +// NOTE: This test only works with a KubeadmControlPlane. |
| 64 | +// NOTE: This test works with Clusters with and without ClusterClass. |
| 65 | +// When using ClusterClass the ClusterClass must have the variables "etcdImageTag" and "coreDNSImageTag" of type string. |
| 66 | +// Those variables should have corresponding patches which set the etcd and CoreDNS tags in KCP. |
| 67 | +func clusterUpgradeWithRuntimeSDKSpec(ctx context.Context, inputGetter func() clusterUpgradeWithRuntimeSDKSpecInput) { |
| 68 | + const ( |
| 69 | + textExtensionPathVariable = "TEST_EXTENSION" |
| 70 | + specName = "k8s-upgrade-with-runtimesdk" |
| 71 | + ) |
| 72 | + |
| 73 | + var ( |
| 74 | + input clusterUpgradeWithRuntimeSDKSpecInput |
| 75 | + namespace *corev1.Namespace |
| 76 | + ext *runtimev1.ExtensionConfig |
| 77 | + cancelWatches context.CancelFunc |
| 78 | + |
| 79 | + controlPlaneMachineCount int64 |
| 80 | + workerMachineCount int64 |
| 81 | + |
| 82 | + clusterResources *clusterctl.ApplyClusterTemplateAndWaitResult |
| 83 | + testExtensionPath string |
| 84 | + ) |
| 85 | + |
| 86 | + BeforeEach(func() { |
| 87 | + Expect(ctx).NotTo(BeNil(), "ctx is required for %s spec", specName) |
| 88 | + input = inputGetter() |
| 89 | + Expect(input.E2EConfig).ToNot(BeNil(), "Invalid argument. input.E2EConfig can't be nil when calling %s spec", specName) |
| 90 | + Expect(input.ClusterctlConfigPath).To(BeAnExistingFile(), "Invalid argument. input.ClusterctlConfigPath must be an existing file when calling %s spec", specName) |
| 91 | + Expect(input.BootstrapClusterProxy).ToNot(BeNil(), "Invalid argument. input.BootstrapClusterProxy can't be nil when calling %s spec", specName) |
| 92 | + Expect(os.MkdirAll(input.ArtifactFolder, 0750)).To(Succeed(), "Invalid argument. input.ArtifactFolder can't be created for %s spec", specName) |
| 93 | + |
| 94 | + Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersionUpgradeFrom)) |
| 95 | + Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersionUpgradeTo)) |
| 96 | + Expect(input.E2EConfig.Variables).To(HaveKey(EtcdVersionUpgradeTo)) |
| 97 | + Expect(input.E2EConfig.Variables).To(HaveKey(CoreDNSVersionUpgradeTo)) |
| 98 | + |
| 99 | + testExtensionPath = input.E2EConfig.GetVariable(textExtensionPathVariable) |
| 100 | + Expect(testExtensionPath).To(BeAnExistingFile(), "The %s variable should resolve to an existing file", textExtensionPathVariable) |
| 101 | + |
| 102 | + if input.ControlPlaneMachineCount == nil { |
| 103 | + controlPlaneMachineCount = 1 |
| 104 | + } else { |
| 105 | + controlPlaneMachineCount = *input.ControlPlaneMachineCount |
| 106 | + } |
| 107 | + |
| 108 | + if input.WorkerMachineCount == nil { |
| 109 | + workerMachineCount = 2 |
| 110 | + } else { |
| 111 | + workerMachineCount = *input.WorkerMachineCount |
| 112 | + } |
| 113 | + |
| 114 | + // Setup a Namespace where to host objects for this spec and create a watcher for the Namespace events. |
| 115 | + namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder) |
| 116 | + clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult) |
| 117 | + }) |
| 118 | + |
| 119 | + It("Should create and upgrade a workload cluster", func() { |
| 120 | + By("Deploy Test Extension") |
| 121 | + testExtensionDeploymentTemplate, err := os.ReadFile(testExtensionPath) //nolint:gosec |
| 122 | + Expect(err).ToNot(HaveOccurred(), "Failed to read the extension config deployment manifest file") |
| 123 | + |
| 124 | + // Set the SERVICE_NAMESPACE, which is used in the cert-manager Certificate CR. |
| 125 | + // We have to dynamically set the namespace here, because it depends on the test run and thus |
| 126 | + // cannot be set when rendering the test extension YAML with kustomize. |
| 127 | + testExtensionDeployment := strings.ReplaceAll(string(testExtensionDeploymentTemplate), "${SERVICE_NAMESPACE}", namespace.Name) |
| 128 | + Expect(testExtensionDeployment).ToNot(BeEmpty(), "Test Extension deployment manifest file should not be empty") |
| 129 | + |
| 130 | + Expect(input.BootstrapClusterProxy.Apply(ctx, []byte(testExtensionDeployment), "--namespace", namespace.Name)).To(Succeed()) |
| 131 | + |
| 132 | + By("Deploy Test Extension ExtensionConfig") |
| 133 | + ext = extensionConfig(specName, namespace) |
| 134 | + err = input.BootstrapClusterProxy.GetClient().Create(ctx, ext) |
| 135 | + Expect(err).ToNot(HaveOccurred(), "Failed to create the extension config") |
| 136 | + |
| 137 | + By("Creating a workload cluster") |
| 138 | + |
| 139 | + clusterctl.ApplyClusterTemplateAndWait(ctx, clusterctl.ApplyClusterTemplateAndWaitInput{ |
| 140 | + ClusterProxy: input.BootstrapClusterProxy, |
| 141 | + ConfigCluster: clusterctl.ConfigClusterInput{ |
| 142 | + LogFolder: filepath.Join(input.ArtifactFolder, "clusters", input.BootstrapClusterProxy.GetName()), |
| 143 | + ClusterctlConfigPath: input.ClusterctlConfigPath, |
| 144 | + KubeconfigPath: input.BootstrapClusterProxy.GetKubeconfigPath(), |
| 145 | + InfrastructureProvider: clusterctl.DefaultInfrastructureProvider, |
| 146 | + Flavor: pointer.StringDeref(input.Flavor, "upgrades"), |
| 147 | + Namespace: namespace.Name, |
| 148 | + ClusterName: fmt.Sprintf("%s-%s", specName, util.RandomString(6)), |
| 149 | + KubernetesVersion: input.E2EConfig.GetVariable(KubernetesVersionUpgradeFrom), |
| 150 | + ControlPlaneMachineCount: pointer.Int64Ptr(controlPlaneMachineCount), |
| 151 | + WorkerMachineCount: pointer.Int64Ptr(workerMachineCount), |
| 152 | + }, |
| 153 | + WaitForClusterIntervals: input.E2EConfig.GetIntervals(specName, "wait-cluster"), |
| 154 | + WaitForControlPlaneIntervals: input.E2EConfig.GetIntervals(specName, "wait-control-plane"), |
| 155 | + WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"), |
| 156 | + WaitForMachinePools: input.E2EConfig.GetIntervals(specName, "wait-machine-pool-nodes"), |
| 157 | + }, clusterResources) |
| 158 | + |
| 159 | + // Upgrade the Cluster topology to run through an entire cluster lifecycle to test the lifecycle hooks. |
| 160 | + By("Upgrading the Cluster topology") |
| 161 | + framework.UpgradeClusterTopologyAndWaitForUpgrade(ctx, framework.UpgradeClusterTopologyAndWaitForUpgradeInput{ |
| 162 | + ClusterProxy: input.BootstrapClusterProxy, |
| 163 | + Cluster: clusterResources.Cluster, |
| 164 | + ControlPlane: clusterResources.ControlPlane, |
| 165 | + EtcdImageTag: input.E2EConfig.GetVariable(EtcdVersionUpgradeTo), |
| 166 | + DNSImageTag: input.E2EConfig.GetVariable(CoreDNSVersionUpgradeTo), |
| 167 | + MachineDeployments: clusterResources.MachineDeployments, |
| 168 | + KubernetesUpgradeVersion: input.E2EConfig.GetVariable(KubernetesVersionUpgradeTo), |
| 169 | + WaitForMachinesToBeUpgraded: input.E2EConfig.GetIntervals(specName, "wait-machine-upgrade"), |
| 170 | + WaitForKubeProxyUpgrade: input.E2EConfig.GetIntervals(specName, "wait-machine-upgrade"), |
| 171 | + WaitForDNSUpgrade: input.E2EConfig.GetIntervals(specName, "wait-machine-upgrade"), |
| 172 | + WaitForEtcdUpgrade: input.E2EConfig.GetIntervals(specName, "wait-machine-upgrade"), |
| 173 | + }) |
| 174 | + |
| 175 | + // Only attempt to upgrade MachinePools if they were provided in the template. |
| 176 | + if len(clusterResources.MachinePools) > 0 && workerMachineCount > 0 { |
| 177 | + By("Upgrading the machinepool instances") |
| 178 | + framework.UpgradeMachinePoolAndWait(ctx, framework.UpgradeMachinePoolAndWaitInput{ |
| 179 | + ClusterProxy: input.BootstrapClusterProxy, |
| 180 | + Cluster: clusterResources.Cluster, |
| 181 | + UpgradeVersion: input.E2EConfig.GetVariable(KubernetesVersionUpgradeTo), |
| 182 | + WaitForMachinePoolToBeUpgraded: input.E2EConfig.GetIntervals(specName, "wait-machine-pool-upgrade"), |
| 183 | + MachinePools: clusterResources.MachinePools, |
| 184 | + }) |
| 185 | + } |
| 186 | + |
| 187 | + By("Waiting until nodes are ready") |
| 188 | + workloadProxy := input.BootstrapClusterProxy.GetWorkloadCluster(ctx, namespace.Name, clusterResources.Cluster.Name) |
| 189 | + workloadClient := workloadProxy.GetClient() |
| 190 | + framework.WaitForNodesReady(ctx, framework.WaitForNodesReadyInput{ |
| 191 | + Lister: workloadClient, |
| 192 | + KubernetesVersion: input.E2EConfig.GetVariable(KubernetesVersionUpgradeTo), |
| 193 | + Count: int(clusterResources.ExpectedTotalNodes()), |
| 194 | + WaitForNodesReady: input.E2EConfig.GetIntervals(specName, "wait-nodes-ready"), |
| 195 | + }) |
| 196 | + |
| 197 | + By("PASSED!") |
| 198 | + }) |
| 199 | + |
| 200 | + AfterEach(func() { |
| 201 | + // Dumps all the resources in the spec Namespace, then cleanups the cluster object and the spec Namespace itself. |
| 202 | + dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup) |
| 203 | + |
| 204 | + Eventually(func() error { |
| 205 | + return input.BootstrapClusterProxy.GetClient().Delete(ctx, ext) |
| 206 | + }, 10*time.Second, 1*time.Second).Should(Succeed()) |
| 207 | + }) |
| 208 | +} |
| 209 | + |
| 210 | +// extensionConfig generates an ExtensionConfig. |
| 211 | +// We make sure this cluster-wide object does not conflict with others by using a random generated |
| 212 | +// name and a NamespaceSelector selecting on the namespace of the current test. |
| 213 | +// Thus, this object is "namespaced" to the current test even though it's a cluster-wide object. |
| 214 | +func extensionConfig(specName string, namespace *corev1.Namespace) *runtimev1.ExtensionConfig { |
| 215 | + return &runtimev1.ExtensionConfig{ |
| 216 | + ObjectMeta: metav1.ObjectMeta{ |
| 217 | + Name: fmt.Sprintf("%s-%s", specName, util.RandomString(6)), |
| 218 | + Annotations: map[string]string{ |
| 219 | + "cert-manager.io/inject-ca-from-secret": fmt.Sprintf("%s/webhook-service-cert", namespace.Name), |
| 220 | + }, |
| 221 | + }, |
| 222 | + Spec: runtimev1.ExtensionConfigSpec{ |
| 223 | + ClientConfig: runtimev1.ClientConfig{ |
| 224 | + Service: &runtimev1.ServiceReference{ |
| 225 | + Name: "webhook-service", |
| 226 | + Namespace: namespace.Name, |
| 227 | + }, |
| 228 | + }, |
| 229 | + NamespaceSelector: &metav1.LabelSelector{ |
| 230 | + MatchLabels: map[string]string{ |
| 231 | + "kubernetes.io/metadata.name:": namespace.Name, |
| 232 | + }, |
| 233 | + }, |
| 234 | + }, |
| 235 | + } |
| 236 | +} |
0 commit comments