@@ -15,7 +15,6 @@ import (
15
15
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
16
"k8s.io/apimachinery/pkg/runtime/schema"
17
17
"k8s.io/apimachinery/pkg/types"
18
- "k8s.io/utils/pointer"
19
18
ctrl "sigs.k8s.io/controller-runtime"
20
19
"sigs.k8s.io/controller-runtime/pkg/client"
21
20
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -25,7 +24,6 @@ import (
25
24
26
25
api "sigs.k8s.io/hierarchical-namespaces/api/v1alpha2"
27
26
"sigs.k8s.io/hierarchical-namespaces/internal/apimeta"
28
- "sigs.k8s.io/hierarchical-namespaces/internal/config"
29
27
"sigs.k8s.io/hierarchical-namespaces/internal/crd"
30
28
"sigs.k8s.io/hierarchical-namespaces/internal/forest"
31
29
"sigs.k8s.io/hierarchical-namespaces/internal/objects"
@@ -241,21 +239,24 @@ func (r *Reconciler) writeSingleton(ctx context.Context, inst *api.HNCConfigurat
241
239
}
242
240
243
241
func (r * Reconciler ) syncObjectWebhookConfigs (ctx context.Context ) error {
244
- namespacedScope := apiadmissionregistrationv1 .NamespacedScope
242
+ // Group GR by group
243
+ groups := make (map [string ][]string )
244
+ for gr := range r .activeGVKMode {
245
+ groups [gr .Group ] = append (groups [gr .Group ], gr .Resource )
246
+ }
245
247
246
248
var rules []apiadmissionregistrationv1.RuleWithOperations
247
- for gr := range r . activeGVKMode {
249
+ for g , res := range groups {
248
250
rule := apiadmissionregistrationv1.RuleWithOperations {}
249
- rule .APIGroups = []string {gr . Group }
250
- rule .Resources = [] string { gr . Resource }
251
+ rule .APIGroups = []string {g }
252
+ rule .Resources = res
251
253
rule .APIVersions = []string {"*" }
252
- rule .Scope = & namespacedScope
253
254
rule .Operations = []apiadmissionregistrationv1.OperationType {apiadmissionregistrationv1 .Create , apiadmissionregistrationv1 .Update , apiadmissionregistrationv1 .Delete }
254
255
rules = append (rules , rule )
255
256
}
256
257
257
258
vwc := & apiadmissionregistrationv1.ValidatingWebhookConfiguration {}
258
- if err := r .Get (ctx , client.ObjectKey {Name : webhooks .ValidatingWebhookName }, vwc ); err != nil {
259
+ if err := r .Get (ctx , client.ObjectKey {Name : webhooks .ValidatingWebhookConfigurationName }, vwc ); err != nil {
259
260
if errors .IsNotFound (err ) {
260
261
// todo(erikgb): See if the tests can/should be bootstrapped with this webhook
261
262
// Webhook not found; nothing to reconcile
@@ -265,49 +266,13 @@ func (r *Reconciler) syncObjectWebhookConfigs(ctx context.Context) error {
265
266
}
266
267
cleanVWC := vwc .DeepCopy ()
267
268
268
- webhookFound := false
269
269
for i , wh := range vwc .Webhooks {
270
- if wh .Name == "objects.hnc.x-k8s.io" {
270
+ if wh .Name == webhooks . ObjectsWebhookName {
271
271
vwc .Webhooks [i ].Rules = rules
272
- webhookFound = true
273
- break
272
+ return r .Patch (ctx , vwc , client .MergeFrom (cleanVWC ))
274
273
}
275
274
}
276
- if ! webhookFound {
277
- failurePolicy := apiadmissionregistrationv1 .Fail
278
- sideEffects := apiadmissionregistrationv1 .SideEffectClassNone
279
- vw := apiadmissionregistrationv1.ValidatingWebhook {
280
- Name : "objects.hnc.x-k8s.io" ,
281
- ClientConfig : apiadmissionregistrationv1.WebhookClientConfig {
282
- Service : & apiadmissionregistrationv1.ServiceReference {
283
- Namespace : config .GetHNCNamespace (),
284
- Name : "webhook-service" ,
285
- Path : pointer .String ("/validate-objects" ),
286
- },
287
- },
288
- Rules : rules ,
289
- FailurePolicy : & failurePolicy ,
290
- SideEffects : & sideEffects ,
291
- TimeoutSeconds : pointer .Int32 (2 ),
292
- AdmissionReviewVersions : []string {"v1" },
293
- // We only apply this object validator on non-excluded namespaces, which have
294
- // the "included-namespace" label set by the HC reconciler, so that when HNC
295
- // (webhook service specifically) is down, operations in the excluded
296
- // namespaces won't be affected. Validators on HNC CRs are not filtered because
297
- // they are supposed to prevent abuse of HNC CRs in excluded namespaces.
298
- // Namespace validator is not filtered to prevent abuse of the included-namespace
299
- // label on excluded namespaces. Unfortunately, this means that when HNC is
300
- // down, we will block updates on all namespaces, even "excluded" ones, but
301
- // anyone who can update namespaces like `kube-system` should likely be able to
302
- // delete the VWHConfiguration to make the updates.
303
- NamespaceSelector : & metav1.LabelSelector {
304
- MatchLabels : map [string ]string {"hnc.x-k8s.io/included-namespace" : "true" },
305
- },
306
- }
307
- vwc .Webhooks = append (vwc .Webhooks , vw )
308
- }
309
-
310
- return r .Patch (ctx , vwc , client .MergeFrom (cleanVWC ))
275
+ return fmt .Errorf ("webhook %q not found in ValidatingWebhookConfiguration %q" , webhooks .ObjectsWebhookName , webhooks .ValidatingWebhookConfigurationName )
311
276
}
312
277
313
278
// syncObjectReconcilers creates or syncs ObjectReconcilers.
0 commit comments