Skip to content

Commit 5aa8bc9

Browse files
committed
feat: filter containers seen by docker-gen
1 parent bbb5f84 commit 5aa8bc9

File tree

5 files changed

+28
-40
lines changed

5 files changed

+28
-40
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ Options:
125125
only include containers with published ports (implies -only-exposed)
126126
-include-stopped
127127
include stopped containers
128+
-container-filter
129+
container filter for inclusion by docker-gen (e.g -container-filter status=running).
130+
Using this option bypass the -include-stopped option and set it to true.
131+
You can pass this option multiple times to combine filters with AND.
132+
https://docs.docker.com/engine/reference/commandline/ps/#filter
128133
-tlscacert string
129134
path to TLS CA certificate file (default "~/.docker/machine/machines/default/ca.pem")
130135
-tlscert string

cmd/docker-gen/main.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
onlyExposed bool
3535
onlyPublished bool
3636
includeStopped bool
37+
containerFilter mapstringslice = make(mapstringslice)
3738
configFiles stringslice
3839
configs config.ConfigFile
3940
eventFilter mapstringslice = mapstringslice{"event": {"start", "stop", "die", "health_status"}}
@@ -111,6 +112,8 @@ func initFlags() {
111112
flag.BoolVar(&onlyPublished, "only-published", false,
112113
"only include containers with published ports (implies -only-exposed)")
113114
flag.BoolVar(&includeStopped, "include-stopped", false, "include stopped containers")
115+
flag.Var(&containerFilter, "container-filter",
116+
"container filter for inclusion by docker-gen. Using this option bypass the -include-stopped option and set it to true. You can pass this option multiple times to combine filters with AND. https://docs.docker.com/engine/reference/commandline/ps/#filter")
114117
flag.BoolVar(&notifyOutput, "notify-output", false, "log the output(stdout/stderr) of notify command")
115118
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)")
116119
flag.Var(&sighupContainerID, "notify-sighup",
@@ -179,7 +182,7 @@ func main() {
179182
NotifyContainers: make(map[string]int),
180183
OnlyExposed: onlyExposed,
181184
OnlyPublished: onlyPublished,
182-
IncludeStopped: includeStopped,
185+
ContainerFilter: containerFilter,
183186
Interval: interval,
184187
KeepBlankLines: keepBlankLines,
185188
}
@@ -193,25 +196,20 @@ func main() {
193196
cfg.NotifyContainersFilter = notifyContainerFilter
194197
cfg.NotifyContainersSignal = notifyContainerSignal
195198
}
199+
if len(containerFilter) == 0 && !includeStopped {
200+
cfg.ContainerFilter = map[string][]string{"status": {"running"}}
201+
}
196202
configs = config.ConfigFile{
197203
Config: []config.Config{cfg},
198204
}
199205
}
200206

201-
all := false
202-
for _, config := range configs.Config {
203-
if config.IncludeStopped {
204-
all = true
205-
}
206-
}
207-
208207
generator, err := generator.NewGenerator(generator.GeneratorConfig{
209208
Endpoint: endpoint,
210209
TLSKey: tlsKey,
211210
TLSCert: tlsCert,
212211
TLSCACert: tlsCaCert,
213212
TLSVerify: tlsVerify,
214-
All: all,
215213
EventFilter: eventFilter,
216214
ConfigFile: configs,
217215
})

internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Config struct {
1818
NotifyContainersSignal int
1919
OnlyExposed bool
2020
OnlyPublished bool
21-
IncludeStopped bool
21+
ContainerFilter map[string][]string
2222
Interval int
2323
KeepBlankLines bool
2424
}

internal/generator/generator.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ type GeneratorConfig struct {
3939
TLSKey string
4040
TLSCACert string
4141
TLSVerify bool
42-
All bool
4342

4443
EventFilter map[string][]string
4544

@@ -72,7 +71,6 @@ func NewGenerator(gc GeneratorConfig) (*generator, error) {
7271
TLSCert: gc.TLSCert,
7372
TLSCaCert: gc.TLSCACert,
7473
TLSKey: gc.TLSKey,
75-
All: gc.All,
7674
EventFilter: gc.EventFilter,
7775
Configs: gc.ConfigFile,
7876
retry: true,
@@ -124,12 +122,13 @@ func (g *generator) generateFromSignals() {
124122
}
125123

126124
func (g *generator) generateFromContainers() {
127-
containers, err := g.getContainers()
128-
if err != nil {
129-
log.Printf("Error listing containers: %s\n", err)
130-
return
131-
}
132125
for _, config := range g.Configs.Config {
126+
containers, err := g.getContainers(config)
127+
if err != nil {
128+
log.Printf("Error listing containers: %s\n", err)
129+
return
130+
}
131+
133132
changed := template.GenerateFile(config, containers)
134133
if !changed {
135134
log.Printf("Contents of %s did not change. Skipping notification '%s'", config.Dest, config.NotifyCmd)
@@ -159,7 +158,7 @@ func (g *generator) generateAtInterval() {
159158
for {
160159
select {
161160
case <-ticker.C:
162-
containers, err := g.getContainers()
161+
containers, err := g.getContainers(cfg)
163162
if err != nil {
164163
log.Printf("Error listing containers: %s\n", err)
165164
continue
@@ -205,7 +204,7 @@ func (g *generator) generateFromEvents() {
205204
defer g.wg.Done()
206205
debouncedChan := newDebounceChannel(watcher, cfg.Wait)
207206
for range debouncedChan {
208-
containers, err := g.getContainers()
207+
containers, err := g.getContainers(cfg)
209208
if err != nil {
210209
log.Printf("Error listing containers: %s\n", err)
211210
continue
@@ -389,7 +388,7 @@ func (g *generator) sendSignalToFilteredContainers(config config.Config) {
389388
}
390389
}
391390

392-
func (g *generator) getContainers() ([]*context.RuntimeContainer, error) {
391+
func (g *generator) getContainers(config config.Config) ([]*context.RuntimeContainer, error) {
393392
apiInfo, err := g.Client.Info()
394393
if err != nil {
395394
log.Printf("Error retrieving docker server info: %s\n", err)
@@ -398,8 +397,9 @@ func (g *generator) getContainers() ([]*context.RuntimeContainer, error) {
398397
}
399398

400399
apiContainers, err := g.Client.ListContainers(docker.ListContainersOptions{
401-
All: g.All,
402-
Size: false,
400+
All: true,
401+
Size: false,
402+
Filters: config.ContainerFilter,
403403
})
404404
if err != nil {
405405
return nil, err

internal/template/template.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,37 +149,22 @@ func removeBlankLines(reader io.Reader, writer io.Writer) {
149149
bwriter.Flush()
150150
}
151151

152-
func filterRunning(config config.Config, containers context.Context) context.Context {
153-
if config.IncludeStopped {
154-
return containers
155-
} else {
156-
filteredContainers := context.Context{}
157-
for _, container := range containers {
158-
if container.State.Running {
159-
filteredContainers = append(filteredContainers, container)
160-
}
161-
}
162-
return filteredContainers
163-
}
164-
}
165-
166152
func GenerateFile(config config.Config, containers context.Context) bool {
167-
filteredRunningContainers := filterRunning(config, containers)
168153
filteredContainers := context.Context{}
169154
if config.OnlyPublished {
170-
for _, container := range filteredRunningContainers {
155+
for _, container := range containers {
171156
if len(container.PublishedAddresses()) > 0 {
172157
filteredContainers = append(filteredContainers, container)
173158
}
174159
}
175160
} else if config.OnlyExposed {
176-
for _, container := range filteredRunningContainers {
161+
for _, container := range containers {
177162
if len(container.Addresses) > 0 {
178163
filteredContainers = append(filteredContainers, container)
179164
}
180165
}
181166
} else {
182-
filteredContainers = filteredRunningContainers
167+
filteredContainers = containers
183168
}
184169

185170
contents := executeTemplate(config.Template, filteredContainers)

0 commit comments

Comments
 (0)