This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmanager_test.go
389 lines (325 loc) · 13.8 KB
/
manager_test.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
package extension_test
import (
"context"
"encoding/base64"
"fmt"
"os"
"path/filepath"
"time"
credsgen "code.cloudfoundry.org/quarks-utils/pkg/credsgen"
gfakes "code.cloudfoundry.org/quarks-utils/pkg/credsgen/fakes"
. "code.cloudfoundry.org/eirinix"
catalog "code.cloudfoundry.org/eirinix/testing"
cfakes "code.cloudfoundry.org/eirinix/testing/fakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/afero"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes/scheme"
watchtools "k8s.io/client-go/tools/watch"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
crc "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
var _ = Describe("Extension Manager", func() {
var (
manager *cfakes.FakeManager
client *cfakes.FakeClient
ctx context.Context
generator *gfakes.FakeGenerator
eirinixcatalog catalog.Catalog
ServiceManager, Manager Manager
eiriniServiceManager, eiriniManager *DefaultExtensionManager
)
BeforeEach(func() {
eirinixcatalog = catalog.NewCatalog()
Manager = eirinixcatalog.SimpleManager()
eiriniManager, _ = Manager.(*DefaultExtensionManager)
ServiceManager = eirinixcatalog.SimpleManagerService()
eiriniServiceManager, _ = ServiceManager.(*DefaultExtensionManager)
AddToScheme(scheme.Scheme)
client = &cfakes.FakeClient{}
restMapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{})
restMapper.Add(schema.GroupVersionKind{Group: "", Kind: "Pod", Version: "v1"}, meta.RESTScopeNamespace)
manager = &cfakes.FakeManager{}
manager.GetSchemeReturns(scheme.Scheme)
manager.GetClientReturns(client)
manager.GetRESTMapperReturns(restMapper)
manager.GetWebhookServerReturns(&webhook.Server{})
generator = &gfakes.FakeGenerator{}
generator.GenerateCertificateReturns(credsgen.Certificate{Certificate: []byte("thecert")}, nil)
ctx = catalog.NewContext()
eiriniManager.Context = ctx
eiriniManager.KubeManager = manager
eiriniManager.Options.Namespace = "default"
eiriniManager.Credsgen = generator
eiriniManager.Options.WatcherStartRV = "1"
eiriniServiceManager.Context = ctx
eiriniServiceManager.KubeManager = manager
eiriniServiceManager.Options.Namespace = "default"
eiriniServiceManager.Credsgen = generator
})
Context("DefaultExtensionManager", func() {
It("satisfies the Manager interface", func() {
Expect(Manager.GetManagerOptions().Namespace).To(Equal("default"))
Expect(Manager.GetManagerOptions().Host).To(Equal("127.0.0.1"))
Expect(Manager.GetManagerOptions().Port).To(Equal(int32(90)))
defaultPolicy := admissionregistrationv1beta1.Fail
Expect(Manager.GetManagerOptions().FailurePolicy).To(Equal(&defaultPolicy))
Expect(Manager.GetManagerOptions().OperatorFingerprint).To(Equal("eirini-x"))
Expect(Manager.GetManagerOptions().KubeConfig).To(Equal(""))
Expect(Manager.GetManagerOptions().Logger).NotTo(Equal(nil))
Expect(*Manager.GetManagerOptions().FilterEiriniApps).To(BeTrue())
Expect(Manager.GetLogger()).ToNot(BeNil())
Expect(Manager.ListExtensions()).To(BeEmpty())
})
It("provides option setter", func() {
o := Manager.GetManagerOptions()
o.Namespace = "test"
Manager.SetManagerOptions(o)
Expect(Manager.GetManagerOptions().Namespace).To(Equal("test"))
})
It("Setups correctly the operator structures", func() {
err := eiriniManager.OperatorSetup()
Expect(err).ToNot(HaveOccurred())
Expect(eiriniManager.WebhookServer.Port).To(Equal(int(eiriniManager.Options.Port)))
Expect(eiriniManager.WebhookServer.Host).To(Equal(eiriniManager.Options.Host))
})
It("called from the interface fails to start with no kube connection", func() {
_, err := Manager.GetKubeConnection()
Expect(err).ToNot(BeNil())
err = Manager.Start()
Expect(err).ToNot(BeNil())
})
})
Context("if there is no cert secret yet", func() {
It("generates and persists the certificates on disk and in a secret", func() {
Expect(eiriniManager.Options.SetupCertificateName).To(Equal("eirini-x-setupcertificate"))
eiriniManager.Options.SetupCertificateName = "test-setupcert"
os.RemoveAll(filepath.Join(os.TempDir(), eiriniManager.Options.SetupCertificateName))
defer os.RemoveAll(filepath.Join(os.TempDir(), eiriniManager.Options.SetupCertificateName))
Expect(eiriniManager.WebhookServer).To(BeNil())
Expect(afero.Exists(afero.NewOsFs(), filepath.Join(os.TempDir(), eiriniManager.Options.SetupCertificateName, "tls.key"))).To(BeFalse())
err := eiriniManager.OperatorSetup()
Expect(err).ToNot(HaveOccurred())
err = eiriniManager.LoadExtensions()
Expect(err).ToNot(HaveOccurred())
Expect(eiriniManager.WebhookServer.CertDir).To(Equal(filepath.Join(os.TempDir(), eiriniManager.Options.SetupCertificateName)))
// Expect(eiriniManager.WebhookServer.BootstrapOptions.MutatingWebhookConfigName).To(Equal("eirini-x-mutating-hook-default"))
Expect(eiriniManager.WebhookConfig.CertDir).To(Equal(eiriniManager.WebhookServer.CertDir))
// Expect(eiriniManager.WebhookConfig.ConfigName).To(Equal(eiriniManager.WebhookServer.BootstrapOptions.MutatingWebhookConfigName))
Expect(afero.Exists(afero.NewOsFs(), filepath.Join(os.TempDir(), eiriniManager.Options.SetupCertificateName, "tls.key"))).To(BeTrue())
Expect(generator.GenerateCertificateCallCount()).To(Equal(2)) // Generate CA and certificate
Expect(client.CreateCallCount()).To(Equal(2)) // Persist secret and the webhook config
})
})
It("sets the operator namespace label", func() {
client.UpdateCalls(func(_ context.Context, object runtime.Object, _ ...crc.UpdateOption) error {
ns := object.(*unstructured.Unstructured)
labels := ns.GetLabels()
Expect(labels["eirini-x-ns"]).To(Equal(eiriniManager.Options.Namespace))
return nil
})
err := eiriniManager.OperatorSetup()
Expect(err).ToNot(HaveOccurred())
err = eiriniManager.LoadExtensions()
Expect(err).ToNot(HaveOccurred())
})
It("doesn't set the operator namespace label if no namespace if defined", func() {
eiriniManager.Options.Namespace = ""
err := eiriniManager.OperatorSetup()
Expect(err).ToNot(HaveOccurred())
err = eiriniManager.LoadExtensions()
Expect(err).ToNot(HaveOccurred())
Expect(client.UpdateCallCount()).To(Equal(0))
})
Context("if there is a persisted cert secret already", func() {
BeforeEach(func() {
secret := &unstructured.Unstructured{
Object: map[string]interface{}{
"metadata": map[string]interface{}{
"name": "eirinix",
"namespace": eiriniManager.Options.Namespace,
},
"data": map[string]interface{}{
"certificate": base64.StdEncoding.EncodeToString([]byte("the-cert")),
"private_key": base64.StdEncoding.EncodeToString([]byte("the-key")),
"ca_certificate": base64.StdEncoding.EncodeToString([]byte("the-ca-cert")),
"ca_private_key": base64.StdEncoding.EncodeToString([]byte("the-ca-key")),
},
},
}
client.GetCalls(func(context context.Context, nn types.NamespacedName, object runtime.Object) error {
switch object.(type) {
case *unstructured.Unstructured:
secret.DeepCopyInto(object.(*unstructured.Unstructured))
return nil
}
return apierrors.NewNotFound(schema.GroupResource{}, nn.Name)
})
})
It("does not overwrite the existing secret", func() {
err := eiriniManager.OperatorSetup()
Expect(err).ToNot(HaveOccurred())
err = eiriniManager.LoadExtensions()
Expect(err).ToNot(HaveOccurred())
Expect(client.CreateCallCount()).To(Equal(1)) // webhook config
Expect(generator.GenerateCertificateCallCount()).To(Equal(0)) // Generate CA and certificate
})
It("generates the webhook configuration", func() {
client.CreateCalls(func(context context.Context, object runtime.Object, _ ...crc.CreateOption) error {
config := object.(*admissionregistrationv1beta1.MutatingWebhookConfiguration)
Expect(config.Name).To(Equal("eirini-x-mutating-hook"))
Expect(len(config.Webhooks)).To(Equal(1))
wh := config.Webhooks[0]
Expect(wh.Name).To(Equal("0.eirini-x.org"))
Expect(*wh.ClientConfig.URL).To(Equal(fmt.Sprintf("https://%s:%d/0", eiriniManager.Options.Host, eiriniManager.Options.Port)))
Expect(wh.ClientConfig.CABundle).To(ContainSubstring("the-ca-cert"))
Expect(*wh.FailurePolicy).To(Equal(admissionregistrationv1beta1.Fail))
return nil
})
err := eiriniManager.OperatorSetup()
Expect(err).ToNot(HaveOccurred())
eiriniManager.AddExtension(eirinixcatalog.SimpleExtension())
err = eiriniManager.LoadExtensions()
Expect(err).ToNot(HaveOccurred())
Expect(Manager.ListExtensions()).ToNot(BeEmpty())
})
})
Context("Watchers", func() {
w := eirinixcatalog.SimpleWatcher()
BeforeEach(func() {
w = eirinixcatalog.SimpleWatcher()
})
It("Registers new watchers correctly", func() {
eiriniManager.AddWatcher(w)
Expect(len(eiriniManager.ListWatchers())).To(Equal(1))
eiriniManager.AddWatcher(w)
Expect(len(eiriniManager.ListWatchers())).To(Equal(2))
})
It("Handles events correctly", func() {
eiriniManager.AddWatcher(w)
eiriniManager.HandleEvent(watch.Event{Type: watch.EventType("test")})
sw, ok := w.(*catalog.SimpleWatch)
Expect(ok).To(Equal(true))
Expect(len(sw.Handled)).To(Equal(1))
Expect(string(sw.Handled[0].Type)).To(Equal("test"))
eiriniManager.SetKubeConnection(&rest.Config{})
eiriniManager.HandleEvent(watch.Event{Type: watch.EventType("1")})
Expect(string(sw.Handled[1].Type)).To(Equal("1"))
})
It("Generates the watcher correctly if filtering eirini apps", func() {
Expect(*eiriniManager.Options.FilterEiriniApps).To(Equal(true))
fakeCorev1 := &cfakes.FakeCoreV1Interface{}
fakePod := &cfakes.FakePodInterface{}
fakeWatch := &cfakes.FakeInterface{}
var label string
fakePod.WatchCalls(func(ctx context.Context, m metav1.ListOptions) (watch.Interface, error) {
label = m.LabelSelector
return fakeWatch, nil
})
fakeCorev1.PodsCalls(func(s string) corev1client.PodInterface { return fakePod })
w, err := eiriniManager.GenWatcher(fakeCorev1)
cw, ok := w.(*watchtools.RetryWatcher)
// Give the watcher a chance to get to sending events (blocking)
time.Sleep(10 * time.Millisecond)
cw.Stop()
select {
case <-cw.Done():
break
case <-time.After(10 * time.Millisecond):
}
// RetryWatcher result channel should be closed
_, ok = <-cw.ResultChan()
Expect(ok).ToNot(BeTrue())
Expect(label).To(Equal(LabelSourceType + "=APP"))
Expect(err).ToNot(HaveOccurred())
})
It("Generates the watcher correctly if not filtering eirini apps", func() {
Expect(*eiriniManager.Options.FilterEiriniApps).To(Equal(true))
filtering := false
eiriniManager.Options.FilterEiriniApps = &filtering
fakeCorev1 := &cfakes.FakeCoreV1Interface{}
fakePod := &cfakes.FakePodInterface{}
fakeWatch := &cfakes.FakeInterface{}
label := "foo"
fakePod.WatchCalls(func(ctx context.Context, m metav1.ListOptions) (watch.Interface, error) {
label = m.LabelSelector
return fakeWatch, nil
})
fakeCorev1.PodsCalls(func(s string) corev1client.PodInterface { return fakePod })
w, err := eiriniManager.GenWatcher(fakeCorev1)
cw, ok := w.(*watchtools.RetryWatcher)
// Give the watcher a chance to get to sending events (blocking)
time.Sleep(10 * time.Millisecond)
cw.Stop()
select {
case <-cw.Done():
break
case <-time.After(10 * time.Millisecond):
}
// RetryWatcher result channel should be closed
_, ok = <-cw.ResultChan()
Expect(ok).ToNot(BeTrue())
Expect(label).To(Equal(""))
Expect(err).ToNot(HaveOccurred())
})
})
Context("Extensions with services", func() {
It("doesn't fail", func() {
Expect(eiriniServiceManager.Options.Port).To(Equal(int32(8001)))
eiriniServiceManager.AddExtension(eirinixcatalog.SimpleExtension())
err := eiriniServiceManager.OperatorSetup()
Expect(err).ToNot(HaveOccurred())
err = eiriniServiceManager.LoadExtensions()
Expect(err).ToNot(HaveOccurred())
Expect(len(eiriniServiceManager.ListExtensions())).To(Equal(1))
})
})
Context("Reconcilers", func() {
r := eirinixcatalog.SimpleReconciler()
BeforeEach(func() {
r = eirinixcatalog.SimpleReconciler()
})
It("Registers new reconcilers correctly", func() {
eiriniManager.AddReconciler(r)
Expect(len(eiriniManager.ListReconcilers())).To(Equal(1))
eiriniManager.AddReconciler(r)
Expect(len(eiriniManager.ListReconcilers())).To(Equal(2))
})
})
Context("Registering different Extensions types from the same API", func() {
r := eirinixcatalog.SimpleReconciler()
w := eirinixcatalog.SimpleWatcher()
e := eirinixcatalog.SimpleExtension()
foo := struct{ Bar string }{}
BeforeEach(func() {
r = eirinixcatalog.SimpleReconciler()
w = eirinixcatalog.SimpleWatcher()
e = eirinixcatalog.SimpleExtension()
foo = struct{ Bar string }{}
})
It("Registers correctly with AddExtension", func() {
err := eiriniManager.AddExtension(e)
Expect(err).ToNot(HaveOccurred())
Expect(len(eiriniManager.ListExtensions())).To(Equal(1))
err = eiriniManager.AddExtension(r)
Expect(err).ToNot(HaveOccurred())
Expect(len(eiriniManager.ListReconcilers())).To(Equal(1))
err = eiriniManager.AddExtension(w)
Expect(err).ToNot(HaveOccurred())
Expect(len(eiriniManager.ListWatchers())).To(Equal(1))
err = eiriniManager.AddExtension(foo)
Expect(err).To(HaveOccurred())
})
})
})