Skip to content

fix(k8s): select a running pod, not the first pod of a label #7377

Description

@dan-petty

Problem or Motivation

Two commands read "the Mattermost pod" as the first pod that a label selector returns, with no filter on the phase: src/devops_cli/k8s/manifest_plans.py (line 143) and src/devops_cli/commands/k8s/mattermost_bootstrap.py (line 72) take .items[0].

Namespace mattermost of the homelab holds a second pod with the same labels: mattermost-87ccc77fb-vbvrj, phase Succeeded since 2026-11-21, beside the running pod of the same ReplicaSet. The commands get the running pod only because its name sorts first. With another name order they exec into a pod that is not running and fail.

The same kind of leftover pod (phase Succeeded or Failed, of a ReplicaSet that has a running pod) is in other namespaces after node restarts, for example gitea, local-path-storage and argo-workflows.

Evidence

  • kubectl -n mattermost get pods -l app=mattermost shows two pods, one Running and one Completed (2026-11-27).
  • Read from the code; the failure was not reproduced.

Acceptance Criteria

  • Each of the two commands selects a pod that is Running (a field selector status.phase=Running, or a filter on the Ready condition).
  • A test with a fake pod list that holds a Succeeded pod before the running pod proves it.
  • A search of src/ for .items[0] on a pod list shows no other command with the same assumption, or the change fixes each one.

Target Subsystem

Kubernetes & Minikube Automation

Existing Tool Considered

kubectl --field-selector=status.phase=Running covers the filter. No new tool.

Alternatives check of 2026-11-27: none.
Tool: none found.
Reason: Searches found only the Kubernetes Python client and kubectl, which the project already uses and which offer the phase filter; neither is a tool that fixes the .items[0] selection, and the issue asks for no new tool.

Lacks:

  • No library replaces the two .items[0] call sites
  • Phase and Ready filtering must be written in project code
  • Fake-pod-list test for the ordering defect stays project work

Proposed design

Problem statement

Two commands read the first pod of a label selector. A leftover Succeeded pod can sort first, so kubectl exec targets a pod that is not running. The change filters the lookup to status.phase=Running and tests the order with a fake pod list.

Acceptance criteria

Requirement: Dry-run pod lookup filters on Running

The Mattermost bootstrap dry-run plan SHALL include --field-selector=status.phase=Running in its pod lookup command.

Scenario: Plan argv includes the phase filter

  • WHEN a test calls plan_k8s_bootstrap_mattermost with team devops and no context
  • THEN the first planned request has --field-selector=status.phase=Running in its argv.

Requirement: Live pod selection skips ended pods

The live Mattermost bootstrap SHALL select a pod whose phase is Running and ignore a pod whose phase is Succeeded or Failed.

Scenario: A Succeeded pod comes before the Running pod

  • WHEN a test passes a fake pod list with a Succeeded pod before a Running pod to _first_running_pod
  • THEN the helper returns the name of the Running pod.

Requirement: Live pod lookup requests Running phase

The live Mattermost bootstrap SHALL add --field-selector=status.phase=Running to each kubectl get pods command that it runs.

Scenario: Both label selectors request the phase filter

  • WHEN a test mocks runtime._run_cmd so the first label selector returns no pod and calls _resolve_mattermost_pod with no context
  • THEN each recorded kubectl get pods command contains --field-selector=status.phase=Running.

Requirement: No unfiltered pod-list first-item lookup remains

The source tree SHALL contain no pod-list .items[0] lookup that lacks a Running phase filter.

Scenario: A source scan finds only filtered pod lookups

  • WHEN a test scans src/devops_cli for get pods commands that contain .items[0]
  • THEN each match also contains --field-selector=status.phase=Running or calls _first_running_pod.

Existing Tool Considered

The alternatives check in the issue body records verdict none and no tool. No open tool or library covers the selection. The change uses the existing kubectl --field-selector=status.phase=Running and a small project helper. Cost: about 2,000 tokens and 3 hours to build, keep and fix.

Key questions

  • Which pod filter should the two commands use?: Use --field-selector=status.phase=Running on each kubectl get pods call. The dry-run plan keeps jsonpath={.items[0].metadata.name} after the filter. The live path adds the same filter, reads -o json, and calls a pure _first_running_pod helper. (best practice) [sources: src/devops_cli/k8s/manifest_plans.py:143, src/devops_cli/commands/k8s/mattermost_bootstrap.py:72]
    • Chosen: kubectl field selector status.phase=Running. It filters on the server, needs no new library and matches the issue.
    • Rejected: Ready condition filter. It adds condition parsing and does not cover the phase-only case more simply.
    • Rejected: Kubernetes Python client list with a phase filter. It adds a client dependency for a lookup that kubectl already does.
    • Rejected: Client-side filter only. It transfers ended pods and repeats parsing in both call sites.
  • How does the live command prove the ordering defect offline?: Add a pure _first_running_pod(items) helper and test it with a fake list that has a Succeeded pod before the Running pod. The live resolver calls the helper after kubectl get pods -o json. (best practice) [sources: src/devops_cli/commands/k8s/mattermost_bootstrap.py:72, tests/commands/test_k8s_bootstrap_mattermost.py]
    • Chosen: Pure helper plus fake pod list. It proves the order defect with no cluster.
    • Rejected: Assert only the command string. It shows the filter but does not prove the selection order.
    • Rejected: Live cluster test. It needs a cluster and is not an offline test.
  • Should the dry-run plan keep .items[0]?: Yes. The field selector limits the list to Running pods, so .items[0] selects a Running pod. The plan makes no request. (best practice) [sources: src/devops_cli/k8s/manifest_plans.py:143]
    • Chosen: Keep .items[0] after the field selector. The filter makes the first item safe and the plan stays simple.
    • Rejected: Use a jsonpath filter expression. It is harder to read and adds no safety after the field selector.
  • How does the change catch other pod-list .items[0] call sites?: Add a source-scan test. It fails on any get pods command that uses .items[0] without --field-selector=status.phase=Running. The issue names two call sites, and the change fixes both. (best practice) [sources: src/devops_cli/k8s/manifest_plans.py:143, src/devops_cli/commands/k8s/mattermost_bootstrap.py:72]
    • Chosen: Source-scan test. It keeps the search result enforced after the change.
    • Rejected: Manual search only. A later change can add the same defect again.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. dan-petty commented on Nov 27, 2026

    @dan-petty
    OwnerAuthor
  2. added
    type/bug
    scope/k8s
    source/agent
    status/new
    value/medium
    effort/low
    priority/p2-medium
    on Nov 27, 2026
  3. devops-cli-service commented on Nov 27, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (4)

    Tools considered:

    Searches run:

    • kubernetes python client list pods field_selector status.phase=Running, results: 5
    • tool that selects running pod instead of first pod from label selector kubectl, results: 5
    • kubernetes python client field selector pod phase Running example, results: 5
    • kubectl field selector status.phase Running filter pods documentation, results: 5

    Sources:

  4. added
    status/ready
    and removed
    status/new
    on Nov 28, 2026
  5. devops-cli-service commented on Nov 28, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

  6. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 28, 2026
  7. removed
    status/in-review
    owner/agent
    on Nov 28, 2026
  8. devops-cli-service commented on Nov 29, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-29 00:41 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p2-mediumscope/k8ssource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions