{"record":{"id":"41dbe528468aacea","repo":"slimtoolkit/slim","slug":"no-containers","errorCode":null,"errorMessage":"no containers","messagePattern":"no containers","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/app/master/command/debug/handle_kubernetes_runtime.go","lineNumber":218,"sourceCode":"\t\t\t\t}\n\t\t\t\tif info.ExitMessage != \"\" {\n\t\t\t\t\toutParams[\"exit.message\"] = info.ExitMessage\n\t\t\t\t}\n\t\t\t}\n\n\t\t\txc.Out.Info(\"debug.session\", outParams)\n\t\t}\n\n\t\treturn\n\t}\n\n\tif commandParams.TargetRef == \"\" {\n\t\tlogger.Debug(\"no explicit target container... pick one\")\n\t\t//TODO: improve this logic (to also check for the default container)\n\t\tif len(pod.Spec.Containers) > 0 {\n\t\t\tcommandParams.TargetRef = pod.Spec.Containers[0].Name\n\t\t} else {\n\t\t\txc.FailOn(fmt.Errorf(\"no containers\"))\n\t\t}\n\t}\n\n\tif commandParams.ActionShowSessionLogs {\n\t\t//list sessions before we pick a target container,\n\t\t//so we can list all debug session for the selected pod\n\t\txc.Out.State(\"action.show_session_logs\",\n\t\t\tovars{\n\t\t\t\t\"namespace\": nsName,\n\t\t\t\t\"pod\":       podName,\n\t\t\t\t\"target\":    commandParams.TargetRef,\n\t\t\t\t\"session\":   commandParams.Session})\n\n\t\tif commandParams.Session == \"\" {\n\t\t\tresult, err := listK8sDebugContainers(ctx, api, nsName, podName, commandParams.TargetRef, false)\n\t\t\tif err != nil {\n\t\t\t\tlogger.WithError(err).Error(\"listK8sDebugContainers\")\n\t\t\t\txc.FailOn(err)","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/slimtoolkit/slim/blob/81940d17fa112cc678e32209214bcb2355cb3004/pkg/app/master/command/debug/handle_kubernetes_runtime.go#L200-L236","documentation":"When no explicit target container is given, HandleKubernetesRuntime falls back to the first container in pod.Spec.Containers. If the pod spec has zero containers, there is nothing to attach to and it fails with 'no containers'.","triggerScenarios":"Running the kubernetes runtime debug command without a --target container while the resolved pod's spec.containers array is empty — e.g. ephemeral/sidecar-only specs, truncated or bogus pod manifests, or a pod created with only initContainers.","commonSituations":"Targeting a pod whose normal containers were removed by a controller mutation, custom/edge-case workloads with only ephemeral or init containers, or stale/corrupted manifests from an operator.","solutions":["Specify the target container explicitly via the target ref instead of relying on auto-pick","Verify the pod manifest actually defines spec.containers entries (kubectl get pod -o jsonpath='{.spec.containers[*].name}')","If only initContainers/ephemeral containers exist, recreate the pod with at least one regular container"],"exampleFix":"// before\ndebug --target pod/my-pod                // auto-pick fails: no containers\n// after\ndebug --target pod/my-pod --container my-container","handlingStrategy":"type-guard","validationCode":"pod, _ := clientset.CoreV1().Pods(ns).Get(ctx, name, metav1.GetOptions{})\nif len(pod.Spec.Containers) == 0 {\n    return fmt.Errorf(\"pod %s/%s has no containers; pass an explicit target\", ns, name)\n}","typeGuard":"func hasContainers(p *corev1.Pod) bool { return p != nil && len(p.Spec.Containers) > 0 }","tryCatchPattern":"err := HandleKubernetesRuntime(...)\nif err != nil && strings.Contains(err.Error(), \"no containers\") {\n    return fmt.Errorf(\"pod has no regular containers; specify a target container or fix the pod spec: %w\", err)\n}","preventionTips":["Always specify the target container explicitly for unusual workloads","Inspect spec.containers before attaching to operator-managed or mutated pods","Remember initContainers/ephemeralContainers don't count for the auto-pick fallback"],"tags":["kubernetes","pod-spec","precondition"],"backgroundTag":"pod-has-no-containers","analyzedSha":"81940d17fa112cc678e32209214bcb2355cb3004","analyzedAt":"2026-08-31T23:06:12.682Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}