Skip to content

Commit

Permalink
Split kubeconfig path after extracting from environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
russjones committed Nov 29, 2018
1 parent b64555a commit 2003cea
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
3 changes: 3 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ const (
// ComponentTSH is the "tsh" binary.
ComponentTSH = "tsh"

// ComponentKubeClient is the Kubernetes client.
ComponentKubeClient = "client:kube"

// DebugEnvVar tells tests to use verbose debug output
DebugEnvVar = "DEBUG"

Expand Down
43 changes: 40 additions & 3 deletions lib/kube/client/kubeclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ package client
import (
"fmt"
"os"
"runtime"
"strings"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/client"
"github.com/gravitational/teleport/lib/utils"

"github.com/gravitational/trace"

"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

var log = logrus.WithFields(logrus.Fields{
trace.Component: teleport.ComponentKubeClient,
})

// UpdateKubeconfig adds Teleport configuration to kubeconfig.
func UpdateKubeconfig(tc *client.TeleportClient) error {
config, err := LoadKubeConfig()
Expand Down Expand Up @@ -41,7 +48,7 @@ func UpdateKubeconfig(tc *client.TeleportClient) error {
ClientKeyData: creds.Priv,
}
config.Clusters[clusterName] = &clientcmdapi.Cluster{
Server: clusterAddr,
Server: clusterAddr,
CertificateAuthorityData: certAuthorities,
}

Expand Down Expand Up @@ -91,7 +98,9 @@ func RemoveKubeconifg(tc *client.TeleportClient, clusterName string) error {
// One exception, missing files result in empty configs, not an error.
func LoadKubeConfig() (*clientcmdapi.Config, error) {
filename, err := utils.EnsureLocalPath(
os.Getenv(teleport.EnvKubeConfig), teleport.KubeConfigDir, teleport.KubeConfigFile)
kubeconfigFromEnv(),
teleport.KubeConfigDir,
teleport.KubeConfigFile)
if err != nil {
return nil, trace.Wrap(err)
}
Expand All @@ -102,20 +111,48 @@ func LoadKubeConfig() (*clientcmdapi.Config, error) {
if config == nil {
config = clientcmdapi.NewConfig()
}

return config, nil
}

// SaveKubeConfig saves updated config to location specified by environment variable or
// default location
func SaveKubeConfig(config clientcmdapi.Config) error {
filename, err := utils.EnsureLocalPath(
os.Getenv(teleport.EnvKubeConfig), teleport.KubeConfigDir, teleport.KubeConfigFile)
kubeconfigFromEnv(),
teleport.KubeConfigDir,
teleport.KubeConfigFile)
if err != nil {
return trace.Wrap(err)
}

err = clientcmd.WriteToFile(config, filename)
if err != nil {
return trace.ConvertSystemError(err)
}
return nil
}

// kubeconfigFromEnv extracts location of kubeconfig from the environment.
func kubeconfigFromEnv() string {
kubeconfig := os.Getenv(teleport.EnvKubeConfig)

// The KUBECONFIG environment variable is a list. On Windows it's
// semicolon-delimited. On Linux and macOS it's colon-delimited.
var parts []string
switch runtime.GOOS {
case teleport.WindowsOS:
parts = strings.Split(kubeconfig, ";")
default:
parts = strings.Split(kubeconfig, ":")
}

// Default behavior of kubectl is to return the first file from list.
var configpath string
if len(parts) > 0 {
configpath = parts[0]
}
log.Debugf("Found kubeconfig in environment at '%v'.", configpath)

return configpath
}

0 comments on commit 2003cea

Please sign in to comment.