From 847cdef35f8531a0ff8be20511e1b0c6de74630e Mon Sep 17 00:00:00 2001 From: Benedikt Iltisberger Date: Mon, 17 Jun 2024 21:23:45 +0200 Subject: [PATCH] fix: now supports KUBECONFIG with multiple contexts seperated by semicolon. --- kubernetes/addResources.go | 2 +- kubernetes/k8s-provider-default.go | 13 +++++++++++-- kubernetes/utils.go | 22 +++++++++++++++------- utils/config.go | 10 +++++----- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/kubernetes/addResources.go b/kubernetes/addResources.go index 19fcad8..c8a063f 100644 --- a/kubernetes/addResources.go +++ b/kubernetes/addResources.go @@ -249,7 +249,7 @@ func CreateContextSecretIfNotExist(provider *KubeProvider) (*dtos.PunqContext, e } func writeContextSecret(secretClient v1.SecretInterface, existingSecret *core.Secret, getErr error) (*dtos.PunqContext, error) { - kubeconfigEnvVar := utils.GetDefaultKubeConfig() + kubeconfigEnvVar := utils.GetDefaultKubeConfig()[0] // get the first kubeconfig this is ok because it will stop fatal if there is no kubeconfig kubeconfigData, err := os.ReadFile(kubeconfigEnvVar) if err != nil { diff --git a/kubernetes/k8s-provider-default.go b/kubernetes/k8s-provider-default.go index 9734953..78a53a8 100644 --- a/kubernetes/k8s-provider-default.go +++ b/kubernetes/k8s-provider-default.go @@ -92,7 +92,16 @@ func ContextSwitcher(contextId *string) (*rest.Config, error) { if contextId != nil && *contextId != "" { return ContextConfigLoader(contextId) } else { - var kubeconfig string = utils.GetDefaultKubeConfig() - return clientcmd.BuildConfigFromFlags("", kubeconfig) + var kubeconfigs []string = utils.GetDefaultKubeConfig() + var config *rest.Config + var err error + for _, kubeconfig := range kubeconfigs { + config, err = clientcmd.BuildConfigFromFlags("", kubeconfig) + if err == nil { + return config, nil + } + } + + return nil, fmt.Errorf("Error loading kubeconfig: %s", err.Error()) } } diff --git a/kubernetes/utils.go b/kubernetes/utils.go index b30f456..87179de 100644 --- a/kubernetes/utils.go +++ b/kubernetes/utils.go @@ -17,6 +17,7 @@ import ( "github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/text" "github.com/mogenius/punq/version" + "k8s.io/client-go/tools/clientcmd/api" "github.com/mogenius/punq/utils" @@ -243,13 +244,20 @@ func NewWorkload(name string, yaml string, description string) K8sNewWorkload { } func CurrentContextName() string { - var kubeconfig string = utils.GetDefaultKubeConfig() - - config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig}, - &clientcmd.ConfigOverrides{ - CurrentContext: "", - }).RawConfig() + var kubeconfigs []string = utils.GetDefaultKubeConfig() + + var config api.Config + var err error + for _, kubeconfig := range kubeconfigs { + config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig( + &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig}, + &clientcmd.ConfigOverrides{ + CurrentContext: "", + }).RawConfig() + if err == nil { + break + } + } if err != nil { return fmt.Sprintf("Error: %v", err) diff --git a/utils/config.go b/utils/config.go index 2fb2073..0de3696 100644 --- a/utils/config.go +++ b/utils/config.go @@ -265,7 +265,7 @@ func WriteDefaultConfig(stage string) { } } -func GetDefaultKubeConfig() string { +func GetDefaultKubeConfig() []string { var kubeconfig string = os.Getenv("KUBECONFIG") if kubeconfig == "" { if home := homedir.HomeDir(); home != "" { @@ -277,15 +277,15 @@ func GetDefaultKubeConfig() string { kubeconfigs := strings.Split(kubeconfig, ":") // at least one kubeconfig file must exist for _, singleConfig := range kubeconfigs { - if _, err := os.Stat(singleConfig); err == nil { - return singleConfig + if _, err := os.Stat(singleConfig); os.IsNotExist(err) { + logger.Log.Fatalf("Error: $KUBECONFIG is not set, and the default Kubernetes context cannot be loaded. The $KUBECONFIG environment variable specifies the kubeconfig file path, which is essential for connecting to your Kubernetes cluster. To resolve this, please set $KUBECONFIG by using 'kubectx' for easier context switching or by manually specifying the path to your kubeconfig file. For detailed instructions on configuring your kubeconfig, please refer to https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/.") } } - logger.Log.Fatalf("Error: $KUBECONFIG is not set, and the default Kubernetes context cannot be loaded. The $KUBECONFIG environment variable specifies the kubeconfig file path, which is essential for connecting to your Kubernetes cluster. To resolve this, please set $KUBECONFIG by using 'kubectx' for easier context switching or by manually specifying the path to your kubeconfig file. For detailed instructions on configuring your kubeconfig, please refer to https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/.") + return kubeconfigs } else { if _, err := os.Stat(kubeconfig); os.IsNotExist(err) { logger.Log.Fatalf("Error: $KUBECONFIG is not set, and the default Kubernetes context cannot be loaded. The $KUBECONFIG environment variable specifies the kubeconfig file path, which is essential for connecting to your Kubernetes cluster. To resolve this, please set $KUBECONFIG by using 'kubectx' for easier context switching or by manually specifying the path to your kubeconfig file. For detailed instructions on configuring your kubeconfig, please refer to https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/.") } } - return kubeconfig + return []string{kubeconfig} }