Skip to content

Commit 241b8e4

Browse files
committed
Make scale e2e test reusable for CAPM3 and other providers
- Export ScaleSpec and ScaleSpecInput to allow reuse in other providers. - Add PostScaleClusterNamespaceCreated hook to be called after generating cluster name and namespace but before applying the template. Signed-off-by: Mohammed Boukhalfa <[email protected]>
1 parent 0000f09 commit 241b8e4

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

test/e2e/scale.go

+34-10
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ const (
6060
scaleClusterNamespacePlaceholder = "scale-cluster-namespace-placeholder"
6161
)
6262

63-
// scaleSpecInput is the input for scaleSpec.
64-
type scaleSpecInput struct {
63+
// ScaleSpecInput is the input for ScaleSpec.
64+
type ScaleSpecInput struct {
6565
E2EConfig *clusterctl.E2EConfig
6666
ClusterctlConfigPath string
6767
BootstrapClusterProxy framework.ClusterProxy
@@ -119,6 +119,16 @@ type scaleSpecInput struct {
119119
// If not specified, this is a no-op.
120120
PostNamespaceCreated func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string)
121121

122+
// Allows to inject a function to be run after test workload cluster name and namespace are generated and
123+
// before applying the clusterclass and the cluster template.
124+
// If not specified, this is a no-op.
125+
PostScaleClusterNamespaceCreated func(
126+
clusterProxy framework.ClusterProxy,
127+
clusterNamespace string,
128+
clusterName string,
129+
clusterClassYAML []byte,
130+
clusterTemplateYAML []byte) ([]byte, []byte)
131+
122132
// FailFast if set to true will return immediately after the first cluster operation fails.
123133
// If set to false, the test suite will not exit immediately after the first cluster operation fails.
124134
// Example: When creating clusters from c1 to c20 consider c6 fails creation. If FailFast is set to true
@@ -141,11 +151,11 @@ type scaleSpecInput struct {
141151
SkipWaitForCreation bool
142152
}
143153

144-
// scaleSpec implements a scale test for clusters with MachineDeployments.
145-
func scaleSpec(ctx context.Context, inputGetter func() scaleSpecInput) {
154+
// ScaleSpec implements a scale test for clusters with MachineDeployments.
155+
func ScaleSpec(ctx context.Context, inputGetter func() ScaleSpecInput) {
146156
var (
147157
specName = "scale"
148-
input scaleSpecInput
158+
input ScaleSpecInput
149159
namespace *corev1.Namespace
150160
cancelWatches context.CancelFunc
151161
)
@@ -331,7 +341,7 @@ func scaleSpec(ctx context.Context, inputGetter func() scaleSpecInput) {
331341
Concurrency: concurrency,
332342
FailFast: input.FailFast,
333343
WorkerFunc: func(ctx context.Context, inputChan chan string, resultChan chan workResult, wg *sync.WaitGroup) {
334-
createClusterWorker(ctx, input.BootstrapClusterProxy, inputChan, resultChan, wg, namespace.Name, input.DeployClusterInSeparateNamespaces, baseClusterClassYAML, baseClusterTemplateYAML, creator)
344+
createClusterWorker(ctx, input.BootstrapClusterProxy, inputChan, resultChan, wg, namespace.Name, input.DeployClusterInSeparateNamespaces, baseClusterClassYAML, baseClusterTemplateYAML, creator, input.PostScaleClusterNamespaceCreated)
335345
},
336346
})
337347
if err != nil {
@@ -568,7 +578,9 @@ func getClusterCreateFn(clusterProxy framework.ClusterProxy) clusterCreator {
568578
}
569579
}
570580

571-
func createClusterWorker(ctx context.Context, clusterProxy framework.ClusterProxy, inputChan <-chan string, resultChan chan<- workResult, wg *sync.WaitGroup, defaultNamespace string, deployClusterInSeparateNamespaces bool, baseClusterClassYAML, baseClusterTemplateYAML []byte, create clusterCreator) {
581+
type PostScaleClusterNamespaceCreated func(clusterProxy framework.ClusterProxy, clusterNamespace string, clusterName string, clusterClassYAML []byte, clusterTemplateYAML []byte) ([]byte, []byte)
582+
583+
func createClusterWorker(ctx context.Context, clusterProxy framework.ClusterProxy, inputChan <-chan string, resultChan chan<- workResult, wg *sync.WaitGroup, defaultNamespace string, deployClusterInSeparateNamespaces bool, baseClusterClassYAML, baseClusterTemplateYAML []byte, create clusterCreator, postScaleClusterNamespaceCreated PostScaleClusterNamespaceCreated) {
572584
defer wg.Done()
573585

574586
for {
@@ -604,24 +616,36 @@ func createClusterWorker(ctx context.Context, clusterProxy framework.ClusterProx
604616
// If every cluster should be deployed in a separate namespace:
605617
// * Adjust namespace in ClusterClass YAML.
606618
// * Create new namespace.
607-
// * Deploy ClusterClass in new namespace.
608619
if deployClusterInSeparateNamespaces {
609620
log.Logf("Create namespace %", namespaceName)
610621
_ = framework.CreateNamespace(ctx, framework.CreateNamespaceInput{
611622
Creator: clusterProxy.GetClient(),
612623
Name: namespaceName,
613624
IgnoreAlreadyExists: true,
614625
}, "40s", "10s")
626+
}
615627

628+
// Call postScaleClusterNamespaceCreated hook to apply custom requirements based on the cluster name and namespace
629+
// User might need to apply additional custom resource in the cluster namespace or customize the templates
630+
customizedClusterTemplateYAML := baseClusterTemplateYAML
631+
customizedClusterClassYAML := baseClusterClassYAML
632+
if postScaleClusterNamespaceCreated != nil {
633+
log.Logf("Calling postScaleClusterNamespaceCreated for cluster %s in namespace %s", clusterName, namespaceName)
634+
customizedClusterClassYAML, customizedClusterTemplateYAML = postScaleClusterNamespaceCreated(clusterProxy, namespaceName, clusterName, baseClusterClassYAML, baseClusterTemplateYAML)
635+
}
636+
637+
// If every cluster should be deployed in a separate namespace:
638+
// * Deploy ClusterClass in new namespace.
639+
if deployClusterInSeparateNamespaces {
616640
log.Logf("Apply ClusterClass in namespace %", namespaceName)
617-
clusterClassYAML := bytes.Replace(baseClusterClassYAML, []byte(scaleClusterNamespacePlaceholder), []byte(namespaceName), -1)
641+
clusterClassYAML := bytes.Replace(customizedClusterClassYAML, []byte(scaleClusterNamespacePlaceholder), []byte(namespaceName), -1)
618642
Eventually(func() error {
619643
return clusterProxy.CreateOrUpdate(ctx, clusterClassYAML)
620644
}, 1*time.Minute).Should(Succeed())
621645
}
622646

623647
// Adjust namespace and name in Cluster YAML
624-
clusterTemplateYAML := bytes.Replace(baseClusterTemplateYAML, []byte(scaleClusterNamespacePlaceholder), []byte(namespaceName), -1)
648+
clusterTemplateYAML := bytes.Replace(customizedClusterTemplateYAML, []byte(scaleClusterNamespacePlaceholder), []byte(namespaceName), -1)
625649
clusterTemplateYAML = bytes.Replace(clusterTemplateYAML, []byte(scaleClusterNamePlaceholder), []byte(clusterName), -1)
626650

627651
// Deploy Cluster.

test/e2e/scale_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626

2727
var _ = Describe("When testing the machinery for scale testing using in-memory provider", func() {
2828
// Note: This test does not support MachinePools.
29-
scaleSpec(ctx, func() scaleSpecInput {
30-
return scaleSpecInput{
29+
ScaleSpec(ctx, func() ScaleSpecInput {
30+
return ScaleSpecInput{
3131
E2EConfig: e2eConfig,
3232
ClusterctlConfigPath: clusterctlConfigPath,
3333
InfrastructureProvider: ptr.To("in-memory"),

0 commit comments

Comments
 (0)