mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
client-go: share http client for all GV
This commit is contained in:
parent
f519ab25ab
commit
be15e5ed78
@ -77,9 +77,10 @@ func (g *genClientset) GenerateType(c *generator.Context, t *types.Type, w io.Wr
|
|||||||
"Config": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Config"}),
|
"Config": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Config"}),
|
||||||
"DefaultKubernetesUserAgent": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "DefaultKubernetesUserAgent"}),
|
"DefaultKubernetesUserAgent": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "DefaultKubernetesUserAgent"}),
|
||||||
"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"}),
|
||||||
|
"RESTHTTPClientFor": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "HTTPClientFor"}),
|
||||||
"DiscoveryInterface": c.Universe.Type(types.Name{Package: "k8s.io/client-go/discovery", Name: "DiscoveryInterface"}),
|
"DiscoveryInterface": c.Universe.Type(types.Name{Package: "k8s.io/client-go/discovery", Name: "DiscoveryInterface"}),
|
||||||
"DiscoveryClient": c.Universe.Type(types.Name{Package: "k8s.io/client-go/discovery", Name: "DiscoveryClient"}),
|
"DiscoveryClient": c.Universe.Type(types.Name{Package: "k8s.io/client-go/discovery", Name: "DiscoveryClient"}),
|
||||||
"NewDiscoveryClientForConfig": c.Universe.Function(types.Name{Package: "k8s.io/client-go/discovery", Name: "NewDiscoveryClientForConfig"}),
|
"NewDiscoveryClientForConfigAndClient": c.Universe.Function(types.Name{Package: "k8s.io/client-go/discovery", Name: "NewDiscoveryClientForConfigAndClient"}),
|
||||||
"NewDiscoveryClientForConfigOrDie": c.Universe.Function(types.Name{Package: "k8s.io/client-go/discovery", Name: "NewDiscoveryClientForConfigOrDie"}),
|
"NewDiscoveryClientForConfigOrDie": c.Universe.Function(types.Name{Package: "k8s.io/client-go/discovery", Name: "NewDiscoveryClientForConfigOrDie"}),
|
||||||
"NewDiscoveryClient": c.Universe.Function(types.Name{Package: "k8s.io/client-go/discovery", Name: "NewDiscoveryClient"}),
|
"NewDiscoveryClient": c.Universe.Function(types.Name{Package: "k8s.io/client-go/discovery", Name: "NewDiscoveryClient"}),
|
||||||
"flowcontrolNewTokenBucketRateLimiter": c.Universe.Function(types.Name{Package: "k8s.io/client-go/util/flowcontrol", Name: "NewTokenBucketRateLimiter"}),
|
"flowcontrolNewTokenBucketRateLimiter": c.Universe.Function(types.Name{Package: "k8s.io/client-go/util/flowcontrol", Name: "NewTokenBucketRateLimiter"}),
|
||||||
@ -91,6 +92,7 @@ func (g *genClientset) GenerateType(c *generator.Context, t *types.Type, w io.Wr
|
|||||||
}
|
}
|
||||||
sw.Do(getDiscoveryTemplate, m)
|
sw.Do(getDiscoveryTemplate, m)
|
||||||
sw.Do(newClientsetForConfigTemplate, m)
|
sw.Do(newClientsetForConfigTemplate, m)
|
||||||
|
sw.Do(newClientsetForConfigAndClientTemplate, m)
|
||||||
sw.Do(newClientsetForConfigOrDieTemplate, m)
|
sw.Do(newClientsetForConfigOrDieTemplate, m)
|
||||||
sw.Do(newClientsetForRESTClientTemplate, m)
|
sw.Do(newClientsetForRESTClientTemplate, m)
|
||||||
|
|
||||||
@ -136,22 +138,43 @@ var newClientsetForConfigTemplate = `
|
|||||||
// NewForConfig creates a new Clientset for the given config.
|
// NewForConfig creates a new Clientset for the given config.
|
||||||
// If config's RateLimiter is not set and QPS and Burst are acceptable,
|
// If config's RateLimiter is not set and QPS and Burst are acceptable,
|
||||||
// NewForConfig will generate a rate-limiter in configShallowCopy.
|
// NewForConfig will generate a rate-limiter in configShallowCopy.
|
||||||
|
// NewForConfig is equivalent to NewForConfigAndClient(c, httpClient),
|
||||||
|
// where httpClient was generated with rest.HTTPClientFor(c).
|
||||||
func NewForConfig(c *$.Config|raw$) (*Clientset, error) {
|
func NewForConfig(c *$.Config|raw$) (*Clientset, error) {
|
||||||
configShallowCopy := *c
|
configShallowCopy := *c
|
||||||
|
|
||||||
|
// share the transport between all clients
|
||||||
|
httpClient, err := $.RESTHTTPClientFor|raw$(&configShallowCopy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewForConfigAndClient(&configShallowCopy, httpClient)
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
var newClientsetForConfigAndClientTemplate = `
|
||||||
|
// NewForConfigAndClient creates a new Clientset for the given config and http client.
|
||||||
|
// Note the http client provided takes precedence over the configured transport values.
|
||||||
|
// If config's RateLimiter is not set and QPS and Burst are acceptable,
|
||||||
|
// NewForConfigAndClient will generate a rate-limiter in configShallowCopy.
|
||||||
|
func NewForConfigAndClient(c *$.Config|raw$, httpClient *http.Client) (*Clientset, error) {
|
||||||
|
configShallowCopy := *c
|
||||||
if configShallowCopy.RateLimiter == nil && configShallowCopy.QPS > 0 {
|
if configShallowCopy.RateLimiter == nil && configShallowCopy.QPS > 0 {
|
||||||
if configShallowCopy.Burst <= 0 {
|
if configShallowCopy.Burst <= 0 {
|
||||||
return nil, fmt.Errorf("burst is required to be greater than 0 when RateLimiter is not set and QPS is set to greater than 0")
|
return nil, fmt.Errorf("burst is required to be greater than 0 when RateLimiter is not set and QPS is set to greater than 0")
|
||||||
}
|
}
|
||||||
configShallowCopy.RateLimiter = $.flowcontrolNewTokenBucketRateLimiter|raw$(configShallowCopy.QPS, configShallowCopy.Burst)
|
configShallowCopy.RateLimiter = $.flowcontrolNewTokenBucketRateLimiter|raw$(configShallowCopy.QPS, configShallowCopy.Burst)
|
||||||
}
|
}
|
||||||
|
|
||||||
var cs Clientset
|
var cs Clientset
|
||||||
var err error
|
var err error
|
||||||
$range .allGroups$ cs.$.LowerCaseGroupGoName$$.Version$, err =$.PackageAlias$.NewForConfig(&configShallowCopy)
|
$range .allGroups$ cs.$.LowerCaseGroupGoName$$.Version$, err =$.PackageAlias$.NewForConfigAndClient(&configShallowCopy, httpClient)
|
||||||
if err!=nil {
|
if err!=nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
$end$
|
$end$
|
||||||
cs.DiscoveryClient, err = $.NewDiscoveryClientForConfig|raw$(&configShallowCopy)
|
cs.DiscoveryClient, err = $.NewDiscoveryClientForConfigAndClient|raw$(&configShallowCopy, httpClient)
|
||||||
if err!=nil {
|
if err!=nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -163,11 +186,11 @@ var newClientsetForConfigOrDieTemplate = `
|
|||||||
// NewForConfigOrDie creates a new Clientset for the given config and
|
// NewForConfigOrDie creates a new Clientset for the given config and
|
||||||
// panics if there is an error in the config.
|
// panics if there is an error in the config.
|
||||||
func NewForConfigOrDie(c *$.Config|raw$) *Clientset {
|
func NewForConfigOrDie(c *$.Config|raw$) *Clientset {
|
||||||
var cs Clientset
|
cs, err := NewForConfig(c)
|
||||||
$range .allGroups$ cs.$.LowerCaseGroupGoName$$.Version$ =$.PackageAlias$.NewForConfigOrDie(c)
|
if err!=nil {
|
||||||
$end$
|
panic(err)
|
||||||
cs.DiscoveryClient = $.NewDiscoveryClientForConfigOrDie|raw$(c)
|
}
|
||||||
return &cs
|
return cs
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
@ -101,7 +101,9 @@ func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer
|
|||||||
"restConfig": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Config"}),
|
"restConfig": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Config"}),
|
||||||
"restDefaultKubernetesUserAgent": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "DefaultKubernetesUserAgent"}),
|
"restDefaultKubernetesUserAgent": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "DefaultKubernetesUserAgent"}),
|
||||||
"restRESTClientInterface": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Interface"}),
|
"restRESTClientInterface": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Interface"}),
|
||||||
|
"RESTHTTPClientFor": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "HTTPClientFor"}),
|
||||||
"restRESTClientFor": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "RESTClientFor"}),
|
"restRESTClientFor": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "RESTClientFor"}),
|
||||||
|
"restRESTClientForConfigAndClient": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "RESTClientForConfigAndClient"}),
|
||||||
"SchemeGroupVersion": c.Universe.Variable(types.Name{Package: path.Vendorless(g.inputPackage), Name: "SchemeGroupVersion"}),
|
"SchemeGroupVersion": c.Universe.Variable(types.Name{Package: path.Vendorless(g.inputPackage), Name: "SchemeGroupVersion"}),
|
||||||
}
|
}
|
||||||
sw.Do(groupInterfaceTemplate, m)
|
sw.Do(groupInterfaceTemplate, m)
|
||||||
@ -123,6 +125,7 @@ func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
sw.Do(newClientForConfigTemplate, m)
|
sw.Do(newClientForConfigTemplate, m)
|
||||||
|
sw.Do(newClientForConfigAndClientTemplate, m)
|
||||||
sw.Do(newClientForConfigOrDieTemplate, m)
|
sw.Do(newClientForConfigOrDieTemplate, m)
|
||||||
sw.Do(newClientForRESTClientTemplate, m)
|
sw.Do(newClientForRESTClientTemplate, m)
|
||||||
if g.version == "" {
|
if g.version == "" {
|
||||||
@ -164,12 +167,30 @@ func (c *$.GroupGoName$$.Version$Client) $.type|publicPlural$() $.type|public$In
|
|||||||
|
|
||||||
var newClientForConfigTemplate = `
|
var newClientForConfigTemplate = `
|
||||||
// NewForConfig creates a new $.GroupGoName$$.Version$Client for the given config.
|
// NewForConfig creates a new $.GroupGoName$$.Version$Client for the given config.
|
||||||
|
// NewForConfig is equivalent to NewForConfigAndClient(c, httpClient),
|
||||||
|
// where httpClient was generated with rest.HTTPClientFor(c).
|
||||||
func NewForConfig(c *$.restConfig|raw$) (*$.GroupGoName$$.Version$Client, error) {
|
func NewForConfig(c *$.restConfig|raw$) (*$.GroupGoName$$.Version$Client, error) {
|
||||||
config := *c
|
config := *c
|
||||||
if err := setConfigDefaults(&config); err != nil {
|
if err := setConfigDefaults(&config); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
client, err := $.restRESTClientFor|raw$(&config)
|
httpClient, err := $.RESTHTTPClientFor|raw$(&config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return NewForConfigAndClient(&config, httpClient)
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
var newClientForConfigAndClientTemplate = `
|
||||||
|
// NewForConfigAndClient creates a new $.GroupGoName$$.Version$Client for the given config and http client.
|
||||||
|
// Note the http client provided takes precedence over the configured transport values.
|
||||||
|
func NewForConfigAndClient(c *$.restConfig|raw$, h *http.Client) (*$.GroupGoName$$.Version$Client, error) {
|
||||||
|
config := *c
|
||||||
|
if err := setConfigDefaults(&config); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
client, err := $.restRESTClientForConfigAndClient|raw$(&config, h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user