Skip to content

Commit 987f485

Browse files
Webhook support in envtest
Co-authored-by: Solly Ross <[email protected]>
1 parent 82a78f9 commit 987f485

File tree

10 files changed

+768
-4
lines changed

10 files changed

+768
-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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ limitations under the License.
1717
package envtest
1818

1919
import (
20+
"k8s.io/apimachinery/pkg/runtime"
2021
"testing"
2122

2223
. "github.com/onsi/ginkgo"
2324
. "github.com/onsi/gomega"
25+
admissionv1beta1 "k8s.io/api/admissionregistration/v1beta1"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2427
logf "sigs.k8s.io/controller-runtime/pkg/log"
2528
"sigs.k8s.io/controller-runtime/pkg/log/zap"
2629
)
@@ -35,12 +38,62 @@ var env *Environment
3538
var _ = BeforeSuite(func(done Done) {
3639
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
3740
env = &Environment{}
41+
// we're initializing webhook here and not in webhook.go to also test the envtest install code via WebhookOptions
42+
initializeWebhookInEnvironment()
3843
_, err := env.Start()
3944
Expect(err).NotTo(HaveOccurred())
4045

4146
close(done)
4247
}, StartTimeout)
4348

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

pkg/envtest/envtest_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,16 +777,18 @@ 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())
783+
Expect(env.Stop()).To(Succeed())
783784
close(done)
784785
}, 30)
785786

786787
It("should not raise an error on invalid dir when flag is disabled", func(done Done) {
787-
env = &Environment{ErrorIfCRDPathMissing: false, CRDDirectoryPaths: []string{invalidDirectory}}
788+
env := &Environment{ErrorIfCRDPathMissing: false, CRDDirectoryPaths: []string{invalidDirectory}}
788789
_, err := env.Start()
789790
Expect(err).NotTo(HaveOccurred())
791+
Expect(env.Stop()).To(Succeed())
790792
close(done)
791793
}, 30)
792794
})

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)