@@ -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"
@@ -238,21 +236,24 @@ func (r *Reconciler) writeSingleton(ctx context.Context, inst *api.HNCConfigurat
238
236
}
239
237
240
238
func (r * Reconciler ) syncObjectWebhookConfigs (ctx context.Context ) error {
241
- namespacedScope := apiadmissionregistrationv1 .NamespacedScope
239
+ // Group GR by group
240
+ groups := make (map [string ][]string )
241
+ for gr := range r .activeGVKMode {
242
+ groups [gr .Group ] = append (groups [gr .Group ], gr .Resource )
243
+ }
242
244
243
245
var rules []apiadmissionregistrationv1.RuleWithOperations
244
- for gr := range r . activeGVKMode {
246
+ for g , res := range groups {
245
247
rule := apiadmissionregistrationv1.RuleWithOperations {}
246
- rule .APIGroups = []string {gr . Group }
247
- rule .Resources = [] string { gr . Resource }
248
+ rule .APIGroups = []string {g }
249
+ rule .Resources = res
248
250
rule .APIVersions = []string {"*" }
249
- rule .Scope = & namespacedScope
250
251
rule .Operations = []apiadmissionregistrationv1.OperationType {apiadmissionregistrationv1 .Create , apiadmissionregistrationv1 .Update , apiadmissionregistrationv1 .Delete }
251
252
rules = append (rules , rule )
252
253
}
253
254
254
255
vwc := & apiadmissionregistrationv1.ValidatingWebhookConfiguration {}
255
- if err := r .Get (ctx , client.ObjectKey {Name : webhooks .ValidatingWebhookName }, vwc ); err != nil {
256
+ if err := r .Get (ctx , client.ObjectKey {Name : webhooks .ValidatingWebhookConfigurationName }, vwc ); err != nil {
256
257
if errors .IsNotFound (err ) {
257
258
// todo(erikgb): See if the tests can/should be bootstrapped with this webhook
258
259
// Webhook not found; nothing to reconcile
@@ -262,49 +263,13 @@ func (r *Reconciler) syncObjectWebhookConfigs(ctx context.Context) error {
262
263
}
263
264
cleanVWC := vwc .DeepCopy ()
264
265
265
- webhookFound := false
266
266
for i , wh := range vwc .Webhooks {
267
- if wh .Name == "objects.hnc.x-k8s.io" {
267
+ if wh .Name == webhooks . ObjectsWebhookName {
268
268
vwc .Webhooks [i ].Rules = rules
269
- webhookFound = true
270
- break
269
+ return r .Patch (ctx , vwc , client .MergeFrom (cleanVWC ))
271
270
}
272
271
}
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 ))
272
+ return fmt .Errorf ("webhook %q not found in ValidatingWebhookConfiguration %q" , webhooks .ObjectsWebhookName , webhooks .ValidatingWebhookConfigurationName )
308
273
}
309
274
310
275
// syncObjectReconcilers creates or syncs ObjectReconcilers.
0 commit comments