Skip to content

Commit 8c922cc

Browse files
Merge pull request #68 from stevekuznetsov/skuznets/optional-apply-verbs
🐛 clientgen: conditionally generate apply verbs
2 parents 9c99a2d + f1e4456 commit 8c922cc

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

pkg/generators/clientgen/clientgen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type Generator struct {
4848

4949
// SingleClusterApplyConfigurationsPackagePath is the root directory under which single-cluster-aware apply configurations exist.
5050
// e.g. "k8s.io/client-go/applyconfigurations"
51-
SingleClusterApplyConfigurationsPackagePath string `marker:""`
51+
SingleClusterApplyConfigurationsPackagePath string `marker:",optional"`
5252

5353
// OutputPackagePath is the root directory under which this tool will output files.
5454
// e.g. "github.com/kcp-dev/client-go/clients"

pkg/internal/clientgen/fake_type.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,12 @@ func (c *FakeTypedClient) WriteContent(w io.Writer) error {
117117
"hasMethods": c.Kind.SupportedVerbs.Len() > 0 || len(c.Kind.Extensions) > 0,
118118
"needsApply": allVerbs.Has("apply"),
119119
"needsList": allVerbs.Has("list"),
120+
"needsPatch": allVerbs.Has("patch"),
120121
"apiPackagePath": c.APIPackagePath,
121122
"packagePath": c.PackagePath,
122123
"singleClusterClientPackagePath": c.SingleClusterClientPackagePath,
123124
"singleClusterApplyConfigurationsPackagePath": c.SingleClusterApplyConfigurationsPackagePath,
125+
"generateApplyVerbs": len(c.SingleClusterApplyConfigurationsPackagePath) > 0,
124126
}
125127
return templ.Execute(w, m)
126128
}
@@ -158,12 +160,14 @@ import (
158160
"k8s.io/client-go/testing"
159161
{{end}}
160162
161-
{{- if .needsApply }}
163+
{{- if and .generateApplyVerbs .needsApply }}
162164
"fmt"
163165
"encoding/json"
166+
{{end}}
167+
{{- if or (and .generateApplyVerbs .needsApply) .needsPatch }}
164168
"k8s.io/apimachinery/pkg/types"
165169
{{end}}
166-
{{- if "apply" | .kind.SupportedVerbs.Has }}
170+
{{- if and .generateApplyVerbs ("apply" | .kind.SupportedVerbs.Has) }}
167171
applyconfigurations{{.group.PackageAlias}} "{{.singleClusterApplyConfigurationsPackagePath}}/{{.group.Group.PackageName}}/{{.group.Version.PackageName}}"
168172
{{end}}
169173
@@ -330,7 +334,7 @@ func (c *{{.kind.Plural | lowerFirst}}Client) Patch(ctx context.Context, name st
330334
}
331335
{{end -}}
332336
333-
{{if "apply" | .kind.SupportedVerbs.Has}}
337+
{{if and .generateApplyVerbs ("apply" | .kind.SupportedVerbs.Has) }}
334338
func (c *{{.kind.Plural | lowerFirst}}Client) Apply(ctx context.Context, applyConfiguration *applyconfigurations{{.group.PackageAlias}}.{{.kind.String}}ApplyConfiguration, opts metav1.ApplyOptions) (*{{.group.PackageAlias}}.{{.kind.String}}, error) {
335339
if applyConfiguration == nil {
336340
return nil, fmt.Errorf("applyConfiguration provided to Apply must not be nil")
@@ -351,7 +355,7 @@ func (c *{{.kind.Plural | lowerFirst}}Client) Apply(ctx context.Context, applyCo
351355
}
352356
{{end -}}
353357
354-
{{if "applyStatus" | .kind.SupportedVerbs.Has}}
358+
{{if and .generateApplyVerbs ("applyStatus" | .kind.SupportedVerbs.Has) }}
355359
func (c *{{.kind.Plural | lowerFirst}}Client) ApplyStatus(ctx context.Context, applyConfiguration *applyconfigurations{{.group.PackageAlias}}.{{.kind.String}}ApplyConfiguration, opts metav1.ApplyOptions) (*{{.group.PackageAlias}}.{{.kind.String}}, error) {
356360
if applyConfiguration == nil {
357361
return nil, fmt.Errorf("applyConfiguration provided to Apply must not be nil")
@@ -435,7 +439,7 @@ func (c *{{$.kind.Plural | lowerFirst}}Client) {{.Method}}(ctx context.Context,
435439
}
436440
{{end -}}
437441
438-
{{if eq .Verb "apply"}}
442+
{{if and $.generateApplyVerbs (eq .Verb "apply") }}
439443
func (c *{{$.kind.Plural | lowerFirst}}Client) {{.Method}}(ctx context.Context, {{$.kind.String | lowerFirst}}Name string, applyConfiguration {{if .InputType}}*{{index $.extraImports .InputPath}}.{{.InputType}}{{else}}*applyconfigurations{{$.group.PackageAlias}}.{{$.kind.String}}ApplyConfiguration,{{end}}, opts metav1.ApplyOptions) (*{{index $.extraImports .ResultPath}}.{{if .ResultType}}{{.ResultType}}{{else}}{{$.kind.String}}{{end}}, error) {
440444
if applyConfiguration == nil {
441445
return nil, fmt.Errorf("applyConfiguration provided to Apply must not be nil")

0 commit comments

Comments
 (0)