8000 Display different colors for different containers (#305) · stern/stern@d1b5d74 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit d1b5d74

Browse files
authored
Display different colors for different containers (#305)
* Display different colors for different containers * move index calculation into a separate function * fix verify-readme error
1 parent 6afabde commit d1b5d74

File tree

7 files changed

+46
-14
lines changed

7 files changed

+46
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/dist
22
/hack/tools/bin
33
vendor
4+
.idea
5+
.vscode

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Supported Kubernetes resources are `pod`, `replicationcontroller`, `service`, `d
7878
`--container`, `-c` | `.*` | Container name when multiple containers in pod. (regular expression)
7979
`--container-state` | `all` | Tail containers with state in running, waiting, terminated, or all. 'all' matches all container states. To specify multiple states, repeat this or set comma-separated value.
8080
`--context` | | The name of the kubeconfig context to use
81+
`--diff-container`, `-d` | `false` | Display different colors for different containers.
8182
`--ephemeral-containers` | `true` | Include or exclude ephemeral containers.
8283
`--exclude`, `-e` | `[]` | Log lines to exclude. (regular expression)
8384
`--exclude-container`, `-E` | `[]` | Container name to exclude when multiple containers in pod. (regular expression)

cmd/cmd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type options struct {
8686
configFilePath string
8787
showHiddenOptions bool
8888
stdin bool
89+
diffContainer bool
8990

9091
client kubernetes.Interface
9192
clientConfig clientcmd.ClientConfig
@@ -318,6 +319,7 @@ func (o *options) sternConfig() (*stern.Config, error) {
318319
OnlyLogLines: o.onlyLogLines,
319320
MaxLogRequests: maxLogRequests,
320321
Stdin: o.stdin,
322+
DiffContainer: o.diffContainer,
321323

322324
Out: o.Out,
323325
ErrOut: o.ErrOut,
@@ -435,6 +437,7 @@ func (o *options) AddFlags(fs *pflag.FlagSet) {
435437
fs.BoolVarP(&o.version, "version", "v", o.version, "Print the version and exit.")
436438
fs.BoolVar(&o.showHiddenOptions, "show-hidden-options", o.showHiddenOptions, "Print a list of hidden options.")
437439
fs.BoolVar(&o.stdin, "stdin", o.stdin, "Parse logs from stdin. All Kubernetes related flags are ignored when it is set.")
440+
fs.BoolVarP(&o.diffContainer, "diff-container", "d", o.diffContainer, "Display different colors for different containers.")
438441

439442
fs.Lookup("timestamps").NoOptDefVal = "default"
440443
}

stern/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type Config struct {
5151
OnlyLogLines bool
5252
MaxLogRequests int
5353
Stdin bool
54+
DiffContainer bool
5455

5556
Out io.Writer
5657
ErrOut io.Writer

stern/stern.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func Run(ctx context.Context, client kubernetes.Interface, config *Config) error
6565
}
6666
}
6767
newTail := func(t *Target) *Tail {
68-
return NewTail(client.CoreV1(), t.Node, t.Namespace, t.Pod, t.Container, config.Template, config.Out, config.ErrOut, newTailOptions())
68+
return NewTail(client.CoreV1(), t.Node, t.Namespace, t.Pod, t.Container, config.Template, config.Out, config.ErrOut, newTailOptions(), config.DiffContainer)
6969
}
7070

7171
if config.Stdin {

stern/tail.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ type ResumeRequest struct {
6767
}
6868

6969
// NewTail returns a new tail for a Kubernetes container inside a pod
70-
func NewTail(clientset corev1client.CoreV1Interface, nodeName, namespace, podName, containerName string, tmpl *template.Template, out, errOut io.Writer, options *TailOptions) *Tail {
71-
podColor, containerColor := determineColor(podName)
70+
func NewTail(clientset corev1client.CoreV1Interface, nodeName, namespace, podName, containerName string, tmpl *template.Template, out, errOut io.Writer, options *TailOptions, diffContainer bool) *Tail {
71+
podColor, containerColor := determineColor(podName, containerName, diffContainer)
7272

7373
return &Tail{
7474
clientset: clientset,
@@ -96,15 +96,20 @@ var colorList = [][2]*color.Color{
9696
{color.New(color.FgHiRed), color.New(color.FgRed)},
9797
}
9898

99-
func determineColor(podName string) (podColor, containerColor *color.Color) {
100-
hash := fnv.New32()
101-
_, _ = hash.Write([]byte(podName))
102-
idx := hash.Sum32() % uint32(len(colorList))
103-
104-
colors := colorList[idx]
99+
func determineColor(podName, containerName string, diffContainer bool) (podColor, containerColor *color.Color) {
100+
colors := colorList[colorIndex(podName)]
101+
if diffContainer {
102+
return colors[0], colorList[colorIndex(containerName)][1]
103+
}
105104
return colors[0], colors[1]
106105
}
107106

107+
func colorIndex(name string) uint32 {
108+
hash := fnv.New32()
109+
_, _ = hash.Write([]byte(name))
110+
return hash.Sum32() % uint32(len(colorList))
111+
}
112+
108113
// Start starts tailing
109114
func (t *Tail) Start(ctx context.Context) error {
110115
ctx, cancel := context.WithCancel(ctx)

stern/tail_test.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313

1414
func TestDetermineColor(t *testing.T) {
1515
podName := "stern"
16-
podColor1, containerColor1 := determineColor(podName)
17-
podColor2, containerColor2 := determineColor(podName)
16+
containerName := "foo"
17+
diffContainer := false
18+
podColor1, containerColor1 := determineColor(podName, containerName, diffContainer)
19+
podColor2, containerColor2 := determineColor(podName, containerName, diffContainer)
1820

1921
if podColor1 != podColor2 {
2022
t.Errorf("expected color for pod to be the same between invocations but was %v and %v",
@@ -26,6 +28,24 @@ func TestDetermineColor(t *testing.T) {
2628
}
2729
}
2830

31+
func TestDetermineColorDiffContainer(t *testing.T) {
32+
podName := "stern"
33+
containerName1 := "foo"
34+
containerName2 := "bar"
35+
diffContainer := true
36+
podColor1, containerColor1 := determineColor(podName, containerName1, diffContainer)
37+
podColor2, containerColor2 := determineColor(podName, containerName2, diffContainer)
38+
39+
if podColor1 != podColor2 {
40+
t.Errorf("expected color for pod to be the same between invocations but was %v and %v",
41+
podColor1, podColor2)
42+
}
43+
if containerColor1 == containerColor2 {
44+
t.Errorf("expected color for container to be different between invocations but was the same: %v",
45+
containerColor1)
46+
}
47+
}
48+
2949
func TestConsumeStreamTail(t *testing.T) {
3050
logLines := `2023-02-13T21:20:30.000000001Z line 1
3151
2023-02-13T21:20:30.000000002Z line 2
@@ -83,7 +103,7 @@ line 4 (my-node/my-namespace/my-pod/my-container)
83103
for i, tt := range tests {
84104
t.Run(tt.name, func(t *testing.T) {
85105
out := new(bytes.Buffer)
86-
tail := NewTail(clientset.CoreV1(), "my-node", "my-namespace", "my-pod", "my-container", tmpl, out, io.Discard, &TailOptions{})
106+
tail := NewTail(clientset.CoreV1(), "my-node", "my-namespace", "my-pod", "my-container", tmpl, out, io.Discard, &TailOptions{}, false)
87107
tail.resumeRequest = tt.resumeReq
88108
if err := tail.ConsumeRequest(context.TODO(), &responseWrapperMock{data: bytes.NewBufferString(logLines)}); err != nil {
89109
t.Fatalf("%d: unexpected err %v", i, err)
@@ -142,7 +162,7 @@ func TestPrintStarting(t *testing.T) {
142162
clientset := fake.NewSimpleClientset()
143163
for i, tt := range tests {
144164
errOut := new(bytes.Buffer)
145-
tail := NewTail(clientset.CoreV1(), "my-node", "my-namespace", "my-pod", "my-container", nil, io.Discard, errOut, tt.options)
165+
tail := NewTail(clientset.CoreV1(), "my-node", "my-namespace", "my-pod", "my-container", nil, io.Discard, errOut, tt.options, false)
146166
tail.printStarting()
147167

148168
if !bytes.Equal(tt.expected, errOut.Bytes()) {
@@ -184,7 +204,7 @@ func TestPrintStopping(t *testing.T) {
184204
clientset := fake.NewSimpleClientset()
185205
for i, tt := range tests {
186206
errOut := new(bytes.Buffer)
187-
tail := NewTail(clientset.CoreV1(), "my-node", "my-namespace", "my-pod", "my-container", nil, io.Discard, errOut, tt.options)
207+
tail := NewTail(clientset.CoreV1(), "my-node", "my-namespace", "my-pod", "my-container", nil, io.Discard, errOut, tt.options, false)
188208
tail.printStopping()
189209

190210
if !bytes.Equal(tt.expected, errOut.Bytes()) {

0 commit comments

Comments
 (0)
0