Skip to content

Commit

Permalink
Adds flag to include/exclude init-containers
Browse files Browse the repository at this point in the history
  • Loading branch information
cbron committed Nov 14, 2019
1 parent e4067d5 commit 35fa1ef
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Options struct {
kubeConfig string
exclude []string
include []string
initContainers bool
allNamespaces bool
selector string
tail int64
Expand All @@ -61,6 +62,7 @@ type Options struct {
var opts = &Options{
container: ".*",
containerState: []string{stern.RUNNING, stern.WAITING},
initContainers: true,
tail: -1,
color: "auto",
template: "",
Expand All @@ -84,6 +86,7 @@ func Run() {
cmd.Flags().MarkDeprecated("kube-config", "Use --kubeconfig instead.")
cmd.Flags().StringSliceVarP(&opts.exclude, "exclude", "e", opts.exclude, "Regex of log lines to exclude")
cmd.Flags().StringSliceVarP(&opts.include, "include", "i", opts.include, "Regex of log lines to include")
cmd.Flags().BoolVar(&opts.initContainers, "init-containers", opts.initContainers, "Include init containers")
cmd.Flags().BoolVar(&opts.allNamespaces, "all-namespaces", opts.allNamespaces, "If present, tail across all namespaces. A specific namespace is ignored even if specified with --namespace.")
cmd.Flags().StringVarP(&opts.selector, "selector", "l", opts.selector, "Selector (label query) to filter on. If present, default to \".*\" for the pod-query.")
cmd.Flags().Int64Var(&opts.tail, "tail", opts.tail, "The number of lines from the end of the logs to show. Defaults to -1, showing all logs.")
Expand Down
1 change: 1 addition & 0 deletions stern/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Config struct {
ContainerState ContainerState
Exclude []*regexp.Regexp
Include []*regexp.Regexp
InitContainers bool
Since time.Duration
AllNamespaces bool
LabelSelector labels.Selector
Expand Down
2 changes: 1 addition & 1 deletion stern/container_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package stern
import (
"errors"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
)

type ContainerState []string
Expand Down
9 changes: 8 additions & 1 deletion stern/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ func Run(ctx context.Context, config *Config) error {
}
}

added, removed, err := Watch(ctx, clientset.CoreV1().Pods(namespace), config.PodQuery, config.ContainerQuery, config.ExcludeContainerQuery, config.ContainerState, config.LabelSelector)
added, removed, err := Watch(ctx,
clientset.CoreV1().Pods(namespace),
config.PodQuery,
config.ContainerQuery,
config.ExcludeContainerQuery,
config.InitContainers,
config.ContainerState,
config.LabelSelector)
if err != nil {
return errors.Wrap(err, "failed to set up watch")
}
Expand Down
12 changes: 8 additions & 4 deletions stern/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes/typed/core/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

// Target is a target to watch
Expand All @@ -43,7 +43,7 @@ func (t *Target) GetID() string {
// Watch starts listening to Kubernetes events and emits modified
// containers/pods. The first result is targets added, the second is targets
// removed
func Watch(ctx context.Context, i v1.PodInterface, podFilter *regexp.Regexp, containerFilter *regexp.Regexp, containerExcludeFilter *regexp.Regexp, containerState ContainerState, labelSelector labels.Selector) (chan *Target, chan *Target, error) {
func Watch(ctx context.Context, i v1.PodInterface, podFilter *regexp.Regexp, containerFilter *regexp.Regexp, containerExcludeFilter *regexp.Regexp, initContainers bool, containerState ContainerState, labelSelector labels.Selector) (chan *Target, chan *Target, error) {
watcher, err := i.Watch(metav1.ListOptions{Watch: true, LabelSelector: labelSelector.String()})
if err != nil {
return nil, nil, errors.Wrap(err, "failed to set up watch")
Expand All @@ -70,8 +70,10 @@ func Watch(ctx context.Context, i v1.PodInterface, podFilter *regexp.Regexp, con
switch e.Type {
case watch.Added, watch.Modified:
var statuses []corev1.ContainerStatus
statuses = append(statuses, pod.Status.InitContainerStatuses...)
statuses = append(statuses, pod.Status.ContainerStatuses...)
if initContainers {
statuses = append(statuses, pod.Status.InitContainerStatuses...)
}

for _, c := range statuses {
if !containerFilter.MatchString(c.Name) {
Expand All @@ -92,7 +94,9 @@ func Watch(ctx context.Context, i v1.PodInterface, podFilter *regexp.Regexp, con
case watch.Deleted:
var containers []corev1.Container
containers = append(containers, pod.Spec.Containers...)
containers = append(containers, pod.Spec.InitContainers...)
if initContainers {
containers = append(containers, pod.Spec.InitContainers...)
}

for _, c := range containers {
if !containerFilter.MatchString(c.Name) {
Expand Down

0 comments on commit 35fa1ef

Please sign in to comment.