knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/codegen/cmd/injection-gen/generators/client/client.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 client 18 19 import ( 20 "io" 21 "path/filepath" 22 23 "k8s.io/gengo/v2/generator" 24 "k8s.io/gengo/v2/namer" 25 "k8s.io/gengo/v2/types" 26 "k8s.io/klog/v2" 27 "knative.dev/pkg/codegen/cmd/injection-gen/args" 28 ) 29 30 // New creates a new clientGenerator with the given args 31 func New(args *args.Args) *clientGenerator { 32 return &clientGenerator{ 33 GoGenerator: generator.GoGenerator{ 34 OutputFilename: "client.go", 35 }, 36 outputPackage: filepath.Join(args.OutputPackagePath, "client"), 37 imports: generator.NewImportTracker(), 38 clientSetPackage: args.VersionedClientSetPackage, 39 } 40 } 41 42 // clientGenerator produces a file of listers for a given GroupVersion and 43 // type. 44 type clientGenerator struct { 45 generator.GoGenerator 46 47 outputPackage string 48 imports namer.ImportTracker 49 clientSetPackage string 50 filtered bool 51 } 52 53 func (g *clientGenerator) OutputPackagePath() string { 54 return g.outputPackage 55 } 56 57 var _ generator.Generator = (*clientGenerator)(nil) 58 59 func (g *clientGenerator) Filter(c *generator.Context, t *types.Type) bool { 60 // We generate a single client, so return true once. 61 if !g.filtered { 62 g.filtered = true 63 return true 64 } 65 return false 66 } 67 68 func (g *clientGenerator) Namers(c *generator.Context) namer.NameSystems { 69 return namer.NameSystems{ 70 "raw": namer.NewRawNamer(g.outputPackage, g.imports), 71 } 72 } 73 74 func (g *clientGenerator) Imports(c *generator.Context) (imports []string) { 75 imports = append(imports, g.imports.ImportLines()...) 76 return 77 } 78 79 func (g *clientGenerator) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error { 80 sw := generator.NewSnippetWriter(w, c, "{{", "}}") 81 82 klog.V(5).Info("processing type ", t) 83 84 m := map[string]interface{}{ 85 "clientSetNewForConfigOrDie": c.Universe.Function(types.Name{Package: g.clientSetPackage, Name: "NewForConfigOrDie"}), 86 "clientSetInterface": c.Universe.Type(types.Name{Package: g.clientSetPackage, Name: "Interface"}), 87 "injectionRegisterClient": c.Universe.Function(types.Name{Package: "knative.dev/pkg/injection", Name: "Default.RegisterClient"}), 88 "injectionRegisterClientFetcher": c.Universe.Function(types.Name{ 89 Package: "knative.dev/pkg/injection", 90 Name: "Default.RegisterClientFetcher", 91 }), 92 "restConfig": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Config"}), 93 "loggingFromContext": c.Universe.Function(types.Name{ 94 Package: "knative.dev/pkg/logging", 95 Name: "FromContext", 96 }), 97 "contextContext": c.Universe.Type(types.Name{ 98 Package: "context", 99 Name: "Context", 100 }), 101 } 102 103 sw.Do(injectionClient, m) 104 105 return sw.Error() 106 } 107 108 var injectionClient = ` 109 func init() { 110 {{.injectionRegisterClient|raw}}(withClientFromConfig) 111 {{.injectionRegisterClientFetcher|raw}}(func(ctx context.Context) interface{} { 112 return Get(ctx) 113 }) 114 } 115 116 // Key is used as the key for associating information with a context.Context. 117 type Key struct{} 118 119 func withClientFromConfig(ctx {{.contextContext|raw}}, cfg *{{.restConfig|raw}}) context.Context { 120 return context.WithValue(ctx, Key{}, {{.clientSetNewForConfigOrDie|raw}}(cfg)) 121 } 122 123 // Get extracts the {{.clientSetInterface|raw}} client from the context. 124 func Get(ctx {{.contextContext|raw}}) {{.clientSetInterface|raw}} { 125 untyped := ctx.Value(Key{}) 126 if untyped == nil { 127 if injection.GetConfig(ctx) == nil { 128 {{.loggingFromContext|raw}}(ctx).Panic( 129 "Unable to fetch {{.clientSetInterface}} from context. This context is not the application context (which is typically given to constructors via sharedmain).") 130 } else { 131 {{.loggingFromContext|raw}}(ctx).Panic( 132 "Unable to fetch {{.clientSetInterface}} from context.") 133 } 134 } 135 return untyped.({{.clientSetInterface|raw}}) 136 } 137 `