{"record":{"id":"12431e3bea58edac","repo":"GoogleContainerTools/skaffold","slug":"getting-kubernetes-client-w-12431e","errorCode":null,"errorMessage":"getting Kubernetes client: %w","messagePattern":"getting Kubernetes client: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/hooks/container.go","lineNumber":97,"sourceCode":"\t}\n}\n\n// containerHook represents a lifecycle hook to be executed inside a running container\ntype containerHook struct {\n\tcfg        latest.ContainerHook\n\tcli        *kubectl.CLI\n\tselector   containerSelector\n\tnamespaces []string\n\tformatter  logger.Formatter\n}\n\n// run executes the lifecycle hook inside the target container\nfunc (h containerHook) run(ctx context.Context, out io.Writer) error {\n\terrs, ctx := errgroup.WithContext(ctx)\n\n\tclient, err := kubernetesclient.Client(h.cli.KubeContext)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"getting Kubernetes client: %w\", err)\n\t}\n\n\tfor _, ns := range h.namespaces {\n\t\tpods, err := client.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{})\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"getting pods for namespace %q: %w\", ns, err)\n\t\t}\n\n\t\tfor _, p := range pods.Items {\n\t\t\tfor _, c := range p.Spec.Containers {\n\t\t\t\tif matched, err := h.selector(p, c); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t} else if !matched {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\targs := []string{p.Name, \"--namespace\", p.Namespace, \"-c\", c.Name, \"--\"}\n\t\t\t\targs = append(args, h.cfg.Command...)\n\t\t\t\tcmd := h.cli.Command(ctx, \"exec\", args...)","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/hooks/container.go#L79-L115","documentation":"containerHook.run executes lifecycle hooks inside containers of running pods. It first builds a Kubernetes client via kubernetesclient.Client for the configured kube context. If client construction fails, the error is wrapped as 'getting Kubernetes client: %w'.","triggerScenarios":"Calling run (from RunPostHooks or RunPreHooks of a container hook) with a h.cli.KubeContext that cannot produce a client — missing/invalid kubeconfig, unreachable context, or failed REST config loading.","commonSituations":"KubeContext referencing a context absent from ~/.kube/config; corrupted or unreadable kubeconfig; KUBECONFIG env var pointing to a nonexistent file; cluster credentials expired/removed before hooks run.","solutions":["Verify the context exists: kubectl config get-contexts, and fix the kubeContext in skaffold.yaml","Check KUBECONFIG points to a valid, readable kubeconfig file","Run 'kubectl --context <ctx> get pods' to confirm connectivity, then re-run skaffold","Refresh credentials (e.g. gcloud auth login / aws eks update-kubeconfig) if auth is the cause"],"exampleFix":"// before (skaffold.yaml)\nkubeContext: \"staging-cluster\"  // not in kubeconfig\n// after, on the shell:\nkubectl config get-contexts\nskaffold deploy --kube-context staging-cluster  # or set a valid context","handlingStrategy":"retry","validationCode":"ctxName := h.cli.KubeContext\nif err := exec.Command(\"kubectl\", \"--context\", ctxName, \"get\", \"ns\").Run(); err != nil {\n    return fmt.Errorf(\"context %q unusable before hooks: %w\", ctxName, err)\n}","typeGuard":null,"tryCatchPattern":"err := hook.Run(ctx, out)\nif err != nil && strings.Contains(err.Error(), \"getting Kubernetes client\") {\n    // brief retry for transient kubeconfig/auth issues\n    return retry.Do(func() error { return hook.Run(ctx, out) }, retry.Attempts(3))\n}","preventionTips":["Ensure kubeContext in skaffold.yaml exists in ~/.kube/config","Refresh cloud credentials before long-running deploy jobs","Set KUBECONFIG explicitly in CI environments"],"tags":["kubernetes","client","kubeconfig","hooks"],"backgroundTag":"kubeconfig-context-not-found","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"}