Skip to content

Commit ec10ca2

Browse files
authored
Merge pull request #619 from nginx-proxy/feature/notify-filter
feat: filter containers to notify
2 parents 3d5a86a + e34bc32 commit ec10ca2

File tree

4 files changed

+86
-33
lines changed

4 files changed

+86
-33
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,14 @@ Options:
101101
log the output(stdout/stderr) of notify command
102102
-notify-container container-ID
103103
container to send a signal to
104+
-notify-filter key=value
105+
container filter for notification (e.g -notify-filter name=foo).
106+
You can have multiple of these.
107+
https://docs.docker.com/engine/reference/commandline/ps/#filter
104108
-notify-signal signal
105-
signal to send to the -notify-container. -1 to call docker restart. Defaults to 1 aka. HUP.
106-
All available signals available on the [dockerclient](https://github.com/fsouza/go-dockerclient/blob/01804dec8a84d0a77e63611f2b62d33e9bb2b64a/signal.go)
109+
signal to send to the -notify-container and -notify-filter. -1 to call docker restart. Defaults to 1 aka. HUP.
110+
All available signals available on the dockerclient
111+
https://github.com/fsouza/go-dockerclient/blob/01804dec8a84d0a77e63611f2b62d33e9bb2b64a/signal.go
107112
-notify-sighup container-ID
108113
send HUP signal to container. Equivalent to 'docker kill -s HUP container-ID', or `-notify-container container-ID -notify-signal 1`
109114
-only-exposed

cmd/docker-gen/main.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/signal"
99
"path/filepath"
10+
"strings"
1011
"syscall"
1112

1213
"github.com/BurntSushi/toml"
@@ -16,6 +17,7 @@ import (
1617
)
1718

1819
type stringslice []string
20+
type mapstringslice map[string][]string
1921

2022
var (
2123
buildVersion string
@@ -27,6 +29,7 @@ var (
2729
sighupContainerID string
2830
notifyContainerID string
2931
notifyContainerSignal int
32+
notifyContainerFilter mapstringslice = make(mapstringslice)
3033
onlyExposed bool
3134
onlyPublished bool
3235
includeStopped bool
@@ -51,6 +54,18 @@ func (strings *stringslice) Set(value string) error {
5154
return nil
5255
}
5356

57+
func (filter *mapstringslice) String() string {
58+
return "[string][]string"
59+
}
60+
61+
func (filter *mapstringslice) Set(value string) error {
62+
name, value, found := strings.Cut(value, "=")
63+
if found {
64+
(*filter)[name] = append((*filter)[name], value)
65+
}
66+
return nil
67+
}
68+
5469
func usage() {
5570
println(`Usage: docker-gen [options] template [dest]
5671
@@ -101,8 +116,10 @@ func initFlags() {
101116
"send HUP signal to container. Equivalent to docker kill -s HUP `container-ID`")
102117
flag.StringVar(&notifyContainerID, "notify-container", "",
103118
"container to send a signal to")
119+
flag.Var(&notifyContainerFilter, "notify-filter",
120+
"container filter for notification (e.g -notify-filter name=foo). You can have multiple of these. https://docs.docker.com/engine/reference/commandline/ps/#filter")
104121
flag.IntVar(&notifyContainerSignal, "notify-signal", int(docker.SIGHUP),
105-
"signal to send to the notify-container. Defaults to SIGHUP")
122+
"signal to send to the notify-container and notify-filter. Defaults to SIGHUP")
106123
flag.Var(&configFiles, "config", "config files with template directives. Config files will be merged if this option is specified multiple times.")
107124
flag.IntVar(&interval, "interval", 0, "notify command interval (secs)")
108125
flag.BoolVar(&keepBlankLines, "keep-blank-lines", false, "keep blank lines in the output file")
@@ -165,6 +182,10 @@ func main() {
165182
if notifyContainerID != "" {
166183
cfg.NotifyContainers[notifyContainerID] = notifyContainerSignal
167184
}
185+
if len(notifyContainerFilter) > 0 {
186+
cfg.NotifyContainersFilter = notifyContainerFilter
187+
cfg.NotifyContainersSignal = notifyContainerSignal
188+
}
168189
configs = config.ConfigFile{
169190
Config: []config.Config{cfg}}
170191
}

internal/config/config.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ import (
77
)
88

99
type Config struct {
10-
Template string
11-
Dest string
12-
Watch bool
13-
Wait *Wait
14-
NotifyCmd string
15-
NotifyOutput bool
16-
NotifyContainers map[string]int
17-
OnlyExposed bool
18-
OnlyPublished bool
19-
IncludeStopped bool
20-
Interval int
21-
KeepBlankLines bool
10+
Template string
11+
Dest string
12+
Watch bool
13+
Wait *Wait
14+
NotifyCmd string
15+
NotifyOutput bool
16+
NotifyContainers map[string]int
17+
NotifyContainersFilter map[string][]string
18+
NotifyContainersSignal int
19+
OnlyExposed bool
20+
OnlyPublished bool
21+
IncludeStopped bool
22+
Interval int
23+
KeepBlankLines bool
2224
}
2325

2426
type ConfigFile struct {

internal/generator/generator.go

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ func (g *generator) generateFromContainers() {
132132
continue
133133
}
134134
g.runNotifyCmd(config)
135-
g.sendSignalToContainer(config)
135+
g.sendSignalToContainers(config)
136+
g.sendSignalToFilteredContainers(config)
136137
}
137138
}
138139

@@ -162,7 +163,8 @@ func (g *generator) generateAtInterval() {
162163
// ignore changed return value. always run notify command
163164
template.GenerateFile(cfg, containers)
164165
g.runNotifyCmd(cfg)
165-
g.sendSignalToContainer(cfg)
166+
g.sendSignalToContainers(cfg)
167+
g.sendSignalToFilteredContainers(cfg)
166168
case sig := <-sigChan:
167169
log.Printf("Received signal: %s\n", sig)
168170
switch sig {
@@ -210,7 +212,8 @@ func (g *generator) generateFromEvents() {
210212
continue
211213
}
212214
g.runNotifyCmd(cfg)
213-
g.sendSignalToContainer(cfg)
215+
g.sendSignalToContainers(cfg)
216+
g.sendSignalToFilteredContainers(cfg)
214217
}
215218
}(cfg)
216219
}
@@ -332,28 +335,50 @@ func (g *generator) runNotifyCmd(config config.Config) {
332335
}
333336
}
334337

335-
func (g *generator) sendSignalToContainer(config config.Config) {
338+
func (g *generator) sendSignalToContainer(container string, signal int) {
339+
log.Printf("Sending container '%s' signal '%v'", container, signal)
340+
341+
if signal == -1 {
342+
if err := g.Client.RestartContainer(container, 10); err != nil {
343+
log.Printf("Error sending restarting container: %s", err)
344+
}
345+
return
346+
}
347+
348+
killOpts := docker.KillContainerOptions{
349+
ID: container,
350+
Signal: docker.Signal(signal),
351+
}
352+
if err := g.Client.KillContainer(killOpts); err != nil {
353+
log.Printf("Error sending signal to container: %s", err)
354+
}
355+
}
356+
357+
func (g *generator) sendSignalToContainers(config config.Config) {
336358
if len(config.NotifyContainers) < 1 {
337359
return
338360
}
339361

340362
for container, signal := range config.NotifyContainers {
341-
log.Printf("Sending container '%s' signal '%v'", container, signal)
363+
g.sendSignalToContainer(container, signal)
364+
}
365+
}
342366

343-
if signal == -1 {
344-
if err := g.Client.RestartContainer(container, 10); err != nil {
345-
log.Printf("Error sending restarting container: %s", err)
346-
}
347-
return
348-
}
367+
func (g *generator) sendSignalToFilteredContainers(config config.Config) {
368+
if len(config.NotifyContainersFilter) < 1 {
369+
return
370+
}
349371

350-
killOpts := docker.KillContainerOptions{
351-
ID: container,
352-
Signal: docker.Signal(signal),
353-
}
354-
if err := g.Client.KillContainer(killOpts); err != nil {
355-
log.Printf("Error sending signal to container: %s", err)
356-
}
372+
containers, err := g.Client.ListContainers(docker.ListContainersOptions{
373+
Filters: config.NotifyContainersFilter,
374+
})
375+
if err != nil {
376+
log.Printf("Error getting containers: %s", err)
377+
return
378+
}
379+
380+
for _, container := range containers {
381+
g.sendSignalToContainer(container.ID, config.NotifyContainersSignal)
357382
}
358383
}
359384

0 commit comments

Comments
 (0)