forked from spectrocloud/cluster-api-provider-maas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envtest.go
280 lines (246 loc) · 8.97 KB
/
envtest.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
/*
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 helpers
import (
"context"
"fmt"
"net"
"os"
"path"
"path/filepath"
goruntime "runtime"
"strconv"
"strings"
"time"
"github.com/onsi/ginkgo"
"github.com/pkg/errors"
"github.com/spectrocloud/cluster-api-provider-maas/test/helpers/external"
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/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
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"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/cmd/clusterctl/log"
utilyaml "sigs.k8s.io/cluster-api/util/yaml"
)
const (
mutatingWebhookKind = "MutatingWebhookConfiguration"
validatingWebhookKind = "ValidatingWebhookConfiguration"
defaultMutatingWebhookName = "mutating-webhook-configuration"
defaultValidatingWebhookName = "validating-webhook-configuration"
)
var (
root string
)
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))
// Get the root of the current file to use in CRD paths.
_, filename, _, _ := goruntime.Caller(0) //nolint
root = path.Join(path.Dir(filename), "..", "..")
}
type webhookConfiguration struct {
tag string
relativeFilePath string
}
// TestEnvironmentConfiguration encapsulates the interim, mutable configuration of the Kubernetes local test environment.
type TestEnvironmentConfiguration struct {
env *envtest.Environment
webhookConfigurations []webhookConfiguration
}
// TestEnvironment encapsulates a Kubernetes local test environment.
type TestEnvironment struct {
manager.Manager
client.Client
Config *rest.Config
env *envtest.Environment
cancel context.CancelFunc
}
// Cleanup deletes all the given objects.
func (t *TestEnvironment) Cleanup(ctx context.Context, objs ...client.Object) error {
errs := []error{}
for _, o := range objs {
err := t.Client.Delete(ctx, o)
if apierrors.IsNotFound(err) {
continue
}
errs = append(errs, err)
}
return kerrors.NewAggregate(errs)
}
// CreateNamespace creates a new namespace with a generated name.
func (t *TestEnvironment) 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 := t.Client.Create(ctx, ns); err != nil {
return nil, err
}
return ns, nil
}
// NewTestEnvironmentConfiguration creates a new test environment configuration for running tests.
func NewTestEnvironmentConfiguration(crdDirectoryPaths []string) *TestEnvironmentConfiguration {
resolvedCrdDirectoryPaths := make([]string, len(crdDirectoryPaths))
for i, p := range crdDirectoryPaths {
resolvedCrdDirectoryPaths[i] = path.Join(root, p)
}
return &TestEnvironmentConfiguration{
env: &envtest.Environment{
ErrorIfCRDPathMissing: true,
CRDDirectoryPaths: resolvedCrdDirectoryPaths,
CRDs: []*apiextensionsv1.CustomResourceDefinition{
external.TestClusterCRD.DeepCopy(),
external.TestMachineCRD.DeepCopy(),
},
},
}
}
// WithWebhookConfiguration adds the CRD webhook configuration given a named tag and file path for the webhook manifest.
func (t *TestEnvironmentConfiguration) WithWebhookConfiguration(tag string, relativeFilePath string) *TestEnvironmentConfiguration {
t.webhookConfigurations = append(t.webhookConfigurations, webhookConfiguration{tag: tag, relativeFilePath: relativeFilePath})
return t
}
// Build 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 (t *TestEnvironmentConfiguration) Build() (*TestEnvironment, error) {
mutatingWebhooks := make([]*admissionv1.MutatingWebhookConfiguration, 0, len(t.webhookConfigurations))
validatingWebhooks := make([]*admissionv1.ValidatingWebhookConfiguration, 0, len(t.webhookConfigurations))
for _, w := range t.webhookConfigurations {
m, v, err := buildModifiedWebhook(w.tag, w.relativeFilePath)
if err != nil {
return nil, err
}
if m.Webhooks != nil {
// No mutating webhook defined.
mutatingWebhooks = append(mutatingWebhooks, &m)
}
if v.Webhooks != nil {
// No validating webhook defined.
validatingWebhooks = append(validatingWebhooks, &v)
}
}
t.env.WebhookInstallOptions = envtest.WebhookInstallOptions{
MaxTime: 20 * time.Second,
PollInterval: time.Second,
ValidatingWebhooks: validatingWebhooks,
MutatingWebhooks: mutatingWebhooks,
}
if _, err := t.env.Start(); err != nil {
panic(err)
}
options := manager.Options{
Scheme: scheme.Scheme,
MetricsBindAddress: "0",
CertDir: t.env.WebhookInstallOptions.LocalServingCertDir,
Port: t.env.WebhookInstallOptions.LocalServingPort,
}
mgr, err := ctrl.NewManager(t.env.Config, options)
if err != nil {
klog.Fatalf("Failed to start testenv manager: %v", err)
}
return &TestEnvironment{
Manager: mgr,
Client: mgr.GetClient(),
Config: mgr.GetConfig(),
env: t.env,
}, nil
}
func buildModifiedWebhook(tag string, relativeFilePath string) (admissionv1.MutatingWebhookConfiguration, admissionv1.ValidatingWebhookConfiguration, error) {
var mutatingWebhook admissionv1.MutatingWebhookConfiguration
var validatingWebhook admissionv1.ValidatingWebhookConfiguration
data, err := os.ReadFile(filepath.Clean(filepath.Join(root, relativeFilePath)))
if err != nil {
return mutatingWebhook, validatingWebhook, errors.Wrap(err, "failed to read webhook configuration file")
}
objs, err := utilyaml.ToUnstructured(data)
if err != nil {
return mutatingWebhook, validatingWebhook, errors.Wrap(err, "failed to parse yaml")
}
for i := range objs {
o := objs[i]
if o.GetKind() == mutatingWebhookKind {
// update the name in metadata
if o.GetName() == defaultMutatingWebhookName {
o.SetName(strings.Join([]string{defaultMutatingWebhookName, "-", tag}, ""))
if err := scheme.Scheme.Convert(&o, &mutatingWebhook, nil); err != nil {
klog.Fatalf("failed to convert MutatingWebhookConfiguration %s", o.GetName())
}
}
}
if o.GetKind() == validatingWebhookKind {
// update the name in metadata
if o.GetName() == defaultValidatingWebhookName {
o.SetName(strings.Join([]string{defaultValidatingWebhookName, "-", tag}, ""))
if err := scheme.Scheme.Convert(&o, &validatingWebhook, nil); err != nil {
klog.Fatalf("failed to convert ValidatingWebhookConfiguration %s", o.GetName())
}
}
}
}
return mutatingWebhook, validatingWebhook, nil
}
// StartManager starts the test controller against the local API server.
func (t *TestEnvironment) StartManager(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
t.cancel = cancel
return t.Manager.Start(ctx)
}
// WaitForWebhooks will not return until the webhook port is open.
func (t *TestEnvironment) WaitForWebhooks() {
port := t.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
}
conn.Close()
klog.V(2).Info("Webhook port is now open. Continuing with tests...")
return
}
}
// Stop stops the test environment.
func (t *TestEnvironment) Stop() error {
t.cancel()
return t.env.Stop()
}