Skip to content

Commit

Permalink
add CreateOrUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
cahillsf committed Jun 3, 2024
1 parent 6b423fd commit f90ae23
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 13 deletions.
2 changes: 1 addition & 1 deletion test/e2e/clusterctl_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,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.CreateOrUpdate(ctx, workloadClusterTemplate)).To(Succeed())

if input.PreWaitForCluster != nil {
By("Running PreWaitForCluster steps against the management cluster")
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/kcp_adoption.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func KCPAdoptionSpec(ctx context.Context, inputGetter func() KCPAdoptionSpecInpu
Expect(workloadClusterTemplate).ToNot(BeNil(), "Failed to get the cluster template")

By("Applying the cluster template yaml to the cluster with the 'initial' selector")
Expect(input.BootstrapClusterProxy.Apply(ctx, workloadClusterTemplate, "--selector", "kcp-adoption.step1")).ShouldNot(HaveOccurred())
Expect(input.BootstrapClusterProxy.CreateOrUpdate(ctx, workloadClusterTemplate, "kcp-adoption.step1")).ShouldNot(HaveOccurred())

cluster = framework.DiscoveryAndWaitForCluster(ctx, framework.DiscoveryAndWaitForClusterInput{
Getter: client,
Expand All @@ -159,7 +159,7 @@ func KCPAdoptionSpec(ctx context.Context, inputGetter func() KCPAdoptionSpecInpu
}, WaitForControlPlaneIntervals...)

By("Applying the cluster template yaml to the cluster with the 'kcp' selector")
Expect(input.BootstrapClusterProxy.Apply(ctx, workloadClusterTemplate, "--selector", "kcp-adoption.step2")).ShouldNot(HaveOccurred())
Expect(input.BootstrapClusterProxy.CreateOrUpdate(ctx, workloadClusterTemplate, "kcp-adoption.step2")).ShouldNot(HaveOccurred())

var controlPlane *controlplanev1.KubeadmControlPlane
Eventually(func() *controlplanev1.KubeadmControlPlane {
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 @@ -489,7 +489,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.CreateOrUpdate(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
8 changes: 4 additions & 4 deletions test/e2e/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,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.CreateOrUpdate(ctx, clusterClassYAML)
}, 1*time.Minute).Should(Succeed())
} else {
log.Logf("ClusterClass already exists. Skipping creation.")
Expand Down Expand Up @@ -551,7 +551,7 @@ func getClusterCreateAndWaitFn(input clusterctl.ApplyCustomClusterTemplateAndWai
WaitForClusterIntervals: input.WaitForClusterIntervals,
WaitForControlPlaneIntervals: input.WaitForControlPlaneIntervals,
WaitForMachineDeployments: input.WaitForMachineDeployments,
Args: input.Args,
LabelSelectors: input.LabelSelectors,
PreWaitForCluster: input.PreWaitForCluster,
PostMachinesProvisioned: input.PostMachinesProvisioned,
ControlPlaneWaiters: input.ControlPlaneWaiters,
Expand All @@ -563,7 +563,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.CreateOrUpdate(ctx, clusterTemplateYAML)
}, 1*time.Minute).Should(Succeed(), "Failed to apply the cluster template of cluster %s", klog.KRef(namespace, clusterName))
}
}
Expand Down Expand Up @@ -616,7 +616,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.CreateOrUpdate(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 @@ -107,7 +107,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.CreateOrUpdate(ctx, workloadYaml)).To(Succeed(), "failed to apply %s", workloadYamlTemplate)

By("Wait for the autoscaler deployment and collect logs")
deployment := &appsv1.Deployment{
Expand Down
57 changes: 57 additions & 0 deletions test/framework/cluster_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"path"
goruntime "runtime"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -92,6 +93,9 @@ type ClusterProxy interface {
// GetLogCollector returns the machine log collector for the Kubernetes cluster.
GetLogCollector() ClusterLogCollector

// Creates objects using the clusterProxy client
CreateOrUpdate(ctx context.Context, resources []byte, selector ...string) error

// Apply to apply YAML to the Kubernetes cluster, `kubectl apply`.
Apply(ctx context.Context, resources []byte, args ...string) error

Expand Down Expand Up @@ -250,6 +254,59 @@ func (p *clusterProxy) GetCache(ctx context.Context) cache.Cache {
return p.cache
}

// Creates objects using the clusterProxy client

Check failure on line 257 in test/framework/cluster_proxy.go

View workflow job for this annotation

GitHub Actions / lint (test)

Comment should end in a period (godot)
func (p *clusterProxy) CreateOrUpdate(ctx context.Context, resources []byte, selector ...string) error {
Expect(ctx).NotTo(BeNil(), "ctx is required for Create")
Expect(resources).NotTo(BeNil(), "resources is required for Create")
labelSelector := labels.Everything()
// Construct label selector from parameters if provided
if selector != nil {
ls, err := metav1.ParseToLabelSelector(selector[0])
if err != nil {
return fmt.Errorf("could not parse selector: %+w", err)
}
labelSelector, err = metav1.LabelSelectorAsSelector(ls)
if err != nil {
return fmt.Errorf("could not convert metav1.LabelSelector to labels.Selector: %+w", err)
}
}
var retErrs []error
objs, err := yaml.ToUnstructured(resources)
if err != nil {
return err
}

existingObject := &unstructured.Unstructured{}
for _, o := range objs {
o := o
objectKey := types.NamespacedName{
Name: o.GetName(),
Namespace: o.GetNamespace(),
}
existingObject.SetAPIVersion(o.GetAPIVersion())
existingObject.SetKind(o.GetKind())
labels := labels.Set(o.GetLabels())
if labelSelector.Matches(labels) {
if err := p.GetClient().Get(ctx, objectKey, existingObject); err != nil {
// Expected error -- if the object does not exist, create it
if strings.Contains(err.Error(), "not found") {
if err := p.GetClient().Create(ctx, &o); err != nil {
retErrs = append(retErrs, err)
}
} else {
retErrs = append(retErrs, err)
}
} else {
o.SetResourceVersion(existingObject.GetResourceVersion())
if err := p.GetClient().Update(ctx, &o); err != nil {
retErrs = append(retErrs, err)
}
}
}
}
return kerrors.NewAggregate(retErrs)
}

// Apply wraps `kubectl apply ...` and prints the output so we can see what gets applied to the cluster.
func (p *clusterProxy) Apply(ctx context.Context, resources []byte, args ...string) error {
Expect(ctx).NotTo(BeNil(), "ctx is required for Apply")
Expand Down
8 changes: 4 additions & 4 deletions test/framework/clusterctl/clusterctl_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func ApplyClusterTemplateAndWait(ctx context.Context, input ApplyClusterTemplate
WaitForControlPlaneIntervals: input.WaitForControlPlaneIntervals,
WaitForMachineDeployments: input.WaitForMachineDeployments,
WaitForMachinePools: input.WaitForMachinePools,
Args: input.Args,
LabelSelectors: input.Args,
PreWaitForCluster: input.PreWaitForCluster,
PostMachinesProvisioned: input.PostMachinesProvisioned,
ControlPlaneWaiters: input.ControlPlaneWaiters,
Expand All @@ -377,7 +377,7 @@ type ApplyCustomClusterTemplateAndWaitInput struct {
WaitForControlPlaneIntervals []interface{}
WaitForMachineDeployments []interface{}
WaitForMachinePools []interface{}
Args []string // extra args to be used during `kubectl apply`
LabelSelectors []string // label selectory to be used during creation or update of objects
PreWaitForCluster func()
PostMachinesProvisioned func()
ControlPlaneWaiters
Expand Down Expand Up @@ -412,7 +412,7 @@ func ApplyCustomClusterTemplateAndWait(ctx context.Context, input ApplyCustomClu

log.Logf("Applying the cluster template yaml of cluster %s", klog.KRef(input.Namespace, input.ClusterName))
Eventually(func() error {
return input.ClusterProxy.Apply(ctx, input.CustomTemplateYAML, input.Args...)
return input.ClusterProxy.CreateOrUpdate(ctx, input.CustomTemplateYAML, input.LabelSelectors...)
}, 1*time.Minute).Should(Succeed(), "Failed to apply the cluster template")

// Once we applied the cluster template we can run PreWaitForCluster.
Expand Down Expand Up @@ -448,7 +448,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.CreateOrUpdate(ctx, cniYaml)).ShouldNot(HaveOccurred())
}

log.Logf("Waiting for control plane of cluster %s to be ready", klog.KRef(input.Namespace, input.ClusterName))
Expand Down

0 comments on commit f90ae23

Please sign in to comment.