Skip to content

Commit 6e0d5fc

Browse files
author
Takashi Kusumi
authored
Add all option to --container-state flag (#222)
1 parent cd83193 commit 6e0d5fc

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Supported Kubernetes resources are `pod`, `replicationcontroller`, `service`, `d
7575
`--color` | `auto` | Force set color output. 'auto': colorize if tty attached, 'always': always colorize, 'never': never colorize.
7676
`--completion` | | Output stern command-line completion code for the specified shell. Can be 'bash', 'zsh' or 'fish'.
7777
`--container`, `-c` | `.*` | Container name when multiple containers in pod. (regular expression)
78-
`--container-state` | `running` | Tail containers with state in running, waiting or terminated. To specify multiple states, repeat this or set comma-separated value.
78+
`--container-state` | `running` | 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.
7979
`--context` | | Kubernetes context to use. Default to current context configured in kubeconfig.
8080
`--ephemeral-containers` | `true` | Include or exclude ephemeral containers.
8181
`--exclude`, `-e` | `[]` | Log lines to exclude. (regular expression)

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func (o *options) AddFlags(fs *pflag.FlagSet) {
362362
fs.StringVar(&o.color, "color", o.color, "Force set color output. 'auto': colorize if tty attached, 'always': always colorize, 'never': never colorize.")
363363
fs.StringVar(&o.completion, "completion", o.completion, "Output stern command-line completion code for the specified shell. Can be 'bash', 'zsh' or 'fish'.")
364364
fs.StringVarP(&o.container, "container", "c", o.container, "Container name when multiple containers in pod. (regular expression)")
365-
fs.StringSliceVar(&o.containerStates, "container-state", o.containerStates, "Tail containers with state in running, waiting or terminated. To specify multiple states, repeat this or set comma-separated value.")
365+
fs.StringSliceVar(&o.containerStates, "container-state", o.containerStates, "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.")
366366
fs.StringVar(&o.context, "context", o.context, "Kubernetes context to use. Default to current context configured in kubeconfig.")
367367
fs.StringArrayVarP(&o.exclude, "exclude", "e", o.exclude, "Log lines to exclude. (regular expression)")
368368
fs.StringArrayVarP(&o.excludeContainer, "exclude-container", "E", o.excludeContainer, "Container name to exclude when multiple containers in pod. (regular expression)")

cmd/flag_completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
var flagChoices = map[string][]string{
3333
"color": []string{"always", "never", "auto"},
3434
"completion": []string{"bash", "zsh", "fish"},
35-
"container-state": []string{stern.RUNNING, stern.WAITING, stern.TERMINATED},
35+
"container-state": []string{stern.RUNNING, stern.WAITING, stern.TERMINATED, stern.ALL_STATES},
3636
"output": []string{"default", "raw", "json", "extjson", "ppextjson"},
3737
}
3838

stern/container_state.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package stern
1717
import (
1818
"errors"
1919

20-
"k8s.io/api/core/v1"
20+
v1 "k8s.io/api/core/v1"
2121
)
2222

2323
type ContainerState string
@@ -26,6 +26,7 @@ const (
2626
RUNNING = "running"
2727
WAITING = "waiting"
2828
TERMINATED = "terminated"
29+
ALL_STATES = "all"
2930
)
3031

3132
// NewContainerState returns corresponding ContainerState
@@ -36,13 +37,18 @@ func NewContainerState(stateConfig string) (ContainerState, error) {
3637
return WAITING, nil
3738
} else if stateConfig == TERMINATED {
3839
return TERMINATED, nil
40+
} else if stateConfig == ALL_STATES {
41+
return ALL_STATES, nil
3942
}
4043

4144
return "", errors.New("containerState should be one of 'running', 'waiting', or 'terminated'")
4245
}
4346

4447
// Match returns ContainerState is matched
4548
func (stateConfig ContainerState) Match(containerState v1.ContainerState) bool {
49+
if stateConfig == ALL_STATES {
50+
return true
51+
}
4652
return (stateConfig == RUNNING && containerState.Running != nil) ||
4753
(stateConfig == WAITING && containerState.Waiting != nil) ||
4854
(stateConfig == TERMINATED && containerState.Terminated != nil)

stern/container_state_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package stern
33
import (
44
"testing"
55

6-
"k8s.io/api/core/v1"
6+
v1 "k8s.io/api/core/v1"
77
)
88

99
func TestNewContainerState(t *testing.T) {
@@ -27,6 +27,11 @@ func TestNewContainerState(t *testing.T) {
2727
ContainerState(TERMINATED),
2828
false,
2929
},
30+
{
31+
"all",
32+
ContainerState(ALL_STATES),
33+
false,
34+
},
3035
{
3136
"wrongValue",
3237
ContainerState(""),
@@ -80,6 +85,12 @@ func TestMatch(t *testing.T) {
8085
},
8186
true,
8287
},
88+
{
89+
// "all" always matches all containers regardless of their states
90+
ContainerState(ALL_STATES),
91+
v1.ContainerState{},
92+
true,
93+
},
8394
{
8495
ContainerState(RUNNING),
8596
v1.ContainerState{

0 commit comments

Comments
 (0)