Skip to content

Commit dceabab

Browse files
committed
client-gen: intro watchList
1 parent 7943c17 commit dceabab

File tree

1 file changed

+63
-30
lines changed

1 file changed

+63
-30
lines changed

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

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -148,31 +148,34 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
148148
extendedMethods = append(extendedMethods, extendedMethod)
149149
}
150150
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"}),
176179
}
177180

178181
if generateApply {
@@ -225,6 +228,7 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
225228
if tags.HasVerb("list") {
226229
sw.Do(listTemplate, m)
227230
sw.Do(privateListTemplate, m)
231+
sw.Do(watchListTemplate, m)
228232
}
229233
if tags.HasVerb("watch") {
230234
sw.Do(watchTemplate, m)
@@ -301,6 +305,7 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
301305
} else {
302306
sw.Do(adjustTemplate(e.VerbName, e.VerbType, listTemplate), m)
303307
sw.Do(adjustTemplate(e.VerbName, e.VerbType, privateListTemplate), m)
308+
sw.Do(adjustTemplate(e.VerbName, e.VerbType, watchListTemplate), m)
304309
}
305310
}
306311

@@ -461,13 +466,22 @@ func new$.type|publicPlural$(c *$.GroupGoName$$.Version$Client) *$.type|privateP
461466
`
462467
var listTemplate = `
463468
// 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)
466474
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
468477
}
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
471485
}
472486
`
473487

@@ -673,6 +687,25 @@ func (c *$.type|privatePlural$) Watch(ctx context.Context, opts $.ListOptions|ra
673687
}
674688
`
675689

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+
676709
var patchTemplate = `
677710
// Patch applies the patch and returns the patched $.resultType|private$.
678711
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

Comments
 (0)