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 controller runtime create #9736

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 @@ -386,7 +386,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")
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
log.Logf("Applying the cluster template yaml to the cluster")
log.Logf("Creating 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 @@ -485,7 +485,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")
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func scaleSpec(ctx context.Context, inputGetter func() scaleSpecInput) {
clusterClassYAML := bytes.Replace(baseClusterClassYAML, []byte(scaleClusterNamespacePlaceholder), []byte(namespace.Name), -1)
log.Logf("Apply ClusterClass")
Eventually(func() error {
return input.BootstrapClusterProxy.Apply(ctx, clusterClassYAML)
return input.BootstrapClusterProxy.Create(ctx, clusterClassYAML)
}, 1*time.Minute).Should(Succeed())
} else {
log.Logf("ClusterClass already exists. Skipping creation.")
Expand Down Expand Up @@ -511,7 +511,7 @@ func getClusterCreateFn(clusterProxy framework.ClusterProxy) clusterCreator {
return func(ctx context.Context, namespace, clusterName string, clusterTemplateYAML []byte) {
log.Logf("Applying the cluster template yaml of cluster %s", klog.KRef(namespace, clusterName))
Eventually(func() error {
return clusterProxy.Apply(ctx, clusterTemplateYAML)
return clusterProxy.Create(ctx, clusterTemplateYAML)
}, 1*time.Minute).Should(Succeed(), "Failed to apply the cluster template of cluster %s", klog.KRef(namespace, clusterName))
}
}
Expand Down Expand Up @@ -564,7 +564,7 @@ func createClusterWorker(ctx context.Context, clusterProxy framework.ClusterProx
log.Logf("Apply ClusterClass in namespace %", namespaceName)
clusterClassYAML := bytes.Replace(baseClusterClassYAML, []byte(scaleClusterNamespacePlaceholder), []byte(namespaceName), -1)
Eventually(func() error {
return clusterProxy.Apply(ctx, clusterClassYAML)
return clusterProxy.Create(ctx, clusterClassYAML)
}, 1*time.Minute).Should(Succeed())
}

Expand Down
2 changes: 1 addition & 1 deletion test/framework/autoscaler_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func ApplyAutoscalerToWorkloadCluster(ctx context.Context, input ApplyAutoscaler
},
})
Expect(err).ToNot(HaveOccurred(), "failed to parse %s", workloadYamlTemplate)
Expect(input.WorkloadClusterProxy.Apply(ctx, workloadYaml)).To(Succeed(), "failed to apply %s", workloadYamlTemplate)
Expect(input.WorkloadClusterProxy.Create(ctx, workloadYaml)).To(Succeed(), "failed to apply %s", workloadYamlTemplate)
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
Expect(input.WorkloadClusterProxy.Create(ctx, workloadYaml)).To(Succeed(), "failed to apply %s", workloadYamlTemplate)
Expect(input.WorkloadClusterProxy.Create(ctx, workloadYaml)).To(Succeed(), "failed to create %s", workloadYamlTemplate)


By("Wait for the autoscaler deployment and collect logs")
deployment := &appsv1.Deployment{
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, options ...Option) ClusterProxy

Expand Down Expand Up @@ -253,6 +259,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 {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need a create func? Why not use GetClient().Create ?

I'm a bit concerned about silently ignoring IsAlreadyExists errors on this level (could lead to all sorts of surprises for the caller of this func if its YAML is not deployed)

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
}
Comment on lines +274 to +276
Copy link
Member

Choose a reason for hiding this comment

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

kubectl create fails when the resource already exists, while apply informs (through logging) that the resource already exists, and updates it in place.

What problem are we trying to solve here?

Copy link
Member

Choose a reason for hiding this comment

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

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 @@ -410,7 +410,7 @@ func ApplyCustomClusterTemplateAndWait(ctx context.Context, input ApplyCustomClu
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 of cluster %s to be ready", klog.KRef(input.Namespace, input.ClusterName))
Expand Down