Skip to content

Commit

Permalink
fix: now supports KUBECONFIG with multiple contexts seperated by semi…
Browse files Browse the repository at this point in the history
…colon.
  • Loading branch information
beneiltis committed Jun 17, 2024
1 parent 3cc343f commit 847cdef
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion kubernetes/addResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions kubernetes/k8s-provider-default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
22 changes: 15 additions & 7 deletions kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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}
}

0 comments on commit 847cdef

Please sign in to comment.