From 0523cf23ef8be6b76f9b8c3f77264d001a82f7b3 Mon Sep 17 00:00:00 2001 From: Vincent Composieux Date: Mon, 22 Jul 2019 20:40:23 +0200 Subject: [PATCH] Added ability to specify a custom Kubernetes config path (fixes #4) --- example.yaml | 1 + internal/config/model.go | 7 ++++--- internal/config/reader.go | 5 +++++ internal/config/reader_test.go | 3 ++- internal/tests/config/config.yaml | 1 + pkg/forwarder/kubernetes/forwarder.go | 17 ++++++++++++++--- pkg/forwarder/kubernetes/forwarder_test.go | 20 ++++++++++++++++++++ 7 files changed, 47 insertions(+), 7 deletions(-) diff --git a/example.yaml b/example.yaml index 43604f3b..e0ce14fc 100644 --- a/example.yaml +++ b/example.yaml @@ -1,5 +1,6 @@ # Settings +kubeconfig: /dev/custom/.kube/config # Optional, default to user's .kube/config file path gopath: /dev/golang # Optional, default to user's $GOPATH env var watcher: # Optional diff --git a/internal/config/model.go b/internal/config/model.go index 73e94bf0..cb73f67d 100644 --- a/internal/config/model.go +++ b/internal/config/model.go @@ -37,9 +37,10 @@ var ( // Config represents the root configuration item type Config struct { - GoPath string `yaml:"gopath"` - Projects []*Project `yaml:"projects"` - Watcher *Watcher `yaml:"watcher"` + GoPath string `yaml:"gopath"` + KubeConfig string `yaml:"kubeconfig"` + Projects []*Project `yaml:"projects"` + Watcher *Watcher `yaml:"watcher"` } // Project represents a project name, that could be a group of multiple projects diff --git a/internal/config/reader.go b/internal/config/reader.go index 34d1d8e5..a9e5d9d3 100644 --- a/internal/config/reader.go +++ b/internal/config/reader.go @@ -62,6 +62,11 @@ func Load() (*Config, error) { os.Setenv("GOPATH", conf.GoPath) } + // Set Kubeconfig filepath if defined in configuration + if conf.KubeConfig != "" { + os.Setenv("MONDAY_KUBE_CONFIG", conf.KubeConfig) + } + return &conf, nil } diff --git a/internal/config/reader_test.go b/internal/config/reader_test.go index 3dec2023..71737757 100644 --- a/internal/config/reader_test.go +++ b/internal/config/reader_test.go @@ -25,8 +25,9 @@ func TestLoadSingleFile(t *testing.T) { ".git", "node_modules", }) -} + assert.Equal(t, conf.KubeConfig, "/home/test/.customkube/config") +} func TestLoadMultipleFiles(t *testing.T) { // Given dir, _ := os.Getwd() diff --git a/internal/tests/config/config.yaml b/internal/tests/config/config.yaml index 5a9c96e1..779de139 100644 --- a/internal/tests/config/config.yaml +++ b/internal/tests/config/config.yaml @@ -1,5 +1,6 @@ # Settings +kubeconfig: "/home/test/.customkube/config" watcher: exclude: - .git diff --git a/pkg/forwarder/kubernetes/forwarder.go b/pkg/forwarder/kubernetes/forwarder.go index 4cf90af3..d6106ea2 100644 --- a/pkg/forwarder/kubernetes/forwarder.go +++ b/pkg/forwarder/kubernetes/forwarder.go @@ -60,7 +60,9 @@ type Forwarder struct { } func NewForwarder(forwardType, name, context, namespace string, ports []string, labels map[string]string) (*Forwarder, error) { - clientConfig, err := initializeClientConfig(context) + kubeConfigPath := getKubeConfigPath() + + clientConfig, err := initializeClientConfig(context, kubeConfigPath) if err != nil { return nil, err } @@ -102,6 +104,7 @@ func (f *Forwarder) GetStopChannel() chan struct{} { return f.readyChannel } +// Forward method executes the local or remote port-forward depending on the given type func (f *Forwarder) Forward() error { selector := f.getSelector() @@ -288,11 +291,11 @@ func (f *Forwarder) getSelector() string { return selector } -func initializeClientConfig(context string) (*restclient.Config, error) { +func initializeClientConfig(context string, kubeConfigPath string) (*restclient.Config, error) { overrides := &clientcmd.ConfigOverrides{CurrentContext: context} clientConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - &clientcmd.ClientConfigLoadingRules{ExplicitPath: defaultKubeConfigPath}, + &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfigPath}, overrides, ).ClientConfig() if err != nil { @@ -315,3 +318,11 @@ func buildPath(request *restclient.Request) string { parts := strings.Split(request.URL().Path, "/namespaces") return parts[0] + "/api/v1/namespaces" + parts[1] } + +func getKubeConfigPath() string { + if value := os.Getenv("MONDAY_KUBE_CONFIG"); value != "" { + return value + } + + return defaultKubeConfigPath +} diff --git a/pkg/forwarder/kubernetes/forwarder_test.go b/pkg/forwarder/kubernetes/forwarder_test.go index 58b32cf0..158cf289 100644 --- a/pkg/forwarder/kubernetes/forwarder_test.go +++ b/pkg/forwarder/kubernetes/forwarder_test.go @@ -57,6 +57,26 @@ func TestNewForwarder(t *testing.T) { assert.Len(t, forwarder.deployments, 0) } +func TestGetKubeConfigPathWhenDefault(t *testing.T) { + // When + configPath := getKubeConfigPath() + + // Then + assert.Equal(t, configPath, defaultKubeConfigPath) +} + +func TestGetKubeConfigPathWhenCustom(t *testing.T) { + // Given + os.Setenv("MONDAY_KUBE_CONFIG", "/tmp/custom/.kube/test.config") + defer os.Setenv("MONDAY_KUBE_CONFIG", "") + + // When + configPath := getKubeConfigPath() + + // Then + assert.Equal(t, configPath, "/tmp/custom/.kube/test.config") +} + func TestGetForwardType(t *testing.T) { // Given initKubeConfig(t)