{"record":{"id":"2a421fb12df81887","repo":"GoogleContainerTools/skaffold","slug":"error-creating-rest-client-config-in-cluster-w","errorCode":null,"errorMessage":"error creating REST client config in-cluster: %w","messagePattern":"error creating REST client config in-cluster: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/kubernetes/context/context.go","lineNumber":100,"sourceCode":"\t}\n\treturn c, nil\n}\n\nfunc getRestClientConfig(kctx string, kcfg string) (*restclient.Config, error) {\n\tlog.Entry(context.TODO()).Debugf(\"getting client config for kubeContext: `%s`\", kctx)\n\n\trawConfig, err := getCurrentConfig()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tclientConfig := clientcmd.NewNonInteractiveClientConfig(rawConfig, kctx, &clientcmd.ConfigOverrides{CurrentContext: kctx}, clientcmd.NewDefaultClientConfigLoadingRules())\n\trestConfig, err := clientConfig.ClientConfig()\n\tif kctx == \"\" && kcfg == \"\" && clientcmd.IsEmptyConfig(err) {\n\t\tlog.Entry(context.TODO()).Debug(\"no kube-context set and no kubeConfig found, attempting in-cluster config\")\n\t\trestConfig, err := restclient.InClusterConfig()\n\t\tif err != nil {\n\t\t\treturn restConfig, fmt.Errorf(\"error creating REST client config in-cluster: %w\", err)\n\t\t}\n\n\t\treturn restConfig, nil\n\t}\n\tif err != nil {\n\t\treturn restConfig, fmt.Errorf(\"error creating REST client config for kubeContext %q: %w\", kctx, err)\n\t}\n\n\treturn restConfig, nil\n}\n\n// getCurrentConfig retrieves and caches the raw kubeConfig. The cache ensures that Skaffold always works with the identical kubeconfig,\n// even if it was changed on disk.\nfunc getCurrentConfig() (clientcmdapi.Config, error) {\n\tkubeConfigOnce.Do(func() {\n\t\tloadingRules := clientcmd.NewDefaultClientConfigLoadingRules()\n\t\tloadingRules.ExplicitPath = kubeConfigFile\n\t\tkubeConfig = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/kubernetes/context/context.go#L82-L118","documentation":"In getRestClientConfig, when no kubeContext and no kubeconfig path are given and the resulting client config is empty (clientcmd.IsEmptyConfig), Skaffold falls back to restclient.InClusterConfig(). If that fails — typically because the process is not running inside a Kubernetes pod — this wrapped error is returned.","triggerScenarios":"GetDefaultRestClientConfig (or GetRestClientConfig with empty kctx/kcfg) executed where: no KUBECONFIG file exists, and KUBERNETES_SERVICE_HOST/KUBERNETES_SERVICE_PORT env vars or /var/run/secrets/kubernetes.io/serviceaccount/token are absent (i.e., outside a pod).","commonSituations":"Running skaffold deploy on a laptop without any kubeconfig; CI container that is not an in-cluster pod; in-cluster pod with automountServiceAccountToken disabled.","solutions":["Provide a kubeconfig: set KUBECONFIG or run `kubectl config use-context <ctx>` so the in-cluster fallback isn't taken","If in-cluster use is intended, run inside a pod with a mounted service-account token","In-cluster, set automountServiceAccountToken: true on the pod spec"],"exampleFix":"// before: no kubeconfig, outside cluster -> InClusterConfig fails\n// after\nexport KUBECONFIG=$HOME/.kube/config\nkubectl config use-context minikube","handlingStrategy":"validation","validationCode":"inCluster := os.Getenv(\"KUBERNETES_SERVICE_HOST\") != \"\" && os.Getenv(\"KUBERNETES_SERVICE_PORT\") != \"\"\nhasKubeconfig := os.Getenv(\"KUBECONFIG\") != \"\"\ncfgExists, _ := fileExists(filepath.Join(homedir.HomeDir(), \".kube\", \"config\"))\nif !inCluster && !hasKubeconfig && !cfgExists {\n    return errors.New(\"neither kubeconfig nor in-cluster credentials available\")\n}","typeGuard":"func hasConfigSource() bool {\n    if os.Getenv(\"KUBECONFIG\") != \"\" { return true }\n    if _, err := os.Stat(filepath.Join(homedir.HomeDir(), \".kube\", \"config\")); err == nil { return true }\n    _, err := restclient.InClusterConfig()\n    return err == nil\n}","tryCatchPattern":"cfg, err := context.GetDefaultRestClientConfig()\nif err != nil && strings.Contains(err.Error(), \"in-cluster\") {\n    return fmt.Errorf(\"not running in a pod and no kubeconfig; set KUBECONFIG: %w\", err)\n}","preventionTips":["Always provide a kubeconfig when running outside a cluster","In pods, keep automountServiceAccountToken enabled","Detect in-cluster via KUBERNETES_SERVICE_HOST before choosing a config strategy"],"tags":["kubernetes","in-cluster","rest-config"],"backgroundTag":"in-cluster-config-unavailable","analyzedSha":"a1189de023efc32d4b8e11f395acc678aa555011","analyzedAt":"2026-09-05T12:09:27.064Z","contentChangedAt":"2026-09-05T12:09:27.064Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}