-
Notifications
You must be signed in to change notification settings - Fork 157
/
envtest.go
222 lines (186 loc) · 5.54 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
// Copyright (C) 2017 ScyllaDB
package integration
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net"
"path"
"path/filepath"
goruntime "runtime"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/scylladb/go-log"
scyllav1alpha1 "github.com/scylladb/scylla-operator/pkg/api/v1alpha1"
utilyaml "github.com/scylladb/scylla-operator/pkg/util/yaml"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
var (
env *envtest.Environment
)
// Get the root of the current file to use in CRD paths.
func rootPath() string {
_, filename, _, _ := goruntime.Caller(0)
return path.Join(path.Dir(filename), "..", "..", "..")
}
func init() {
// Register the scheme.
utilruntime.Must(scyllav1alpha1.AddToScheme(scheme.Scheme))
root := rootPath()
env = &envtest.Environment{
ErrorIfCRDPathMissing: true,
CRDDirectoryPaths: []string{
filepath.Join(root, "config", "operator", "crd", "bases"),
},
}
}
// TestEnvironment encapsulates a Kubernetes local test environment.
type TestEnvironment struct {
manager.Manager
Client
Config *rest.Config
logger log.Logger
cancel context.CancelFunc
}
type option struct {
pollRetryInterval time.Duration
pollTimeout time.Duration
}
type EnvOption func(*option)
func WithPollRetryInterval(interval time.Duration) func(*option) {
return func(o *option) {
o.pollRetryInterval = interval
}
}
func WithPollTimeout(timeout time.Duration) func(*option) {
return func(o *option) {
o.pollTimeout = timeout
}
}
// NewTestEnvironment creates a new environment spinning up a local api-server.
func NewTestEnvironment(logger log.Logger, options ...EnvOption) (*TestEnvironment, error) {
envOpts := &option{
pollRetryInterval: 200 * time.Millisecond,
pollTimeout: 5 * time.Second,
}
for _, opt := range options {
opt(envOpts)
}
// initialize webhook here to be able to test the envtest install via webhookOptions
// This should set LocalServingCertDir and LocalServingPort that are used below.
if err := initializeWebhookInEnvironment(); err != nil {
return nil, err
}
if _, err := env.Start(); err != nil {
return nil, err
}
mgrOptions := manager.Options{
Scheme: scheme.Scheme,
MetricsBindAddress: "0",
CertDir: env.WebhookInstallOptions.LocalServingCertDir,
Port: env.WebhookInstallOptions.LocalServingPort,
}
mgr, err := ctrl.NewManager(env.Config, mgrOptions)
if err != nil {
return nil, errors.Wrap(err, "start testenv manager")
}
if err := (&scyllav1alpha1.ScyllaCluster{}).SetupWebhookWithManager(mgr); err != nil {
return nil, errors.Wrap(err, "create webhook")
}
return &TestEnvironment{
Manager: mgr,
Client: Client{
Client: mgr.GetClient(),
RetryInterval: envOpts.pollTimeout,
Timeout: envOpts.pollTimeout,
},
Config: mgr.GetConfig(),
logger: logger,
}, nil
}
const (
mutatingWebhookKind = "MutatingWebhookConfiguration"
validatingWebhookKind = "ValidatingWebhookConfiguration"
)
func appendWebhookConfiguration(mutatingWebhooks []runtime.Object, validatingWebhooks []runtime.Object, configyamlFile io.Reader, tag string) ([]runtime.Object, []runtime.Object, error) {
objs, err := utilyaml.ToUnstructured(configyamlFile)
if err != nil {
return nil, nil, errors.Wrap(err, "parse yaml")
}
for i := range objs {
o := objs[i]
if o.GetKind() == mutatingWebhookKind {
mutatingWebhooks = append(mutatingWebhooks, &o)
}
if o.GetKind() == validatingWebhookKind {
validatingWebhooks = append(validatingWebhooks, &o)
}
}
return mutatingWebhooks, validatingWebhooks, err
}
func initializeWebhookInEnvironment() error {
var validatingWebhooks, mutatingWebhooks []runtime.Object
root := rootPath()
configyamlFile, err := ioutil.ReadFile(filepath.Join(root, "config", "operator", "webhook", "manifests.yaml"))
if err != nil {
return errors.Wrap(err, "read core webhook configuration file")
}
mutatingWebhooks, validatingWebhooks, err = appendWebhookConfiguration(mutatingWebhooks, validatingWebhooks, bytes.NewReader(configyamlFile), "config")
if err != nil {
return errors.Wrap(err, "controller webhook config")
}
env.WebhookInstallOptions = envtest.WebhookInstallOptions{
MaxTime: 20 * time.Second,
PollInterval: time.Second,
ValidatingWebhooks: validatingWebhooks,
MutatingWebhooks: mutatingWebhooks,
}
return nil
}
func (t *TestEnvironment) StartManager(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
t.cancel = cancel
return t.Manager.Start(ctx.Done())
}
func (t *TestEnvironment) WaitForWebhooks() {
port := env.WebhookInstallOptions.LocalServingPort
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 {
continue
}
_ = conn.Close()
return
}
}
func (t *TestEnvironment) Stop() error {
t.cancel()
return env.Stop()
}
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
}