Skip to content

Commit

Permalink
bugfix: fetching Kubeconfig file (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kavinraja-G authored Dec 23, 2023
1 parent 65a3eca commit e3e25da
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ var Cfg Config
// GetKubeConfig is used to fetch the kubeConfig based on the KUBECONFIG env or '~/.kube/config' location
func GetKubeConfig() (*rest.Config, error) {
var kubeConfigPath string
var defaultKubeConfigPath = "~/.kube/config"

if home := homedir.HomeDir(); home != "" {
kubeConfigPath = filepath.Join(home, ".kube", "config")
} else {
kubeConfigPath = GetEnv("KUBECONFIG", "~/.kube/config")
defaultKubeConfigPath = filepath.Join(home, ".kube", "config")
}
kubeConfigPath = GetEnv("KUBECONFIG", defaultKubeConfigPath)

k8sConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
return k8sConfig, err
Expand All @@ -33,6 +34,10 @@ func GetKubeConfig() (*rest.Config, error) {
// k8sAuth is used to get the Kubernetes clientset from the config
func k8sAuth() (*k8s.Clientset, error) {
k8sConfig, err := GetKubeConfig()
if err != nil {
log.Fatalf("Error while getting kubeconfig: %v", err)
}

clientset, err := k8s.NewForConfig(k8sConfig)
if err != nil {
log.Fatalf("Error while creating the clientset: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// GetEnv Helper function for fetching envs with defaults
func GetEnv(env, defaults string) string {
if val, ok := os.LookupEnv(env); ok {
if val, ok := os.LookupEnv(env); ok && val != "" {
return val
}
return defaults
Expand Down

0 comments on commit e3e25da

Please sign in to comment.