Skip to content

Commit e18f11b

Browse files
committed
Filter events by type and label
1 parent 1e42cdc commit e18f11b

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

cli/docker_handler.go

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
docker "github.com/fsouza/go-dockerclient"
9-
"github.com/go-viper/mapstructure/v2"
10-
"github.com/mcuadros/ofelia/core"
118
"os"
129
"strings"
1310
"time"
11+
12+
docker "github.com/fsouza/go-dockerclient"
13+
"github.com/go-viper/mapstructure/v2"
14+
"github.com/mcuadros/ofelia/core"
1415
)
1516

1617
const (
@@ -32,7 +33,7 @@ type DockerHandler struct {
3233
notifier labelConfigUpdater
3334
configsFromLabels bool
3435
logger core.Logger
35-
filters []string
36+
filters map[string][]string
3637
}
3738

3839
type labelConfigUpdater interface {
@@ -58,8 +59,20 @@ func NewDockerHandler(config *Config, dockerFilters []string, configsFromLabels
5859
return nil, fmt.Errorf("docker filters can only be provided together with '--docker' flag")
5960
}
6061

62+
var filters = map[string][]string{
63+
"label": {requiredLabelFilter},
64+
}
65+
66+
for _, f := range dockerFilters {
67+
key, value, err := parseFilter(f)
68+
if err != nil {
69+
config.logger.Errorf("Error parsing filter '%s': %v", f, err)
70+
}
71+
filters[key] = append(filters[key], value)
72+
}
73+
6174
c := &DockerHandler{
62-
filters: dockerFilters,
75+
filters: filters,
6376
configsFromLabels: configsFromLabels,
6477
notifier: config,
6578
logger: logger,
@@ -88,28 +101,32 @@ func (c *DockerHandler) ConfigFromLabelsEnabled() bool {
88101
// Watch for Docker events and update the labels accordingly
89102
func (c *DockerHandler) watch() {
90103
c.logger.Debugf("Listening for Docker events to hot reload configs...")
91-
92104
events := make(chan *docker.APIEvents)
93-
err := c.dockerClient.AddEventListener(events)
94-
if err != nil {
105+
var filters = map[string][]string{
106+
"type": {"container"},
107+
"label": c.filters["label"],
108+
}
109+
110+
if err := c.dockerClient.AddEventListenerWithOptions(docker.EventsOptions{
111+
Filters: filters}, events); err != nil {
95112
c.logger.Errorf("Error adding event listener: %v", err)
113+
return
96114
}
115+
97116
defer func() {
98-
// remove the listener when the program exits
99-
err := c.dockerClient.RemoveEventListener(events)
100-
if err != nil {
117+
if err := c.dockerClient.RemoveEventListener(events); err != nil {
101118
c.logger.Errorf("Error removing event listener: %v", err)
102119
}
103120
}()
104121

105122
for event := range events {
106-
if event.Type != "container" {
107-
continue
108-
}
109-
110123
switch event.Action {
111-
// |-----------------------lifecycle events---------------------| | ----- other management events -----|
112-
case "create", "start", "restart", "stop", "kill", "die", "destroy", "pause", "unpause", "rename", "update":
124+
case
125+
// lifecycle events
126+
"create", "start", "restart", "stop", "kill", "die", "destroy",
127+
// other management events
128+
"pause", "unpause", "rename", "update":
129+
c.logger.Debugf("Refreshing configurations. Docker event received - [%s] %s (%s)", event.Action, event.Actor.Attributes["name"], event.Actor.ID)
113130
labels, err := c.GetDockerLabels()
114131
// Do not print or care if there is no container up right now
115132
if err != nil && !errors.Is(err, errNoContainersMatchingFilters) {
@@ -152,22 +169,11 @@ func (c *DockerHandler) WaitForLabels() {
152169
}
153170

154171
func (c *DockerHandler) GetDockerLabels() (map[string]map[string]string, error) {
155-
var filters = map[string][]string{
156-
"label": {requiredLabelFilter},
157-
}
158-
for _, f := range c.filters {
159-
key, value, err := parseFilter(f)
160-
if err != nil {
161-
return nil, fmt.Errorf("%w: %s", err, f)
162-
}
163-
filters[key] = append(filters[key], value)
164-
}
165-
166-
conts, err := c.dockerClient.ListContainers(docker.ListContainersOptions{Filters: filters})
172+
conts, err := c.dockerClient.ListContainers(docker.ListContainersOptions{Filters: c.filters})
167173
if err != nil {
168174
return nil, fmt.Errorf("%w: %w", errFailedToListContainers, err)
169175
} else if len(conts) == 0 {
170-
return nil, fmt.Errorf("%w: %v", errNoContainersMatchingFilters, filters)
176+
return nil, fmt.Errorf("%w: %v", errNoContainersMatchingFilters, c.filters)
171177
}
172178

173179
var labels = make(map[string]map[string]string)

0 commit comments

Comments
 (0)