knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/client/injection/kube/reconciler/core/v1/secret/controller.go (about)

     1  /*
     2  Copyright 2022 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  // Code generated by injection-gen. DO NOT EDIT.
    18  
    19  package secret
    20  
    21  import (
    22  	context "context"
    23  	fmt "fmt"
    24  	reflect "reflect"
    25  	strings "strings"
    26  
    27  	zap "go.uber.org/zap"
    28  	corev1 "k8s.io/api/core/v1"
    29  	labels "k8s.io/apimachinery/pkg/labels"
    30  	types "k8s.io/apimachinery/pkg/types"
    31  	watch "k8s.io/apimachinery/pkg/watch"
    32  	scheme "k8s.io/client-go/kubernetes/scheme"
    33  	v1 "k8s.io/client-go/kubernetes/typed/core/v1"
    34  	record "k8s.io/client-go/tools/record"
    35  	client "knative.dev/pkg/client/injection/kube/client"
    36  	secret "knative.dev/pkg/client/injection/kube/informers/core/v1/secret"
    37  	controller "knative.dev/pkg/controller"
    38  	logging "knative.dev/pkg/logging"
    39  	logkey "knative.dev/pkg/logging/logkey"
    40  	reconciler "knative.dev/pkg/reconciler"
    41  )
    42  
    43  const (
    44  	defaultControllerAgentName = "secret-controller"
    45  	defaultFinalizerName       = "secrets."
    46  )
    47  
    48  // NewImpl returns a controller.Impl that handles queuing and feeding work from
    49  // the queue through an implementation of controller.Reconciler, delegating to
    50  // the provided Interface and optional Finalizer methods. OptionsFn is used to return
    51  // controller.ControllerOptions to be used by the internal reconciler.
    52  func NewImpl(ctx context.Context, r Interface, optionsFns ...controller.OptionsFn) *controller.Impl {
    53  	logger := logging.FromContext(ctx)
    54  
    55  	// Check the options function input. It should be 0 or 1.
    56  	if len(optionsFns) > 1 {
    57  		logger.Fatal("Up to one options function is supported, found: ", len(optionsFns))
    58  	}
    59  
    60  	secretInformer := secret.Get(ctx)
    61  
    62  	lister := secretInformer.Lister()
    63  
    64  	var promoteFilterFunc func(obj interface{}) bool
    65  	var promoteFunc = func(bkt reconciler.Bucket) {}
    66  
    67  	rec := &reconcilerImpl{
    68  		LeaderAwareFuncs: reconciler.LeaderAwareFuncs{
    69  			PromoteFunc: func(bkt reconciler.Bucket, enq func(reconciler.Bucket, types.NamespacedName)) error {
    70  
    71  				// Signal promotion event
    72  				promoteFunc(bkt)
    73  
    74  				all, err := lister.List(labels.Everything())
    75  				if err != nil {
    76  					return err
    77  				}
    78  				for _, elt := range all {
    79  					if promoteFilterFunc != nil {
    80  						if ok := promoteFilterFunc(elt); !ok {
    81  							continue
    82  						}
    83  					}
    84  					enq(bkt, types.NamespacedName{
    85  						Namespace: elt.GetNamespace(),
    86  						Name:      elt.GetName(),
    87  					})
    88  				}
    89  				return nil
    90  			},
    91  		},
    92  		Client:        client.Get(ctx),
    93  		Lister:        lister,
    94  		reconciler:    r,
    95  		finalizerName: defaultFinalizerName,
    96  	}
    97  
    98  	ctrType := reflect.TypeOf(r).Elem()
    99  	ctrTypeName := fmt.Sprintf("%s.%s", ctrType.PkgPath(), ctrType.Name())
   100  	ctrTypeName = strings.ReplaceAll(ctrTypeName, "/", ".")
   101  
   102  	logger = logger.With(
   103  		zap.String(logkey.ControllerType, ctrTypeName),
   104  		zap.String(logkey.Kind, ".Secret"),
   105  	)
   106  
   107  	impl := controller.NewContext(ctx, rec, controller.ControllerOptions{WorkQueueName: ctrTypeName, Logger: logger})
   108  	agentName := defaultControllerAgentName
   109  
   110  	// Pass impl to the options. Save any optional results.
   111  	for _, fn := range optionsFns {
   112  		opts := fn(impl)
   113  		if opts.ConfigStore != nil {
   114  			rec.configStore = opts.ConfigStore
   115  		}
   116  		if opts.FinalizerName != "" {
   117  			rec.finalizerName = opts.FinalizerName
   118  		}
   119  		if opts.AgentName != "" {
   120  			agentName = opts.AgentName
   121  		}
   122  		if opts.DemoteFunc != nil {
   123  			rec.DemoteFunc = opts.DemoteFunc
   124  		}
   125  		if opts.PromoteFilterFunc != nil {
   126  			promoteFilterFunc = opts.PromoteFilterFunc
   127  		}
   128  		if opts.PromoteFunc != nil {
   129  			promoteFunc = opts.PromoteFunc
   130  		}
   131  		if opts.UseServerSideApplyForFinalizers {
   132  			if opts.FinalizerFieldManager == "" {
   133  				logger.Fatal("FinalizerFieldManager must be provided when UseServerSideApplyForFinalizers is enabled")
   134  			}
   135  			rec.useServerSideApplyForFinalizers = true
   136  			rec.finalizerFieldManager = opts.FinalizerFieldManager
   137  			rec.forceApplyFinalizers = opts.ForceApplyFinalizers
   138  		}
   139  	}
   140  
   141  	rec.Recorder = createRecorder(ctx, agentName)
   142  
   143  	return impl
   144  }
   145  
   146  func createRecorder(ctx context.Context, agentName string) record.EventRecorder {
   147  	logger := logging.FromContext(ctx)
   148  
   149  	recorder := controller.GetEventRecorder(ctx)
   150  	if recorder == nil {
   151  		// Create event broadcaster
   152  		logger.Debug("Creating event broadcaster")
   153  		eventBroadcaster := record.NewBroadcaster()
   154  		watches := []watch.Interface{
   155  			eventBroadcaster.StartLogging(logger.Named("event-broadcaster").Infof),
   156  			eventBroadcaster.StartRecordingToSink(
   157  				&v1.EventSinkImpl{Interface: client.Get(ctx).CoreV1().Events("")}),
   158  		}
   159  		recorder = eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: agentName})
   160  		go func() {
   161  			<-ctx.Done()
   162  			for _, w := range watches {
   163  				w.Stop()
   164  			}
   165  		}()
   166  	}
   167  
   168  	return recorder
   169  }
   170  
   171  func init() {
   172  	scheme.AddToScheme(scheme.Scheme)
   173  }