|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2025. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package e2e |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "path/filepath" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/k0sproject/k0smotron/e2e/util" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | + corev1 "k8s.io/api/core/v1" |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 33 | + "k8s.io/client-go/kubernetes" |
| 34 | + "k8s.io/client-go/rest" |
| 35 | + "k8s.io/client-go/tools/clientcmd" |
| 36 | + "k8s.io/utils/ptr" |
| 37 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 38 | + capiframework "sigs.k8s.io/cluster-api/test/framework" |
| 39 | + "sigs.k8s.io/cluster-api/test/framework/clusterctl" |
| 40 | + capiutil "sigs.k8s.io/cluster-api/util" |
| 41 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 42 | +) |
| 43 | + |
| 44 | +func TestMachinedeployment(t *testing.T) { |
| 45 | + setupAndRun(t, func(t *testing.T) { |
| 46 | + testName := "machinedeployment" |
| 47 | + |
| 48 | + // Setup a Namespace where to host objects for this spec and create a watcher for the namespace events. |
| 49 | + namespace, _ := util.SetupSpecNamespace(ctx, testName, bootstrapClusterProxy, artifactFolder) |
| 50 | + |
| 51 | + clusterName := fmt.Sprintf("%s-%s", testName, capiutil.RandomString(6)) |
| 52 | + |
| 53 | + workloadClusterTemplate := clusterctl.ConfigCluster(ctx, clusterctl.ConfigClusterInput{ |
| 54 | + ClusterctlConfigPath: clusterctlConfigPath, |
| 55 | + KubeconfigPath: bootstrapClusterProxy.GetKubeconfigPath(), |
| 56 | + // select cluster templates |
| 57 | + Flavor: "machinedeployment", |
| 58 | + |
| 59 | + Namespace: namespace.Name, |
| 60 | + ClusterName: clusterName, |
| 61 | + KubernetesVersion: "v1.32.2", |
| 62 | + ControlPlaneMachineCount: ptr.To[int64](1), |
| 63 | + // TODO: make infra provider configurable |
| 64 | + InfrastructureProvider: "docker", |
| 65 | + LogFolder: filepath.Join(artifactFolder, "clusters", bootstrapClusterProxy.GetName()), |
| 66 | + ClusterctlVariables: map[string]string{ |
| 67 | + "CLUSTER_NAME": clusterName, |
| 68 | + "NAMESPACE": namespace.Name, |
| 69 | + }, |
| 70 | + }) |
| 71 | + require.NotNil(t, workloadClusterTemplate) |
| 72 | + |
| 73 | + require.Eventually(t, func() bool { |
| 74 | + return bootstrapClusterProxy.CreateOrUpdate(ctx, workloadClusterTemplate) == nil |
| 75 | + }, 10*time.Second, 1*time.Second, "Failed to apply the cluster template") |
| 76 | + |
| 77 | + cluster, err := util.DiscoveryAndWaitForCluster(ctx, capiframework.DiscoveryAndWaitForClusterInput{ |
| 78 | + Getter: bootstrapClusterProxy.GetClient(), |
| 79 | + Namespace: namespace.Name, |
| 80 | + Name: clusterName, |
| 81 | + }, util.GetInterval(e2eConfig, testName, "wait-cluster")) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + defer func() { |
| 85 | + util.DumpSpecResourcesAndCleanup( |
| 86 | + ctx, |
| 87 | + testName, |
| 88 | + bootstrapClusterProxy, |
| 89 | + artifactFolder, |
| 90 | + namespace, |
| 91 | + cancelWatches, |
| 92 | + cluster, |
| 93 | + util.GetInterval(e2eConfig, testName, "wait-delete-cluster"), |
| 94 | + skipCleanup, |
| 95 | + ) |
| 96 | + }() |
| 97 | + |
| 98 | + err = util.DiscoveryAndWaitForK0smotronControlPlaneInitialized(ctx, capiframework.DiscoveryAndWaitForControlPlaneInitializedInput{ |
| 99 | + Lister: bootstrapClusterProxy.GetClient(), |
| 100 | + Cluster: cluster, |
| 101 | + }, util.GetInterval(e2eConfig, testName, "wait-control-plane")) |
| 102 | + require.NoError(t, err) |
| 103 | + |
| 104 | + err = util.WaitForK0smotronControlPlaneToBeReady(ctx, bootstrapClusterProxy.GetClient(), clusterName, namespace.Name, util.GetInterval(e2eConfig, testName, "wait-control-plane")) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + fmt.Print("Verifying K0smotronControlPlane version format\n") |
| 108 | + verifyK0smotronControlPlaneVersionFormat(ctx, t, bootstrapClusterProxy, clusterName, namespace.Name) |
| 109 | + |
| 110 | + // Get the kubeconfig for the workload cluster |
| 111 | + workloadClusterKubeconfig := getWorkloadClusterKubeconfig(ctx, t, bootstrapClusterProxy, clusterName, namespace.Name) |
| 112 | + |
| 113 | + fmt.Print("Waiting for MachineDeployment to be ready\n") |
| 114 | + require.Eventually(t, func() bool { |
| 115 | + md := &clusterv1.MachineDeployment{} |
| 116 | + err := bootstrapClusterProxy.GetClient().Get(ctx, client.ObjectKey{ |
| 117 | + Namespace: namespace.Name, |
| 118 | + Name: clusterName, |
| 119 | + }, md) |
| 120 | + if err != nil { |
| 121 | + return false |
| 122 | + } |
| 123 | + return md.Status.ReadyReplicas == 2 |
| 124 | + }, 5*time.Minute, 10*time.Second, "MachineDeployment failed to become ready") |
| 125 | + |
| 126 | + fmt.Print("Verifying worker nodes are ready in the workload cluster\n") |
| 127 | + verifyWorkerNodesReady(ctx, t, workloadClusterKubeconfig, 2) |
| 128 | + |
| 129 | + fmt.Print("MachineDeployment test completed successfully\n") |
| 130 | + }) |
| 131 | +} |
| 132 | + |
| 133 | +func verifyK0smotronControlPlaneVersionFormat(ctx context.Context, t *testing.T, clusterProxy capiframework.ClusterProxy, clusterName, namespace string) { |
| 134 | + kcp := &unstructured.Unstructured{} |
| 135 | + kcp.SetAPIVersion("controlplane.cluster.x-k8s.io/v1beta1") |
| 136 | + kcp.SetKind("K0smotronControlPlane") |
| 137 | + |
| 138 | + require.Eventually(t, func() bool { |
| 139 | + err := clusterProxy.GetClient().Get(ctx, client.ObjectKey{ |
| 140 | + Namespace: namespace, |
| 141 | + Name: clusterName, |
| 142 | + }, kcp) |
| 143 | + if err != nil { |
| 144 | + return false |
| 145 | + } |
| 146 | + |
| 147 | + status, found, err := unstructured.NestedMap(kcp.Object, "status") |
| 148 | + if err != nil || !found { |
| 149 | + return false |
| 150 | + } |
| 151 | + |
| 152 | + version, found, err := unstructured.NestedString(status, "version") |
| 153 | + if err != nil || !found { |
| 154 | + return false |
| 155 | + } |
| 156 | + |
| 157 | + if version != "v1.32.2" { |
| 158 | + t.Errorf("Expected version %s, but got: %s", "v1.32.2", version) |
| 159 | + return false |
| 160 | + } |
| 161 | + |
| 162 | + return true |
| 163 | + }, 3*time.Minute, 10*time.Second, "K0smotronControlPlane version format verification failed") |
| 164 | +} |
| 165 | + |
| 166 | +func getWorkloadClusterKubeconfig(ctx context.Context, t *testing.T, clusterProxy capiframework.ClusterProxy, clusterName, namespace string) *rest.Config { |
| 167 | + kubeconfigSecret := &corev1.Secret{} |
| 168 | + require.Eventually(t, func() bool { |
| 169 | + err := clusterProxy.GetClient().Get(ctx, client.ObjectKey{ |
| 170 | + Namespace: namespace, |
| 171 | + Name: fmt.Sprintf("%s-kubeconfig", clusterName), |
| 172 | + }, kubeconfigSecret) |
| 173 | + return err == nil |
| 174 | + }, 2*time.Minute, 10*time.Second, "Failed to get kubeconfig secret") |
| 175 | + |
| 176 | + kubeconfig, ok := kubeconfigSecret.Data["value"] |
| 177 | + require.True(t, ok, "kubeconfig secret should contain 'value' key") |
| 178 | + |
| 179 | + restConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeconfig) |
| 180 | + require.NoError(t, err, "Failed to parse kubeconfig") |
| 181 | + |
| 182 | + return restConfig |
| 183 | +} |
| 184 | + |
| 185 | +func verifyWorkerNodesReady(ctx context.Context, t *testing.T, kubeconfig *rest.Config, expectedNodes int) { |
| 186 | + clientset, err := kubernetes.NewForConfig(kubeconfig) |
| 187 | + require.NoError(t, err, "Failed to create Kubernetes clientset") |
| 188 | + |
| 189 | + require.Eventually(t, func() bool { |
| 190 | + nodes, err := clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) |
| 191 | + if err != nil { |
| 192 | + return false |
| 193 | + } |
| 194 | + |
| 195 | + readyCount := 0 |
| 196 | + for _, node := range nodes.Items { |
| 197 | + for _, condition := range node.Status.Conditions { |
| 198 | + if condition.Type == corev1.NodeReady && condition.Status == corev1.ConditionTrue { |
| 199 | + readyCount++ |
| 200 | + break |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return readyCount == expectedNodes |
| 206 | + }, 5*time.Minute, 10*time.Second, "Expected number of worker nodes not ready") |
| 207 | +} |
0 commit comments