Repository navigation
Assign distinct colors to pods created by the same workload - #375
Merged
Merged
Conversation
jojinkb
requested review from
floryut,
rkmathi,
superbrothers and
tksm
as code owners
July 25, 2026 10:03
tksm
approved these changes
Jul 26, 2026
tksm
left a comment
Contributor
There was a problem hiding this comment.
LGTM, thanks! 🎉 Default behavior makes sense here.
Please rebase onto the latest master.
Pod colors were chosen by hashing the pod name, so replicas of the same Deployment, ReplicaSet, StatefulSet, etc. could easily end up with the same color, making their interleaved logs hard to tell apart. Group pods by their workload using metadata.generateName (removing the pod-template-hash from it so that all ReplicaSets of a Deployment form one group) and pick the least recently assigned color within the group, starting from the hash-based index. Pods that do not share a group with other pods keep exactly the same color as before, and pods of the same workload now get distinct colors as long as unused colors remain in the color list. Fixes stern#347 Signed-off-by: Jojin <[email protected]>
superbrothers
force-pushed
the
347-color-anti-affinity
branch
from
October 18, 2026 06:11
a6a7e26 to
edd67bc
Compare
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #347
What this PR does
Pod colors are currently chosen by hashing the pod name (
fnv32(podName) % len(colorList)).Replicas of the same Deployment/ReplicaSet/StatefulSet differ only in a random suffix, so
each replica is effectively an independent random pick from the color list — with the
default 6 colors, two replicas share a color ~17% of the time and three replicas ~45%.
When replicas print similar messages at similar times, that makes the interleaved output
hard to attribute (#347).
This PR makes stern deliberately spread colors across pods of the same workload:
metadata.generateName, which all workload controllers (ReplicaSet,StatefulSet, DaemonSet, Job, ...) set, as suggested in
WIP: --same-prefix-color: same color pods with same prefix #322 (comment). The
pod-template-hashis stripped from it so that pods from different ReplicaSets of the same Deployment share a
group — during a rolling update, old and new pods also get distinct colors.
recently within its group. This guarantees distinct colors for pods of the same workload
as long as unused colors remain in the color list; once the list is exhausted, the least
recently assigned color (whose previous owner is the most likely to be gone) is reused.
retried/resumed or its container restarts.
Behavior notes
keep exactly the same color as before — the picker falls through to the current
hash-based index for the first pod of every group.
(the initial list is name-ordered, so it is stable for a static set of pods), instead of
being a pure function of the pod name — that is inherent to any anti-affinity scheme,
since distinctness requires knowing which colors are already taken.
--diff-containerbehavior are unchanged, as are all exported APIs(
NewTailsignature is untouched).I implemented this as default behavior (no new flag) since the collision-prone assignment
had no property worth preserving for randomly-named replicas, and singleton pods keep
byte-identical colors. If you would rather gate it behind an opt-in flag like
--diff-container(#305), I am happy to add one — the picker is isolated, so it is a smallchange.
How to validate
Unit tests cover the new behavior (
stern/color_test.go):old hash assignment, so this test fails without the fix)
generateNamekeep the hash-based colorOn a cluster:
Before: replicas regularly share colors (5 or fewer distinct colors for 6 replicas is the
common case). After: all 6 replicas have distinct colors, and during
kubectl rollout restart deployment backendthe new pods pick colors distinct from theold ones.
go build ./...,go test ./stern/...,go vet ./stern/..., andgolangci-lint run ./stern/...(v2.12.x, repo config) all pass.