-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
environment.go
403 lines (360 loc) · 13.7 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
/*
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.
*/
package envtest
import (
"context"
"fmt"
"net"
"os"
"path"
"path/filepath"
goruntime "runtime"
"strconv"
"strings"
"testing"
"time"
"github.com/onsi/ginkgo"
"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"
"k8s.io/klog/v2/klogr"
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"
kcpv1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
addonv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/builder"
"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"
)
func init() {
klog.InitFlags(nil)
logger := klogr.New()
// Use klog as the internal logger for this envtest environment.
log.SetLogger(logger)
// Additionally force all of the 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(addonv1.AddToScheme(scheme.Scheme))
utilruntime.Must(kcpv1.AddToScheme(scheme.Scheme))
utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme))
}
// RunInput is the input for Run.
type RunInput struct {
M *testing.M
ManagerUncachedObjs []client.Object
SetupIndexes func(ctx context.Context, mgr ctrl.Manager)
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: Test environment creation can be skipped by setting the environment variable `CAPI_DISABLE_TEST_ENV`. This only
// makes sense when executing tests which don't require the test environment, e.g. tests using only the fake client.
func Run(ctx context.Context, input RunInput) int {
if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" {
return input.M.Run()
}
// Bootstrapping test environment
env := new(input.ManagerUncachedObjs...)
if input.SetupIndexes != nil {
input.SetupIndexes(ctx, env.Manager)
}
if input.SetupReconcilers != nil {
input.SetupReconcilers(ctx, env.Manager)
}
// Start the environment.
env.start(ctx)
// Expose the environment.
input.SetupEnv(env)
// Run tests
code := input.M.Run()
// 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
}
// new 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 new(uncachedObjs ...client.Object) *Environment {
// Get the root of the current file to use in CRD paths.
_, filename, _, _ := goruntime.Caller(0) //nolint
root := path.Join(path.Dir(filename), "..", "..")
// Create the test environment.
env := &envtest.Environment{
ErrorIfCRDPathMissing: true,
CRDDirectoryPaths: []string{
filepath.Join(root, "config", "crd", "bases"),
filepath.Join(root, "controlplane", "kubeadm", "config", "crd", "bases"),
filepath.Join(root, "bootstrap", "kubeadm", "config", "crd", "bases"),
},
CRDs: []*apiextensionsv1.CustomResourceDefinition{
builder.GenericBootstrapConfigCRD.DeepCopy(),
builder.GenericBootstrapConfigTemplateCRD.DeepCopy(),
builder.GenericControlPlaneCRD.DeepCopy(),
builder.GenericControlPlaneTemplateCRD.DeepCopy(),
builder.GenericInfrastructureMachineCRD.DeepCopy(),
builder.GenericInfrastructureMachineTemplateCRD.DeepCopy(),
builder.GenericInfrastructureClusterCRD.DeepCopy(),
builder.GenericInfrastructureClusterTemplateCRD.DeepCopy(),
builder.GenericRemediationCRD.DeepCopy(),
builder.GenericRemediationTemplateCRD.DeepCopy(),
},
// initialize webhook here to be able to test the envtest install via webhookOptions
// This should set LocalServingCertDir and LocalServingPort that are used below.
WebhookInstallOptions: initWebhookInstallOptions(),
}
if _, err := env.Start(); err != nil {
err = kerrors.NewAggregate([]error{err, env.Stop()})
panic(err)
}
objs := []client.Object{}
if len(uncachedObjs) > 0 {
objs = append(objs, uncachedObjs...)
}
// Localhost is used on MacOS to avoid Firewall warning popups.
host := "localhost"
if strings.ToLower(os.Getenv("USE_EXISTING_CLUSTER")) == "true" {
// 0.0.0.0 is required on Linux when using kind because otherwise the kube-apiserver running in kind
// is unable to reach the webhook, because the webhook would be only listening on 127.0.0.1.
// Somehow that's not an issue on MacOS.
if goruntime.GOOS == "linux" {
host = "0.0.0.0"
}
}
options := manager.Options{
Scheme: scheme.Scheme,
MetricsBindAddress: "0",
CertDir: env.WebhookInstallOptions.LocalServingCertDir,
Port: env.WebhookInstallOptions.LocalServingPort,
ClientDisableCacheFor: objs,
Host: host,
}
mgr, err := ctrl.NewManager(env.Config, options)
if err != nil {
klog.Fatalf("Failed to start testenv manager: %v", err)
}
// Set minNodeStartupTimeout for Test, so it does not need to be at least 30s
clusterv1.SetMinNodeStartupTimeout(metav1.Duration{Duration: 1 * time.Millisecond})
if err := (&clusterv1.Cluster{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook: %+v", err)
}
if err := (&clusterv1.ClusterClass{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook: %+v", err)
}
if err := (&clusterv1.Machine{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook: %+v", err)
}
if err := (&clusterv1.MachineHealthCheck{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook: %+v", err)
}
if err := (&clusterv1.Machine{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook: %+v", err)
}
if err := (&clusterv1.MachineSet{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook: %+v", err)
}
if err := (&clusterv1.MachineDeployment{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook: %+v", err)
}
if err := (&bootstrapv1.KubeadmConfig{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook: %+v", err)
}
if err := (&bootstrapv1.KubeadmConfigTemplate{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook: %+v", err)
}
if err := (&bootstrapv1.KubeadmConfigTemplateList{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook: %+v", err)
}
if err := (&kcpv1.KubeadmControlPlane{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook: %+v", err)
}
if err := (&addonv1.ClusterResourceSet{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook for crs: %+v", err)
}
if err := (&expv1.MachinePool{}).SetupWebhookWithManager(mgr); err != nil {
klog.Fatalf("unable to create webhook for machinepool: %+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()
e.WaitForWebhooks()
}
// stop stops the test environment.
func (e *Environment) stop() error {
fmt.Println("Stopping the test environment")
e.cancelManager()
return e.env.Stop()
}
// WaitForWebhooks waits for the webhook server to be available.
func (e *Environment) WaitForWebhooks() {
port := e.env.WebhookInstallOptions.LocalServingPort
klog.V(2).Infof("Waiting for webhook port %d to be open prior to running tests", port)
timeout := 1 * time.Second
for {
time.Sleep(1 * time.Second)
conn, err := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", strconv.Itoa(port)), timeout)
if err != nil {
klog.V(2).Infof("Webhook port is not ready, will retry in %v: %s", timeout, err)
continue
}
if err := conn.Close(); err != nil {
klog.V(2).Infof("Closing connection when testing if webhook port is ready failed: %v", err)
}
klog.V(2).Info("Webhook port is now open. Continuing with tests...")
return
}
}
// 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
}
// CreateNamespace creates a new namespace with a generated name.
func (e *Environment) CreateNamespace(ctx context.Context, generateName string) (*corev1.Namespace, error) {
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-", generateName),
Labels: map[string]string{
"testenv/original-name": generateName,
},
},
}
if err := e.Client.Create(ctx, ns); err != nil {
return nil, err
}
return ns, nil
}