mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Allow the api-version on the command to override the client builder
This commit is contained in:
parent
158f322301
commit
203246b7e0
@ -33,10 +33,15 @@ import (
|
|||||||
type Builder interface {
|
type Builder interface {
|
||||||
// BindFlags must bind and keep track of all the flags required to build a client config object
|
// BindFlags must bind and keep track of all the flags required to build a client config object
|
||||||
BindFlags(flags *pflag.FlagSet)
|
BindFlags(flags *pflag.FlagSet)
|
||||||
// Config uses the values of the bound flags and builds a complete client config
|
|
||||||
Config() (*client.Config, error)
|
|
||||||
// Client calls BuildConfig under the covers and uses that config to return a client
|
// Client calls BuildConfig under the covers and uses that config to return a client
|
||||||
Client() (*client.Client, error)
|
Client() (*client.Client, error)
|
||||||
|
|
||||||
|
// Config uses the values of the bound flags and builds a complete client config
|
||||||
|
Config() (*client.Config, error)
|
||||||
|
// Override invokes Config(), then passes that to the provided function, and returns a new
|
||||||
|
// builder that will use that config as its default. If Config() returns an error for the default
|
||||||
|
// values the function will not be invoked, and the error will be available when Client() is called.
|
||||||
|
Override(func(*client.Config)) Builder
|
||||||
}
|
}
|
||||||
|
|
||||||
// cmdAuthInfo is used to track whether flags have been set
|
// cmdAuthInfo is used to track whether flags have been set
|
||||||
@ -58,6 +63,8 @@ type builder struct {
|
|||||||
apiserver string
|
apiserver string
|
||||||
apiVersion string
|
apiVersion string
|
||||||
matchApiVersion bool
|
matchApiVersion bool
|
||||||
|
|
||||||
|
config *client.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBuilder returns a valid Builder that uses the passed authLoader. If authLoader is nil, the NewDefaultAuthLoader is used.
|
// NewBuilder returns a valid Builder that uses the passed authLoader. If authLoader is nil, the NewDefaultAuthLoader is used.
|
||||||
@ -124,6 +131,26 @@ func (builder *builder) Client() (*client.Client, error) {
|
|||||||
|
|
||||||
// Config implements Builder
|
// Config implements Builder
|
||||||
func (builder *builder) Config() (*client.Config, error) {
|
func (builder *builder) Config() (*client.Config, error) {
|
||||||
|
if builder.config != nil {
|
||||||
|
return builder.config, nil
|
||||||
|
}
|
||||||
|
return builder.newConfig()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Override implements Builder
|
||||||
|
func (builder *builder) Override(fn func(*client.Config)) Builder {
|
||||||
|
config, err := builder.newConfig()
|
||||||
|
if err != nil {
|
||||||
|
return builder
|
||||||
|
}
|
||||||
|
fn(config)
|
||||||
|
b := *builder
|
||||||
|
b.config = config
|
||||||
|
return &b
|
||||||
|
}
|
||||||
|
|
||||||
|
// newConfig creates a new config object for this builder
|
||||||
|
func (builder *builder) newConfig() (*client.Config, error) {
|
||||||
clientConfig := client.Config{}
|
clientConfig := client.Config{}
|
||||||
if len(builder.apiserver) > 0 {
|
if len(builder.apiserver) > 0 {
|
||||||
clientConfig.Host = builder.apiserver
|
clientConfig.Host = builder.apiserver
|
||||||
@ -140,12 +167,11 @@ func (builder *builder) Config() (*client.Config, error) {
|
|||||||
authInfoFileFound := true
|
authInfoFileFound := true
|
||||||
authInfo, err := builder.authLoader.LoadAuth(builder.authPath)
|
authInfo, err := builder.authLoader.LoadAuth(builder.authPath)
|
||||||
if authInfo == nil && err != nil { // only consider failing if we don't have any auth info
|
if authInfo == nil && err != nil { // only consider failing if we don't have any auth info
|
||||||
if os.IsNotExist(err) { // if it's just a case of a missing file, simply flag the auth as not found and use the command line arguments
|
if !os.IsNotExist(err) { // if it's just a case of a missing file, simply flag the auth as not found and use the command line arguments
|
||||||
authInfoFileFound = false
|
|
||||||
authInfo = &clientauth.Info{}
|
|
||||||
} else {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
authInfoFileFound = false
|
||||||
|
authInfo = &clientauth.Info{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If provided, the command line options override options from the auth file
|
// If provided, the command line options override options from the auth file
|
||||||
|
@ -29,6 +29,7 @@ import (
|
|||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/clientauth"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/clientauth"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -251,6 +252,40 @@ func TestLoadClientAuthInfoOrPrompt(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOverride(t *testing.T) {
|
||||||
|
b := NewBuilder(nil)
|
||||||
|
cfg, err := b.Config()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if cfg.Version != "" {
|
||||||
|
t.Errorf("unexpected default config version")
|
||||||
|
}
|
||||||
|
|
||||||
|
newCfg, err := b.Override(func(cfg *client.Config) {
|
||||||
|
if cfg.Version != "" {
|
||||||
|
t.Errorf("unexpected default config version")
|
||||||
|
}
|
||||||
|
cfg.Version = "test"
|
||||||
|
}).Config()
|
||||||
|
|
||||||
|
if newCfg.Version != "test" {
|
||||||
|
t.Errorf("unexpected override config version")
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Version != "" {
|
||||||
|
t.Errorf("original object should not change")
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err = b.Config()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if cfg.Version != "" {
|
||||||
|
t.Errorf("override should not be persistent")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func matchStringArg(expected, got string, t *testing.T) {
|
func matchStringArg(expected, got string, t *testing.T) {
|
||||||
if expected != got {
|
if expected != got {
|
||||||
t.Errorf("Expected %v, got %v", expected, got)
|
t.Errorf("Expected %v, got %v", expected, got)
|
||||||
|
@ -64,7 +64,9 @@ func NewFactory(clientBuilder clientcmd.Builder) *Factory {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Client: func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.RESTClient, error) {
|
Client: func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.RESTClient, error) {
|
||||||
return clientBuilder.Client()
|
return clientBuilder.Override(func(c *client.Config) {
|
||||||
|
c.Version = mapping.APIVersion
|
||||||
|
}).Client()
|
||||||
},
|
},
|
||||||
Describer: func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Describer, error) {
|
Describer: func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Describer, error) {
|
||||||
client, err := clientBuilder.Client()
|
client, err := clientBuilder.Client()
|
||||||
|
Loading…
Reference in New Issue
Block a user