Skip to content

Commit fca2a6d

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

File tree

10 files changed

+770
-7
lines changed

10 files changed

+770
-7
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: 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
@@ -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)