{"record":{"id":"b05565156bc4f81f","repo":"ahmetb/kubectx","slug":"failed-to-load-kubeconfig-w","errorCode":null,"errorMessage":"failed to load kubeconfig: %w","messagePattern":"failed to load kubeconfig: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/proxy/readonly.go","lineNumber":68,"sourceCode":"}\n\n// Config holds information needed to start the readonly proxy.\ntype Config struct {\n\tKubeconfigPath string\n\tContextName    string\n}\n\n// Start creates and starts a readonly reverse proxy on a random localhost port.\n// The proxy loads TLS/auth config from the kubeconfig and forwards only\n// GET, HEAD, and OPTIONS requests (without protocol upgrades) to the real API server.\nfunc Start(cfg Config) (*ReadonlyProxy, error) {\n\tloadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: cfg.KubeconfigPath}\n\toverrides := &clientcmd.ConfigOverrides{CurrentContext: cfg.ContextName}\n\tclientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)\n\n\trestCfg, err := clientConfig.ClientConfig()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to load kubeconfig: %w\", err)\n\t}\n\n\ttargetURL, err := url.Parse(restCfg.Host)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to parse server URL %q: %w\", restCfg.Host, err)\n\t}\n\n\ttransport, err := rest.TransportFor(restCfg)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create transport: %w\", err)\n\t}\n\n\thandler := NewHandler(targetURL, transport)\n\n\tlistener, err := net.Listen(\"tcp\", \"127.0.0.1:0\")\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to listen: %w\", err)\n\t}","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/internal/proxy/readonly.go#L50-L86","documentation":"readonly proxy Start builds a clientcmd loading config from cfg.KubeconfigPath (explicit path) and cfg.ContextName (context override), then asks for a rest.Config via ClientConfig(). This error wraps any failure from that chain: the kubeconfig file missing or unreadable, invalid YAML, or clientcmd validation errors such as the context/cluster/user not existing or no server defined.","triggerScenarios":"Calling Start with cfg.KubeconfigPath pointing at a non-existent or unreadable file, cfg.ContextName naming a context absent from the kubeconfig, or a kubeconfig whose selected context lacks a valid cluster/user/server or auth data.","commonSituations":"Typo in --kubeconfig path or context name; context deleted from the kubeconfig after the proxy was configured; kubeconfig with stale/empty cluster entry; running the proxy on a machine without the operator's kubeconfig (CI, container).","solutions":["Verify cfg.KubeconfigPath exists and is readable; test with kubectl --kubeconfig <path> config get-contexts","Confirm cfg.ContextName matches an existing context exactly (kubectl config get-contexts); clear it to use the current context","Fix validation errors clientcmd reports (missing server, missing user for the chosen context) via kubectl config set-* commands","Check KUBECONFIG env var interactions if KubeconfigPath is empty — loading rules still consult the environment"],"exampleFix":"// before\ncfg := proxy.Config{KubeconfigPath: \"~/.kube/conf\"}        // wrong path\ncfg := proxy.Config{KubeconfigPath: \"/home/u/.kube/config\"}","handlingStrategy":"validation","validationCode":"// Go\n// Pre-check path, context existence and server before Start\nif _, err := os.Stat(cfg.KubeconfigPath); err != nil {\n    return fmt.Errorf(\"kubeconfig %s: %w\", cfg.KubeconfigPath, err)\n}\nraw, _ := os.ReadFile(cfg.KubeconfigPath)\nparsed, _ := clientcmd.Load(raw)\nif cfg.ContextName != \"\" && !parsed.Contexts[cfg.ContextName].Cluster != true {\n    // context missing\n    return fmt.Errorf(\"context %q not found in %s\", cfg.ContextName, cfg.KubeconfigPath)\n}","typeGuard":null,"tryCatchPattern":"p, err := proxy.Start(ctx, cfg)\nif err != nil && strings.Contains(err.Error(), \"failed to load kubeconfig\") {\n    return fmt.Errorf(\"verify --kubeconfig path and --context name (kubectl config get-contexts): %w\", err)\n}","preventionTips":["Validate the kubeconfig path and context name with kubectl before wiring them into Config","Use the resolved absolute path, not ~ (Go does not expand tilde)","Keep kubeconfig contexts in sync with configuration (contexts get deleted)","Provision the operator kubeconfig on machines running the proxy (CI, containers)"],"tags":["kubeconfig","client-go","configuration","go"],"backgroundTag":"kubeconfig-load-failed","analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}