@@ -23,14 +23,21 @@ import (
23
23
24
24
. "github.com/onsi/ginkgo/v2" //lint:ignore ST1001 Ignoring this for now
25
25
. "github.com/onsi/gomega" //lint:ignore ST1001 Ignoring this for now
26
+ apiadmissionregistrationv1 "k8s.io/api/admissionregistration/v1"
26
27
corev1 "k8s.io/api/core/v1"
27
28
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
29
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
30
"k8s.io/client-go/kubernetes/scheme"
31
+ "k8s.io/utils/pointer"
29
32
ctrl "sigs.k8s.io/controller-runtime"
30
33
"sigs.k8s.io/controller-runtime/pkg/client"
31
34
"sigs.k8s.io/controller-runtime/pkg/envtest"
32
35
logf "sigs.k8s.io/controller-runtime/pkg/log"
33
36
"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"
34
41
35
42
// +kubebuilder:scaffold:imports
36
43
@@ -69,8 +76,28 @@ func HNCBeforeSuite() {
69
76
SetDefaultEventuallyTimeout (time .Second * 4 )
70
77
71
78
By ("configuring test environment" )
79
+ sideEffectClassNone := apiadmissionregistrationv1 .SideEffectClassNone
72
80
testEnv = & envtest.Environment {
73
81
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
+ },
74
101
}
75
102
76
103
By ("starting test environment" )
@@ -94,13 +121,20 @@ func HNCBeforeSuite() {
94
121
// CF: https://github.com/microsoft/azure-databricks-operator/blob/0f722a710fea06b86ecdccd9455336ca712bf775/controllers/suite_test.go
95
122
96
123
By ("creating manager" )
124
+ webhookInstallOptions := & testEnv .WebhookInstallOptions
97
125
k8sManager , err := ctrl .NewManager (cfg , ctrl.Options {
98
126
NewClient : config .NewClient (false ),
99
127
MetricsBindAddress : "0" , // disable metrics serving since 'go test' runs multiple suites in parallel processes
100
128
Scheme : scheme .Scheme ,
129
+ Host : webhookInstallOptions .LocalServingHost ,
130
+ Port : webhookInstallOptions .LocalServingPort ,
131
+ CertDir : webhookInstallOptions .LocalServingCertDir ,
101
132
})
102
133
Expect (err ).ToNot (HaveOccurred ())
103
134
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
+
104
138
By ("creating reconcilers" )
105
139
opts := setup.Options {
106
140
MaxReconciles : 100 ,
@@ -125,6 +159,12 @@ func HNCBeforeSuite() {
125
159
}()
126
160
}
127
161
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
+
128
168
func HNCAfterSuite () {
129
169
if k8sManagerCancelFn != nil {
130
170
k8sManagerCancelFn ()
0 commit comments