Skip to content

Commit

Permalink
Added ability to skip containers (#416)
Browse files Browse the repository at this point in the history
* Added ability to skip containers on custom label key:value pairs and not just the default label=true

* refactored logic for ignoring containers with custom labels

* fixes to simplify logic

* fixes to simplify logic

* Updated README and CHANGELOG markdowns

* pump.go needs to reassign label var if label is of the form key:value

* fixing up a few things based on feedback
  • Loading branch information
ibrokethecloud authored and gbolo committed Sep 17, 2018
1 parent 9a510f9 commit e671009
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
- @gbolo added ability to control the TLS client trust store
- @gbolo added option to harden the TLS client
- @chopmann added option to bind the http server to an address
- @ibrokethecloud added ability to add custom key:value pairs as EXCLUDE_LABEL.

### Removed

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ If you use multiline logging with raw, it's recommended to json encode the Data
* `BACKLOG` - suppress container tail backlog
* `TAIL` - specify the number of lines in the log tail to capture when logspout starts (default `all`)
* `DEBUG` - emit debug logs
* `EXCLUDE_LABEL` - exclude logs with a given label
* `EXCLUDE_LABEL` - exclude containers with a given label. The label can have a value of true or a custom value matched with : after the label name like label_name:label_value.
* `INACTIVITY_TIMEOUT` - detect hang in Docker API (default 0)
* `HTTP_BIND_ADDRESS` - configure which interface address to listen on (default 0.0.0.0)
* `PORT` or `HTTP_PORT` - configure which port to listen on (default 80)
Expand Down
13 changes: 11 additions & 2 deletions router/pump.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,18 @@ func ignoreContainer(container *docker.Container) bool {
return true
}
}

excludeLabel := getopt("EXCLUDE_LABEL", "")
excludeValue := "true"
// support EXCLUDE_LABEL having a custom label value
excludeLabelArr := strings.Split(excludeLabel, ":")
if len(excludeLabelArr) == 2 {
excludeValue = excludeLabelArr[1]
excludeLabel = excludeLabelArr[0]
}

if value, ok := container.Config.Labels[excludeLabel]; ok {
return len(excludeLabel) > 0 && strings.ToLower(value) == "true"
return len(excludeLabel) > 0 && strings.ToLower(value) == strings.ToLower(excludeValue)
}
return false
}
Expand Down Expand Up @@ -212,7 +221,7 @@ func (p *LogsPump) pumpLogs(event *docker.APIEvents, backlog bool, inactivityTim

// RawTerminal with container Tty=false injects binary headers into
// the log stream that show up as garbage unicode characters
rawTerminal := false
rawTerminal := false
if allowTTY && container.Config.Tty {
rawTerminal = true
}
Expand Down
18 changes: 18 additions & 0 deletions router/pump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ func TestPumpIgnoreContainer(t *testing.T) {
}
}

func TestPumpIgnoreContainerCustomLabels(t *testing.T) {
os.Setenv("EXCLUDE_LABEL", "k8s-app:canal")
defer os.Unsetenv("EXCLUDE_LABEL")
containers := []struct {
in *docker.Config
out bool
}{
{&docker.Config{Labels: map[string]string{"k8s-app": "canal"}}, true},
{&docker.Config{Labels: map[string]string{"app": "demo-app"}}, false},
}

for _, conf := range containers {
if actual := ignoreContainer(&docker.Container{Config: conf.in}); actual != conf.out {
t.Errorf("expected %v got %v", conf.out, actual)
}
}
}

func TestPumpIgnoreContainerAllowTTYDefault(t *testing.T) {
containers := []struct {
in *docker.Config
Expand Down

0 comments on commit e671009

Please sign in to comment.