Skip to content

Commit 5de3c1e

Browse files
authored
Merge pull request kubernetes#127292 from skitt/fix-client-go-extensions-without-test
client-go: add missing template functions and types for extensions
2 parents 36bbdd6 + 13dfa4c commit 5de3c1e

File tree

29 files changed

+1804
-16
lines changed

29 files changed

+1804
-16
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
140140
"UpdateOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "UpdateOptions"}),
141141
"ApplyOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ApplyOptions"}),
142142
"PatchType": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/types", Name: "PatchType"}),
143-
"jsonMarshal": c.Universe.Type(types.Name{Package: "encoding/json", Name: "Marshal"}),
143+
"PatchOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "PatchOptions"}),
144+
"jsonMarshal": c.Universe.Function(types.Name{Package: "encoding/json", Name: "Marshal"}),
144145
"context": c.Universe.Type(types.Name{Package: "context", Name: "Context"}),
145146
},
146147
}
@@ -173,9 +174,12 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
173174
"watchInterface": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/watch", Name: "Interface"}),
174175
"RESTClientInterface": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Interface"}),
175176
"schemeParameterCodec": c.Universe.Variable(types.Name{Package: path.Join(g.clientsetPackage, "scheme"), Name: "ParameterCodec"}),
176-
"jsonMarshal": c.Universe.Type(types.Name{Package: "encoding/json", Name: "Marshal"}),
177-
"fmtErrorf": c.Universe.Type(types.Name{Package: "fmt", Name: "Errorf"}),
177+
"jsonMarshal": c.Universe.Function(types.Name{Package: "encoding/json", Name: "Marshal"}),
178+
"fmtErrorf": c.Universe.Function(types.Name{Package: "fmt", Name: "Errorf"}),
179+
"klogWarningf": c.Universe.Function(types.Name{Package: "k8s.io/klog/v2", Name: "Warningf"}),
178180
"context": c.Universe.Type(types.Name{Package: "context", Name: "Context"}),
181+
"timeDuration": c.Universe.Type(types.Name{Package: "time", Name: "Duration"}),
182+
"timeSecond": c.Universe.Type(types.Name{Package: "time", Name: "Second"}),
179183
"resourceVersionMatchNotOlderThan": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ResourceVersionMatchNotOlderThan"}),
180184
"CheckListFromCacheDataConsistencyIfRequested": c.Universe.Function(types.Name{Package: "k8s.io/client-go/util/consistencydetector", Name: "CheckListFromCacheDataConsistencyIfRequested"}),
181185
"CheckWatchListFromCacheDataConsistencyIfRequested": c.Universe.Function(types.Name{Package: "k8s.io/client-go/util/consistencydetector", Name: "CheckWatchListFromCacheDataConsistencyIfRequested"}),
@@ -580,14 +584,14 @@ var listTemplate = `
580584
// $.verb$ takes label and field selectors, and returns the list of $.resultType|publicPlural$ that match those selectors.
581585
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, opts $.ListOptions|raw$) (*$.resultType|raw$List, error) {
582586
if watchListOptions, hasWatchListOptionsPrepared, watchListOptionsErr := $.PrepareWatchListOptionsFromListOptions|raw$(opts); watchListOptionsErr != nil {
583-
klog.Warningf("Failed preparing watchlist options for $.type|resource$, falling back to the standard LIST semantics, err = %v", watchListOptionsErr )
587+
$.klogWarningf|raw$("Failed preparing watchlist options for $.type|resource$, falling back to the standard LIST semantics, err = %v", watchListOptionsErr )
584588
} else if hasWatchListOptionsPrepared {
585589
result, err := c.watchList(ctx, watchListOptions)
586590
if err == nil {
587591
$.CheckWatchListFromCacheDataConsistencyIfRequested|raw$(ctx, "watchlist request for $.type|resource$", c.list, opts, result)
588592
return result, nil
589593
}
590-
klog.Warningf("The watchlist request for $.type|resource$ ended with an error, falling back to the standard LIST semantics, err = %v", err)
594+
$.klogWarningf|raw$("The watchlist request for $.type|resource$ ended with an error, falling back to the standard LIST semantics, err = %v", err)
591595
}
592596
result, err := c.list(ctx, opts)
593597
if err == nil {
@@ -600,9 +604,9 @@ func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, opts $.ListOptions|r
600604
var privateListTemplate = `
601605
// list takes label and field selectors, and returns the list of $.resultType|publicPlural$ that match those selectors.
602606
func (c *$.type|privatePlural$) list(ctx $.context|raw$, opts $.ListOptions|raw$) (result *$.resultType|raw$List, err error) {
603-
var timeout time.Duration
607+
var timeout $.timeDuration|raw$
604608
if opts.TimeoutSeconds != nil{
605-
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
609+
timeout = $.timeDuration|raw$(*opts.TimeoutSeconds) * $.timeSecond|raw$
606610
}
607611
result = &$.resultType|raw$List{}
608612
err = c.GetClient().Get().
@@ -619,9 +623,9 @@ func (c *$.type|privatePlural$) list(ctx $.context|raw$, opts $.ListOptions|raw$
619623
var listSubresourceTemplate = `
620624
// $.verb$ takes $.type|raw$ name, label and field selectors, and returns the list of $.resultType|publicPlural$ that match those selectors.
621625
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, $.type|private$Name string, opts $.ListOptions|raw$) (result *$.resultType|raw$List, err error) {
622-
var timeout time.Duration
626+
var timeout $.timeDuration|raw$
623627
if opts.TimeoutSeconds != nil{
624-
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
628+
timeout = $.timeDuration|raw$(*opts.TimeoutSeconds) * $.timeSecond|raw$
625629
}
626630
result = &$.resultType|raw$List{}
627631
err = c.GetClient().Get().
@@ -749,9 +753,9 @@ func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, $.inputType|private$
749753
var watchTemplate = `
750754
// $.verb$ returns a $.watchInterface|raw$ that watches the requested $.type|privatePlural$.
751755
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, opts $.ListOptions|raw$) ($.watchInterface|raw$, error) {
752-
var timeout time.Duration
756+
var timeout $.timeDuration|raw$
753757
if opts.TimeoutSeconds != nil{
754-
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
758+
timeout = $.timeDuration|raw$(*opts.TimeoutSeconds) * $.timeSecond|raw$
755759
}
756760
opts.Watch = true
757761
return c.GetClient().Get().
@@ -765,13 +769,13 @@ func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, opts $.ListOptions|r
765769
var watchListTemplate = `
766770
// watchList establishes a watch stream with the server and returns the list of $.resultType|publicPlural$
767771
func (c *$.type|privatePlural$) watchList(ctx $.context|raw$, opts $.ListOptions|raw$) (result *$.resultType|raw$List, err error) {
768-
var timeout time.Duration
772+
var timeout $.timeDuration|raw$
769773
if opts.TimeoutSeconds != nil{
770-
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
774+
timeout = $.timeDuration|raw$(*opts.TimeoutSeconds) * $.timeSecond|raw$
771775
}
772776
result = &$.resultType|raw$List{}
773-
err = c.client.Get().
774-
$if .namespaced$Namespace(c.ns).$end$
777+
err = c.GetClient().Get().
778+
$if .namespaced$Namespace(c.GetNamespace()).$end$
775779
Resource("$.type|resource$").
776780
VersionedParams(&opts, $.schemeParameterCodec|raw$).
777781
Timeout(timeout).
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright 2024 The Kubernetes 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+
// +k8s:deepcopy-gen=package
18+
// +k8s:defaulter-gen=TypeMeta
19+
// +groupName=extensions.test.crd.code-generator.k8s.io
20+
// +groupGoName=ExtensionsExample
21+
22+
package v1 // import "k8s.io/code-generator/examples/crd/apis/extensions/v1"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2024 The Kubernetes 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 v1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/apimachinery/pkg/runtime"
22+
"k8s.io/apimachinery/pkg/runtime/schema"
23+
)
24+
25+
var SchemeGroupVersion = schema.GroupVersion{Group: "extensions.test.crd.code-generator.k8s.io", Version: "v1"}
26+
27+
var (
28+
// TODO: move SchemeBuilder with zz_generated.deepcopy.go to k8s.io/api.
29+
// localSchemeBuilder and AddToScheme will stay in k8s.io/kubernetes.
30+
SchemeBuilder runtime.SchemeBuilder
31+
localSchemeBuilder = &SchemeBuilder
32+
AddToScheme = localSchemeBuilder.AddToScheme
33+
)
34+
35+
func init() {
36+
// We only register manually written functions here. The registration of the
37+
// generated functions takes place in the generated files. The separation
38+
// makes the code compile even when the generated files are missing.
39+
localSchemeBuilder.Register(addKnownTypes)
40+
}
41+
42+
// Resource takes an unqualified resource and returns a Group qualified GroupResource
43+
func Resource(resource string) schema.GroupResource {
44+
return SchemeGroupVersion.WithResource(resource).GroupResource()
45+
}
46+
47+
// Adds the list of known types to api.Scheme.
48+
func addKnownTypes(scheme *runtime.Scheme) error {
49+
scheme.AddKnownTypes(SchemeGroupVersion,
50+
&TestType{},
51+
&TestTypeList{},
52+
)
53+
54+
scheme.AddKnownTypes(SchemeGroupVersion,
55+
&metav1.Status{},
56+
)
57+
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
58+
return nil
59+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2024 The Kubernetes 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 v1
18+
19+
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
21+
// +genclient
22+
// +genclient:method=GetExtended,verb=get
23+
// +genclient:method=ListExtended,verb=list
24+
// +genclient:method=CreateExtended,verb=create
25+
// +genclient:method=UpdateExtended,verb=update
26+
// +genclient:method=PatchExtended,verb=patch
27+
// +genclient:method=ApplyExtended,verb=apply
28+
// +genclient:method=GetSubresource,verb=get,subresource=testsubresource,result=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource
29+
// +genclient:method=CreateSubresource,verb=create,subresource=testsubresource,input=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource,result=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource
30+
// +genclient:method=UpdateSubresource,verb=update,subresource=subresource,input=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource,result=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource
31+
// +genclient:method=ApplySubresource,verb=apply,subresource=subresource,input=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource,result=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource
32+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
33+
34+
// TestType is a top-level type. A client is created for it.
35+
type TestType struct {
36+
metav1.TypeMeta `json:",inline"`
37+
// +optional
38+
metav1.ObjectMeta `json:"metadata,omitempty"`
39+
// +optional
40+
Status TestTypeStatus `json:"status,omitempty"`
41+
}
42+
43+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
44+
45+
type TestSubresource struct {
46+
metav1.TypeMeta `json:",inline"`
47+
48+
Name string `json:"name"`
49+
}
50+
51+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
52+
53+
// TestTypeList is a top-level list type. The client methods for lists are automatically created.
54+
// You are not supposed to create a separate client for this one.
55+
type TestTypeList struct {
56+
metav1.TypeMeta `json:",inline"`
57+
// +optional
58+
metav1.ListMeta `json:"metadata,omitempty"`
59+
60+
Items []TestType `json:"items"`
61+
}
62+
63+
type TestTypeStatus struct {
64+
Blah string `json:"blah"`
65+
}

staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/zz_generated.deepcopy.go

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)