Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split kubeconfig path after extracting from environment. #2373

Merged
merged 1 commit into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}