diff --git a/test/e2e/clusterctl_upgrade.go b/test/e2e/clusterctl_upgrade.go index d38f2b4ad77f..242cad4f747e 100644 --- a/test/e2e/clusterctl_upgrade.go +++ b/test/e2e/clusterctl_upgrade.go @@ -361,7 +361,7 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg Expect(workloadClusterTemplate).ToNot(BeNil(), "Failed to get the cluster template") log.Logf("Applying the cluster template yaml to the cluster") - Expect(managementClusterProxy.Apply(ctx, workloadClusterTemplate)).To(Succeed()) + Expect(managementClusterProxy.Create(ctx, workloadClusterTemplate)).To(Succeed()) if input.PreWaitForCluster != nil { By("Running PreWaitForCluster steps against the management cluster") diff --git a/test/e2e/kcp_remediations.go b/test/e2e/kcp_remediations.go index 64fc229b0b59..5d350f2c353a 100644 --- a/test/e2e/kcp_remediations.go +++ b/test/e2e/kcp_remediations.go @@ -472,7 +472,7 @@ func createWorkloadClusterAndWait(ctx context.Context, input createWorkloadClust Expect(workloadClusterTemplate).ToNot(BeNil(), "Failed to get the cluster template") Eventually(func() error { - return input.Proxy.Apply(ctx, workloadClusterTemplate) + return input.Proxy.Create(ctx, workloadClusterTemplate) }, 10*time.Second).Should(Succeed(), "Failed to apply the cluster template") log.Logf("Waiting for the cluster infrastructure to be provisioned") diff --git a/test/framework/cluster_proxy.go b/test/framework/cluster_proxy.go index 0c30dbec68da..13be52e51724 100644 --- a/test/framework/cluster_proxy.go +++ b/test/framework/cluster_proxy.go @@ -30,7 +30,9 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" + kerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -45,6 +47,7 @@ import ( "sigs.k8s.io/cluster-api/test/framework/exec" "sigs.k8s.io/cluster-api/test/framework/internal/log" "sigs.k8s.io/cluster-api/test/infrastructure/container" + "sigs.k8s.io/cluster-api/util/yaml" ) const ( @@ -90,6 +93,9 @@ type ClusterProxy interface { // Apply to apply YAML to the Kubernetes cluster, `kubectl apply`. Apply(ctx context.Context, resources []byte, args ...string) error + // Create creates using the controller-runtime client. + Create(ctx context.Context, resources []byte) error + // GetWorkloadCluster returns a proxy to a workload cluster defined in the Kubernetes cluster. GetWorkloadCluster(ctx context.Context, namespace, name string) ClusterProxy @@ -247,6 +253,27 @@ func (p *clusterProxy) Apply(ctx context.Context, resources []byte, args ...stri return exec.KubectlApply(ctx, p.kubeconfigPath, resources, args...) } +// Create creates using the controller-runtime client. +func (p *clusterProxy) Create(ctx context.Context, resources []byte) error { + Expect(ctx).NotTo(BeNil(), "ctx is required for Create") + Expect(resources).NotTo(BeNil(), "resources is required for Create") + + var retErrs []error + objs, err := yaml.ToUnstructured(resources) + if err != nil { + return err + } + for o := range objs { + if err := p.GetClient().Create(ctx, &objs[o]); err != nil { + if apierrors.IsAlreadyExists(err) { + continue + } + retErrs = append(retErrs, err) + } + } + return kerrors.NewAggregate(retErrs) +} + func (p *clusterProxy) GetRESTConfig() *rest.Config { config, err := clientcmd.LoadFromFile(p.kubeconfigPath) Expect(err).ToNot(HaveOccurred(), "Failed to load Kubeconfig file from %q", p.kubeconfigPath) diff --git a/test/framework/clusterctl/clusterctl_helpers.go b/test/framework/clusterctl/clusterctl_helpers.go index 677afaa0c745..ff4b77e2a6df 100644 --- a/test/framework/clusterctl/clusterctl_helpers.go +++ b/test/framework/clusterctl/clusterctl_helpers.go @@ -342,7 +342,7 @@ func ApplyClusterTemplateAndWait(ctx context.Context, input ApplyClusterTemplate cniYaml, err := os.ReadFile(input.CNIManifestPath) Expect(err).ShouldNot(HaveOccurred()) - Expect(workloadCluster.Apply(ctx, cniYaml)).ShouldNot(HaveOccurred()) + Expect(workloadCluster.Create(ctx, cniYaml)).ShouldNot(HaveOccurred()) } log.Logf("Waiting for control plane to be ready")