Repository navigation
Description
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=mattermostshows two pods, oneRunningand oneCompleted(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
Succeededpod 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_mattermostwith teamdevopsand no context - THEN the first planned request has
--field-selector=status.phase=Runningin itsargv.
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
Succeededpod before aRunningpod to_first_running_pod - THEN the helper returns the name of the
Runningpod.
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_cmdso the first label selector returns no pod and calls_resolve_mattermost_podwith no context - THEN each recorded
kubectl get podscommand 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_cliforget podscommands that contain.items[0] - THEN each match also contains
--field-selector=status.phase=Runningor 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=Runningon eachkubectl get podscall. The dry-run plan keepsjsonpath={.items[0].metadata.name}after the filter. The live path adds the same filter, reads-o json, and calls a pure_first_running_podhelper. (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.
- Chosen: kubectl field selector
- 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 aSucceededpod before theRunningpod. The live resolver calls the helper afterkubectl 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.
- Chosen: Keep
- How does the change catch other pod-list
.items[0]call sites?: Add a source-scan test. It fails on anyget podscommand 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
dan-petty commentedon Nov 27, 2026
tab-k8s) #686, which asks for a different change.Both handle k8s pod phase awareness, but #686 adds status display and filters to the dashboard while this candidate fixes pod selection in two CLI commands.devops-cli-service commentedon Nov 27, 2026
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:
Sources:
devops-cli-service commentedon Nov 28, 2026
Next step: the stage job writes the plan of the item. See the section Proposed design of the body.
chore(main): release 0.241.0 (#7656)
devops-cli-service commentedon Nov 28, 2026
Released in v0.241.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.241.0
devops-cli-service commentedon Nov 29, 2026
Live since 2026-11-29 00:41 UTC