Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 7a4c13f

Browse files
committed
configure webhooks in tests (almost)
1 parent 9828aff commit 7a4c13f

File tree

8 files changed

+51
-58
lines changed

8 files changed

+51
-58
lines changed

config/webhook/kustomization.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,3 @@ resources:
44

55
configurations:
66
- kustomizeconfig.yaml
7-
8-
patchesStrategicMerge:
9-
- webhook_patch.yaml

config/webhook/manifests.yaml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,6 @@ webhooks:
7373
resources:
7474
- hierarchyconfigurations
7575
sideEffects: None
76-
- admissionReviewVersions:
77-
- v1
78-
clientConfig:
79-
service:
80-
name: webhook-service
81-
namespace: system
82-
path: /validate-objects
83-
failurePolicy: Fail
84-
name: objects.hnc.x-k8s.io
85-
rules:
86-
- {}
87-
sideEffects: None
8876
- admissionReviewVersions:
8977
- v1
9078
clientConfig:

config/webhook/webhook_patch.yaml

Lines changed: 0 additions & 25 deletions
This file was deleted.

internal/hncconfig/reconciler.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1616
"k8s.io/apimachinery/pkg/runtime/schema"
1717
"k8s.io/apimachinery/pkg/types"
18+
"k8s.io/utils/pointer"
1819
ctrl "sigs.k8s.io/controller-runtime"
1920
"sigs.k8s.io/controller-runtime/pkg/client"
2021
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -24,6 +25,7 @@ import (
2425

2526
api "sigs.k8s.io/hierarchical-namespaces/api/v1alpha2"
2627
"sigs.k8s.io/hierarchical-namespaces/internal/apimeta"
28+
"sigs.k8s.io/hierarchical-namespaces/internal/config"
2729
"sigs.k8s.io/hierarchical-namespaces/internal/crd"
2830
"sigs.k8s.io/hierarchical-namespaces/internal/forest"
2931
"sigs.k8s.io/hierarchical-namespaces/internal/objects"
@@ -260,16 +262,49 @@ func (r *Reconciler) syncObjectWebhookConfigs(ctx context.Context) error {
260262
}
261263
cleanVWC := vwc.DeepCopy()
262264

265+
webhookFound := false
263266
for i, wh := range vwc.Webhooks {
264267
if wh.Name == "objects.hnc.x-k8s.io" {
265268
vwc.Webhooks[i].Rules = rules
266-
if err := r.Patch(ctx, vwc, client.MergeFrom(cleanVWC)); err != nil {
267-
return err
268-
}
269+
webhookFound = true
269270
break
270271
}
271272
}
272-
return nil
273+
if !webhookFound {
274+
failurePolicy := apiadmissionregistrationv1.Fail
275+
sideEffects := apiadmissionregistrationv1.SideEffectClassNone
276+
vw := apiadmissionregistrationv1.ValidatingWebhook{
277+
Name: "objects.hnc.x-k8s.io",
278+
ClientConfig: apiadmissionregistrationv1.WebhookClientConfig{
279+
Service: &apiadmissionregistrationv1.ServiceReference{
280+
Namespace: config.GetHNCNamespace(),
281+
Name: "webhook-service",
282+
Path: pointer.String("/validate-objects"),
283+
},
284+
},
285+
Rules: rules,
286+
FailurePolicy: &failurePolicy,
287+
SideEffects: &sideEffects,
288+
TimeoutSeconds: pointer.Int32(2),
289+
AdmissionReviewVersions: []string{"v1"},
290+
// We only apply this object validator on non-excluded namespaces, which have
291+
// the "included-namespace" label set by the HC reconciler, so that when HNC
292+
// (webhook service specifically) is down, operations in the excluded
293+
// namespaces won't be affected. Validators on HNC CRs are not filtered because
294+
// they are supposed to prevent abuse of HNC CRs in excluded namespaces.
295+
// Namespace validator is not filtered to prevent abuse of the included-namespace
296+
// label on excluded namespaces. Unfortunately, this means that when HNC is
297+
// down, we will block updates on all namespaces, even "excluded" ones, but
298+
// anyone who can update namespaces like `kube-system` should likely be able to
299+
// delete the VWHConfiguration to make the updates.
300+
NamespaceSelector: &metav1.LabelSelector{
301+
MatchLabels: map[string]string{"hnc.x-k8s.io/included-namespace": "true"},
302+
},
303+
}
304+
vwc.Webhooks = append(vwc.Webhooks, vw)
305+
}
306+
307+
return r.Patch(ctx, vwc, client.MergeFrom(cleanVWC))
273308
}
274309

275310
// syncObjectReconcilers creates or syncs ObjectReconcilers.

internal/integtest/setup.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func HNCBeforeSuite() {
7171
By("configuring test environment")
7272
testEnv = &envtest.Environment{
7373
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
74+
// todo(erikgb): Fix tests that breaks when enabling webhooks
75+
//WebhookInstallOptions: envtest.WebhookInstallOptions{
76+
// Paths: []string{filepath.Join("..", "..", "config", "webhook")},
77+
//},
7478
}
7579

7680
By("starting test environment")
@@ -94,10 +98,14 @@ func HNCBeforeSuite() {
9498
// CF: https://github.com/microsoft/azure-databricks-operator/blob/0f722a710fea06b86ecdccd9455336ca712bf775/controllers/suite_test.go
9599

96100
By("creating manager")
101+
webhookInstallOptions := &testEnv.WebhookInstallOptions
97102
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
98103
NewClient: config.NewClient(false),
99104
MetricsBindAddress: "0", // disable metrics serving since 'go test' runs multiple suites in parallel processes
100105
Scheme: scheme.Scheme,
106+
Host: webhookInstallOptions.LocalServingHost,
107+
Port: webhookInstallOptions.LocalServingPort,
108+
CertDir: webhookInstallOptions.LocalServingCertDir,
101109
})
102110
Expect(err).ToNot(HaveOccurred())
103111

@@ -111,6 +119,7 @@ func HNCBeforeSuite() {
111119
TestForest = forest.NewForest()
112120
err = setup.CreateReconcilers(k8sManager, TestForest, opts)
113121
Expect(err).ToNot(HaveOccurred())
122+
setup.CreateWebhooks(k8sManager, TestForest, opts)
114123

115124
By("Creating clients")
116125
K8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})

internal/objects/validator.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ const (
3232
ServingPath = "/validate-objects"
3333
)
3434

35-
// Note: the validating webhook FAILS CLOSE. This means that if the webhook goes
36-
// down, all further changes are forbidden. In addition, the webhook `rules`
37-
// (groups, resources, versions, verbs) specified in the below kubebuilder marker
38-
// are overwritten by the `rules` configured in config/webhook/webhook_patch.yaml,
39-
// because there's no marker for `scope` and we only want this object webhook
40-
// to work on `namespaced` objects. Please make sure you edit the webhook_patch.yaml
41-
// file if you want to change the webhook `rules` and better make the rules
42-
// here the same as what's in the webhook_patch.yaml.
43-
//
44-
// +kubebuilder:webhook:admissionReviewVersions=v1,path=/validate-objects,mutating=false,failurePolicy=fail,groups=,resources=,sideEffects=None,verbs=,versions=,name=objects.hnc.x-k8s.io
45-
4635
type Validator struct {
4736
Log logr.Logger
4837
Forest *forest.Forest

internal/setup/reconcilers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func Create(log logr.Logger, mgr ctrl.Manager, f *forest.Forest, opts Options) {
3535

3636
if !opts.NoWebhooks {
3737
log.Info("Registering validating webhook (won't work when running locally; use --no-webhooks)")
38-
createWebhooks(mgr, f, opts)
38+
CreateWebhooks(mgr, f, opts)
3939
}
4040

4141
log.Info("Registering reconcilers")

internal/setup/webhooks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ func ManageCerts(mgr ctrl.Manager, setupFinished chan struct{}, restartOnSecretR
5454
})
5555
}
5656

57-
// createWebhooks creates all mutators and validators.
58-
func createWebhooks(mgr ctrl.Manager, f *forest.Forest, opts Options) {
57+
// CreateWebhooks creates all mutators and validators.
58+
func CreateWebhooks(mgr ctrl.Manager, f *forest.Forest, opts Options) {
5959
// Create webhook for Hierarchy
6060
mgr.GetWebhookServer().Register(hierarchyconfig.ServingPath, &webhook.Admission{Handler: &hierarchyconfig.Validator{
6161
Log: ctrl.Log.WithName("hierarchyconfig").WithName("validate"),

0 commit comments

Comments
 (0)