ahmetb/kubectx · error
kubeconfig error: %w
Error message
kubeconfig error: %w
What it means
In the fzf interactive mode, Run parses the kubeconfig before listing namespaces. Unlike the plain current-namespace command, a "not found" error is handled gracefully with a warning; any other parse failure is wrapped as "kubeconfig error: %w". It indicates the kubeconfig exists but cannot be parsed.
Source
Thrown at cmd/kubens/fzf.go:44
"github.com/ahmetb/kubectx/internal/cmdutil"
"github.com/ahmetb/kubectx/internal/env"
"github.com/ahmetb/kubectx/internal/kubeconfig"
"github.com/ahmetb/kubectx/internal/printer"
)
type InteractiveSwitchOp struct{}
// TODO(ahmetb) This method is heavily repetitive vs kubectx/fzf.go.
func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
// parse kubeconfig just to see if it can be loaded
kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
defer kc.Close()
if err := kc.Parse(); err != nil {
if cmdutil.IsNotFoundErr(err) {
printer.Warning(stderr, "kubeconfig file not found")
return nil
}
return fmt.Errorf("kubeconfig error: %w", err)
}
var in bytes.Buffer
if err := formatNamespaceList(kc, &in); err != nil {
return err
}
if in.Len() == 0 {
return errors.New("no namespaces found")
}
cmd := exec.Command("fzf", "--ansi", "--no-preview")
var out bytes.Buffer
cmd.Stdin = &in
cmd.Stderr = stderr
cmd.Stdout = &out
cmd.Env = append(os.Environ(),
fmt.Sprintf("%s=1", env.EnvForceColor))View on GitHub (pinned to 12ad6fb22e)
Solutions
- Validate the file with `kubectl config view --raw` to reproduce and locate the parse error
- Fix YAML syntax (indentation, tabs, duplicate keys)
- Restore the kubeconfig from your cluster provider or backup
- Check KUBECONFIG for a wrong file and correct it
Example fix
// before # ~/.kube/config clusters: - name: foo # tab instead of spaces // after clusters: - name: foo # valid indentation
Defensive patterns
Strategy: validation
Validate before calling
kcPath := os.Getenv("KUBECONFIG")
if kcPath == "" {
kcPath = filepath.Join(os.Getenv("HOME"), ".kube", "config")
}
data, err := os.ReadFile(kcPath)
if err != nil {
return fmt.Errorf("cannot read kubeconfig: %w", err)
}
var v map[string]interface{}
if err := yaml.Unmarshal(data, &v); err != nil {
return fmt.Errorf("kubeconfig YAML invalid: %w", err)
} Try / catch
if err := op.Run(stdout, stderr); err != nil {
if strings.Contains(err.Error(), "kubeconfig error:") {
// validate with `kubectl config view` and repair the YAML
}
return err
} Prevention
- Lint kubeconfig YAML after manual edits
- Use kubectl config commands instead of hand-editing
- Back up the config before credential-refresh tools rewrite it
- Distinguish 'not found' (handled with warning) from parse errors
When it happens
Trigger: `kubens` (interactive fzf mode) invoked when the kubeconfig file loads but kc.Parse() fails on malformed YAML or schema-invalid content; only cmdutil.IsNotFoundErr is special-cased.
Common situations: Hand-edited ~/.kube/config with YAML mistakes; truncated config from an interrupted credential write; KUBECONFIG pointing at a non-kubeconfig YAML file.
Related errors
- kubeconfig error: %w
- failed to switch namespace: %w
- failed to get current context: %w
- failed to read namespace of "%s": %w
- failed to close encoder for file %d: %w
AI-assisted analysis of ahmetb/kubectx@12ad6fb22e (2026-09-02).
Data as JSON: /api/errors/7beb76675d9cf0e5.
Report an issue: GitHub.