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
/
webhook_configuration_test.go
130 lines (114 loc) · 5.03 KB
/
webhook_configuration_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
package extension_test
import (
"context"
. "code.cloudfoundry.org/eirinix"
catalog "code.cloudfoundry.org/eirinix/testing"
cfakes "code.cloudfoundry.org/eirinix/testing/fakes"
credsgen "code.cloudfoundry.org/quarks-utils/pkg/credsgen"
gfakes "code.cloudfoundry.org/quarks-utils/pkg/credsgen/fakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
var _ = Describe("Webhook configuration implementation", func() {
var (
manager *cfakes.FakeManager
client *cfakes.FakeClient
ctx context.Context
generator *gfakes.FakeGenerator
eirinixcatalog catalog.Catalog
ServiceManager, Manager Manager
eiriniServiceManager, eiriniManager *DefaultExtensionManager
)
failurePolicy := admissionregistrationv1beta1.Fail
BeforeEach(func() {
eirinixcatalog = catalog.NewCatalog()
ServiceManager = eirinixcatalog.SimpleManagerService()
eiriniServiceManager, _ = ServiceManager.(*DefaultExtensionManager)
Manager = eirinixcatalog.SimpleManager()
eiriniManager, _ = Manager.(*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 = "eirini"
eiriniManager.Credsgen = generator
eiriniManager.GenWebHookServer()
eiriniServiceManager.Context = ctx
eiriniServiceManager.KubeManager = manager
eiriniServiceManager.Options.Namespace = "eirini"
eiriniServiceManager.Credsgen = generator
})
Context("With a fake extension with a Host specified", func() {
It("generates correctly services metadata", func() {
w := NewWebhook(eirinixcatalog.SimpleExtension(), eiriniManager)
err := w.RegisterAdmissionWebHook(eiriniManager.WebhookServer, WebhookOptions{ID: "volume", ManagerOptions: ManagerOptions{
FailurePolicy: &failurePolicy,
Namespace: "eirini",
OperatorFingerprint: "eirini-x"}})
Expect(err).ToNot(HaveOccurred())
admissions := eiriniManager.WebhookConfig.GenerateAdmissionWebhook([]MutatingWebhook{w})
Expect(len(admissions)).To(Equal(1))
url := "https://127.0.0.1:90/volume"
Expect(admissions[0].ClientConfig.URL).To(Equal(&url))
Expect(admissions[0].ClientConfig.Service).To(BeNil())
})
})
Context("With a fake extension with a Service", func() {
It("generates correctly services metadata", func() {
w := NewWebhook(eirinixcatalog.SimpleExtension(), eiriniServiceManager)
err := w.RegisterAdmissionWebHook(eiriniManager.WebhookServer, WebhookOptions{ID: "volume", ManagerOptions: ManagerOptions{
FailurePolicy: &failurePolicy,
Namespace: "eirini",
OperatorFingerprint: "eirini-x"}})
Expect(err).ToNot(HaveOccurred())
eiriniServiceManager.GenWebHookServer()
admissions := eiriniServiceManager.WebhookConfig.GenerateAdmissionWebhook([]MutatingWebhook{w})
Expect(len(admissions)).To(Equal(1))
url := "/volume"
port := int32(8001)
Expect(admissions[0].ClientConfig.URL).To(BeNil())
Expect(admissions[0].ClientConfig.Service.Name).To(Equal("extension"))
Expect(admissions[0].ClientConfig.Service.Namespace).To(Equal("cf"))
Expect(admissions[0].ClientConfig.Service.Port).To(Equal(&port))
Expect(admissions[0].ClientConfig.Service.Path).To(Equal(&url))
})
})
Context("with eirini filtering turned on", func() {
It("adds an ObjectSelector to the webhook config", func() {
w := NewWebhook(eirinixcatalog.SimpleExtension(), eiriniServiceManager)
filter := true
err := w.RegisterAdmissionWebHook(eiriniManager.WebhookServer, WebhookOptions{ID: "volume", ManagerOptions: ManagerOptions{
FailurePolicy: &failurePolicy,
Namespace: "eirini",
OperatorFingerprint: "eirini-x",
FilterEiriniApps: &filter,
}})
Expect(err).ToNot(HaveOccurred())
eiriniServiceManager.GenWebHookServer()
admissions := eiriniServiceManager.WebhookConfig.GenerateAdmissionWebhook([]MutatingWebhook{w})
Expect(admissions).To(HaveLen(1))
Expect(admissions[0].ObjectSelector).To(PointTo(Equal(metav1.LabelSelector{
MatchLabels: map[string]string{
LabelSourceType: "APP",
},
})))
})
})
})