mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
Create an experimental client cache.
Previously, we would initialize the experimental client at factory creation time. This is problematic because the clientConfig loader has values populated from the command-line flag parsing, and these are not populated until after Factory creation. Introduce an ExperimentalClientCache to create (and reuse) ExperimentalClients.
This commit is contained in:
parent
5000252e46
commit
7c58f94edc
@ -89,3 +89,28 @@ func (c *ClientCache) ClientForVersion(version string) (*client.Client, error) {
|
|||||||
c.clients[config.Version] = client
|
c.clients[config.Version] = client
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ExperimentalClientCache struct {
|
||||||
|
loader clientcmd.ClientConfig
|
||||||
|
client *client.ExperimentalClient
|
||||||
|
err error
|
||||||
|
init bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewExperimentalClientCache(loader clientcmd.ClientConfig) *ExperimentalClientCache {
|
||||||
|
return &ExperimentalClientCache{loader: loader}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cc *ExperimentalClientCache) Client() (*client.ExperimentalClient, error) {
|
||||||
|
if cc.init {
|
||||||
|
return cc.client, cc.err
|
||||||
|
}
|
||||||
|
cfg, err := cc.loader.ClientConfig()
|
||||||
|
if err != nil {
|
||||||
|
cc.err = err
|
||||||
|
} else {
|
||||||
|
cc.client, cc.err = client.NewExperimental(cfg)
|
||||||
|
}
|
||||||
|
cc.init = true
|
||||||
|
return cc.client, cc.err
|
||||||
|
}
|
||||||
|
@ -110,14 +110,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clients := NewClientCache(clientConfig)
|
clients := NewClientCache(clientConfig)
|
||||||
|
expClients := NewExperimentalClientCache(clientConfig)
|
||||||
// Initialize the experimental client (if possible). Failing here is non-fatal, errors
|
|
||||||
// will be returned when an experimental client is explicitly requested.
|
|
||||||
var experimentalClient *client.ExperimentalClient
|
|
||||||
cfg, experimentalClientErr := clientConfig.ClientConfig()
|
|
||||||
if experimentalClientErr == nil {
|
|
||||||
experimentalClient, experimentalClientErr = client.NewExperimental(cfg)
|
|
||||||
}
|
|
||||||
|
|
||||||
noClientErr := errors.New("could not get client")
|
noClientErr := errors.New("could not get client")
|
||||||
getBothClients := func(group string, version string) (client *client.Client, expClient *client.ExperimentalClient, err error) {
|
getBothClients := func(group string, version string) (client *client.Client, expClient *client.ExperimentalClient, err error) {
|
||||||
@ -126,7 +119,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
|||||||
case "api":
|
case "api":
|
||||||
client, err = clients.ClientForVersion(version)
|
client, err = clients.ClientForVersion(version)
|
||||||
case "experimental":
|
case "experimental":
|
||||||
expClient, err = experimentalClient, experimentalClientErr
|
expClient, err = expClients.Client()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -146,7 +139,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
|||||||
return clients.ClientForVersion("")
|
return clients.ClientForVersion("")
|
||||||
},
|
},
|
||||||
ExperimentalClient: func() (*client.ExperimentalClient, error) {
|
ExperimentalClient: func() (*client.ExperimentalClient, error) {
|
||||||
return experimentalClient, experimentalClientErr
|
return expClients.Client()
|
||||||
},
|
},
|
||||||
ClientConfig: func() (*client.Config, error) {
|
ClientConfig: func() (*client.Config, error) {
|
||||||
return clients.ClientConfigForVersion("")
|
return clients.ClientConfigForVersion("")
|
||||||
@ -164,7 +157,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
|||||||
}
|
}
|
||||||
return client.RESTClient, nil
|
return client.RESTClient, nil
|
||||||
case "experimental":
|
case "experimental":
|
||||||
client, err := experimentalClient, experimentalClientErr
|
client, err := expClients.Client()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -260,7 +253,8 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &clientSwaggerSchema{client, experimentalClient, api.Scheme}, nil
|
expClient, _ := expClients.Client()
|
||||||
|
return &clientSwaggerSchema{client, expClient, api.Scheme}, nil
|
||||||
}
|
}
|
||||||
return validation.NullSchema{}, nil
|
return validation.NullSchema{}, nil
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user