Support KUBECONFIG with multiple kube config files (#1126)

* support KUBECONFIG with multiple kube config files
* use a more cross-platform friendly implementation to get the current users home dir
This commit is contained in:
Jon Carl
2019-04-01 15:25:05 +08:00
committed by Ta-Ching Chen
parent b7a41cbd89
commit 24600c20c5
4 changed files with 40 additions and 29 deletions
+2 -2
View File
@@ -754,8 +754,8 @@ func fnTest(c *cli.Context) error {
routerURL := os.Getenv("FISSION_ROUTER") routerURL := os.Getenv("FISSION_ROUTER")
if len(routerURL) == 0 { if len(routerURL) == 0 {
// Portforward to the fission router // Portforward to the fission router
localRouterPort := util.SetupPortForward(util.GetKubeConfigPath(), localRouterPort := util.SetupPortForward(util.GetFissionNamespace(),
util.GetFissionNamespace(), "application=fission-router") "application=fission-router")
routerURL = "127.0.0.1:" + localRouterPort routerURL = "127.0.0.1:" + localRouterPort
} else { } else {
routerURL = strings.TrimPrefix(routerURL, "http://") routerURL = strings.TrimPrefix(routerURL, "http://")
+1 -1
View File
@@ -60,7 +60,7 @@ func DumpInfo(c *cli.Context) error {
} }
client := util.GetApiClient(util.GetServerUrl()) client := util.GetApiClient(util.GetServerUrl())
_, k8sClient := util.GetKubernetesClient(util.GetKubeConfigPath()) _, k8sClient := util.GetKubernetesClient()
ress := map[string]resources.Resource{ ress := map[string]resources.Resource{
// kubernetes info // kubernetes info
+7 -7
View File
@@ -25,7 +25,7 @@ import (
"strings" "strings"
"time" "time"
"k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/portforward" "k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy" "k8s.io/client-go/transport/spdy"
@@ -39,9 +39,9 @@ import (
// is found by looking for a service in the same namespace and using // is found by looking for a service in the same namespace and using
// its targetPort. Once the port forward is started, wait for it to // its targetPort. Once the port forward is started, wait for it to
// start accepting connections before returning. // start accepting connections before returning.
func SetupPortForward(kubeConfig, namespace, labelSelector string) string { func SetupPortForward(namespace, labelSelector string) string {
log.Verbose(2, "Setting up port forward to %s in namespace %s using the kubeconfig at %s", log.Verbose(2, "Setting up port forward to %s in namespace %s",
labelSelector, namespace, kubeConfig) labelSelector, namespace)
localPort, err := findFreePort() localPort, err := findFreePort()
if err != nil { if err != nil {
@@ -62,7 +62,7 @@ func SetupPortForward(kubeConfig, namespace, labelSelector string) string {
log.Verbose(2, "Starting port forward from local port %v", localPort) log.Verbose(2, "Starting port forward from local port %v", localPort)
go func() { go func() {
err := runPortForward(kubeConfig, labelSelector, localPort, namespace) err := runPortForward(labelSelector, localPort, namespace)
if err != nil { if err != nil {
log.Fatal(fmt.Sprintf("Error forwarding to controller port: %s", err.Error())) log.Fatal(fmt.Sprintf("Error forwarding to controller port: %s", err.Error()))
} }
@@ -101,8 +101,8 @@ func findFreePort() (string, error) {
} }
// runPortForward creates a local port forward to the specified pod // runPortForward creates a local port forward to the specified pod
func runPortForward(kubeConfig string, labelSelector string, localPort string, ns string) error { func runPortForward(labelSelector string, localPort string, ns string) error {
config, clientset := GetKubernetesClient(kubeConfig) config, clientset := GetKubernetesClient()
log.Verbose(2, "Connected to Kubernetes API") log.Verbose(2, "Connected to Kubernetes API")
+30 -19
View File
@@ -19,6 +19,7 @@ package util
import ( import (
"fmt" "fmt"
"os" "os"
"os/user"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@@ -53,20 +54,6 @@ func GetFissionNamespace() string {
return fissionNamespace return fissionNamespace
} }
func GetKubeConfigPath() string {
kubeConfig := os.Getenv("KUBECONFIG")
if len(kubeConfig) == 0 {
home := os.Getenv("HOME")
kubeConfig = filepath.Join(home, ".kube", "config")
if _, err := os.Stat(kubeConfig); os.IsNotExist(err) {
log.Fatal("Couldn't find kubeconfig file. " +
"Set the KUBECONFIG environment variable to your kubeconfig's path.")
}
}
return kubeConfig
}
func GetServerUrl() string { func GetServerUrl() string {
return GetApplicationUrl("application=fission-api") return GetApplicationUrl("application=fission-api")
} }
@@ -77,8 +64,7 @@ func GetApplicationUrl(selector string) string {
fissionUrl := os.Getenv("FISSION_URL") fissionUrl := os.Getenv("FISSION_URL")
if len(fissionUrl) == 0 { if len(fissionUrl) == 0 {
fissionNamespace := GetFissionNamespace() fissionNamespace := GetFissionNamespace()
kubeConfig := GetKubeConfigPath() localPort := SetupPortForward(fissionNamespace, "application=fission-api")
localPort := SetupPortForward(kubeConfig, fissionNamespace, "application=fission-api")
serverUrl = "http://127.0.0.1:" + localPort serverUrl = "http://127.0.0.1:" + localPort
} else { } else {
serverUrl = fissionUrl serverUrl = fissionUrl
@@ -128,10 +114,35 @@ func KubifyName(old string) string {
return newName return newName
} }
func GetKubernetesClient(kubeConfig string) (*restclient.Config, *kubernetes.Clientset) { // GetKubernetesClient builds a new kubernetes client. If the KUBECONFIG
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig) // environment variable is empty or doesn't exist, ~/.kube/config is used for
// the kube config path
func GetKubernetesClient() (*restclient.Config, *kubernetes.Clientset) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
kubeConfigPath := os.Getenv("KUBECONFIG")
if len(kubeConfigPath) == 0 {
usr, err := user.Current()
if err != nil {
log.Fatal(fmt.Sprintf("Could not get the current users directory: %s", err))
}
kubeConfigPath = filepath.Join(usr.HomeDir, ".kube", "config")
if _, err := os.Stat(kubeConfigPath); os.IsNotExist(err) {
log.Fatal("Couldn't find kubeconfig file. " +
"Set the KUBECONFIG environment variable to your kubeconfig's path.")
}
loadingRules.ExplicitPath = kubeConfigPath
log.Verbose(2, "Using kubeconfig from %q", kubeConfigPath)
} else {
log.Verbose(2, "Using kubeconfig from environment %q", kubeConfigPath)
}
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingRules, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil { if err != nil {
log.Fatal(fmt.Sprintf("Failed to connect to Kubernetes: %s", err)) log.Fatal(fmt.Sprintf("Failed to build Kubernetes config: %s", err))
} }
clientset, err := kubernetes.NewForConfig(config) clientset, err := kubernetes.NewForConfig(config)