Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support kubeconfig env var #9

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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