{"record":{"id":"4d7c3ba7e1b73ecc","repo":"apache/druid","slug":"could-not-find-container-named-s-in-podspec","errorCode":null,"errorMessage":"Could not find container named: %s in PodSpec","messagePattern":"Could not find container named: (.+?) in PodSpec","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"extensions-core/kubernetes-overlord-extensions/src/main/java/org/apache/druid/k8s/overlord/taskadapter/K8sTaskAdapter.java","lineNumber":434,"sourceCode":"    podTemplate.setSpec(podSpec);\n    return podTemplate;\n  }\n\n  @VisibleForTesting\n  static void massageSpec(PodSpec spec, String primaryContainerName)\n  {\n    // find the primary container and make it first,\n    if (StringUtils.isNotBlank(primaryContainerName)) {\n      int i = 0;\n      while (i < spec.getContainers().size()) {\n        if (primaryContainerName.equals(spec.getContainers().get(i).getName())) {\n          break;\n        }\n        i++;\n      }\n      // if the primaryContainer is not found, assume the primary container is the first container.\n      if (i >= spec.getContainers().size()) {\n        throw new IllegalArgumentException(\"Could not find container named: \"\n                                           + primaryContainerName\n                                           + \" in PodSpec\");\n      }\n      Container primary = spec.getContainers().get(i);\n      spec.getContainers().remove(i);\n      spec.getContainers().add(0, primary);\n    }\n  }\n\n  private List<String> javaOpts(Task task)\n  {\n    final List<String> javaOpts = new ArrayList<>();\n    Iterables.addAll(javaOpts, taskRunnerConfig.getJavaOptsArray());\n\n    // Override task specific javaOpts\n    Object taskJavaOpts = task.getContextValue(\n        ForkingTaskRunnerConfig.JAVA_OPTS_PROPERTY\n    );","sourceCodeStart":416,"sourceCodeEnd":452,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/extensions-core/kubernetes-overlord-extensions/src/main/java/org/apache/druid/k8s/overlord/taskadapter/K8sTaskAdapter.java#L416-L452","documentation":"massageSpec reorders the PodSpec's containers so the configured primary container (druid.k8s.adapter.environment variable K8S_TASK_ADAPTER primary container name) is first. If it iterates through all containers without finding a container whose name matches primaryContainerName, it throws this IllegalArgumentException, because it cannot unambiguously pick which container is the Druid peon.","triggerScenarios":"fromTask/toTask on a pod spec whose container names do not include the configured primary container name (default \"main\"); container renamed in a custom pod template; case mismatch in the name.","commonSituations":"Teams customize their peon pod template and rename the main container (e.g. to \"druid\" or \"peon\") without updating the overlord's primary container config; copy-pasted templates where the container is named differently per environment.","solutions":["Rename the container in the pod template back to the expected primary container name (default `main`), or","Set the adapter's primary container name config (druid.k8s.task.adapter primary container setting) to match the actual container name in the PodSpec.","Check the task's dynamic config / k8s pod template for typos in the container name.","As a quick check, list container names in the template with `kubectl get podtemplate <name> -o jsonpath='{.template.spec.containers[*].name}'`."],"exampleFix":"// before (pod template)\ncontainers:\n  - name: druid-peon\n    image: apache/druid:tag\n// after\ncontainers:\n  - name: main\n    image: apache/druid:tag","handlingStrategy":"validation","validationCode":"// java\nboolean found = podSpec.getContainers().stream()\n    .anyMatch(c -> c.getName().equals(primaryContainerName));\nif (!found) { throw new IllegalArgumentException(\"pod spec lacks container \" + primaryContainerName); }","typeGuard":null,"tryCatchPattern":"// java\ntry { adapter.toTask(pod); }\ncatch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"Could not find container named\")) {\n    // align container name or primary container config\n  }\n}","preventionTips":["Keep the main container named `main` across all environments.","Keep primary container name setting and template in the same config repo/change.","Assert container names after editing pod templates."],"tags":["kubernetes","task-adapter","pod-spec","container-name"],"backgroundTag":"resource-not-found","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}