-
Notifications
You must be signed in to change notification settings - Fork 4
/
environment.go
415 lines (373 loc) · 12.6 KB
/
environment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//nolint:gochecknoinits,lll // Code is copied from upstream.
package envtest
import (
"context"
"fmt"
"maps"
"os"
"testing"
"time"
"github.com/onsi/ginkgo/v2"
"github.com/pkg/errors"
admissionv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kerrors "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
"sigs.k8s.io/cluster-api/cmd/clusterctl/log"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1"
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
"sigs.k8s.io/cluster-api/util/kubeconfig"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/internal/test/builder"
)
func init() {
logger := klog.Background()
// Use klog as the internal logger for this envtest environment.
log.SetLogger(logger)
// Additionally force all controllers to use the Ginkgo logger.
ctrl.SetLogger(logger)
// Add logger for ginkgo.
klog.SetOutput(ginkgo.GinkgoWriter)
// Calculate the scheme.
utilruntime.Must(apiextensionsv1.AddToScheme(scheme.Scheme))
utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme))
utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme))
utilruntime.Must(bootstrapv1.AddToScheme(scheme.Scheme))
utilruntime.Must(expv1.AddToScheme(scheme.Scheme))
utilruntime.Must(addonsv1.AddToScheme(scheme.Scheme))
utilruntime.Must(controlplanev1.AddToScheme(scheme.Scheme))
utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme))
utilruntime.Must(runtimev1.AddToScheme(scheme.Scheme))
utilruntime.Must(ipamv1.AddToScheme(scheme.Scheme))
}
// RunInput is the input for Run.
type RunInput struct {
M *testing.M
ManagerUncachedObjs []client.Object
SetupReconcilers func(ctx context.Context, mgr ctrl.Manager)
SetupEnv func(e *Environment)
}
// Run executes the tests of the given testing.M in a test environment.
// Note: The environment will be created in this func and should not be created before. This func takes a *Environment
//
// because our tests require access to the *Environment. We use this field to make the created Environment available
// to the consumer.
//
// Note: It's possible to write a kubeconfig for the test environment to a file by setting `TEST_ENV_KUBECONFIG`.
// Note: It's possible to skip stopping the test env after the tests have been run by setting `TEST_ENV_SKIP_STOP`
// to a non-empty value.
func Run(ctx context.Context, input RunInput) int {
// Bootstrapping test environment
env := newEnvironment(input.ManagerUncachedObjs...)
if input.SetupReconcilers != nil {
input.SetupReconcilers(ctx, env.Manager)
}
// Start the environment.
env.start(ctx)
if kubeconfigPath := os.Getenv("TEST_ENV_KUBECONFIG"); kubeconfigPath != "" {
klog.Infof("Writing test env kubeconfig to %q", kubeconfigPath)
config := kubeconfig.FromEnvTestConfig(env.Config, &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: "test"},
})
if err := os.WriteFile(kubeconfigPath, config, 0o600); err != nil {
panic(errors.Wrapf(err, "failed to write the test env kubeconfig"))
}
}
// Expose the environment.
input.SetupEnv(env)
// Run tests
code := input.M.Run()
if skipStop := os.Getenv("TEST_ENV_SKIP_STOP"); skipStop != "" {
klog.Info("Skipping test env stop as TEST_ENV_SKIP_STOP is set")
return code
}
// Tearing down the test environment
if err := env.stop(); err != nil {
panic(fmt.Sprintf("Failed to stop the test environment: %v", err))
}
return code
}
var cacheSyncBackoff = wait.Backoff{
Duration: 100 * time.Millisecond,
Factor: 1.5,
Steps: 8,
Jitter: 0.4,
}
// Environment encapsulates a Kubernetes local test environment.
type Environment struct {
manager.Manager
client.Client
Config *rest.Config
env *envtest.Environment
cancelManager context.CancelFunc
}
// newEnvironment creates a new environment spinning up a local api-server.
//
// This function should be called only once for each package you're running tests within,
// usually the environment is initialized in a suite_test.go file within a `BeforeSuite` ginkgo block.
func newEnvironment(uncachedObjs ...client.Object) *Environment {
// Create the test environment.
env := &envtest.Environment{
ErrorIfCRDPathMissing: true,
CRDDirectoryPaths: ExternalCRDDirectoryPaths(),
CRDs: []*apiextensionsv1.CustomResourceDefinition{
builder.GenericBootstrapConfigCRD.DeepCopy(),
builder.GenericBootstrapConfigTemplateCRD.DeepCopy(),
builder.GenericControlPlaneCRD.DeepCopy(),
builder.GenericControlPlaneTemplateCRD.DeepCopy(),
builder.GenericInfrastructureMachineCRD.DeepCopy(),
builder.GenericInfrastructureMachineTemplateCRD.DeepCopy(),
builder.GenericInfrastructureMachinePoolCRD.DeepCopy(),
builder.GenericInfrastructureMachinePoolTemplateCRD.DeepCopy(),
builder.GenericInfrastructureClusterCRD.DeepCopy(),
builder.GenericInfrastructureClusterTemplateCRD.DeepCopy(),
builder.GenericRemediationCRD.DeepCopy(),
builder.GenericRemediationTemplateCRD.DeepCopy(),
builder.TestInfrastructureClusterTemplateCRD.DeepCopy(),
builder.TestInfrastructureClusterCRD.DeepCopy(),
builder.TestInfrastructureMachineTemplateCRD.DeepCopy(),
builder.TestInfrastructureMachinePoolCRD.DeepCopy(),
builder.TestInfrastructureMachinePoolTemplateCRD.DeepCopy(),
builder.TestInfrastructureMachineCRD.DeepCopy(),
builder.TestBootstrapConfigTemplateCRD.DeepCopy(),
builder.TestBootstrapConfigCRD.DeepCopy(),
builder.TestControlPlaneTemplateCRD.DeepCopy(),
builder.TestControlPlaneCRD.DeepCopy(),
},
}
if _, err := env.Start(); err != nil {
err = kerrors.NewAggregate([]error{err, env.Stop()})
panic(err)
}
options := manager.Options{
Scheme: scheme.Scheme,
Metrics: metricsserver.Options{
BindAddress: "0",
},
Client: client.Options{
Cache: &client.CacheOptions{
DisableFor: uncachedObjs,
},
},
}
mgr, err := ctrl.NewManager(env.Config, options)
if err != nil {
klog.Fatalf("Failed to start testenv manager: %v", err)
}
return &Environment{
Manager: mgr,
Client: mgr.GetClient(),
Config: mgr.GetConfig(),
env: env,
}
}
// start starts the manager.
func (e *Environment) start(ctx context.Context) {
ctx, cancel := context.WithCancel(ctx)
e.cancelManager = cancel
go func() {
fmt.Println("Starting the test environment manager")
if err := e.Manager.Start(ctx); err != nil {
panic(fmt.Sprintf("Failed to start the test environment manager: %v", err))
}
}()
<-e.Manager.Elected()
}
// stop stops the test environment.
func (e *Environment) stop() error {
fmt.Println("Stopping the test environment")
e.cancelManager()
return e.env.Stop()
}
// CreateKubeconfigSecret generates a new Kubeconfig secret from the envtest config.
func (e *Environment) CreateKubeconfigSecret(
ctx context.Context,
cluster *clusterv1.Cluster,
) error {
return e.Create(
ctx,
kubeconfig.GenerateSecret(cluster, kubeconfig.FromEnvTestConfig(e.Config, cluster)),
)
}
// Cleanup deletes all the given objects.
func (e *Environment) Cleanup(ctx context.Context, objs ...client.Object) error {
errs := []error{}
for _, o := range objs {
err := e.Client.Delete(ctx, o)
if apierrors.IsNotFound(err) {
continue
}
errs = append(errs, err)
}
return kerrors.NewAggregate(errs)
}
// CleanupAndWait deletes all the given objects and waits for the cache to be updated accordingly.
//
// NOTE: Waiting for the cache to be updated helps in preventing test flakes due to the cache sync delays.
func (e *Environment) CleanupAndWait(ctx context.Context, objs ...client.Object) error {
if err := e.Cleanup(ctx, objs...); err != nil {
return err
}
// Makes sure the cache is updated with the deleted object
errs := []error{}
for _, o := range objs {
// Ignoring namespaces because in testenv the namespace cleaner is not running.
if o.GetObjectKind().
GroupVersionKind().
GroupKind() ==
corev1.SchemeGroupVersion.WithKind("Namespace").
GroupKind() {
continue
}
oCopy := o.DeepCopyObject().(client.Object)
key := client.ObjectKeyFromObject(o)
err := wait.ExponentialBackoff(
cacheSyncBackoff,
func() (done bool, err error) {
if err := e.Get(ctx, key, oCopy); err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
return false, err
}
return false, nil
})
errs = append(
errs,
errors.Wrapf(
err,
"key %s, %s is not being deleted from the testenv client cache",
o.GetObjectKind().GroupVersionKind().String(),
key,
),
)
}
return kerrors.NewAggregate(errs)
}
// CreateAndWait creates the given object and waits for the cache to be updated accordingly.
//
// NOTE: Waiting for the cache to be updated helps in preventing test flakes due to the cache sync delays.
func (e *Environment) CreateAndWait(
ctx context.Context,
obj client.Object,
opts ...client.CreateOption,
) error {
if err := e.Client.Create(ctx, obj, opts...); err != nil {
return err
}
// Makes sure the cache is updated with the new object
objCopy := obj.DeepCopyObject().(client.Object)
key := client.ObjectKeyFromObject(obj)
if err := wait.ExponentialBackoff(
cacheSyncBackoff,
func() (done bool, err error) {
if err := e.Get(ctx, key, objCopy); err != nil {
if apierrors.IsNotFound(err) {
return false, nil
}
return false, err
}
return true, nil
}); err != nil {
return errors.Wrapf(
err,
"object %s, %s is not being added to the testenv client cache",
obj.GetObjectKind().GroupVersionKind().String(),
key,
)
}
return nil
}
// PatchAndWait creates or updates the given object using server-side apply and waits for the cache to be updated accordingly.
//
// NOTE: Waiting for the cache to be updated helps in preventing test flakes due to the cache sync delays.
func (e *Environment) PatchAndWait(
ctx context.Context,
obj client.Object,
opts ...client.PatchOption,
) error {
key := client.ObjectKeyFromObject(obj)
objCopy := obj.DeepCopyObject().(client.Object)
if err := e.GetAPIReader().Get(ctx, key, objCopy); err != nil {
if !apierrors.IsNotFound(err) {
return err
}
}
// Store old resource version, empty string if not found.
oldResourceVersion := objCopy.GetResourceVersion()
if err := e.Client.Patch(ctx, obj, client.Apply, opts...); err != nil {
return err
}
// Makes sure the cache is updated with the new object
if err := wait.ExponentialBackoff(
cacheSyncBackoff,
func() (done bool, err error) {
if err := e.Get(ctx, key, objCopy); err != nil {
if apierrors.IsNotFound(err) {
return false, nil
}
return false, err
}
if objCopy.GetResourceVersion() == oldResourceVersion {
return false, nil
}
return true, nil
}); err != nil {
return errors.Wrapf(
err,
"object %s, %s is not being added to or did not get updated in the testenv client cache",
obj.GetObjectKind().GroupVersionKind().String(),
key,
)
}
return nil
}
// CreateNamespace creates a new namespace with a generated name.
func (e *Environment) CreateNamespace(
ctx context.Context,
generateName string,
additionalLabels map[string]string,
) (*corev1.Namespace, error) {
labels := map[string]string{
"testenv/original-name": generateName,
}
maps.Copy(labels, additionalLabels)
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-", generateName),
Labels: labels,
},
}
if err := e.Client.Create(ctx, ns); err != nil {
return nil, err
}
return ns, nil
}