Skip to content

Commit 6adc411

Browse files
committed
Remove hard coded references in generated code
1 parent 1606be6 commit 6adc411

File tree

11 files changed

+139
-117
lines changed

11 files changed

+139
-117
lines changed

staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/internal.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ func (g *internalGenerator) GenerateType(c *generator.Context, _ *types.Type, w
7474
"schemaYAML": string(schemaYAML),
7575
"smdParser": smdParser,
7676
"smdNewParser": smdNewParser,
77+
"fmtSprintf": fmtSprintf,
78+
"syncOnce": syncOnce,
7779
"yamlObject": yamlObject,
7880
"yamlUnmarshal": yamlUnmarshal,
7981
})
@@ -87,13 +89,13 @@ func Parser() *{{.smdParser|raw}} {
8789
var err error
8890
parser, err = {{.smdNewParser|raw}}(schemaYAML)
8991
if err != nil {
90-
panic(fmt.Sprintf("Failed to parse schema: %v", err))
92+
panic({{.fmtSprintf|raw}}("Failed to parse schema: %v", err))
9193
}
9294
})
9395
return parser
9496
}
9597
96-
var parserOnce sync.Once
98+
var parserOnce {{.syncOnce|raw}}
9799
var parser *{{.smdParser|raw}}
98100
var schemaYAML = {{.yamlObject|raw}}(` + "`{{.schemaYAML}}`" + `)
99101
`

staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package generators
1919
import "k8s.io/gengo/v2/types"
2020

2121
var (
22+
fmtSprintf = types.Ref("fmt", "Sprintf")
23+
syncOnce = types.Ref("sync", "Once")
2224
applyConfiguration = types.Ref("k8s.io/apimachinery/pkg/runtime", "ApplyConfiguration")
2325
groupVersionKind = types.Ref("k8s.io/apimachinery/pkg/runtime/schema", "GroupVersionKind")
2426
typeMeta = types.Ref("k8s.io/apimachinery/pkg/apis/meta/v1", "TypeMeta")

staging/src/k8s.io/code-generator/cmd/client-gen/generators/fake/generator_fake_for_type.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
123123
"ApplyPatchType": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/types", Name: "ApplyPatchType"}),
124124
"watchInterface": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/watch", Name: "Interface"}),
125125
"jsonMarshal": c.Universe.Type(types.Name{Package: "encoding/json", Name: "Marshal"}),
126+
"fmtErrorf": c.Universe.Type(types.Name{Package: "fmt", Name: "Errorf"}),
127+
"contextContext": c.Universe.Type(types.Name{Package: "context", Name: "Context"}),
126128

127129
"NewRootListActionWithOptions": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewRootListActionWithOptions"}),
128130
"NewListActionWithOptions": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewListActionWithOptions"}),
@@ -332,7 +334,7 @@ var $.type|allLowercasePlural$Kind = $.SchemeGroupVersion|raw$.WithKind("$.type|
332334

333335
var listTemplate = `
334336
// List takes label and field selectors, and returns the list of $.type|publicPlural$ that match those selectors.
335-
func (c *Fake$.type|publicPlural$) List(ctx context.Context, opts $.ListOptions|raw$) (result *$.type|raw$List, err error) {
337+
func (c *Fake$.type|publicPlural$) List(ctx $.contextContext|raw$, opts $.ListOptions|raw$) (result *$.type|raw$List, err error) {
336338
emptyResult := &$.type|raw$List{}
337339
obj, err := c.Fake.
338340
$if .namespaced$Invokes($.NewListActionWithOptions|raw$($.type|allLowercasePlural$Resource, $.type|allLowercasePlural$Kind, c.ns, opts), emptyResult)
@@ -346,7 +348,7 @@ func (c *Fake$.type|publicPlural$) List(ctx context.Context, opts $.ListOptions|
346348

347349
var listUsingOptionsTemplate = `
348350
// List takes label and field selectors, and returns the list of $.type|publicPlural$ that match those selectors.
349-
func (c *Fake$.type|publicPlural$) List(ctx context.Context, opts $.ListOptions|raw$) (result *$.type|raw$List, err error) {
351+
func (c *Fake$.type|publicPlural$) List(ctx $.contextContext|raw$, opts $.ListOptions|raw$) (result *$.type|raw$List, err error) {
350352
emptyResult := &$.type|raw$List{}
351353
obj, err := c.Fake.
352354
$if .namespaced$Invokes($.NewListActionWithOptions|raw$($.type|allLowercasePlural$Resource, $.type|allLowercasePlural$Kind, c.ns, opts), emptyResult)
@@ -371,7 +373,7 @@ func (c *Fake$.type|publicPlural$) List(ctx context.Context, opts $.ListOptions|
371373

372374
var getTemplate = `
373375
// Get takes name of the $.type|private$, and returns the corresponding $.resultType|private$ object, and an error if there is any.
374-
func (c *Fake$.type|publicPlural$) Get(ctx context.Context, name string, options $.GetOptions|raw$) (result *$.resultType|raw$, err error) {
376+
func (c *Fake$.type|publicPlural$) Get(ctx $.contextContext|raw$, name string, options $.GetOptions|raw$) (result *$.resultType|raw$, err error) {
375377
emptyResult := &$.resultType|raw${}
376378
obj, err := c.Fake.
377379
$if .namespaced$Invokes($.NewGetActionWithOptions|raw$($.type|allLowercasePlural$Resource, c.ns, name, options), emptyResult)
@@ -385,7 +387,7 @@ func (c *Fake$.type|publicPlural$) Get(ctx context.Context, name string, options
385387

386388
var getSubresourceTemplate = `
387389
// Get takes name of the $.type|private$, and returns the corresponding $.resultType|private$ object, and an error if there is any.
388-
func (c *Fake$.type|publicPlural$) Get(ctx context.Context, $.type|private$Name string, options $.GetOptions|raw$) (result *$.resultType|raw$, err error) {
390+
func (c *Fake$.type|publicPlural$) Get(ctx $.contextContext|raw$, $.type|private$Name string, options $.GetOptions|raw$) (result *$.resultType|raw$, err error) {
389391
emptyResult := &$.resultType|raw${}
390392
obj, err := c.Fake.
391393
$if .namespaced$Invokes($.NewGetSubresourceActionWithOptions|raw$($.type|allLowercasePlural$Resource, c.ns, "$.subresourcePath$", $.type|private$Name, options), emptyResult)
@@ -399,7 +401,7 @@ func (c *Fake$.type|publicPlural$) Get(ctx context.Context, $.type|private$Name
399401

400402
var deleteTemplate = `
401403
// Delete takes name of the $.type|private$ and deletes it. Returns an error if one occurs.
402-
func (c *Fake$.type|publicPlural$) Delete(ctx context.Context, name string, opts $.DeleteOptions|raw$) error {
404+
func (c *Fake$.type|publicPlural$) Delete(ctx $.contextContext|raw$, name string, opts $.DeleteOptions|raw$) error {
403405
_, err := c.Fake.
404406
$if .namespaced$Invokes($.NewDeleteActionWithOptions|raw$($.type|allLowercasePlural$Resource, c.ns, name, opts), &$.type|raw${})
405407
$else$Invokes($.NewRootDeleteActionWithOptions|raw$($.type|allLowercasePlural$Resource, name, opts), &$.type|raw${})$end$
@@ -409,7 +411,7 @@ func (c *Fake$.type|publicPlural$) Delete(ctx context.Context, name string, opts
409411

410412
var deleteCollectionTemplate = `
411413
// DeleteCollection deletes a collection of objects.
412-
func (c *Fake$.type|publicPlural$) DeleteCollection(ctx context.Context, opts $.DeleteOptions|raw$, listOpts $.ListOptions|raw$) error {
414+
func (c *Fake$.type|publicPlural$) DeleteCollection(ctx $.contextContext|raw$, opts $.DeleteOptions|raw$, listOpts $.ListOptions|raw$) error {
413415
$if .namespaced$action := $.NewDeleteCollectionActionWithOptions|raw$($.type|allLowercasePlural$Resource, c.ns, opts, listOpts)
414416
$else$action := $.NewRootDeleteCollectionActionWithOptions|raw$($.type|allLowercasePlural$Resource, opts, listOpts)
415417
$end$
@@ -419,7 +421,7 @@ func (c *Fake$.type|publicPlural$) DeleteCollection(ctx context.Context, opts $.
419421
`
420422
var createTemplate = `
421423
// Create takes the representation of a $.inputType|private$ and creates it. Returns the server's representation of the $.resultType|private$, and an error, if there is any.
422-
func (c *Fake$.type|publicPlural$) Create(ctx context.Context, $.inputType|private$ *$.inputType|raw$, opts $.CreateOptions|raw$) (result *$.resultType|raw$, err error) {
424+
func (c *Fake$.type|publicPlural$) Create(ctx $.contextContext|raw$, $.inputType|private$ *$.inputType|raw$, opts $.CreateOptions|raw$) (result *$.resultType|raw$, err error) {
423425
emptyResult := &$.resultType|raw${}
424426
obj, err := c.Fake.
425427
$if .namespaced$Invokes($.NewCreateActionWithOptions|raw$($.inputType|allLowercasePlural$Resource, c.ns, $.inputType|private$, opts), emptyResult)
@@ -433,7 +435,7 @@ func (c *Fake$.type|publicPlural$) Create(ctx context.Context, $.inputType|priva
433435

434436
var createSubresourceTemplate = `
435437
// Create takes the representation of a $.inputType|private$ and creates it. Returns the server's representation of the $.resultType|private$, and an error, if there is any.
436-
func (c *Fake$.type|publicPlural$) Create(ctx context.Context, $.type|private$Name string, $.inputType|private$ *$.inputType|raw$, opts $.CreateOptions|raw$) (result *$.resultType|raw$, err error) {
438+
func (c *Fake$.type|publicPlural$) Create(ctx $.contextContext|raw$, $.type|private$Name string, $.inputType|private$ *$.inputType|raw$, opts $.CreateOptions|raw$) (result *$.resultType|raw$, err error) {
437439
emptyResult := &$.resultType|raw${}
438440
obj, err := c.Fake.
439441
$if .namespaced$Invokes($.NewCreateSubresourceActionWithOptions|raw$($.type|allLowercasePlural$Resource, $.type|private$Name, "$.subresourcePath$", c.ns, $.inputType|private$, opts), emptyResult)
@@ -447,7 +449,7 @@ func (c *Fake$.type|publicPlural$) Create(ctx context.Context, $.type|private$Na
447449

448450
var updateTemplate = `
449451
// Update takes the representation of a $.inputType|private$ and updates it. Returns the server's representation of the $.resultType|private$, and an error, if there is any.
450-
func (c *Fake$.type|publicPlural$) Update(ctx context.Context, $.inputType|private$ *$.inputType|raw$, opts $.UpdateOptions|raw$) (result *$.resultType|raw$, err error) {
452+
func (c *Fake$.type|publicPlural$) Update(ctx $.contextContext|raw$, $.inputType|private$ *$.inputType|raw$, opts $.UpdateOptions|raw$) (result *$.resultType|raw$, err error) {
451453
emptyResult := &$.resultType|raw${}
452454
obj, err := c.Fake.
453455
$if .namespaced$Invokes($.NewUpdateActionWithOptions|raw$($.inputType|allLowercasePlural$Resource, c.ns, $.inputType|private$, opts), emptyResult)
@@ -461,7 +463,7 @@ func (c *Fake$.type|publicPlural$) Update(ctx context.Context, $.inputType|priva
461463

462464
var updateSubresourceTemplate = `
463465
// Update takes the representation of a $.inputType|private$ and updates it. Returns the server's representation of the $.resultType|private$, and an error, if there is any.
464-
func (c *Fake$.type|publicPlural$) Update(ctx context.Context, $.type|private$Name string, $.inputType|private$ *$.inputType|raw$, opts $.UpdateOptions|raw$) (result *$.resultType|raw$, err error) {
466+
func (c *Fake$.type|publicPlural$) Update(ctx $.contextContext|raw$, $.type|private$Name string, $.inputType|private$ *$.inputType|raw$, opts $.UpdateOptions|raw$) (result *$.resultType|raw$, err error) {
465467
emptyResult := &$.resultType|raw${}
466468
obj, err := c.Fake.
467469
$if .namespaced$Invokes($.NewUpdateSubresourceActionWithOptions|raw$($.type|allLowercasePlural$Resource, "$.subresourcePath$", c.ns, $.inputType|private$, opts), &$.inputType|raw${})
@@ -476,7 +478,7 @@ func (c *Fake$.type|publicPlural$) Update(ctx context.Context, $.type|private$Na
476478
var updateStatusTemplate = `
477479
// UpdateStatus was generated because the type contains a Status member.
478480
// Add a +genclient:noStatus comment above the type to avoid generating UpdateStatus().
479-
func (c *Fake$.type|publicPlural$) UpdateStatus(ctx context.Context, $.type|private$ *$.type|raw$, opts $.UpdateOptions|raw$) (result *$.type|raw$, err error) {
481+
func (c *Fake$.type|publicPlural$) UpdateStatus(ctx $.contextContext|raw$, $.type|private$ *$.type|raw$, opts $.UpdateOptions|raw$) (result *$.type|raw$, err error) {
480482
emptyResult := &$.type|raw${}
481483
obj, err := c.Fake.
482484
$if .namespaced$Invokes($.NewUpdateSubresourceActionWithOptions|raw$($.type|allLowercasePlural$Resource, "status", c.ns, $.type|private$, opts), emptyResult)
@@ -490,7 +492,7 @@ func (c *Fake$.type|publicPlural$) UpdateStatus(ctx context.Context, $.type|priv
490492

491493
var watchTemplate = `
492494
// Watch returns a $.watchInterface|raw$ that watches the requested $.type|privatePlural$.
493-
func (c *Fake$.type|publicPlural$) Watch(ctx context.Context, opts $.ListOptions|raw$) ($.watchInterface|raw$, error) {
495+
func (c *Fake$.type|publicPlural$) Watch(ctx $.contextContext|raw$, opts $.ListOptions|raw$) ($.watchInterface|raw$, error) {
494496
return c.Fake.
495497
$if .namespaced$InvokesWatch($.NewWatchActionWithOptions|raw$($.type|allLowercasePlural$Resource, c.ns, opts))
496498
$else$InvokesWatch($.NewRootWatchActionWithOptions|raw$($.type|allLowercasePlural$Resource, opts))$end$
@@ -499,7 +501,7 @@ func (c *Fake$.type|publicPlural$) Watch(ctx context.Context, opts $.ListOptions
499501

500502
var patchTemplate = `
501503
// Patch applies the patch and returns the patched $.resultType|private$.
502-
func (c *Fake$.type|publicPlural$) Patch(ctx context.Context, name string, pt $.PatchType|raw$, data []byte, opts $.PatchOptions|raw$, subresources ...string) (result *$.resultType|raw$, err error) {
504+
func (c *Fake$.type|publicPlural$) Patch(ctx $.contextContext|raw$, name string, pt $.PatchType|raw$, data []byte, opts $.PatchOptions|raw$, subresources ...string) (result *$.resultType|raw$, err error) {
503505
emptyResult := &$.resultType|raw${}
504506
obj, err := c.Fake.
505507
$if .namespaced$Invokes($.NewPatchSubresourceActionWithOptions|raw$($.type|allLowercasePlural$Resource, c.ns, name, pt, data, opts, subresources... ), emptyResult)
@@ -513,17 +515,17 @@ func (c *Fake$.type|publicPlural$) Patch(ctx context.Context, name string, pt $.
513515

514516
var applyTemplate = `
515517
// Apply takes the given apply declarative configuration, applies it and returns the applied $.resultType|private$.
516-
func (c *Fake$.type|publicPlural$) Apply(ctx context.Context, $.inputType|private$ *$.inputApplyConfig|raw$, opts $.ApplyOptions|raw$) (result *$.resultType|raw$, err error) {
518+
func (c *Fake$.type|publicPlural$) Apply(ctx $.contextContext|raw$, $.inputType|private$ *$.inputApplyConfig|raw$, opts $.ApplyOptions|raw$) (result *$.resultType|raw$, err error) {
517519
if $.inputType|private$ == nil {
518-
return nil, fmt.Errorf("$.inputType|private$ provided to Apply must not be nil")
520+
return nil, $.fmtErrorf|raw$("$.inputType|private$ provided to Apply must not be nil")
519521
}
520522
data, err := $.jsonMarshal|raw$($.inputType|private$)
521523
if err != nil {
522524
return nil, err
523525
}
524526
name := $.inputType|private$.Name
525527
if name == nil {
526-
return nil, fmt.Errorf("$.inputType|private$.Name must be provided to Apply")
528+
return nil, $.fmtErrorf|raw$("$.inputType|private$.Name must be provided to Apply")
527529
}
528530
emptyResult := &$.resultType|raw${}
529531
obj, err := c.Fake.
@@ -539,17 +541,17 @@ func (c *Fake$.type|publicPlural$) Apply(ctx context.Context, $.inputType|privat
539541
var applyStatusTemplate = `
540542
// ApplyStatus was generated because the type contains a Status member.
541543
// Add a +genclient:noStatus comment above the type to avoid generating ApplyStatus().
542-
func (c *Fake$.type|publicPlural$) ApplyStatus(ctx context.Context, $.inputType|private$ *$.inputApplyConfig|raw$, opts $.ApplyOptions|raw$) (result *$.resultType|raw$, err error) {
544+
func (c *Fake$.type|publicPlural$) ApplyStatus(ctx $.contextContext|raw$, $.inputType|private$ *$.inputApplyConfig|raw$, opts $.ApplyOptions|raw$) (result *$.resultType|raw$, err error) {
543545
if $.inputType|private$ == nil {
544-
return nil, fmt.Errorf("$.inputType|private$ provided to Apply must not be nil")
546+
return nil, $.fmtErrorf|raw$("$.inputType|private$ provided to Apply must not be nil")
545547
}
546548
data, err := $.jsonMarshal|raw$($.inputType|private$)
547549
if err != nil {
548550
return nil, err
549551
}
550552
name := $.inputType|private$.Name
551553
if name == nil {
552-
return nil, fmt.Errorf("$.inputType|private$.Name must be provided to Apply")
554+
return nil, $.fmtErrorf|raw$("$.inputType|private$.Name must be provided to Apply")
553555
}
554556
emptyResult := &$.resultType|raw${}
555557
obj, err := c.Fake.
@@ -565,9 +567,9 @@ func (c *Fake$.type|publicPlural$) ApplyStatus(ctx context.Context, $.inputType|
565567
var applySubresourceTemplate = `
566568
// Apply takes top resource name and the apply declarative configuration for $.subresourcePath$,
567569
// applies it and returns the applied $.resultType|private$, and an error, if there is any.
568-
func (c *Fake$.type|publicPlural$) Apply(ctx context.Context, $.type|private$Name string, $.inputType|private$ *$.inputApplyConfig|raw$, opts $.ApplyOptions|raw$) (result *$.resultType|raw$, err error) {
570+
func (c *Fake$.type|publicPlural$) Apply(ctx $.contextContext|raw$, $.type|private$Name string, $.inputType|private$ *$.inputApplyConfig|raw$, opts $.ApplyOptions|raw$) (result *$.resultType|raw$, err error) {
569571
if $.inputType|private$ == nil {
570-
return nil, fmt.Errorf("$.inputType|private$ provided to Apply must not be nil")
572+
return nil, $.fmtErrorf|raw$("$.inputType|private$ provided to Apply must not be nil")
571573
}
572574
data, err := $.jsonMarshal|raw$($.inputType|private$)
573575
if err != nil {

staging/src/k8s.io/code-generator/cmd/client-gen/generators/generator_for_clientset.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ func (g *genClientset) GenerateType(c *generator.Context, t *types.Type, w io.Wr
7373
allGroups := clientgentypes.ToGroupVersionInfo(g.groups, g.groupGoNames)
7474
m := map[string]interface{}{
7575
"allGroups": allGroups,
76+
"fmtErrorf": c.Universe.Type(types.Name{Package: "fmt", Name: "Errorf"}),
7677
"Config": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Config"}),
7778
"DefaultKubernetesUserAgent": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "DefaultKubernetesUserAgent"}),
7879
"RESTClientInterface": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Interface"}),
7980
"RESTHTTPClientFor": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "HTTPClientFor"}),
8081
"DiscoveryInterface": c.Universe.Type(types.Name{Package: "k8s.io/client-go/discovery", Name: "DiscoveryInterface"}),
8182
"DiscoveryClient": c.Universe.Type(types.Name{Package: "k8s.io/client-go/discovery", Name: "DiscoveryClient"}),
83+
"httpClient": c.Universe.Type(types.Name{Package: "net/http", Name: "Client"}),
8284
"NewDiscoveryClientForConfigAndClient": c.Universe.Function(types.Name{Package: "k8s.io/client-go/discovery", Name: "NewDiscoveryClientForConfigAndClient"}),
8385
"NewDiscoveryClientForConfigOrDie": c.Universe.Function(types.Name{Package: "k8s.io/client-go/discovery", Name: "NewDiscoveryClientForConfigOrDie"}),
8486
"NewDiscoveryClient": c.Universe.Function(types.Name{Package: "k8s.io/client-go/discovery", Name: "NewDiscoveryClient"}),
@@ -160,11 +162,11 @@ var newClientsetForConfigAndClientTemplate = `
160162
// Note the http client provided takes precedence over the configured transport values.
161163
// If config's RateLimiter is not set and QPS and Burst are acceptable,
162164
// NewForConfigAndClient will generate a rate-limiter in configShallowCopy.
163-
func NewForConfigAndClient(c *$.Config|raw$, httpClient *http.Client) (*Clientset, error) {
165+
func NewForConfigAndClient(c *$.Config|raw$, httpClient *$.httpClient|raw$) (*Clientset, error) {
164166
configShallowCopy := *c
165167
if configShallowCopy.RateLimiter == nil && configShallowCopy.QPS > 0 {
166168
if configShallowCopy.Burst <= 0 {
167-
return nil, fmt.Errorf("burst is required to be greater than 0 when RateLimiter is not set and QPS is set to greater than 0")
169+
return nil, $.fmtErrorf|raw$("burst is required to be greater than 0 when RateLimiter is not set and QPS is set to greater than 0")
168170
}
169171
configShallowCopy.RateLimiter = $.flowcontrolNewTokenBucketRateLimiter|raw$(configShallowCopy.QPS, configShallowCopy.Burst)
170172
}

0 commit comments

Comments
 (0)