Skip to content
Closed
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
49 changes: 39 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,10 @@ func main() {

// restConfig builds a *rest.Config from the --kubeconfig flag or $KUBECONFIG.
func restConfig() (*rest.Config, error) {
cfg := kubeCfgPath
if cfg == "" {
cfg = os.Getenv("KUBECONFIG")
if path := resolvedKubeconfigPath(); path != "" {
return clientcmd.BuildConfigFromFlags("", path)
}
return clientcmd.BuildConfigFromFlags("", cfg)
return rest.InClusterConfig()
}
Comment on lines +134 to 138
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

KUBECONFIG list-support is broken

resolvedKubeconfigPath() forwards the literal content of $KUBECONFIG to clientcmd.BuildConfigFromFlags.
When the variable contains a path-list (colon/semicolon separated) – which is the documented behaviour – ExplicitPath is set to that multi-path string and client-go treats it as one filename, leading to stat /foo:bar: no such file or directory.

-   if path := resolvedKubeconfigPath(); path != "" {
-       return clientcmd.BuildConfigFromFlags("", path)
+   if path := resolvedKubeconfigPath(); path != "" {
+       // `resolvedKubeconfigPath()` guarantees a *single* existing file.
+       return clientcmd.BuildConfigFromFlags("", path)
    }

Please ensure the helper splits the list and returns the first existing file (see suggestion below).
Without this fix users with a standard multi-path KUBECONFIG will be unable to run any command outside the cluster.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In main.go around lines 134 to 138, the function resolvedKubeconfigPath()
incorrectly passes the entire KUBECONFIG environment variable as a single path
string to clientcmd.BuildConfigFromFlags, which breaks support for multiple
paths separated by colon or semicolon. To fix this, modify
resolvedKubeconfigPath() to split the KUBECONFIG string by the appropriate path
list separator, iterate over the resulting paths, and return the first path that
exists as a valid file. This ensures clientcmd.BuildConfigFromFlags receives a
single valid kubeconfig file path.


// helmCfg returns an initialised Helm configuration bound to a namespace.
Expand Down Expand Up @@ -638,13 +637,25 @@ func cmdDelete() *cobra.Command {

// defaultNamespace returns the namespace from the kubeconfig or "default".
func defaultNamespace() (string, bool, error) {
cfg := kubeCfgPath
if cfg == "" {
cfg = os.Getenv("KUBECONFIG")
path := resolvedKubeconfigPath()
if path != "" {
loading := &clientcmd.ClientConfigLoadingRules{ExplicitPath: path}
cc := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loading, &clientcmd.ConfigOverrides{})
ns, overridden, err := cc.Namespace()
if err != nil {
return "default", false, nil
}
return ns, overridden, nil
}
loading := &clientcmd.ClientConfigLoadingRules{ExplicitPath: cfg}
cc := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loading, &clientcmd.ConfigOverrides{})
return cc.Namespace()

// Try reading namespace from in-cluster location
data, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err == nil {
return strings.TrimSpace(string(data)), false, nil
}

// Fallback
return "default", false, nil
}

// serverTable retrieves a metav1.Table from the API server.
Expand Down Expand Up @@ -1263,3 +1274,21 @@ func discoverKubeVersion(rc *rest.Config) (hchart.KubeVersion, error) {
Minor: info.Minor,
}, nil
}

func resolvedKubeconfigPath() string {
if kubeCfgPath != "" {
return kubeCfgPath
}
if env := os.Getenv("KUBECONFIG"); env != "" {
return env
}
home, err := os.UserHomeDir()
if err != nil {
return ""
}
cfg := filepath.Join(home, ".kube", "config")
if _, err := os.Stat(cfg); err == nil {
return cfg
}
return ""
}
Comment on lines +1278 to +1294
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Handle multi-path $KUBECONFIG + tighten file existence check

Nice centralisation! Two small improvements will make it rock-solid:

  1. Split $KUBECONFIG with filepath.SplitList() and return the first existing file.
  2. Guard the explicit --kubeconfig path with os.Stat as well, providing an early, clear error.

Proposed implementation:

 func resolvedKubeconfigPath() string {
-    if kubeCfgPath != "" {
-        return kubeCfgPath
+    if kubeCfgPath != "" {
+        if _, err := os.Stat(kubeCfgPath); err == nil {
+            return kubeCfgPath
+        }
     }
-    if env := os.Getenv("KUBECONFIG"); env != "" {
-        return env
+    if env := os.Getenv("KUBECONFIG"); env != "" {
+        for _, p := range filepath.SplitList(env) {
+            if p == "" {
+                continue
+            }
+            if _, err := os.Stat(p); err == nil {
+                return p
+            }
+        }
     }
     home, err := os.UserHomeDir()
     if err != nil {
         return ""
     }
-    cfg := filepath.Join(home, ".kube", "config")
-    if _, err := os.Stat(cfg); err == nil {
-        return cfg
+    cfg := filepath.Join(home, ".kube", "config")
+    if _, err := os.Stat(cfg); err == nil {
+        return cfg
     }
     return ""
 }

This preserves the current precedence while eliminating the multi-path and non-existent file pitfalls.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func resolvedKubeconfigPath() string {
if kubeCfgPath != "" {
return kubeCfgPath
}
if env := os.Getenv("KUBECONFIG"); env != "" {
return env
}
home, err := os.UserHomeDir()
if err != nil {
return ""
}
cfg := filepath.Join(home, ".kube", "config")
if _, err := os.Stat(cfg); err == nil {
return cfg
}
return ""
}
func resolvedKubeconfigPath() string {
if kubeCfgPath != "" {
if _, err := os.Stat(kubeCfgPath); err == nil {
return kubeCfgPath
}
}
if env := os.Getenv("KUBECONFIG"); env != "" {
for _, p := range filepath.SplitList(env) {
if p == "" {
continue
}
if _, err := os.Stat(p); err == nil {
return p
}
}
}
home, err := os.UserHomeDir()
if err != nil {
return ""
}
cfg := filepath.Join(home, ".kube", "config")
if _, err := os.Stat(cfg); err == nil {
return cfg
}
return ""
}
🤖 Prompt for AI Agents
In main.go around lines 1278 to 1294, the resolvedKubeconfigPath function should
be improved to handle multi-path KUBECONFIG environment variables and verify
file existence more robustly. Update the function to split the KUBECONFIG
environment variable using filepath.SplitList(), then iterate over the paths and
return the first one that exists. Also, add an os.Stat check for the explicit
kubeCfgPath variable to ensure it points to an existing file before returning
it, returning an empty string or error if it does not exist. This will ensure
the function returns a valid kubeconfig path respecting the current precedence
rules.

Loading