knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/injection/doc.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 injection defines the mechanisms through which clients, informers
    18  // and shared informer factories are injected into a shared controller binary
    19  // implementation.
    20  //
    21  // There are two primary contexts where the usage of the injection package is
    22  // interesting.  The first is in the context of implementations of
    23  // `controller.Reconciler` being wrapped in a `*controller.Impl`:
    24  //
    25  //	import (
    26  //	  // Simply linking this triggers the injection of the informer, which links
    27  //	  // the factory triggering its injection, and which links the client,
    28  //	  // triggering its injection.  All you need to know is that it works :)
    29  //	  deployinformer "knative.dev/pkg/injection/informers/kubeinformers/appsv1/deployment"
    30  //	  "knative.dev/pkg/injection"
    31  //	)
    32  //
    33  //	func NewController(ctx context.Context) *controller.Impl {
    34  //	  deploymentInformer := deployinformer.Get(ctx)
    35  //	  // Pass deploymentInformer.Lister() to Reconciler
    36  //	  ...
    37  //	  // Set up events on deploymentInformer.Informer()
    38  //	  ...
    39  //	}
    40  //
    41  // Then in `package main` the entire controller process can be set up via:
    42  //
    43  //	package main
    44  //
    45  //	import (
    46  //		// The set of controllers this controller process runs.
    47  //	   // Linking these will register their transitive dependencies, after
    48  //	   // which the shared main can set up the rest.
    49  //		"github.com/knative/foo/pkg/reconciler/matt"
    50  //		"github.com/knative/foo/pkg/reconciler/scott"
    51  //		"github.com/knative/foo/pkg/reconciler/ville"
    52  //		"github.com/knative/foo/pkg/reconciler/dave"
    53  //
    54  //		// This defines the shared main for injected controllers.
    55  //		"knative.dev/pkg/injection/sharedmain"
    56  //	)
    57  //
    58  //	func main() {
    59  //		sharedmain.Main("mycomponent",
    60  //	      // We pass in the list of controllers to construct, and that's it!
    61  //	      // If we forget to add this, go will complain about the unused import.
    62  //	      matt.NewController,
    63  //	      scott.NewController,
    64  //	      ville.NewController,
    65  //	      dave.NewController,
    66  //	   )
    67  //	}
    68  //
    69  // If you want to adapt the above to run the controller within a single
    70  // namespace, you can instead do something like:
    71  //
    72  //	package main
    73  //
    74  //	import (
    75  //		// The set of controllers this controller process runs.
    76  //	   // Linking these will register their transitive dependencies, after
    77  //	   // which the shared main can set up the rest.
    78  //		"github.com/knative/foo/pkg/reconciler/matt"
    79  //		"github.com/knative/foo/pkg/reconciler/scott"
    80  //		"github.com/knative/foo/pkg/reconciler/ville"
    81  //		"github.com/knative/foo/pkg/reconciler/dave"
    82  //
    83  //		// This defines the shared main for injected controllers.
    84  //		"knative.dev/pkg/injection/sharedmain"
    85  //
    86  //	   // These are used to set up the context.
    87  //		"knative.dev/pkg/injection"
    88  //		"knative.dev/pkg/signals"
    89  //	)
    90  //
    91  //	func main() {
    92  //	   // Scope the shared informer factories to the provided namespace.
    93  //	   ctx := injection.WithNamespace(signals.NewContext(), "the-namespace")
    94  //
    95  //	   // Use our initial context when setting up the controllers.
    96  //		sharedmain.MainWithContext(ctx, "mycomponent",
    97  //	      // We pass in the list of controllers to construct, and that's it!
    98  //	      // If we forget to add this, go will complain about the unused import.
    99  //	      matt.NewController,
   100  //	      scott.NewController,
   101  //	      ville.NewController,
   102  //	      dave.NewController,
   103  //	   )
   104  //	}
   105  package injection