Skip to content
Merged
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
19 changes: 9 additions & 10 deletions internal/clients/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/url"
"os"
"path"
"path/filepath"
"time"

appsv1 "k8s.io/api/apps/v1"
Expand All @@ -36,18 +35,18 @@ func (c *Client) Clientset() kubernetes.Interface {
return c.clientset
}

// NewClient creates a new Kubernetes client
// NewClient creates a new Kubernetes client.
// Kubeconfig precedence: explicit flag > KUBECONFIG env var > ~/.kube/config
func NewClient(kubeconfigPath string, debug bool) (*Client, error) {
if kubeconfigPath == "" {
// Use default kubeconfig location
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to get home directory: %w", err)
}
kubeconfigPath = filepath.Join(home, ".kube", "config")
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
if kubeconfigPath != "" {
loadingRules.ExplicitPath = kubeconfigPath
}

config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingRules,
&clientcmd.ConfigOverrides{},
).ClientConfig()
if err != nil {
return nil, fmt.Errorf("failed to build config: %w", err)
}
Expand Down
Loading