ahmetb/kubectx · error
context with name %q not found
Error message
context with name %q not found
What it means
The clean no-file-errors variant of the lookup failure: no kubeconfig file errored, but none contained a context with the given name. Raised by contextNodeWithFileIndex when fileErrors is empty, meaning the search was complete and the context name genuinely does not exist.
Source
Thrown at internal/kubeconfig/contexts.go:63
contexts, err := contextsNodeOf(&k.files[i])
if err != nil {
fileErrors = append(fileErrors, fmt.Errorf("file %d: %w", i, err))
continue
}
context, err := contexts.Pipe(yaml.Lookup("[name=" + name + "]"))
if err != nil {
fileErrors = append(fileErrors, fmt.Errorf("file %d lookup: %w", i, err))
continue
}
if context != nil {
return context, i, nil
}
}
if len(fileErrors) > 0 {
return nil, -1, fmt.Errorf("context with name %q not found (errors in files: %w)",
name, errors.Join(fileErrors...))
}
return nil, -1, fmt.Errorf("context with name %q not found", name)
}
func (k *Kubeconfig) contextNode(name string) (*yaml.RNode, error) {
node, _, err := k.contextNodeWithFileIndex(name)
return node, err
}
func (k *Kubeconfig) ContextNames() ([]string, error) {
seen := make(map[string]bool)
var names []string
for i := range k.files {
contexts, err := k.files[i].config.Pipe(yaml.Get("contexts"))
if err != nil {
return nil, fmt.Errorf("failed to get contexts: %w", err)
}
if contexts == nil {
continueView on GitHub (pinned to 12ad6fb22e)
Solutions
- List contexts and copy the exact name: kubectl config get-contexts (or kubens).
- Confirm KUBECONFIG points at the file that actually contains the context.
- Create the context if it should exist: kubectl config set-context <name> --cluster=... --user=...
- Guard the operation in code by checking existence before delete/modify.
Example fix
// before
if err := kc.DeleteContextEntry("stg-context"); err != nil { return err }
// after: check existence first
node, err := kc.ContextNode("stg-context") // or equivalent existence check
if err != nil {
return fmt.Errorf("context %q does not exist, nothing to delete", "stg-context")
} Defensive patterns
Strategy: validation
Validate before calling
// Go: check existence before delete/modify to avoid the not-found path
node, err := kc.ContextNode(name) // uses the same lookup internally
if err != nil {
return fmt.Errorf("context %q does not exist: %w", name, err)
} Try / catch
if err := kc.DeleteContextEntry(name); err != nil {
if strings.Contains(err.Error(), "not found") {
return nil // idempotent delete: treat missing context as success
}
return err
} Prevention
- Verify the exact context name with kubectl config get-contexts before mutating.
- Confirm KUBECONFIG points at the file that contains the target context.
- Make delete operations idempotent when context may already be gone.
- Generate context names from a single source of truth in automation.
When it happens
Trigger: DeleteContextEntry, ModifyContextName, or contextNode called with a context name absent from all parsed kubeconfig files, with all files parsed successfully.
Common situations: Typo in the context name or wrong casing; operating against the wrong KUBECONFIG file than the one holding the context; context removed by a concurrent tool or cluster teardown script.
Related errors
- failed to get current context: %w
- failed to clear namespace: %w
- context with name %q not found (errors in files: %w)
- kubeconfig error: %w
- failed to get current context: %w
AI-assisted analysis of ahmetb/kubectx@12ad6fb22e (2026-09-02).
Data as JSON: /api/errors/7296c8f1ef17b94d.
Report an issue: GitHub.