Skip to content

Commit 44265a2

Browse files
committed
Webhook support in envtest
1 parent 066ff64 commit 44265a2

File tree

8 files changed

+693
-3
lines changed

8 files changed

+693
-3
lines changed

pkg/envtest/envtest_suite_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121

2222
. "github.com/onsi/ginkgo"
2323
. "github.com/onsi/gomega"
24+
admissionv1beta1 "k8s.io/api/admissionregistration/v1beta1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2426
logf "sigs.k8s.io/controller-runtime/pkg/log"
2527
"sigs.k8s.io/controller-runtime/pkg/log/zap"
2628
)
@@ -35,12 +37,61 @@ var env *Environment
3537
var _ = BeforeSuite(func(done Done) {
3638
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
3739
env = &Environment{}
40+
initializeWebhookInEnvironment()
3841
_, err := env.Start()
3942
Expect(err).NotTo(HaveOccurred())
4043

4144
close(done)
4245
}, StartTimeout)
4346

47+
func initializeWebhookInEnvironment() {
48+
namespacedScope := admissionv1beta1.NamespacedScope
49+
failedType := admissionv1beta1.Fail
50+
equivalentType := admissionv1beta1.Equivalent
51+
noSideEffects := admissionv1beta1.SideEffectClassNone
52+
webhookPath := "/failing"
53+
54+
env.WebhookInstallOptions = WebhookInstallOptions{
55+
ValidatingWebhooks: []*admissionv1beta1.ValidatingWebhookConfiguration{
56+
{
57+
ObjectMeta: metav1.ObjectMeta{
58+
Name: "deployment-validation-webhook-config",
59+
},
60+
TypeMeta: metav1.TypeMeta{
61+
Kind: "ValidatingWebhookConfiguration",
62+
APIVersion: "admissionregistration.k8s.io/v1beta1",
63+
},
64+
Webhooks: []admissionv1beta1.ValidatingWebhook{
65+
{
66+
Name: "deployment-validation.kubebuilder.io",
67+
Rules: []admissionv1beta1.RuleWithOperations{
68+
{
69+
Operations: []admissionv1beta1.OperationType{"CREATE", "UPDATE"},
70+
Rule: admissionv1beta1.Rule{
71+
APIGroups: []string{"apps"},
72+
APIVersions: []string{"v1"},
73+
Resources: []string{"deployments"},
74+
Scope: &namespacedScope,
75+
},
76+
},
77+
},
78+
FailurePolicy: &failedType,
79+
MatchPolicy: &equivalentType,
80+
SideEffects: &noSideEffects,
81+
ClientConfig: admissionv1beta1.WebhookClientConfig{
82+
Service: &admissionv1beta1.ServiceReference{
83+
Name: "deployment-validation-service",
84+
Namespace: "default",
85+
Path: &webhookPath,
86+
},
87+
},
88+
},
89+
},
90+
},
91+
},
92+
}
93+
}
94+
4495
var _ = AfterSuite(func(done Done) {
4596
Expect(env.Stop()).NotTo(HaveOccurred())
4697

pkg/envtest/envtest_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,14 +777,14 @@ var _ = Describe("Test", func() {
777777

778778
Describe("Start", func() {
779779
It("should raise an error on invalid dir when flag is enabled", func(done Done) {
780-
env = &Environment{ErrorIfCRDPathMissing: true, CRDDirectoryPaths: []string{invalidDirectory}}
780+
env := &Environment{ErrorIfCRDPathMissing: true, CRDDirectoryPaths: []string{invalidDirectory}}
781781
_, err := env.Start()
782782
Expect(err).To(HaveOccurred())
783783
close(done)
784784
}, 30)
785785

786786
It("should not raise an error on invalid dir when flag is disabled", func(done Done) {
787-
env = &Environment{ErrorIfCRDPathMissing: false, CRDDirectoryPaths: []string{invalidDirectory}}
787+
env := &Environment{ErrorIfCRDPathMissing: false, CRDDirectoryPaths: []string{invalidDirectory}}
788788
_, err := env.Start()
789789
Expect(err).NotTo(HaveOccurred())
790790
close(done)

pkg/envtest/server.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ var DefaultKubeAPIServerFlags = []string{
8282
"--insecure-port={{ if .URL }}{{ .URL.Port }}{{ end }}",
8383
"--insecure-bind-address={{ if .URL }}{{ .URL.Hostname }}{{ end }}",
8484
"--secure-port={{ if .SecurePort }}{{ .SecurePort }}{{ end }}",
85-
"--admission-control=AlwaysAdmit",
85+
"--enable-admission-plugins=ValidatingAdmissionWebhook",
8686
"--service-cluster-ip-range=10.0.0.0/24",
8787
"--allow-privileged=true",
8888
}
@@ -101,6 +101,9 @@ type Environment struct {
101101
// CRDInstallOptions are the options for installing CRDs.
102102
CRDInstallOptions CRDInstallOptions
103103

104+
// CRDInstallOptions are the options for installing webhooks.
105+
WebhookInstallOptions WebhookInstallOptions
106+
104107
// ErrorIfCRDPathMissing provides an interface for the underlying
105108
// CRDInstallOptions.ErrorIfPathMissing. It prevents silent failures
106109
// for missing CRD paths.
@@ -255,6 +258,13 @@ func (te *Environment) Start() (*rest.Config, error) {
255258
te.CRDInstallOptions.ErrorIfPathMissing = te.ErrorIfCRDPathMissing
256259
crds, err := InstallCRDs(te.Config, te.CRDInstallOptions)
257260
te.CRDs = crds
261+
if err != nil {
262+
return te.Config, err
263+
}
264+
265+
log.V(1).Info("installing webhooks")
266+
err = te.WebhookInstallOptions.Install(te.Config)
267+
258268
return te.Config, err
259269
}
260270

0 commit comments

Comments
 (0)