@@ -21,12 +21,19 @@ package e2e
21
21
22
22
import (
23
23
"context"
24
+ "log"
25
+ "path/filepath"
24
26
25
27
. "github.com/onsi/ginkgo/v2"
26
28
"github.com/onsi/ginkgo/v2/types"
27
29
. "github.com/onsi/gomega"
28
30
corev1 "k8s.io/api/core/v1"
31
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
32
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
+ "k8s.io/client-go/kubernetes"
34
+ "k8s.io/utils/ptr"
29
35
kubeadmv1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
36
+ capi_e2e "sigs.k8s.io/cluster-api/test/e2e"
30
37
"sigs.k8s.io/cluster-api/test/framework"
31
38
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
32
39
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -116,3 +123,165 @@ func CheckTestBeforeCleanup() {
116
123
}
117
124
Logf ("Cleaning up after \" %s\" spec" , CurrentSpecReport ().FullText ())
118
125
}
126
+
127
+ func setupSpecNamespace (ctx context.Context , namespaceName string , clusterProxy framework.ClusterProxy , artifactFolder string ) (* corev1.Namespace , context.CancelFunc , error ) {
128
+ Byf ("Creating namespace %q for hosting the cluster" , namespaceName )
129
+ Logf ("starting to create namespace for hosting the %q test spec" , namespaceName )
130
+ logPath := filepath .Join (artifactFolder , "clusters" , clusterProxy .GetName ())
131
+ namespace , err := GetNamespace (ctx , clusterProxy .GetClientSet (), namespaceName )
132
+ if err != nil && ! apierrors .IsNotFound (err ) {
133
+ return nil , nil , err
134
+ }
135
+
136
+ // namespace exists wire it up
137
+ if err == nil {
138
+ Byf ("Creating event watcher for existing namespace %q" , namespace .Name )
139
+ watchesCtx , cancelWatches := context .WithCancel (ctx )
140
+ go func () {
141
+ defer GinkgoRecover ()
142
+ framework .WatchNamespaceEvents (watchesCtx , framework.WatchNamespaceEventsInput {
143
+ ClientSet : clusterProxy .GetClientSet (),
144
+ Name : namespace .Name ,
145
+ LogFolder : logPath ,
146
+ })
147
+ }()
148
+
149
+ return namespace , cancelWatches , nil
150
+ }
151
+
152
+ // create and wire up namespace
153
+ namespace , cancelWatches := framework .CreateNamespaceAndWatchEvents (ctx , framework.CreateNamespaceAndWatchEventsInput {
154
+ Creator : clusterProxy .GetClient (),
155
+ ClientSet : clusterProxy .GetClientSet (),
156
+ Name : namespaceName ,
157
+ LogFolder : logPath ,
158
+ })
159
+
160
+ return namespace , cancelWatches , nil
161
+ }
162
+
163
+ // GetNamespace returns a namespace for with a given name
164
+ func GetNamespace (ctx context.Context , clientset * kubernetes.Clientset , name string ) (* corev1.Namespace , error ) {
165
+ opts := metav1.GetOptions {}
166
+ namespace , err := clientset .CoreV1 ().Namespaces ().Get (ctx , name , opts )
167
+ if err != nil {
168
+ log .Printf ("failed trying to get namespace (%s):%s\n " , name , err .Error ())
169
+ return nil , err
170
+ }
171
+
172
+ return namespace , nil
173
+ }
174
+
175
+ func createApplyClusterTemplateInput (specName string , changes ... func (* clusterctl.ApplyClusterTemplateAndWaitInput )) clusterctl.ApplyClusterTemplateAndWaitInput {
176
+ input := clusterctl.ApplyClusterTemplateAndWaitInput {
177
+ ClusterProxy : bootstrapClusterProxy ,
178
+ ConfigCluster : clusterctl.ConfigClusterInput {
179
+ LogFolder : filepath .Join (artifactFolder , "clusters" , bootstrapClusterProxy .GetName ()),
180
+ ClusterctlConfigPath : clusterctlConfigPath ,
181
+ KubeconfigPath : bootstrapClusterProxy .GetKubeconfigPath (),
182
+ InfrastructureProvider : clusterctl .DefaultInfrastructureProvider ,
183
+ Flavor : clusterctl .DefaultFlavor ,
184
+ Namespace : "default" ,
185
+ ClusterName : "cluster" ,
186
+ KubernetesVersion : e2eConfig .GetVariable (capi_e2e .KubernetesVersion ),
187
+ ControlPlaneMachineCount : ptr.To [int64 ](1 ),
188
+ WorkerMachineCount : ptr.To [int64 ](1 ),
189
+ },
190
+ WaitForClusterIntervals : e2eConfig .GetIntervals (specName , "wait-cluster" ),
191
+ WaitForControlPlaneIntervals : e2eConfig .GetIntervals (specName , "wait-control-plane" ),
192
+ WaitForMachineDeployments : e2eConfig .GetIntervals (specName , "wait-worker-nodes" ),
193
+ WaitForMachinePools : e2eConfig .GetIntervals (specName , "wait-machine-pool-nodes" ),
194
+ CNIManifestPath : "" ,
195
+ }
196
+ for _ , change := range changes {
197
+ change (& input )
198
+ }
199
+
200
+ return input
201
+ }
202
+
203
+ func withClusterProxy (proxy framework.ClusterProxy ) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
204
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
205
+ input .ClusterProxy = proxy
206
+ }
207
+ }
208
+
209
+ func withFlavor (flavor string ) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
210
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
211
+ input .ConfigCluster .Flavor = flavor
212
+ }
213
+ }
214
+
215
+ func withNamespace (namespace string ) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
216
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
217
+ input .ConfigCluster .Namespace = namespace
218
+ }
219
+ }
220
+
221
+ func withClusterName (clusterName string ) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
222
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
223
+ input .ConfigCluster .ClusterName = clusterName
224
+ }
225
+ }
226
+
227
+ func withKubernetesVersion (version string ) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
228
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
229
+ input .ConfigCluster .KubernetesVersion = version
230
+ }
231
+ }
232
+
233
+ func withControlPlaneMachineCount (count int64 ) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
234
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
235
+ input .ConfigCluster .ControlPlaneMachineCount = ptr.To [int64 ](count )
236
+ }
237
+ }
238
+
239
+ func withWorkerMachineCount (count int64 ) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
240
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
241
+ input .ConfigCluster .WorkerMachineCount = ptr.To [int64 ](count )
242
+ }
243
+ }
244
+
245
+ func withClusterInterval (specName string , intervalName string ) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
246
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
247
+ if intervalName != "" {
248
+ input .WaitForClusterIntervals = e2eConfig .GetIntervals (specName , intervalName )
249
+ }
250
+ }
251
+ }
252
+
253
+ func withControlPlaneInterval (specName string , intervalName string ) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
254
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
255
+ if intervalName != "" {
256
+ input .WaitForControlPlaneIntervals = e2eConfig .GetIntervals (specName , intervalName )
257
+ }
258
+ }
259
+ }
260
+
261
+ func withMachineDeploymentInterval (specName string , intervalName string ) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
262
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
263
+ if intervalName != "" {
264
+ input .WaitForMachineDeployments = e2eConfig .GetIntervals (specName , intervalName )
265
+ }
266
+ }
267
+ }
268
+
269
+ func withMachinePoolInterval (specName string , intervalName string ) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
270
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
271
+ if intervalName != "" {
272
+ input .WaitForMachinePools = e2eConfig .GetIntervals (specName , intervalName )
273
+ }
274
+ }
275
+ }
276
+
277
+ func withControlPlaneWaiters (waiters clusterctl.ControlPlaneWaiters ) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
278
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
279
+ input .ControlPlaneWaiters = waiters
280
+ }
281
+ }
282
+
283
+ func withPostMachinesProvisioned (postMachinesProvisioned func ()) func (* clusterctl.ApplyClusterTemplateAndWaitInput ) {
284
+ return func (input * clusterctl.ApplyClusterTemplateAndWaitInput ) {
285
+ input .PostMachinesProvisioned = postMachinesProvisioned
286
+ }
287
+ }
0 commit comments