@@ -23,14 +23,21 @@ import (
2323
2424 . "github.com/onsi/ginkgo/v2" //lint:ignore ST1001 Ignoring this for now
2525 . "github.com/onsi/gomega" //lint:ignore ST1001 Ignoring this for now
26+ apiadmissionregistrationv1 "k8s.io/api/admissionregistration/v1"
2627 corev1 "k8s.io/api/core/v1"
2728 apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
29+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2830 "k8s.io/client-go/kubernetes/scheme"
31+ "k8s.io/utils/pointer"
2932 ctrl "sigs.k8s.io/controller-runtime"
3033 "sigs.k8s.io/controller-runtime/pkg/client"
3134 "sigs.k8s.io/controller-runtime/pkg/envtest"
3235 logf "sigs.k8s.io/controller-runtime/pkg/log"
3336 "sigs.k8s.io/controller-runtime/pkg/log/zap"
37+ "sigs.k8s.io/controller-runtime/pkg/webhook"
38+ "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
39+ "sigs.k8s.io/hierarchical-namespaces/internal/objects"
40+ "sigs.k8s.io/hierarchical-namespaces/internal/webhooks"
3441
3542 // +kubebuilder:scaffold:imports
3643
@@ -69,8 +76,28 @@ func HNCBeforeSuite() {
6976 SetDefaultEventuallyTimeout (time .Second * 4 )
7077
7178 By ("configuring test environment" )
79+ sideEffectClassNone := apiadmissionregistrationv1 .SideEffectClassNone
7280 testEnv = & envtest.Environment {
7381 CRDDirectoryPaths : []string {filepath .Join (".." , ".." , "config" , "crd" , "bases" )},
82+ WebhookInstallOptions : envtest.WebhookInstallOptions {
83+ ValidatingWebhooks : []* apiadmissionregistrationv1.ValidatingWebhookConfiguration {{
84+ ObjectMeta : metav1.ObjectMeta {
85+ Name : webhooks .ValidatingWebhookConfigurationName ,
86+ },
87+ Webhooks : []apiadmissionregistrationv1.ValidatingWebhook {{
88+ Name : webhooks .ObjectsWebhookName ,
89+ AdmissionReviewVersions : []string {"v1" },
90+ SideEffects : & sideEffectClassNone ,
91+ ClientConfig : apiadmissionregistrationv1.WebhookClientConfig {
92+ Service : & apiadmissionregistrationv1.ServiceReference {
93+ Namespace : "system" ,
94+ Name : "webhook-service" ,
95+ Path : pointer .String (objects .ServingPath ),
96+ },
97+ },
98+ }},
99+ }},
100+ },
74101 }
75102
76103 By ("starting test environment" )
@@ -94,13 +121,20 @@ func HNCBeforeSuite() {
94121 // CF: https://github.com/microsoft/azure-databricks-operator/blob/0f722a710fea06b86ecdccd9455336ca712bf775/controllers/suite_test.go
95122
96123 By ("creating manager" )
124+ webhookInstallOptions := & testEnv .WebhookInstallOptions
97125 k8sManager , err := ctrl .NewManager (cfg , ctrl.Options {
98126 NewClient : config .NewClient (false ),
99127 MetricsBindAddress : "0" , // disable metrics serving since 'go test' runs multiple suites in parallel processes
100128 Scheme : scheme .Scheme ,
129+ Host : webhookInstallOptions .LocalServingHost ,
130+ Port : webhookInstallOptions .LocalServingPort ,
131+ CertDir : webhookInstallOptions .LocalServingCertDir ,
101132 })
102133 Expect (err ).ToNot (HaveOccurred ())
103134
135+ // Register a dummy webhook since the test control plane is to test reconcilers
136+ k8sManager .GetWebhookServer ().Register (objects .ServingPath , & webhook.Admission {Handler : & allowAllHandler {}})
137+
104138 By ("creating reconcilers" )
105139 opts := setup.Options {
106140 MaxReconciles : 100 ,
@@ -125,6 +159,12 @@ func HNCBeforeSuite() {
125159 }()
126160}
127161
162+ type allowAllHandler struct {}
163+
164+ func (a allowAllHandler ) Handle (_ context.Context , _ admission.Request ) admission.Response {
165+ return webhooks .Allow ("All requests are allowed by allowAllHandler" )
166+ }
167+
128168func HNCAfterSuite () {
129169 if k8sManagerCancelFn != nil {
130170 k8sManagerCancelFn ()
0 commit comments