Skip to content

Commit b04478c

Browse files
Allow to specify --exclude-pod/container multiple times (#218)
1 parent 995be39 commit b04478c

File tree

5 files changed

+222
-94
lines changed

5 files changed

+222
-94
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ Supported Kubernetes resources are `pod`, `replicationcontroller`, `service`, `d
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)
82-
`--exclude-container`, `-E` | | Container name to exclude when multiple containers in pod. (regular expression)
83-
`--exclude-pod` | | Pod name to exclude. (regular expression)
82+
`--exclude-container`, `-E` | `[]` | Container name to exclude when multiple containers in pod. (regular expression)
83+
`--exclude-pod` | `[]` | Pod name to exclude. (regular expression)
8484
`--field-selector` | | Selector (field query) to filter on. If present, default to ".*" for the pod-query.
8585
`--include`, `-i` | `[]` | Log lines to include. (regular expression)
8686
`--init-containers` | `true` | Include or exclude init containers.

cmd/cmd.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ import (
4040
type options struct {
4141
genericclioptions.IOStreams
4242

43-
excludePod string
43+
excludePod []string
4444
container string
45-
excludeContainer string
45+
excludeContainer []string
4646
containerStates []string
4747
timestamps bool
4848
timezone string
@@ -142,25 +142,29 @@ func (o *options) sternConfig() (*stern.Config, error) {
142142
return nil, errors.Wrap(err, "failed to compile regular expression from query")
143143
}
144144

145-
var excludePod *regexp.Regexp
146-
if o.excludePod != "" {
147-
excludePod, err = regexp.Compile(o.excludePod)
145+
var excludePod []*regexp.Regexp
146+
for _, s := range o.excludePod {
147+
re, err := regexp.Compile(s)
148148
if err != nil {
149-
return nil, errors.Wrap(err, "failed to compile regular exression for excluded pod query")
149+
return nil, errors.Wrap(err, "failed to compile regular expression for excluded pod query")
150150
}
151+
152+
excludePod = append(excludePod, re)
151153
}
152154

153155
container, err := regexp.Compile(o.container)
154156
if err != nil {
155157
return nil, errors.Wrap(err, "failed to compile regular expression for container query")
156158
}
157159

158-
var excludeContainer *regexp.Regexp
159-
if o.excludeContainer != "" {
160-
excludeContainer, err = regexp.Compile(o.excludeContainer)
160+
var excludeContainer []*regexp.Regexp
161+
for _, s := range o.excludeContainer {
162+
re, err := regexp.Compile(s)
161163
if err != nil {
162-
return nil, errors.Wrap(err, "failed to compile regular expression for exclude container query")
164+
return nil, errors.Wrap(err, "failed to compile regular expression for excluded container query")
163165
}
166+
167+
excludeContainer = append(excludeContainer, re)
164168
}
165169

166170
var exclude []*regexp.Regexp
@@ -361,8 +365,8 @@ func (o *options) AddFlags(fs *pflag.FlagSet) {
361365
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.")
362366
fs.StringVar(&o.context, "context", o.context, "Kubernetes context to use. Default to current context configured in kubeconfig.")
363367
fs.StringArrayVarP(&o.exclude, "exclude", "e", o.exclude, "Log lines to exclude. (regular expression)")
364-
fs.StringVarP(&o.excludeContainer, "exclude-container", "E", o.excludeContainer, "Container name to exclude when multiple containers in pod. (regular expression)")
365-
fs.StringVar(&o.excludePod, "exclude-pod", o.excludePod, "Pod name to exclude. (regular expression)")
368+
fs.StringArrayVarP(&o.excludeContainer, "exclude-container", "E", o.excludeContainer, "Container name to exclude when multiple containers in pod. (regular expression)")
369+
fs.StringArrayVar(&o.excludePod, "exclude-pod", o.excludePod, "Pod name to exclude. (regular expression)")
366370
fs.BoolVar(&o.noFollow, "no-follow", o.noFollow, "Exit when all logs have been shown.")
367371
fs.StringArrayVarP(&o.include, "include", "i", o.include, "Log lines to include. (regular expression)")
368372
fs.BoolVar(&o.initContainers, "init-containers", o.initContainers, "Include or exclude init containers.")

stern/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ type Config struct {
3030
ContextName string
3131
Namespaces []string
3232
PodQuery *regexp.Regexp
33-
ExcludePodQuery *regexp.Regexp
33+
ExcludePodQuery []*regexp.Regexp
3434
Timestamps bool
3535
Location *time.Location
3636
ContainerQuery *regexp.Regexp
37-
ExcludeContainerQuery *regexp.Regexp
37+
ExcludeContainerQuery []*regexp.Regexp
3838
ContainerStates []ContainerState
3939
Exclude []*regexp.Regexp
4040
Include []*regexp.Regexp

stern/target.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func (t *Target) GetID() string {
3737
// targetFilter is a filter of Target
3838
type targetFilter struct {
3939
podFilter *regexp.Regexp
40-
excludePodFilter *regexp.Regexp
40+
excludePodFilter []*regexp.Regexp
4141
containerFilter *regexp.Regexp
42-
containerExcludeFilter *regexp.Regexp
42+
containerExcludeFilter []*regexp.Regexp
4343
initContainers bool
4444
ephemeralContainers bool
4545
containerStates []ContainerState
@@ -51,32 +51,44 @@ func (f *targetFilter) visit(pod *corev1.Pod, visitor func(t *Target, containerS
5151
if !f.podFilter.MatchString(pod.Name) {
5252
return
5353
}
54-
if f.excludePodFilter != nil && f.excludePodFilter.MatchString(pod.Name) {
55-
return
54+
55+
for _, re := range f.excludePodFilter {
56+
if re.MatchString(pod.Name) {
57+
return
58+
}
5659
}
5760

5861
// filter by container statuses
5962
var statuses []corev1.ContainerStatus
6063
statuses = append(statuses, pod.Status.ContainerStatuses...)
64+
6165
if f.initContainers {
6266
statuses = append(statuses, pod.Status.InitContainerStatuses...)
6367
}
68+
6469
if f.ephemeralContainers {
6570
statuses = append(statuses, pod.Status.EphemeralContainerStatuses...)
6671
}
72+
73+
OUTER:
6774
for _, c := range statuses {
6875
if !f.containerFilter.MatchString(c.Name) {
6976
continue
7077
}
71-
if f.containerExcludeFilter != nil && f.containerExcludeFilter.MatchString(c.Name) {
72-
continue
78+
79+
for _, re := range f.containerExcludeFilter {
80+
if re.MatchString(c.Name) {
81+
continue OUTER
82+
}
7383
}
84+
7485
t := &Target{
7586
Node: pod.Spec.NodeName,
7687
Namespace: pod.Namespace,
7788
Pod: pod.Name,
7889
Container: c.Name,
7990
}
91+
8092
visitor(t, f.matchContainerState(c.State))
8193
}
8294
}

0 commit comments

Comments
 (0)