knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/webhook/configmaps/controller.go (about)

     1  /*
     2  Copyright 2019 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package configmaps
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  
    23  	// Injection stuff
    24  	kubeclient "knative.dev/pkg/client/injection/kube/client"
    25  	vwhinformer "knative.dev/pkg/client/injection/kube/informers/admissionregistration/v1/validatingwebhookconfiguration"
    26  	secretinformer "knative.dev/pkg/injection/clients/namespacedkube/informers/core/v1/secret"
    27  	"knative.dev/pkg/logging"
    28  	pkgreconciler "knative.dev/pkg/reconciler"
    29  
    30  	"k8s.io/apimachinery/pkg/types"
    31  	"k8s.io/client-go/tools/cache"
    32  	"knative.dev/pkg/configmap"
    33  	"knative.dev/pkg/controller"
    34  	"knative.dev/pkg/system"
    35  	"knative.dev/pkg/webhook"
    36  )
    37  
    38  // NewAdmissionController constructs a reconciler
    39  func NewAdmissionController(
    40  	ctx context.Context,
    41  	name, path string,
    42  	constructors configmap.Constructors,
    43  ) *controller.Impl {
    44  	client := kubeclient.Get(ctx)
    45  	vwhInformer := vwhinformer.Get(ctx)
    46  	secretInformer := secretinformer.Get(ctx)
    47  	options := webhook.GetOptions(ctx)
    48  
    49  	// if this environment variable is set, it overrides the value in the Options
    50  	disableNamespaceOwnership := webhook.DisableNamespaceOwnershipFromEnv()
    51  	if disableNamespaceOwnership != nil {
    52  		options.DisableNamespaceOwnership = *disableNamespaceOwnership
    53  	}
    54  
    55  	key := types.NamespacedName{Name: name}
    56  
    57  	wh := &reconciler{
    58  		LeaderAwareFuncs: pkgreconciler.LeaderAwareFuncs{
    59  			// Have this reconciler enqueue our singleton whenever it becomes leader.
    60  			PromoteFunc: func(bkt pkgreconciler.Bucket, enq func(pkgreconciler.Bucket, types.NamespacedName)) error {
    61  				enq(bkt, key)
    62  				return nil
    63  			},
    64  		},
    65  
    66  		key:  key,
    67  		path: path,
    68  
    69  		constructors:              make(map[string]reflect.Value),
    70  		secretName:                options.SecretName,
    71  		disableNamespaceOwnership: options.DisableNamespaceOwnership,
    72  
    73  		client:       client,
    74  		vwhlister:    vwhInformer.Lister(),
    75  		secretlister: secretInformer.Lister(),
    76  	}
    77  
    78  	for configName, constructor := range constructors {
    79  		wh.registerConfig(configName, constructor)
    80  	}
    81  
    82  	const queueName = "ConfigMapWebhook"
    83  	c := controller.NewContext(ctx, wh, controller.ControllerOptions{WorkQueueName: queueName, Logger: logging.FromContext(ctx).Named(queueName)})
    84  
    85  	// Reconcile when the named ValidatingWebhookConfiguration changes.
    86  	vwhInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
    87  		FilterFunc: controller.FilterWithName(name),
    88  		// It doesn't matter what we enqueue because we will always Reconcile
    89  		// the named VWH resource.
    90  		Handler: controller.HandleAll(c.Enqueue),
    91  	})
    92  
    93  	// Reconcile when the cert bundle changes.
    94  	secretInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
    95  		FilterFunc: controller.FilterWithNameAndNamespace(system.Namespace(), wh.secretName),
    96  		// It doesn't matter what we enqueue because we will always Reconcile
    97  		// the named VWH resource.
    98  		Handler: controller.HandleAll(c.Enqueue),
    99  	})
   100  
   101  	return c
   102  }