Skip to content

Commit

Permalink
Incorporating review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vjsamuel committed Jun 20, 2018
1 parent 300cf63 commit b182429
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ https://github.com/elastic/beats/compare/v6.2.3...master[Check the HEAD diff]
- Add experimental Jolokia Discovery autodiscover provider. {pull}7141[7141]
- Add owner object info to Kubernetes metadata. {pull}7231[7231]
- Add beat export dashboard command. {pull}7239[7239]
- Add support for docker autodiscover to monitor containers on host network {pull}6708[6708]

*Auditbeat*

Expand Down
7 changes: 0 additions & 7 deletions libbeat/autodiscover/providers/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Provider struct {
appenders autodiscover.Appenders
watcher docker.Watcher
templates *template.Mapper
hostIP string
stop chan interface{}
startListener bus.Listener
stopListener bus.Listener
Expand Down Expand Up @@ -66,16 +65,13 @@ func AutodiscoverBuilder(bus bus.Bus, c *common.Config) (autodiscover.Provider,
return nil, err
}

hostIP := docker.GetHostIP(watcher)

return &Provider{
config: config,
bus: bus,
builders: builders,
appenders: appenders,
templates: mapper,
watcher: watcher,
hostIP: hostIP,
stop: make(chan interface{}),
startListener: start,
stopListener: stop,
Expand Down Expand Up @@ -112,10 +108,7 @@ func (d *Provider) emitContainer(event bus.Event, flag string) {
var host string
if len(container.IPAddresses) > 0 {
host = container.IPAddresses[0]
} else {
host = d.hostIP
}

labelMap := common.MapStr{}
for k, v := range container.Labels {
safemapstr.Put(labelMap, k, v)
Expand Down
47 changes: 0 additions & 47 deletions libbeat/common/docker/util.go

This file was deleted.

16 changes: 10 additions & 6 deletions libbeat/common/docker/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ type Container struct {
Labels map[string]string
IPAddresses []string
Ports []types.Port
// Only needed internally
gateways []string
}

// Client for docker interface
type Client interface {
ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
ContainerInspect(ctx context.Context, container string) (types.ContainerJSON, error)
Events(ctx context.Context, options types.EventsOptions) (<-chan events.Message, <-chan error)
}

Expand Down Expand Up @@ -291,14 +290,20 @@ func (w *watcher) listContainers(options types.ContainerListOptions) ([]*Contain
var result []*Container
for _, c := range containers {
var ipaddresses []string
var gateways []string
for _, net := range c.NetworkSettings.Networks {
if net.IPAddress != "" {
ipaddresses = append(ipaddresses, net.IPAddress)
}
}

if net.Gateway != "" {
gateways = append(gateways, net.Gateway)
// If there are no network interfaces, assume that the container is on host network
// Inspect the container directly and use the hostname as the IP address in order
if len(ipaddresses) == 0 {
info, err := w.client.ContainerInspect(w.ctx, c.ID)
if err == nil {
ipaddresses = append(ipaddresses, info.Config.Hostname)
} else {
logp.Warn("unable to inspect container %s due to error %v", c.ID, err)
}
}
result = append(result, &Container{
Expand All @@ -308,7 +313,6 @@ func (w *watcher) listContainers(options types.ContainerListOptions) ([]*Contain
Labels: c.Labels,
Ports: c.Ports,
IPAddresses: ipaddresses,
gateways: gateways,
})
}

Expand Down
5 changes: 5 additions & 0 deletions libbeat/common/docker/watcher_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package docker

import (
"errors"
"testing"
"time"

Expand Down Expand Up @@ -46,6 +47,10 @@ func (m *MockClient) Events(ctx context.Context, options types.EventsOptions) (<
return eventsC, errorsC
}

func (m *MockClient) ContainerInspect(ctx context.Context, container string) (types.ContainerJSON, error) {
return types.ContainerJSON{}, errors.New("unimplemented")
}

func TestWatcherInitialization(t *testing.T) {
watcher := runWatcher(t, true,
[][]types.Container{
Expand Down

0 comments on commit b182429

Please sign in to comment.