Skip to content

Commit e4f7c03

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

File tree

9 files changed

+720
-4
lines changed

9 files changed

+720
-4
lines changed

pkg/cache/cache_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
9696
Expect(cfg).NotTo(BeNil())
9797

9898
By("creating three pods")
99+
cl, err := client.New(cfg, client.Options{})
100+
Expect(err).NotTo(HaveOccurred())
101+
err = ensureNamespace(testNamespaceOne, cl)
102+
Expect(err).NotTo(HaveOccurred())
103+
err = ensureNamespace(testNamespaceTwo, cl)
104+
Expect(err).NotTo(HaveOccurred())
105+
err = ensureNamespace(testNamespaceThree, cl)
106+
Expect(err).NotTo(HaveOccurred())
99107
// Includes restart policy since these objects are indexed on this field.
100108
knownPod1 = createPod("test-pod-1", testNamespaceOne, kcorev1.RestartPolicyNever)
101109
knownPod2 = createPod("test-pod-2", testNamespaceTwo, kcorev1.RestartPolicyAlways)
@@ -111,7 +119,6 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
111119
knownPod4.GetObjectKind().SetGroupVersionKind(podGVK)
112120

113121
By("creating the informer cache")
114-
var err error
115122
informerCache, err = createCacheFunc(cfg, cache.Options{})
116123
Expect(err).NotTo(HaveOccurred())
117124
By("running the cache and waiting for it to sync")
@@ -667,3 +674,21 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
667674
})
668675
})
669676
}
677+
678+
// ensureNamespace installs namespace of a given name if not exists
679+
func ensureNamespace(namespace string, client client.Client) error {
680+
ns := kcorev1.Namespace{
681+
ObjectMeta: kmetav1.ObjectMeta{
682+
Name: namespace,
683+
},
684+
TypeMeta: kmetav1.TypeMeta{
685+
Kind: "Namespace",
686+
APIVersion: "v1",
687+
},
688+
}
689+
err := client.Create(context.TODO(), &ns)
690+
if errors.IsAlreadyExists(err) {
691+
return nil
692+
}
693+
return err
694+
}

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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ 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",
86+
"--disable-admission-plugins=ServiceAccount",
8687
"--service-cluster-ip-range=10.0.0.0/24",
8788
"--allow-privileged=true",
8889
}
@@ -101,6 +102,9 @@ type Environment struct {
101102
// CRDInstallOptions are the options for installing CRDs.
102103
CRDInstallOptions CRDInstallOptions
103104

105+
// CRDInstallOptions are the options for installing webhooks.
106+
WebhookInstallOptions WebhookInstallOptions
107+
104108
// ErrorIfCRDPathMissing provides an interface for the underlying
105109
// CRDInstallOptions.ErrorIfPathMissing. It prevents silent failures
106110
// for missing CRD paths.
@@ -255,6 +259,13 @@ func (te *Environment) Start() (*rest.Config, error) {
255259
te.CRDInstallOptions.ErrorIfPathMissing = te.ErrorIfCRDPathMissing
256260
crds, err := InstallCRDs(te.Config, te.CRDInstallOptions)
257261
te.CRDs = crds
262+
if err != nil {
263+
return te.Config, err
264+
}
265+
266+
log.V(1).Info("installing webhooks")
267+
err = te.WebhookInstallOptions.Install(te.Config)
268+
258269
return te.Config, err
259270
}
260271

0 commit comments

Comments
 (0)