Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Adding to the test framework the equivalent to kubectl create -f. #9731

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/e2e/clusterctl_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/kcp_remediations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}, 10*time.Second).Should(Succeed(), "Failed to apply the cluster template")
}, 10*time.Second).Should(Succeed(), "Failed to create the cluster template")


log.Logf("Waiting for the cluster infrastructure to be provisioned")
Expand Down
27 changes: 27 additions & 0 deletions test/framework/cluster_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/framework/clusterctl/clusterctl_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down