Skip to content

Commit 99e4cac

Browse files
Webhook support in envtest
Co-authored-by: Solly Ross <[email protected]>
1 parent 9f8aab6 commit 99e4cac

File tree

10 files changed

+809
-4
lines changed

10 files changed

+809
-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")
@@ -681,3 +688,21 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
681688
})
682689
})
683690
}
691+
692+
// ensureNamespace installs namespace of a given name if not exists
693+
func ensureNamespace(namespace string, client client.Client) error {
694+
ns := kcorev1.Namespace{
695+
ObjectMeta: kmetav1.ObjectMeta{
696+
Name: namespace,
697+
},
698+
TypeMeta: kmetav1.TypeMeta{
699+
Kind: "Namespace",
700+
APIVersion: "v1",
701+
},
702+
}
703+
err := client.Create(context.TODO(), &ns)
704+
if errors.IsAlreadyExists(err) {
705+
return nil
706+
}
707+
return err
708+
}

pkg/envtest/envtest_suite_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ package envtest
1919
import (
2020
"testing"
2121

22+
"k8s.io/apimachinery/pkg/runtime"
23+
2224
. "github.com/onsi/ginkgo"
2325
. "github.com/onsi/gomega"
26+
admissionv1beta1 "k8s.io/api/admissionregistration/v1beta1"
27+
admissionv1 "k8s.io/api/admissionregistration/v1"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2429
logf "sigs.k8s.io/controller-runtime/pkg/log"
2530
"sigs.k8s.io/controller-runtime/pkg/log/zap"
2631
)
@@ -35,12 +40,101 @@ var env *Environment
3540
var _ = BeforeSuite(func(done Done) {
3641
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
3742
env = &Environment{}
43+
// we're initializing webhook here and not in webhook.go to also test the envtest install code via WebhookOptions
44+
initializeWebhookInEnvironment()
3845
_, err := env.Start()
3946
Expect(err).NotTo(HaveOccurred())
4047

4148
close(done)
4249
}, StartTimeout)
4350

51+
func initializeWebhookInEnvironment() {
52+
namespacedScopeV1Beta1 := admissionv1beta1.NamespacedScope
53+
namespacedScopeV1 := admissionv1.NamespacedScope
54+
failedTypeV1Beta1 := admissionv1beta1.Fail
55+
failedTypeV1 := admissionv1.Fail
56+
equivalentTypeV1Beta1 := admissionv1beta1.Equivalent
57+
equivalentTypeV1 := admissionv1.Equivalent
58+
noSideEffectsV1Beta1 := admissionv1beta1.SideEffectClassNone
59+
noSideEffectsV1 := admissionv1.SideEffectClassNone
60+
webhookPathV1 := "/failing"
61+
62+
env.WebhookInstallOptions = WebhookInstallOptions{
63+
ValidatingWebhooks: []runtime.Object{
64+
&admissionv1beta1.ValidatingWebhookConfiguration{
65+
ObjectMeta: metav1.ObjectMeta{
66+
Name: "deployment-validation-webhook-config",
67+
},
68+
TypeMeta: metav1.TypeMeta{
69+
Kind: "ValidatingWebhookConfiguration",
70+
APIVersion: "admissionregistration.k8s.io/v1beta1",
71+
},
72+
Webhooks: []admissionv1beta1.ValidatingWebhook{
73+
{
74+
Name: "deployment-validation.kubebuilder.io",
75+
Rules: []admissionv1beta1.RuleWithOperations{
76+
{
77+
Operations: []admissionv1beta1.OperationType{"CREATE", "UPDATE"},
78+
Rule: admissionv1beta1.Rule{
79+
APIGroups: []string{"apps"},
80+
APIVersions: []string{"v1"},
81+
Resources: []string{"deployments"},
82+
Scope: &namespacedScopeV1Beta1,
83+
},
84+
},
85+
},
86+
FailurePolicy: &failedTypeV1Beta1,
87+
MatchPolicy: &equivalentTypeV1Beta1,
88+
SideEffects: &noSideEffectsV1Beta1,
89+
ClientConfig: admissionv1beta1.WebhookClientConfig{
90+
Service: &admissionv1beta1.ServiceReference{
91+
Name: "deployment-validation-service",
92+
Namespace: "default",
93+
Path: &webhookPathV1,
94+
},
95+
},
96+
},
97+
},
98+
},
99+
&admissionv1.ValidatingWebhookConfiguration{
100+
ObjectMeta: metav1.ObjectMeta{
101+
Name: "deployment-validation-webhook-config",
102+
},
103+
TypeMeta: metav1.TypeMeta{
104+
Kind: "ValidatingWebhookConfiguration",
105+
APIVersion: "admissionregistration.k8s.io/v1beta1",
106+
},
107+
Webhooks: []admissionv1.ValidatingWebhook{
108+
{
109+
Name: "deployment-validation.kubebuilder.io",
110+
Rules: []admissionv1.RuleWithOperations{
111+
{
112+
Operations: []admissionv1.OperationType{"CREATE", "UPDATE"},
113+
Rule: admissionv1.Rule{
114+
APIGroups: []string{"apps"},
115+
APIVersions: []string{"v1"},
116+
Resources: []string{"deployments"},
117+
Scope: &namespacedScopeV1,
118+
},
119+
},
120+
},
121+
FailurePolicy: &failedTypeV1,
122+
MatchPolicy: &equivalentTypeV1,
123+
SideEffects: &noSideEffectsV1,
124+
ClientConfig: admissionv1.WebhookClientConfig{
125+
Service: &admissionv1.ServiceReference{
126+
Name: "deployment-validation-service",
127+
Namespace: "default",
128+
Path: &webhookPathV1,
129+
},
130+
},
131+
},
132+
},
133+
},
134+
},
135+
}
136+
}
137+
44138
var _ = AfterSuite(func(done Done) {
45139
Expect(env.Stop()).NotTo(HaveOccurred())
46140

pkg/envtest/envtest_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,16 +779,18 @@ var _ = Describe("Test", func() {
779779

780780
Describe("Start", func() {
781781
It("should raise an error on invalid dir when flag is enabled", func(done Done) {
782-
env = &Environment{ErrorIfCRDPathMissing: true, CRDDirectoryPaths: []string{invalidDirectory}}
782+
env := &Environment{ErrorIfCRDPathMissing: true, CRDDirectoryPaths: []string{invalidDirectory}}
783783
_, err := env.Start()
784784
Expect(err).To(HaveOccurred())
785+
Expect(env.Stop()).To(Succeed())
785786
close(done)
786787
}, 30)
787788

788789
It("should not raise an error on invalid dir when flag is disabled", func(done Done) {
789-
env = &Environment{ErrorIfCRDPathMissing: false, CRDDirectoryPaths: []string{invalidDirectory}}
790+
env := &Environment{ErrorIfCRDPathMissing: false, CRDDirectoryPaths: []string{invalidDirectory}}
790791
_, err := env.Start()
791792
Expect(err).NotTo(HaveOccurred())
793+
Expect(env.Stop()).To(Succeed())
792794
close(done)
793795
}, 30)
794796
})

pkg/envtest/server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ type Environment struct {
8686
// CRDInstallOptions are the options for installing CRDs.
8787
CRDInstallOptions CRDInstallOptions
8888

89+
// CRDInstallOptions are the options for installing webhooks.
90+
WebhookInstallOptions WebhookInstallOptions
91+
8992
// ErrorIfCRDPathMissing provides an interface for the underlying
9093
// CRDInstallOptions.ErrorIfPathMissing. It prevents silent failures
9194
// for missing CRD paths.
@@ -240,7 +243,14 @@ func (te *Environment) Start() (*rest.Config, error) {
240243
te.CRDInstallOptions.Paths = mergePaths(te.CRDInstallOptions.Paths, te.CRDDirectoryPaths)
241244
te.CRDInstallOptions.ErrorIfPathMissing = te.ErrorIfCRDPathMissing
242245
crds, err := InstallCRDs(te.Config, te.CRDInstallOptions)
246+
if err != nil {
247+
return te.Config, err
248+
}
243249
te.CRDs = crds
250+
251+
log.V(1).Info("installing webhooks")
252+
err = te.WebhookInstallOptions.Install(te.Config)
253+
244254
return te.Config, err
245255
}
246256

0 commit comments

Comments
 (0)