|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright 2025 The Kubernetes Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +package unmanaged |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + "os/exec" |
| 26 | + "strings" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/onsi/ginkgo/v2" |
| 30 | + . "github.com/onsi/gomega" |
| 31 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 33 | + |
| 34 | + infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" |
| 35 | + "sigs.k8s.io/cluster-api-provider-aws/v2/test/e2e/shared" |
| 36 | +) |
| 37 | + |
| 38 | +var _ = ginkgo.Context("[unmanaged] [conversion]", func() { |
| 39 | + var ( |
| 40 | + ctx context.Context |
| 41 | + ) |
| 42 | + |
| 43 | + ginkgo.BeforeEach(func() { |
| 44 | + ctx = context.TODO() |
| 45 | + }) |
| 46 | + |
| 47 | + ginkgo.Describe("AWSClusterControllerIdentity conversion", func() { |
| 48 | + ginkgo.It("should successfully convert v1beta1 to v1beta2", func() { |
| 49 | + specName := "conversion-awsclustercontrolleridentity" |
| 50 | + namespace := shared.SetupSpecNamespace(ctx, specName, e2eCtx) |
| 51 | + defer shared.DumpSpecResourcesAndCleanup(ctx, specName, namespace, e2eCtx) |
| 52 | + |
| 53 | + bootstrapClient := e2eCtx.Environment.BootstrapClusterProxy.GetClient() |
| 54 | + kubeconfigPath := e2eCtx.Environment.BootstrapClusterProxy.GetKubeconfigPath() |
| 55 | + |
| 56 | + ginkgo.By("Creating AWSClusterControllerIdentity in v1beta1 format using kubectl") |
| 57 | + // Create the resource using kubectl with explicit v1beta1 APIVersion |
| 58 | + // This simulates a resource created by an old provider version |
| 59 | + v1beta1YAML := fmt.Sprintf(`apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 |
| 60 | +kind: AWSClusterControllerIdentity |
| 61 | +metadata: |
| 62 | + name: %s |
| 63 | +spec: |
| 64 | + allowedNamespaces: {} |
| 65 | +`, infrav1.AWSClusterControllerIdentityName) |
| 66 | + |
| 67 | + // Delete existing if present |
| 68 | + existing := &infrav1.AWSClusterControllerIdentity{} |
| 69 | + err := bootstrapClient.Get(ctx, client.ObjectKey{Name: infrav1.AWSClusterControllerIdentityName}, existing) |
| 70 | + if err == nil { |
| 71 | + Expect(bootstrapClient.Delete(ctx, existing)).To(Succeed()) |
| 72 | + // Wait for deletion |
| 73 | + Eventually(func() bool { |
| 74 | + err := bootstrapClient.Get(ctx, client.ObjectKey{Name: infrav1.AWSClusterControllerIdentityName}, existing) |
| 75 | + return apierrors.IsNotFound(err) |
| 76 | + }, 30*time.Second, 1*time.Second).Should(BeTrue()) |
| 77 | + } |
| 78 | + |
| 79 | + // Create using kubectl apply |
| 80 | + cmd := exec.Command("kubectl", "--kubeconfig", kubeconfigPath, "apply", "-f", "-") |
| 81 | + cmd.Stdin = strings.NewReader(v1beta1YAML) |
| 82 | + output, err := cmd.CombinedOutput() |
| 83 | + if err != nil && !strings.Contains(string(output), "already exists") { |
| 84 | + Expect(err).NotTo(HaveOccurred(), "Failed to create v1beta1 AWSClusterControllerIdentity: %s", string(output)) |
| 85 | + } |
| 86 | + |
| 87 | + ginkgo.By("Verifying resource exists via kubectl get") |
| 88 | + // Use kubectl to get the resource and verify it can be retrieved |
| 89 | + // This will trigger conversion if needed |
| 90 | + getCmd := exec.Command("kubectl", "--kubeconfig", kubeconfigPath, |
| 91 | + "get", "awsclustercontrolleridentity", infrav1.AWSClusterControllerIdentityName, |
| 92 | + "-o", "jsonpath={.apiVersion}") |
| 93 | + output, err = getCmd.CombinedOutput() |
| 94 | + Expect(err).NotTo(HaveOccurred(), "Failed to get AWSClusterControllerIdentity: %s", string(output)) |
| 95 | + |
| 96 | + apiVersion := strings.TrimSpace(string(output)) |
| 97 | + ginkgo.By(fmt.Sprintf("Resource APIVersion: %s", apiVersion)) |
| 98 | + |
| 99 | + // Verify the APIVersion is v1beta2 (converted from v1beta1) |
| 100 | + Expect(apiVersion).To(Equal("infrastructure.cluster.x-k8s.io/v1beta2"), |
| 101 | + "Expected APIVersion to be v1beta2 after conversion, got %s", apiVersion) |
| 102 | + |
| 103 | + ginkgo.By("Verifying resource can be retrieved using v1beta2 client") |
| 104 | + // Verify we can get it using the v1beta2 client |
| 105 | + v1beta2Identity := &infrav1.AWSClusterControllerIdentity{} |
| 106 | + err = bootstrapClient.Get(ctx, client.ObjectKey{ |
| 107 | + Name: infrav1.AWSClusterControllerIdentityName, |
| 108 | + }, v1beta2Identity) |
| 109 | + Expect(err).NotTo(HaveOccurred(), "Failed to get AWSClusterControllerIdentity using v1beta2 client") |
| 110 | + |
| 111 | + // Verify the APIVersion is correct |
| 112 | + Expect(v1beta2Identity.APIVersion).To(Equal(infrav1.GroupVersion.String()), |
| 113 | + "Expected APIVersion to be %s, got %s", infrav1.GroupVersion.String(), v1beta2Identity.APIVersion) |
| 114 | + |
| 115 | + ginkgo.By("Verifying conversion webhook works by listing resources") |
| 116 | + // List all AWSClusterControllerIdentity resources to ensure conversion works for list operations |
| 117 | + identityList := &infrav1.AWSClusterControllerIdentityList{} |
| 118 | + err = bootstrapClient.List(ctx, identityList) |
| 119 | + Expect(err).NotTo(HaveOccurred(), "Failed to list AWSClusterControllerIdentity resources") |
| 120 | + |
| 121 | + // Verify at least one item exists and has correct APIVersion |
| 122 | + Expect(len(identityList.Items)).To(BeNumerically(">=", 1), |
| 123 | + "Expected at least one AWSClusterControllerIdentity in the list") |
| 124 | + Expect(identityList.Items[0].APIVersion).To(Equal(infrav1.GroupVersion.String()), |
| 125 | + "Expected list item APIVersion to be %s, got %s", infrav1.GroupVersion.String(), identityList.Items[0].APIVersion) |
| 126 | + |
| 127 | + ginkgo.By("PASSED! Conversion webhook successfully converts v1beta1 to v1beta2") |
| 128 | + }) |
| 129 | + |
| 130 | + ginkgo.It("should handle conversion during provider upgrade", func() { |
| 131 | + specName := "conversion-upgrade-awsclustercontrolleridentity" |
| 132 | + namespace := shared.SetupSpecNamespace(ctx, specName, e2eCtx) |
| 133 | + defer shared.DumpSpecResourcesAndCleanup(ctx, specName, namespace, e2eCtx) |
| 134 | + |
| 135 | + bootstrapClient := e2eCtx.Environment.BootstrapClusterProxy.GetClient() |
| 136 | + kubeconfigPath := e2eCtx.Environment.BootstrapClusterProxy.GetKubeconfigPath() |
| 137 | + |
| 138 | + ginkgo.By("Step 1: Creating AWSClusterControllerIdentity in v1beta1 format (simulating old provider)") |
| 139 | + // Create the resource using kubectl with explicit v1beta1 APIVersion |
| 140 | + // This simulates a resource created by an old provider version |
| 141 | + v1beta1YAML := fmt.Sprintf(`apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 |
| 142 | +kind: AWSClusterControllerIdentity |
| 143 | +metadata: |
| 144 | + name: %s |
| 145 | +spec: |
| 146 | + allowedNamespaces: {} |
| 147 | +`, infrav1.AWSClusterControllerIdentityName) |
| 148 | + |
| 149 | + // Delete if exists first to ensure clean state |
| 150 | + existing := &infrav1.AWSClusterControllerIdentity{} |
| 151 | + err := bootstrapClient.Get(ctx, client.ObjectKey{Name: infrav1.AWSClusterControllerIdentityName}, existing) |
| 152 | + if err == nil { |
| 153 | + Expect(bootstrapClient.Delete(ctx, existing)).To(Succeed()) |
| 154 | + // Wait for deletion |
| 155 | + Eventually(func() bool { |
| 156 | + err := bootstrapClient.Get(ctx, client.ObjectKey{Name: infrav1.AWSClusterControllerIdentityName}, existing) |
| 157 | + return apierrors.IsNotFound(err) |
| 158 | + }, 30*time.Second, 1*time.Second).Should(BeTrue()) |
| 159 | + } |
| 160 | + |
| 161 | + // Create using kubectl apply |
| 162 | + createCmd := exec.Command("kubectl", "--kubeconfig", kubeconfigPath, "apply", "-f", "-") |
| 163 | + createCmd.Stdin = strings.NewReader(v1beta1YAML) |
| 164 | + output, err := createCmd.CombinedOutput() |
| 165 | + Expect(err).NotTo(HaveOccurred(), "Failed to create v1beta1 AWSClusterControllerIdentity: %s", string(output)) |
| 166 | + |
| 167 | + ginkgo.By("Step 2: Verifying resource exists with v1beta1 APIVersion") |
| 168 | + // Verify it was created with v1beta1 by checking the stored version |
| 169 | + // Note: The stored version might be v1beta2 if conversion already happened, |
| 170 | + // but we can verify conversion works when we get it |
| 171 | + |
| 172 | + ginkgo.By("Step 3: Simulating provider upgrade - getting resource should trigger conversion") |
| 173 | + // When we get the resource using v1beta2 client, Kubernetes will request conversion |
| 174 | + // This simulates what happens when the provider is upgraded and the webhook handles conversion |
| 175 | + v1beta2Identity := &infrav1.AWSClusterControllerIdentity{} |
| 176 | + err = bootstrapClient.Get(ctx, client.ObjectKey{ |
| 177 | + Name: infrav1.AWSClusterControllerIdentityName, |
| 178 | + }, v1beta2Identity) |
| 179 | + |
| 180 | + Expect(err).NotTo(HaveOccurred(), "Failed to get AWSClusterControllerIdentity after upgrade - conversion webhook may have failed") |
| 181 | + |
| 182 | + ginkgo.By("Step 4: Verifying converted resource has correct APIVersion") |
| 183 | + // Verify the APIVersion is correct after conversion |
| 184 | + Expect(v1beta2Identity.APIVersion).To(Equal(infrav1.GroupVersion.String()), |
| 185 | + "Expected APIVersion to be %s after conversion, got %s. This indicates the conversion webhook fix is working.", |
| 186 | + infrav1.GroupVersion.String(), v1beta2Identity.APIVersion) |
| 187 | + |
| 188 | + ginkgo.By("Step 5: Verifying resource can be retrieved via kubectl") |
| 189 | + // Verify via kubectl that the resource is accessible and has correct APIVersion |
| 190 | + kubectlCmd := exec.Command("kubectl", "--kubeconfig", kubeconfigPath, |
| 191 | + "get", "awsclustercontrolleridentity", infrav1.AWSClusterControllerIdentityName, |
| 192 | + "-o", "jsonpath={.apiVersion}") |
| 193 | + output, err = kubectlCmd.CombinedOutput() |
| 194 | + Expect(err).NotTo(HaveOccurred(), "kubectl get failed: %s", string(output)) |
| 195 | + |
| 196 | + apiVersion := strings.TrimSpace(string(output)) |
| 197 | + Expect(apiVersion).To(Equal("infrastructure.cluster.x-k8s.io/v1beta2"), |
| 198 | + "kubectl get returned wrong APIVersion: expected v1beta2, got %s", apiVersion) |
| 199 | + |
| 200 | + ginkgo.By("Step 6: Verifying resource spec is preserved after conversion") |
| 201 | + // Verify the spec is preserved correctly |
| 202 | + Expect(v1beta2Identity.Spec.AllowedNamespaces).NotTo(BeNil(), |
| 203 | + "Spec.AllowedNamespaces should be preserved after conversion") |
| 204 | + |
| 205 | + ginkgo.By("PASSED! Provider upgrade conversion test successful") |
| 206 | + }) |
| 207 | + }) |
| 208 | +}) |
0 commit comments