knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/codegen/cmd/injection-gen/generators/factory/factory.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 factory
    18  
    19  import (
    20  	"io"
    21  
    22  	"k8s.io/gengo/v2/generator"
    23  	"k8s.io/gengo/v2/namer"
    24  	"k8s.io/gengo/v2/types"
    25  
    26  	"k8s.io/klog/v2"
    27  )
    28  
    29  // factoryTestGenerator produces a file of factory injection of a given type.
    30  type factoryGenerator struct {
    31  	generator.GoGenerator
    32  	outputPackage                string
    33  	imports                      namer.ImportTracker
    34  	cachingClientSetPackage      string
    35  	sharedInformerFactoryPackage string
    36  	filtered                     bool
    37  }
    38  
    39  var _ generator.Generator = (*factoryGenerator)(nil)
    40  
    41  func (g *factoryGenerator) Filter(c *generator.Context, t *types.Type) bool {
    42  	// We generate a single factory, so return true once.
    43  	if !g.filtered {
    44  		g.filtered = true
    45  		return true
    46  	}
    47  	return false
    48  }
    49  
    50  func (g *factoryGenerator) Namers(c *generator.Context) namer.NameSystems {
    51  	return namer.NameSystems{
    52  		"raw": namer.NewRawNamer(g.outputPackage, g.imports),
    53  	}
    54  }
    55  
    56  func (g *factoryGenerator) Imports(c *generator.Context) (imports []string) {
    57  	imports = append(imports, g.imports.ImportLines()...)
    58  	return imports
    59  }
    60  
    61  func (g *factoryGenerator) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error {
    62  	sw := generator.NewSnippetWriter(w, c, "{{", "}}")
    63  
    64  	klog.V(5).Info("processing type ", t)
    65  
    66  	m := map[string]interface{}{
    67  		"cachingClientGet": c.Universe.Type(types.Name{Package: g.cachingClientSetPackage, Name: "Get"}),
    68  		"informersNewSharedInformerFactoryWithOptions": c.Universe.Function(types.Name{Package: g.sharedInformerFactoryPackage, Name: "NewSharedInformerFactoryWithOptions"}),
    69  		"informersSharedInformerOption":                c.Universe.Function(types.Name{Package: g.sharedInformerFactoryPackage, Name: "SharedInformerOption"}),
    70  		"informersWithNamespace":                       c.Universe.Function(types.Name{Package: g.sharedInformerFactoryPackage, Name: "WithNamespace"}),
    71  		"informersSharedInformerFactory":               c.Universe.Function(types.Name{Package: g.sharedInformerFactoryPackage, Name: "SharedInformerFactory"}),
    72  		"injectionRegisterInformerFactory":             c.Universe.Type(types.Name{Package: "knative.dev/pkg/injection", Name: "Default.RegisterInformerFactory"}),
    73  		"injectionHasNamespace":                        c.Universe.Type(types.Name{Package: "knative.dev/pkg/injection", Name: "HasNamespaceScope"}),
    74  		"injectionGetNamespace":                        c.Universe.Type(types.Name{Package: "knative.dev/pkg/injection", Name: "GetNamespaceScope"}),
    75  		"controllerGetResyncPeriod":                    c.Universe.Type(types.Name{Package: "knative.dev/pkg/controller", Name: "GetResyncPeriod"}),
    76  		"loggingFromContext": c.Universe.Function(types.Name{
    77  			Package: "knative.dev/pkg/logging",
    78  			Name:    "FromContext",
    79  		}),
    80  		"contextContext": c.Universe.Type(types.Name{
    81  			Package: "context",
    82  			Name:    "Context",
    83  		}),
    84  	}
    85  
    86  	sw.Do(injectionFactory, m)
    87  
    88  	return sw.Error()
    89  }
    90  
    91  var injectionFactory = `
    92  func init() {
    93  	{{.injectionRegisterInformerFactory|raw}}(withInformerFactory)
    94  }
    95  
    96  // Key is used as the key for associating information with a context.Context.
    97  type Key struct{}
    98  
    99  func withInformerFactory(ctx {{.contextContext|raw}}) {{.contextContext|raw}} {
   100  	c := {{.cachingClientGet|raw}}(ctx)
   101  	opts := make([]{{.informersSharedInformerOption|raw}}, 0, 1)
   102  	if {{.injectionHasNamespace|raw}}(ctx) {
   103  		opts = append(opts, {{.informersWithNamespace|raw}}({{.injectionGetNamespace|raw}}(ctx)))
   104  	}
   105  	return context.WithValue(ctx, Key{},
   106  		{{.informersNewSharedInformerFactoryWithOptions|raw}}(c, {{.controllerGetResyncPeriod|raw}}(ctx), opts...))
   107  }
   108  
   109  // Get extracts the InformerFactory from the context.
   110  func Get(ctx {{.contextContext|raw}}) {{.informersSharedInformerFactory|raw}} {
   111  	untyped := ctx.Value(Key{})
   112  	if untyped == nil {
   113  		{{.loggingFromContext|raw}}(ctx).Panic(
   114  			"Unable to fetch {{.informersSharedInformerFactory}} from context.")
   115  	}
   116  	return untyped.({{.informersSharedInformerFactory|raw}})
   117  }
   118  `