Return rest config and regular config from pluginutil Init method
-add namespace overriding based on kubectl plugin env var
This commit is contained in:
parent
315679d1c5
commit
b212714c99
|
|
@ -27,12 +27,13 @@ import (
|
||||||
|
|
||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"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
|
// InitClientAndConfig uses the KUBECONFIG environment variable to create
|
||||||
// object based on the existing kubectl config and options passed from the
|
// a new rest client and config object based on the existing kubectl config
|
||||||
// calling plugin framework
|
// and options passed from the plugin framework via environment variables
|
||||||
func InitConfig() (*restclient.Config, error) {
|
func InitClientAndConfig() (*restclient.Config, clientcmd.ClientConfig, error) {
|
||||||
// resolve kubeconfig location, prioritizing the --config global flag,
|
// resolve kubeconfig location, prioritizing the --config global flag,
|
||||||
// then the value of the KUBECONFIG env var (if any), and defaulting
|
// then the value of the KUBECONFIG env var (if any), and defaulting
|
||||||
// to ~/.kube/config as a last resort.
|
// to ~/.kube/config as a last resort.
|
||||||
|
|
@ -59,30 +60,39 @@ func InitConfig() (*restclient.Config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(kubeconfig) == 0 {
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error obtaining kubectl config: %v", err)
|
return nil, nil, fmt.Errorf("error obtaining kubectl config: %v", err)
|
||||||
}
|
}
|
||||||
|
client, err := config.ClientConfig()
|
||||||
err = applyGlobalOptionsToConfig(clientConfig)
|
|
||||||
if err != nil {
|
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}
|
rules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: path}
|
||||||
credentials, err := rules.Load()
|
credentials, err := rules.Load()
|
||||||
if err != nil {
|
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
|
var cfg clientcmd.ClientConfig
|
||||||
context := os.Getenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CONTEXT")
|
context := os.Getenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CONTEXT")
|
||||||
if len(context) > 0 {
|
if len(context) > 0 {
|
||||||
|
|
@ -92,13 +102,7 @@ func clientFromConfig(path string) (*restclient.Config, string, error) {
|
||||||
cfg = clientcmd.NewDefaultClientConfig(*credentials, overrides)
|
cfg = clientcmd.NewDefaultClientConfig(*credentials, overrides)
|
||||||
}
|
}
|
||||||
|
|
||||||
config, err := cfg.ClientConfig()
|
return cfg, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyGlobalOptionsToConfig(config *restclient.Config) error {
|
func applyGlobalOptionsToConfig(config *restclient.Config) error {
|
||||||
|
|
|
||||||
|
|
@ -10,20 +10,25 @@ import (
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("InitConfig", func() {
|
var _ = Describe("plugin client", func() {
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_KUBECONFIG", "testdata/config")
|
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() {
|
Context("When nothing is overridden by the calling framework", func() {
|
||||||
It("finds and parses the preexisting config", func() {
|
It("finds and parses the preexisting config", func() {
|
||||||
config, err := pluginutils.InitConfig()
|
client, config, err := pluginutils.InitClientAndConfig()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
Expect(config.Host).To(Equal("https://notreal.com:1234"))
|
Expect(client.Host).To(Equal("https://notreal.com:1234"))
|
||||||
Expect(config.Username).To(Equal("foo"))
|
Expect(client.Username).To(Equal("foo"))
|
||||||
Expect(config.Password).To(Equal("bar"))
|
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_PASSWORD", "elderberry")
|
||||||
|
|
||||||
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CONTEXT", "california")
|
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() {
|
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(err).NotTo(HaveOccurred())
|
||||||
Expect(config.Impersonate.UserName).To(Equal("apple"))
|
Expect(client.Impersonate.UserName).To(Equal("apple"))
|
||||||
Expect(config.Impersonate.Groups).Should(ConsistOf("banana", "cherry"))
|
Expect(client.Impersonate.Groups).Should(ConsistOf("banana", "cherry"))
|
||||||
|
|
||||||
Expect(config.CertFile).To(Equal("testdata/client.crt"))
|
Expect(client.CertFile).To(Equal("testdata/client.crt"))
|
||||||
Expect(config.KeyFile).To(Equal("testdata/client.key"))
|
Expect(client.KeyFile).To(Equal("testdata/client.key"))
|
||||||
Expect(config.CAFile).To(Equal("testdata/apiserver_ca.crt"))
|
Expect(client.CAFile).To(Equal("testdata/apiserver_ca.crt"))
|
||||||
|
|
||||||
Expect(config.Timeout).To(Equal(45 * time.Second))
|
Expect(client.Timeout).To(Equal(45 * time.Second))
|
||||||
Expect(config.ServerName).To(Equal("some-other-server.com"))
|
Expect(client.ServerName).To(Equal("some-other-server.com"))
|
||||||
Expect(config.BearerToken).To(Equal("bearer notreal"))
|
Expect(client.BearerToken).To(Equal("bearer notreal"))
|
||||||
|
|
||||||
Expect(config.Username).To(Equal("date"))
|
Expect(client.Username).To(Equal("date"))
|
||||||
Expect(config.Password).To(Equal("elderberry"))
|
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())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue