@@ -148,31 +148,34 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
148
148
extendedMethods = append (extendedMethods , extendedMethod )
149
149
}
150
150
m := map [string ]interface {}{
151
- "type" : t ,
152
- "inputType" : t ,
153
- "resultType" : t ,
154
- "package" : pkg ,
155
- "Package" : namer .IC (pkg ),
156
- "namespaced" : ! tags .NonNamespaced ,
157
- "Group" : namer .IC (g .group ),
158
- "subresource" : false ,
159
- "subresourcePath" : "" ,
160
- "GroupGoName" : g .groupGoName ,
161
- "Version" : namer .IC (g .version ),
162
- "CreateOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "CreateOptions" }),
163
- "DeleteOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "DeleteOptions" }),
164
- "GetOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "GetOptions" }),
165
- "ListOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "ListOptions" }),
166
- "PatchOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "PatchOptions" }),
167
- "ApplyOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "ApplyOptions" }),
168
- "UpdateOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "UpdateOptions" }),
169
- "PatchType" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/types" , Name : "PatchType" }),
170
- "ApplyPatchType" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/types" , Name : "ApplyPatchType" }),
171
- "watchInterface" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/watch" , Name : "Interface" }),
172
- "RESTClientInterface" : c .Universe .Type (types.Name {Package : "k8s.io/client-go/rest" , Name : "Interface" }),
173
- "schemeParameterCodec" : c .Universe .Variable (types.Name {Package : path .Join (g .clientsetPackage , "scheme" ), Name : "ParameterCodec" }),
174
- "jsonMarshal" : c .Universe .Type (types.Name {Package : "encoding/json" , Name : "Marshal" }),
175
- "CheckListFromCacheDataConsistencyIfRequested" : c .Universe .Function (types.Name {Package : "k8s.io/client-go/util/consistencydetector" , Name : "CheckListFromCacheDataConsistencyIfRequested" }),
151
+ "type" : t ,
152
+ "inputType" : t ,
153
+ "resultType" : t ,
154
+ "package" : pkg ,
155
+ "Package" : namer .IC (pkg ),
156
+ "namespaced" : ! tags .NonNamespaced ,
157
+ "Group" : namer .IC (g .group ),
158
+ "subresource" : false ,
159
+ "subresourcePath" : "" ,
160
+ "GroupGoName" : g .groupGoName ,
161
+ "Version" : namer .IC (g .version ),
162
+ "CreateOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "CreateOptions" }),
163
+ "DeleteOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "DeleteOptions" }),
164
+ "GetOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "GetOptions" }),
165
+ "ListOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "ListOptions" }),
166
+ "PatchOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "PatchOptions" }),
167
+ "ApplyOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "ApplyOptions" }),
168
+ "UpdateOptions" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "UpdateOptions" }),
169
+ "PatchType" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/types" , Name : "PatchType" }),
170
+ "ApplyPatchType" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/types" , Name : "ApplyPatchType" }),
171
+ "watchInterface" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/watch" , Name : "Interface" }),
172
+ "RESTClientInterface" : c .Universe .Type (types.Name {Package : "k8s.io/client-go/rest" , Name : "Interface" }),
173
+ "schemeParameterCodec" : c .Universe .Variable (types.Name {Package : path .Join (g .clientsetPackage , "scheme" ), Name : "ParameterCodec" }),
174
+ "jsonMarshal" : c .Universe .Type (types.Name {Package : "encoding/json" , Name : "Marshal" }),
175
+ "resourceVersionMatchNotOlderThan" : c .Universe .Type (types.Name {Package : "k8s.io/apimachinery/pkg/apis/meta/v1" , Name : "ResourceVersionMatchNotOlderThan" }),
176
+ "CheckListFromCacheDataConsistencyIfRequested" : c .Universe .Function (types.Name {Package : "k8s.io/client-go/util/consistencydetector" , Name : "CheckListFromCacheDataConsistencyIfRequested" }),
177
+ "CheckWatchListFromCacheDataConsistencyIfRequested" : c .Universe .Function (types.Name {Package : "k8s.io/client-go/util/consistencydetector" , Name : "CheckWatchListFromCacheDataConsistencyIfRequested" }),
178
+ "PrepareWatchListOptionsFromListOptions" : c .Universe .Function (types.Name {Package : "k8s.io/client-go/util/watchlist" , Name : "PrepareWatchListOptionsFromListOptions" }),
176
179
}
177
180
178
181
if generateApply {
@@ -225,6 +228,7 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
225
228
if tags .HasVerb ("list" ) {
226
229
sw .Do (listTemplate , m )
227
230
sw .Do (privateListTemplate , m )
231
+ sw .Do (watchListTemplate , m )
228
232
}
229
233
if tags .HasVerb ("watch" ) {
230
234
sw .Do (watchTemplate , m )
@@ -301,6 +305,7 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
301
305
} else {
302
306
sw .Do (adjustTemplate (e .VerbName , e .VerbType , listTemplate ), m )
303
307
sw .Do (adjustTemplate (e .VerbName , e .VerbType , privateListTemplate ), m )
308
+ sw .Do (adjustTemplate (e .VerbName , e .VerbType , watchListTemplate ), m )
304
309
}
305
310
}
306
311
@@ -461,13 +466,22 @@ func new$.type|publicPlural$(c *$.GroupGoName$$.Version$Client) *$.type|privateP
461
466
`
462
467
var listTemplate = `
463
468
// List takes label and field selectors, and returns the list of $.resultType|publicPlural$ that match those selectors.
464
- func (c *$.type|privatePlural$) List(ctx context.Context, opts $.ListOptions|raw$) (result *$.resultType|raw$List, err error) {
465
- defer func() {
469
+ func (c *$.type|privatePlural$) List(ctx context.Context, opts $.ListOptions|raw$) (*$.resultType|raw$List, error) {
470
+ if watchListOptions, hasWatchListOptionsPrepared, watchListOptionsErr := $.PrepareWatchListOptionsFromListOptions|raw$(opts); watchListOptionsErr != nil {
471
+ klog.Warningf("Failed preparing watchlist options for $.type|resource$, falling back to the standard LIST semantics, err = %v", watchListOptionsErr )
472
+ } else if hasWatchListOptionsPrepared {
473
+ result, err := c.watchList(ctx, watchListOptions)
466
474
if err == nil {
467
- $.CheckListFromCacheDataConsistencyIfRequested|raw$(ctx, "list request for $.type|resource$", c.list, opts, result)
475
+ $.CheckWatchListFromCacheDataConsistencyIfRequested|raw$(ctx, "watchlist request for $.type|resource$", c.list, opts, result)
476
+ return result, nil
468
477
}
469
- }()
470
- return c.list(ctx, opts)
478
+ klog.Warningf("The watchlist request for $.type|resource$ ended with an error, falling back to the standard LIST semantics, err = %v", err)
479
+ }
480
+ result, err := c.list(ctx, opts)
481
+ if err == nil {
482
+ $.CheckListFromCacheDataConsistencyIfRequested|raw$(ctx, "list request for $.type|resource$", c.list, opts, result)
483
+ }
484
+ return result, err
471
485
}
472
486
`
473
487
@@ -673,6 +687,25 @@ func (c *$.type|privatePlural$) Watch(ctx context.Context, opts $.ListOptions|ra
673
687
}
674
688
`
675
689
690
+ var watchListTemplate = `
691
+ // watchList establishes a watch stream with the server and returns the list of $.resultType|publicPlural$
692
+ func (c *$.type|privatePlural$) watchList(ctx context.Context, opts $.ListOptions|raw$) (result *$.resultType|raw$List, err error) {
693
+ var timeout time.Duration
694
+ if opts.TimeoutSeconds != nil{
695
+ timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
696
+ }
697
+ result = &$.resultType|raw$List{}
698
+ err = c.client.Get().
699
+ $if .namespaced$Namespace(c.ns).$end$
700
+ Resource("$.type|resource$").
701
+ VersionedParams(&opts, $.schemeParameterCodec|raw$).
702
+ Timeout(timeout).
703
+ WatchList(ctx).
704
+ Into(result)
705
+ return
706
+ }
707
+ `
708
+
676
709
var patchTemplate = `
677
710
// Patch applies the patch and returns the patched $.resultType|private$.
678
711
func (c *$.type|privatePlural$) Patch(ctx context.Context, name string, pt $.PatchType|raw$, data []byte, opts $.PatchOptions|raw$, subresources ...string) (result *$.resultType|raw$, err error) {
0 commit comments