mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 16:29:21 +00:00
client-gen: intro watchList
This commit is contained in:
parent
7943c17867
commit
dceabab838
@ -148,31 +148,34 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
|
|||||||
extendedMethods = append(extendedMethods, extendedMethod)
|
extendedMethods = append(extendedMethods, extendedMethod)
|
||||||
}
|
}
|
||||||
m := map[string]interface{}{
|
m := map[string]interface{}{
|
||||||
"type": t,
|
"type": t,
|
||||||
"inputType": t,
|
"inputType": t,
|
||||||
"resultType": t,
|
"resultType": t,
|
||||||
"package": pkg,
|
"package": pkg,
|
||||||
"Package": namer.IC(pkg),
|
"Package": namer.IC(pkg),
|
||||||
"namespaced": !tags.NonNamespaced,
|
"namespaced": !tags.NonNamespaced,
|
||||||
"Group": namer.IC(g.group),
|
"Group": namer.IC(g.group),
|
||||||
"subresource": false,
|
"subresource": false,
|
||||||
"subresourcePath": "",
|
"subresourcePath": "",
|
||||||
"GroupGoName": g.groupGoName,
|
"GroupGoName": g.groupGoName,
|
||||||
"Version": namer.IC(g.version),
|
"Version": namer.IC(g.version),
|
||||||
"CreateOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "CreateOptions"}),
|
"CreateOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "CreateOptions"}),
|
||||||
"DeleteOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "DeleteOptions"}),
|
"DeleteOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "DeleteOptions"}),
|
||||||
"GetOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "GetOptions"}),
|
"GetOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "GetOptions"}),
|
||||||
"ListOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ListOptions"}),
|
"ListOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ListOptions"}),
|
||||||
"PatchOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "PatchOptions"}),
|
"PatchOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "PatchOptions"}),
|
||||||
"ApplyOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ApplyOptions"}),
|
"ApplyOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ApplyOptions"}),
|
||||||
"UpdateOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "UpdateOptions"}),
|
"UpdateOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "UpdateOptions"}),
|
||||||
"PatchType": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/types", Name: "PatchType"}),
|
"PatchType": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/types", Name: "PatchType"}),
|
||||||
"ApplyPatchType": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/types", Name: "ApplyPatchType"}),
|
"ApplyPatchType": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/types", Name: "ApplyPatchType"}),
|
||||||
"watchInterface": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/watch", Name: "Interface"}),
|
"watchInterface": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/watch", Name: "Interface"}),
|
||||||
"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"}),
|
||||||
"CheckListFromCacheDataConsistencyIfRequested": c.Universe.Function(types.Name{Package: "k8s.io/client-go/util/consistencydetector", Name: "CheckListFromCacheDataConsistencyIfRequested"}),
|
"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"}),
|
||||||
|
"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 {
|
if err == nil {
|
||||||
$.CheckListFromCacheDataConsistencyIfRequested|raw$(ctx, "list request for $.type|resource$", c.list, opts, result)
|
$.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)
|
||||||
return c.list(ctx, opts)
|
}
|
||||||
|
result, err := c.list(ctx, opts)
|
||||||
|
if err == nil {
|
||||||
|
$.CheckListFromCacheDataConsistencyIfRequested|raw$(ctx, "list request for $.type|resource$", c.list, opts, result)
|
||||||
|
}
|
||||||
|
return result, err
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
@ -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