Skip to content

Commit

Permalink
feat: support kubeconfig env var
Browse files Browse the repository at this point in the history
  • Loading branch information
Telemaco019 committed Dec 1, 2024
1 parent 7c5709c commit cf26a01
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.4

require (
github.com/charmbracelet/huh v0.5.3
github.com/charmbracelet/lipgloss v0.13.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
golang.org/x/text v0.16.0
Expand All @@ -21,7 +22,6 @@ require (
github.com/catppuccin/go v0.2.0 // indirect
github.com/charmbracelet/bubbles v0.19.0 // indirect
github.com/charmbracelet/bubbletea v0.27.0 // indirect
github.com/charmbracelet/lipgloss v0.13.0 // indirect
github.com/charmbracelet/x/ansi v0.2.2 // indirect
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
Expand Down
8 changes: 2 additions & 6 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ package cmd
import (
"github.com/spf13/cobra"
"github.com/telemaco019/duplik8s/pkg/core"
"github.com/telemaco019/duplik8s/pkg/utils"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/util/homedir"
"path/filepath"
)

func NewRootCmd(
Expand All @@ -41,10 +40,7 @@ func NewRootCmd(

// Setup kubeconfig flags
defaultNamespace := "default"
defaultKubeconfig := ""
if home := homedir.HomeDir(); home != "" {
defaultKubeconfig = filepath.Join(home, ".kube", "config")
}
defaultKubeconfig := utils.GetKubeconfigPath()
configFlags := genericclioptions.NewConfigFlags(true)
configFlags.KubeConfig = &defaultKubeconfig
configFlags.Namespace = &defaultNamespace
Expand Down
13 changes: 13 additions & 0 deletions pkg/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
package utils

import (
"os"
"path/filepath"

"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/homedir"
)

// NewClientset creates a new kubernetes clientset
Expand Down Expand Up @@ -70,6 +74,15 @@ func NewDynamicClient(kubeconfig, context string) (*dynamic.DynamicClient, error
return clientSet, nil
}

func GetKubeconfigPath() string {
kubeconfigPath := os.Getenv("KUBECONFIG")
if kubeconfigPath != "" {
return kubeconfigPath
}
home := homedir.HomeDir()
return filepath.Join(home, ".kube", "config")
}

func NewDiscoveryClient(kubeconfig, context string) (*discovery.DiscoveryClient, error) {
config, err := getKubeClientConfig(kubeconfig, context)
if err != nil {
Expand Down

0 comments on commit cf26a01

Please sign in to comment.