Skip to content

Commit 829362d

Browse files
🌱 Add PostCreateNamespace hook to E2E tests (kubernetes-sigs#10067)
* Add PostCreateNamespace hook to E2E tests * Fix for test who are creating an additional management cluster
1 parent 4778539 commit 829362d

18 files changed

+105
-17
lines changed

test/e2e/autoscaler.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ type AutoscalerSpecInput struct {
6161
// Example: dockermachinetemplates.
6262
InfrastructureMachineTemplateKind string
6363
AutoscalerVersion string
64+
65+
// Allows to inject a function to be run after test namespace is created.
66+
// If not specified, this is a no-op.
67+
PostNamespaceCreated func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string)
6468
}
6569

6670
// AutoscalerSpec implements a test for the autoscaler, and more specifically for the autoscaler
@@ -88,7 +92,7 @@ func AutoscalerSpec(ctx context.Context, inputGetter func() AutoscalerSpecInput)
8892
Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion))
8993

9094
// Setup a Namespace where to host objects for this spec and create a watcher for the namespace events.
91-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
95+
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
9296
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
9397
})
9498

test/e2e/cluster_upgrade.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ type ClusterUpgradeConformanceSpecInput struct {
6262

6363
// Flavor to use when creating the cluster for testing, "upgrades" is used if not specified.
6464
Flavor *string
65+
66+
// Allows to inject a function to be run after test namespace is created.
67+
// If not specified, this is a no-op.
68+
PostNamespaceCreated func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string)
6569
}
6670

6771
// ClusterUpgradeConformanceSpec implements a spec that upgrades a cluster and runs the Kubernetes conformance suite.
@@ -126,7 +130,7 @@ func ClusterUpgradeConformanceSpec(ctx context.Context, inputGetter func() Clust
126130
}
127131

128132
// Setup a Namespace where to host objects for this spec and create a watcher for the Namespace events.
129-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
133+
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
130134
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
131135
})
132136

test/e2e/cluster_upgrade_runtimesdk.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ type clusterUpgradeWithRuntimeSDKSpecInput struct {
8181

8282
// Flavor to use when creating the cluster for testing, "upgrades" is used if not specified.
8383
Flavor *string
84+
85+
// Allows to inject a function to be run after test namespace is created.
86+
// If not specified, this is a no-op.
87+
PostNamespaceCreated func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string)
8488
}
8589

8690
// clusterUpgradeWithRuntimeSDKSpec implements a spec that upgrades a cluster and runs the Kubernetes conformance suite.
@@ -130,7 +134,7 @@ func clusterUpgradeWithRuntimeSDKSpec(ctx context.Context, inputGetter func() cl
130134
}
131135

132136
// Set up a Namespace where to host objects for this spec and create a watcher for the Namespace events.
133-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
137+
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
134138
clusterName = fmt.Sprintf("%s-%s", specName, util.RandomString(6))
135139
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
136140
})

test/e2e/clusterclass_changes.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ type ClusterClassChangesSpecInput struct {
106106
// "spec.template.spec.path.to.field": <value>,
107107
// }
108108
ModifyMachinePoolInfrastructureMachinePoolTemplateFields map[string]interface{}
109+
110+
// Allows to inject a function to be run after test namespace is created.
111+
// If not specified, this is a no-op.
112+
PostNamespaceCreated func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string)
109113
}
110114

111115
// ClusterClassChangesSpec implements a test that verifies that ClusterClass changes are rolled out successfully.
@@ -150,7 +154,7 @@ func ClusterClassChangesSpec(ctx context.Context, inputGetter func() ClusterClas
150154
Expect(input.ModifyControlPlaneFields).ToNot(BeEmpty(), "Invalid argument. input.ModifyControlPlaneFields can't be empty when calling %s spec", specName)
151155

152156
// Set up a Namespace where to host objects for this spec and create a watcher for the namespace events.
153-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
157+
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
154158
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
155159
})
156160

test/e2e/clusterclass_rollout.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ type ClusterClassRolloutSpecInput struct {
7171
// NOTE: The template must be using ClusterClass, KCP and CABPK as this test is specifically
7272
// testing ClusterClass and KCP rollout behavior.
7373
Flavor string
74+
75+
// Allows to inject a function to be run after test namespace is created.
76+
// If not specified, this is a no-op.
77+
PostNamespaceCreated func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string)
7478
}
7579

7680
// ClusterClassRolloutSpec implements a test that verifies the ClusterClass rollout behavior.
@@ -108,7 +112,7 @@ func ClusterClassRolloutSpec(ctx context.Context, inputGetter func() ClusterClas
108112
Expect(input.E2EConfig.Variables).To(HaveValidVersion(input.E2EConfig.GetVariable(KubernetesVersion)))
109113

110114
// Set up a Namespace where to host objects for this spec and create a watcher for the namespace events.
111-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
115+
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
112116
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
113117
})
114118

test/e2e/clusterctl_upgrade.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ type ClusterctlUpgradeSpecInput struct {
100100
// multiple infrastructure providers (ex: CAPD + in-memory) are installed on the cluster as clusterctl will not be
101101
// able to identify the default.
102102
InfrastructureProvider *string
103+
// Allows to inject a function to be run after test namespace is created.
104+
// If not specified, this is a no-op.
105+
PostNamespaceCreated func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string)
103106
// PreWaitForCluster is a function that can be used as a hook to apply extra resources (that cannot be part of the template) in the generated namespace hosting the cluster
104107
// This function is called after applying the cluster template and before waiting for the cluster resources.
105108
PreWaitForCluster func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string, workloadClusterName string)
@@ -214,7 +217,7 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
214217
Expect(os.MkdirAll(input.ArtifactFolder, 0750)).To(Succeed(), "Invalid argument. input.ArtifactFolder can't be created for %s spec", specName)
215218

216219
// Setup a Namespace where to host objects for this spec and create a watcher for the namespace events.
217-
managementClusterNamespace, managementClusterCancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
220+
managementClusterNamespace, managementClusterCancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
218221
managementClusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
219222
})
220223

@@ -346,6 +349,11 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
346349
LogFolder: filepath.Join(input.ArtifactFolder, "clusters", "bootstrap"),
347350
})
348351

352+
if input.PostNamespaceCreated != nil {
353+
log.Logf("Calling postNamespaceCreated for namespace %s", testNamespace.Name)
354+
input.PostNamespaceCreated(managementClusterProxy, testNamespace.Name)
355+
}
356+
349357
By("Creating a test workload cluster")
350358

351359
// NOTE: This workload cluster is used to check the old management cluster works fine.

test/e2e/common.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"sigs.k8s.io/controller-runtime/pkg/client"
3131

3232
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
33+
"sigs.k8s.io/cluster-api/test/e2e/internal/log"
3334
"sigs.k8s.io/cluster-api/test/framework"
3435
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
3536
"sigs.k8s.io/cluster-api/util"
@@ -56,7 +57,7 @@ func Byf(format string, a ...interface{}) {
5657
By(fmt.Sprintf(format, a...))
5758
}
5859

59-
func setupSpecNamespace(ctx context.Context, specName string, clusterProxy framework.ClusterProxy, artifactFolder string) (*corev1.Namespace, context.CancelFunc) {
60+
func setupSpecNamespace(ctx context.Context, specName string, clusterProxy framework.ClusterProxy, artifactFolder string, postNamespaceCreated func(framework.ClusterProxy, string)) (*corev1.Namespace, context.CancelFunc) {
6061
Byf("Creating a namespace for hosting the %q test spec", specName)
6162
namespace, cancelWatches := framework.CreateNamespaceAndWatchEvents(ctx, framework.CreateNamespaceAndWatchEventsInput{
6263
Creator: clusterProxy.GetClient(),
@@ -65,6 +66,11 @@ func setupSpecNamespace(ctx context.Context, specName string, clusterProxy frame
6566
LogFolder: filepath.Join(artifactFolder, "clusters", clusterProxy.GetName()),
6667
})
6768

69+
if postNamespaceCreated != nil {
70+
log.Logf("Calling postNamespaceCreated for namespace %s", namespace.Name)
71+
postNamespaceCreated(clusterProxy, namespace.Name)
72+
}
73+
6874
return namespace, cancelWatches
6975
}
7076

test/e2e/k8s_conformance.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ type K8SConformanceSpecInput struct {
5151

5252
Flavor string
5353
ControlPlaneWaiters clusterctl.ControlPlaneWaiters
54+
55+
// Allows to inject a function to be run after test namespace is created.
56+
// If not specified, this is a no-op.
57+
PostNamespaceCreated func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string)
5458
}
5559

5660
// K8SConformanceSpec implements a spec that creates a cluster and runs Kubernetes conformance suite.
@@ -83,7 +87,7 @@ func K8SConformanceSpec(ctx context.Context, inputGetter func() K8SConformanceSp
8387
Expect(kubetestConfigFilePath).To(BeAnExistingFile(), "%s should be a valid kubetest config file")
8488

8589
// Setup a Namespace where to host objects for this spec and create a watcher for the namespace events.
86-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
90+
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
8791
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
8892
})
8993

test/e2e/kcp_adoption.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ type KCPAdoptionSpecInput struct {
6666
// resources must have the kcp-adoption.step2: "" applied to them.
6767
// If not specified, "kcp-adoption" is used.
6868
Flavor *string
69+
70+
// Allows to inject a function to be run after test namespace is created.
71+
// If not specified, this is a no-op.
72+
PostNamespaceCreated func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string)
6973
}
7074

7175
type ClusterProxy interface {
@@ -98,7 +102,7 @@ func KCPAdoptionSpec(ctx context.Context, inputGetter func() KCPAdoptionSpecInpu
98102
Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion))
99103

100104
// Setup a Namespace where to host objects for this spec and create a watcher for the namespace events.
101-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
105+
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
102106
})
103107

104108
It("Should adopt up-to-date control plane Machines without modification", func() {

test/e2e/kcp_remediations.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ type KCPRemediationSpecInput struct {
8181
// timeout: 10s
8282
// If not specified, "kcp-remediation" is used.
8383
Flavor *string
84+
85+
// Allows to inject a function to be run after test namespace is created.
86+
// If not specified, this is a no-op.
87+
PostNamespaceCreated func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string)
8488
}
8589

8690
// KCPRemediationSpec implements a test that verifies that Machines are remediated by MHC during unhealthy conditions.
@@ -103,7 +107,7 @@ func KCPRemediationSpec(ctx context.Context, inputGetter func() KCPRemediationSp
103107
Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion))
104108

105109
// Setup a Namespace where to host objects for this spec and create a watcher for the namespace events.
106-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
110+
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
107111
})
108112

109113
It("Should replace unhealthy machines", func() {

0 commit comments

Comments
 (0)