Skip to content

Commit

Permalink
cmd/kail: add --ignore-ns
Browse files Browse the repository at this point in the history
fixes #33
  • Loading branch information
boz committed May 16, 2019
1 parent 51295f6 commit 14e2382
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Flag | Selection
`-c, --containers CONTAINER-NAME` | restrict which containers logs are shown for
`--ignore LABEL-SELECTOR` | Ignore pods that the selector matches. (default: `kail.ignore=true`)
`--current-ns` | Match pods in the namespace specified in Kubernetes' "current context"
`--ignore-ns NAME` | Ignore pods in the given namespaces. Overridden by `--ns`, `--current-ns`. (default: `kube-system`)

#### Name Selection

Expand Down
5 changes: 5 additions & 0 deletions cmd/kail/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
flagLabel = kingpin.Flag("label", "label").Short('l').PlaceHolder("SELECTOR").Strings()
flagPod = kingpin.Flag("pod", "pod").Short('p').PlaceHolder("NAME").Strings()
flagNs = kingpin.Flag("ns", "namespace").Short('n').PlaceHolder("NAME").Strings()
flagIgnoreNs = kingpin.Flag("ignore-ns", "ignore namespace").PlaceHolder("NAME").Default("kube-system").Strings()
flagSvc = kingpin.Flag("svc", "service").PlaceHolder("NAME").Strings()
flagRc = kingpin.Flag("rc", "replication controller").PlaceHolder("NAME").Strings()
flagRs = kingpin.Flag("rs", "replica set").PlaceHolder("NAME").Strings()
Expand Down Expand Up @@ -232,6 +233,10 @@ func createDSBuilder() kail.DSBuilder {
dsb = dsb.WithNamespace(*flagNs...)
}

if len(*flagIgnoreNs) > 0 {
dsb = dsb.WithIgnoreNamespace(*flagIgnoreNs...)
}

if ids := parseIds("service", *flagSvc); len(ids) > 0 {
dsb = dsb.WithService(ids...)
}
Expand Down
25 changes: 25 additions & 0 deletions ds_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type DSBuilder interface {
WithSelectors(selectors ...labels.Selector) DSBuilder
WithPods(id ...nsname.NSName) DSBuilder
WithNamespace(name ...string) DSBuilder
WithIgnoreNamespace(name ...string) DSBuilder
WithService(id ...nsname.NSName) DSBuilder
WithNode(name ...string) DSBuilder
WithRC(id ...nsname.NSName) DSBuilder
Expand All @@ -43,6 +44,7 @@ type dsBuilder struct {
selectors []labels.Selector
pods []nsname.NSName
namespaces []string
ignoreNS []string
services []nsname.NSName
nodes []string
rcs []nsname.NSName
Expand Down Expand Up @@ -72,6 +74,11 @@ func (b *dsBuilder) WithNamespace(name ...string) DSBuilder {
return b
}

func (b *dsBuilder) WithIgnoreNamespace(name ...string) DSBuilder {
b.ignoreNS = append(b.ignoreNS, name...)
return b
}

func (b *dsBuilder) WithService(id ...nsname.NSName) DSBuilder {
b.services = append(b.services, id...)
return b
Expand Down Expand Up @@ -162,9 +169,12 @@ func (b *dsBuilder) Create(ctx context.Context, cs kubernetes.Interface) (DS, er
}
}

namespaces := make(map[string]bool, len(b.namespaces))

if sz := len(b.namespaces); sz > 0 {
ids := make([]nsname.NSName, 0, sz)
for _, ns := range b.namespaces {
namespaces[ns] = true
ids = append(ids, nsname.New(ns, ""))
}

Expand All @@ -175,6 +185,21 @@ func (b *dsBuilder) Create(ctx context.Context, cs kubernetes.Interface) (DS, er
}
}

if sz := len(b.ignoreNS); sz > 0 {
ids := make([]nsname.NSName, 0, sz)
for _, ns := range b.ignoreNS {
if !namespaces[ns] {
ids = append(ids, nsname.New(ns, ""))
}
}

ds.pods, err = ds.pods.CloneWithFilter(filter.Not(filter.NSName(ids...)))
if err != nil {
ds.closeAll()
return nil, log.Err(err, "ignore namespace filter")
}
}

if len(b.nodes) != 0 {
ds.pods, err = ds.pods.CloneWithFilter(pod.NodeFilter(b.nodes...))
if err != nil {
Expand Down

0 comments on commit 14e2382

Please sign in to comment.