Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
add KUBECONFIG support
Browse files Browse the repository at this point in the history
Supports the following syntax:
- custom path to kube config:
  KUBECONFIG="$HOME/.kube/config"
- custom path and other than default context:
  KUBECONFIG="$HOME/.kube/config:context-name"
- default path, explicit context only:
  KUBECONFIG=":context-name"
  • Loading branch information
Mikulas committed Jul 2, 2018
1 parent c1223c1 commit c07cf2a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions main/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
"os"
"path/filepath"
"strings"
)

var (
Expand All @@ -21,8 +22,25 @@ func homeDir() string {
}

func kubernetesSetup() error {
kubeconfig := filepath.Join(homeDir(), ".kube", "config")
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
configEnv := strings.Split(os.Getenv("KUBECONFIG"), ":")

var kubeconfig string
if len(configEnv) > 0 && configEnv[0] != "" {
kubeconfig = configEnv[0]
} else {
kubeconfig = filepath.Join(homeDir(), ".kube", "config")
}

var overrides clientcmd.ConfigOverrides
if len(configEnv) > 1 && configEnv[1] != "" {
overrides.CurrentContext = configEnv[1]
}

config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
&overrides,
).ClientConfig()

if err != nil {
return err
}
Expand Down

0 comments on commit c07cf2a

Please sign in to comment.