knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/webhook/certificates/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 certificates 18 19 import ( 20 "context" 21 22 // Injection stuff 23 kubeclient "knative.dev/pkg/client/injection/kube/client" 24 secretinformer "knative.dev/pkg/injection/clients/namespacedkube/informers/core/v1/secret" 25 26 "k8s.io/apimachinery/pkg/types" 27 "k8s.io/client-go/tools/cache" 28 "knative.dev/pkg/configmap" 29 "knative.dev/pkg/controller" 30 "knative.dev/pkg/logging" 31 pkgreconciler "knative.dev/pkg/reconciler" 32 "knative.dev/pkg/system" 33 "knative.dev/pkg/webhook" 34 ) 35 36 // NewController constructs a controller for materializing webhook certificates. 37 // In order for it to bootstrap, an empty secret should be created with the 38 // expected name (and lifecycle managed accordingly), and thereafter this controller 39 // will ensure it has the appropriate shape for the webhook. 40 func NewController( 41 ctx context.Context, 42 cmw configmap.Watcher, 43 ) *controller.Impl { 44 client := kubeclient.Get(ctx) 45 secretInformer := secretinformer.Get(ctx) 46 options := webhook.GetOptions(ctx) 47 48 key := types.NamespacedName{ 49 Namespace: system.Namespace(), 50 Name: options.SecretName, 51 } 52 53 wh := &reconciler{ 54 LeaderAwareFuncs: pkgreconciler.LeaderAwareFuncs{ 55 // Enqueue the key whenever we become leader. 56 PromoteFunc: func(bkt pkgreconciler.Bucket, enq func(pkgreconciler.Bucket, types.NamespacedName)) error { 57 enq(bkt, key) 58 return nil 59 }, 60 }, 61 key: key, 62 serviceName: options.ServiceName, 63 64 client: client, 65 secretlister: secretInformer.Lister(), 66 } 67 68 const queueName = "WebhookCertificates" 69 c := controller.NewContext(ctx, wh, controller.ControllerOptions{WorkQueueName: queueName, Logger: logging.FromContext(ctx).Named(queueName)}) 70 71 // Reconcile when the cert bundle changes. 72 secretInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{ 73 FilterFunc: controller.FilterWithNameAndNamespace(key.Namespace, key.Name), 74 // It doesn't matter what we enqueue because we will always Reconcile 75 // the named MWH resource. 76 Handler: controller.HandleAll(c.Enqueue), 77 }) 78 79 return c 80 }