mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
client-gen: intro watchList
This commit is contained in:
parent
7943c17867
commit
dceabab838
@ -172,7 +172,10 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
|
|||||||
"RESTClientInterface": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Interface"}),
|
"RESTClientInterface": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Interface"}),
|
||||||
"schemeParameterCodec": c.Universe.Variable(types.Name{Package: path.Join(g.clientsetPackage, "scheme"), Name: "ParameterCodec"}),
|
"schemeParameterCodec": c.Universe.Variable(types.Name{Package: path.Join(g.clientsetPackage, "scheme"), Name: "ParameterCodec"}),
|
||||||
"jsonMarshal": c.Universe.Type(types.Name{Package: "encoding/json", Name: "Marshal"}),
|
"jsonMarshal": c.Universe.Type(types.Name{Package: "encoding/json", Name: "Marshal"}),
|
||||||
|
"resourceVersionMatchNotOlderThan": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ResourceVersionMatchNotOlderThan"}),
|
||||||
"CheckListFromCacheDataConsistencyIfRequested": c.Universe.Function(types.Name{Package: "k8s.io/client-go/util/consistencydetector", Name: "CheckListFromCacheDataConsistencyIfRequested"}),
|
"CheckListFromCacheDataConsistencyIfRequested": c.Universe.Function(types.Name{Package: "k8s.io/client-go/util/consistencydetector", Name: "CheckListFromCacheDataConsistencyIfRequested"}),
|
||||||
|
"CheckWatchListFromCacheDataConsistencyIfRequested": c.Universe.Function(types.Name{Package: "k8s.io/client-go/util/consistencydetector", Name: "CheckWatchListFromCacheDataConsistencyIfRequested"}),
|
||||||
|
"PrepareWatchListOptionsFromListOptions": c.Universe.Function(types.Name{Package: "k8s.io/client-go/util/watchlist", Name: "PrepareWatchListOptionsFromListOptions"}),
|
||||||
}
|
}
|
||||||
|
|
||||||
if generateApply {
|
if generateApply {
|
||||||
@ -225,6 +228,7 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
|
|||||||
if tags.HasVerb("list") {
|
if tags.HasVerb("list") {
|
||||||
sw.Do(listTemplate, m)
|
sw.Do(listTemplate, m)
|
||||||
sw.Do(privateListTemplate, m)
|
sw.Do(privateListTemplate, m)
|
||||||
|
sw.Do(watchListTemplate, m)
|
||||||
}
|
}
|
||||||
if tags.HasVerb("watch") {
|
if tags.HasVerb("watch") {
|
||||||
sw.Do(watchTemplate, m)
|
sw.Do(watchTemplate, m)
|
||||||
@ -301,6 +305,7 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
|
|||||||
} else {
|
} else {
|
||||||
sw.Do(adjustTemplate(e.VerbName, e.VerbType, listTemplate), m)
|
sw.Do(adjustTemplate(e.VerbName, e.VerbType, listTemplate), m)
|
||||||
sw.Do(adjustTemplate(e.VerbName, e.VerbType, privateListTemplate), m)
|
sw.Do(adjustTemplate(e.VerbName, e.VerbType, privateListTemplate), m)
|
||||||
|
sw.Do(adjustTemplate(e.VerbName, e.VerbType, watchListTemplate), m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -461,13 +466,22 @@ func new$.type|publicPlural$(c *$.GroupGoName$$.Version$Client) *$.type|privateP
|
|||||||
`
|
`
|
||||||
var listTemplate = `
|
var listTemplate = `
|
||||||
// List takes label and field selectors, and returns the list of $.resultType|publicPlural$ that match those selectors.
|
// List takes label and field selectors, and returns the list of $.resultType|publicPlural$ that match those selectors.
|
||||||
func (c *$.type|privatePlural$) List(ctx context.Context, opts $.ListOptions|raw$) (result *$.resultType|raw$List, err error) {
|
func (c *$.type|privatePlural$) List(ctx context.Context, opts $.ListOptions|raw$) (*$.resultType|raw$List, error) {
|
||||||
defer func() {
|
if watchListOptions, hasWatchListOptionsPrepared, watchListOptionsErr := $.PrepareWatchListOptionsFromListOptions|raw$(opts); watchListOptionsErr != nil {
|
||||||
|
klog.Warningf("Failed preparing watchlist options for $.type|resource$, falling back to the standard LIST semantics, err = %v", watchListOptionsErr )
|
||||||
|
} else if hasWatchListOptionsPrepared {
|
||||||
|
result, err := c.watchList(ctx, watchListOptions)
|
||||||
|
if err == nil {
|
||||||
|
$.CheckWatchListFromCacheDataConsistencyIfRequested|raw$(ctx, "watchlist request for $.type|resource$", c.list, opts, result)
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
klog.Warningf("The watchlist request for $.type|resource$ ended with an error, falling back to the standard LIST semantics, err = %v", err)
|
||||||
|
}
|
||||||
|
result, err := c.list(ctx, opts)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
$.CheckListFromCacheDataConsistencyIfRequested|raw$(ctx, "list request for $.type|resource$", c.list, opts, result)
|
$.CheckListFromCacheDataConsistencyIfRequested|raw$(ctx, "list request for $.type|resource$", c.list, opts, result)
|
||||||
}
|
}
|
||||||
}()
|
return result, err
|
||||||
return c.list(ctx, opts)
|
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
@ -673,6 +687,25 @@ func (c *$.type|privatePlural$) Watch(ctx context.Context, opts $.ListOptions|ra
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var watchListTemplate = `
|
||||||
|
// watchList establishes a watch stream with the server and returns the list of $.resultType|publicPlural$
|
||||||
|
func (c *$.type|privatePlural$) watchList(ctx context.Context, opts $.ListOptions|raw$) (result *$.resultType|raw$List, err error) {
|
||||||
|
var timeout time.Duration
|
||||||
|
if opts.TimeoutSeconds != nil{
|
||||||
|
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
|
||||||
|
}
|
||||||
|
result = &$.resultType|raw$List{}
|
||||||
|
err = c.client.Get().
|
||||||
|
$if .namespaced$Namespace(c.ns).$end$
|
||||||
|
Resource("$.type|resource$").
|
||||||
|
VersionedParams(&opts, $.schemeParameterCodec|raw$).
|
||||||
|
Timeout(timeout).
|
||||||
|
WatchList(ctx).
|
||||||
|
Into(result)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
var patchTemplate = `
|
var patchTemplate = `
|
||||||
// Patch applies the patch and returns the patched $.resultType|private$.
|
// Patch applies the patch and returns the patched $.resultType|private$.
|
||||||
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) {
|
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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user