diff --git a/pkg/pluginutils/plugin_client.go b/pkg/pluginutils/plugin_client.go index 3292c500b..d07c3b34d 100644 --- a/pkg/pluginutils/plugin_client.go +++ b/pkg/pluginutils/plugin_client.go @@ -27,12 +27,13 @@ import ( restclient "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" ) -// InitConfig uses the KUBECONFIG environment variable to create a new config -// object based on the existing kubectl config and options passed from the -// calling plugin framework -func InitConfig() (*restclient.Config, error) { +// InitClientAndConfig uses the KUBECONFIG environment variable to create +// a new rest client and config object based on the existing kubectl config +// and options passed from the plugin framework via environment variables +func InitClientAndConfig() (*restclient.Config, clientcmd.ClientConfig, error) { // resolve kubeconfig location, prioritizing the --config global flag, // then the value of the KUBECONFIG env var (if any), and defaulting // to ~/.kube/config as a last resort. @@ -59,30 +60,39 @@ func InitConfig() (*restclient.Config, error) { } if len(kubeconfig) == 0 { - return nil, fmt.Errorf("error initializing config. The KUBECONFIG environment variable must be defined.") + return nil, nil, fmt.Errorf("error initializing config. The KUBECONFIG environment variable must be defined.") } - clientConfig, _, err := clientFromConfig(kubeconfig) + config, err := configFromPath(kubeconfig) if err != nil { - return nil, fmt.Errorf("error obtaining kubectl config: %v", err) + return nil, nil, fmt.Errorf("error obtaining kubectl config: %v", err) } - - err = applyGlobalOptionsToConfig(clientConfig) + client, err := config.ClientConfig() if err != nil { - return nil, fmt.Errorf("error processing global plugin options: %v", err) + return nil, nil, fmt.Errorf("the provided credentials %q could not be used: %v", kubeconfig, err) } - return clientConfig, nil + err = applyGlobalOptionsToConfig(client) + if err != nil { + return nil, nil, fmt.Errorf("error processing global plugin options: %v", err) + } + + return client, config, nil } -func clientFromConfig(path string) (*restclient.Config, string, error) { +func configFromPath(path string) (clientcmd.ClientConfig, error) { rules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: path} credentials, err := rules.Load() if err != nil { - return nil, "", fmt.Errorf("the provided credentials %q could not be loaded: %v", path, err) + return nil, fmt.Errorf("the provided credentials %q could not be loaded: %v", path, err) + } + + overrides := &clientcmd.ConfigOverrides{ + Context: clientcmdapi.Context{ + Namespace: os.Getenv("KUBECTL_PLUGINS_GLOBAL_FLAG_NAMESPACE"), + }, } - overrides := &clientcmd.ConfigOverrides{} var cfg clientcmd.ClientConfig context := os.Getenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CONTEXT") if len(context) > 0 { @@ -92,13 +102,7 @@ func clientFromConfig(path string) (*restclient.Config, string, error) { cfg = clientcmd.NewDefaultClientConfig(*credentials, overrides) } - config, err := cfg.ClientConfig() - if err != nil { - return nil, "", fmt.Errorf("the provided credentials %q could not be used: %v", path, err) - } - - namespace, _, _ := cfg.Namespace() - return config, namespace, nil + return cfg, nil } func applyGlobalOptionsToConfig(config *restclient.Config) error { diff --git a/pkg/pluginutils/plugin_client_test.go b/pkg/pluginutils/plugin_client_test.go index d2e850b94..a8e51b91c 100644 --- a/pkg/pluginutils/plugin_client_test.go +++ b/pkg/pluginutils/plugin_client_test.go @@ -10,20 +10,25 @@ import ( . "github.com/onsi/gomega" ) -var _ = Describe("InitConfig", func() { +var _ = Describe("plugin client", func() { BeforeEach(func() { os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_KUBECONFIG", "testdata/config") }) - Describe("InitConfig", func() { + Describe("InitClientAndConfig", func() { Context("When nothing is overridden by the calling framework", func() { It("finds and parses the preexisting config", func() { - config, err := pluginutils.InitConfig() + client, config, err := pluginutils.InitClientAndConfig() Expect(err).NotTo(HaveOccurred()) - Expect(config.Host).To(Equal("https://notreal.com:1234")) - Expect(config.Username).To(Equal("foo")) - Expect(config.Password).To(Equal("bar")) + Expect(client.Host).To(Equal("https://notreal.com:1234")) + Expect(client.Username).To(Equal("foo")) + Expect(client.Password).To(Equal("bar")) + + namespace, overridden, err := config.Namespace() + Expect(err).NotTo(HaveOccurred()) + Expect(namespace).To(Equal("default")) + Expect(overridden).To(BeFalse()) }) }) @@ -43,25 +48,31 @@ var _ = Describe("InitConfig", func() { os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_PASSWORD", "elderberry") os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CONTEXT", "california") + os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_NAMESPACE", "catalog") }) It("overrides the config settings with the passed in settings", func() { - config, err := pluginutils.InitConfig() + client, config, err := pluginutils.InitClientAndConfig() Expect(err).NotTo(HaveOccurred()) - Expect(config.Impersonate.UserName).To(Equal("apple")) - Expect(config.Impersonate.Groups).Should(ConsistOf("banana", "cherry")) + Expect(client.Impersonate.UserName).To(Equal("apple")) + Expect(client.Impersonate.Groups).Should(ConsistOf("banana", "cherry")) - Expect(config.CertFile).To(Equal("testdata/client.crt")) - Expect(config.KeyFile).To(Equal("testdata/client.key")) - Expect(config.CAFile).To(Equal("testdata/apiserver_ca.crt")) + Expect(client.CertFile).To(Equal("testdata/client.crt")) + Expect(client.KeyFile).To(Equal("testdata/client.key")) + Expect(client.CAFile).To(Equal("testdata/apiserver_ca.crt")) - Expect(config.Timeout).To(Equal(45 * time.Second)) - Expect(config.ServerName).To(Equal("some-other-server.com")) - Expect(config.BearerToken).To(Equal("bearer notreal")) + Expect(client.Timeout).To(Equal(45 * time.Second)) + Expect(client.ServerName).To(Equal("some-other-server.com")) + Expect(client.BearerToken).To(Equal("bearer notreal")) - Expect(config.Username).To(Equal("date")) - Expect(config.Password).To(Equal("elderberry")) + Expect(client.Username).To(Equal("date")) + Expect(client.Password).To(Equal("elderberry")) - Expect(config.Host).To(Equal("https://notrealincalifornia.com:1234")) + Expect(client.Host).To(Equal("https://notrealincalifornia.com:1234")) + + namespace, overridden, err := config.Namespace() + Expect(err).NotTo(HaveOccurred()) + Expect(namespace).To(Equal("catalog")) + Expect(overridden).To(BeTrue()) }) }) })