Skip to content

Commit

Permalink
Added ability to specify a custom Kubernetes config path (fixes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
eko committed Jul 22, 2019
1 parent dce4472 commit 0523cf2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
1 change: 1 addition & 0 deletions 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
Expand Down
7 changes: 4 additions & 3 deletions internal/config/model.go
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions internal/config/reader.go
Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion internal/config/reader_test.go
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions internal/tests/config/config.yaml
@@ -1,5 +1,6 @@
# Settings

kubeconfig: "/home/test/.customkube/config"
watcher:
exclude:
- .git
Expand Down
17 changes: 14 additions & 3 deletions pkg/forwarder/kubernetes/forwarder.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
20 changes: 20 additions & 0 deletions pkg/forwarder/kubernetes/forwarder_test.go
Expand Up @@ -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)
Expand Down

0 comments on commit 0523cf2

Please sign in to comment.