Skip to content

Commit 9801401

Browse files
committed
feat: filter containers seen by docker-gen
1 parent 6d1befd commit 9801401

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
@@ -120,6 +120,11 @@ Options:
120120
only include containers with published ports (implies -only-exposed)
121121
-include-stopped
122122
include stopped containers
123+
-container-filter
124+
container filter for inclusion by docker-gen (e.g -container-filter status=running).
125+
Using this option bypass the -include-stopped option and set it to true.
126+
You can pass this option multiple times to combine filters with AND.
127+
https://docs.docker.com/engine/reference/commandline/ps/#filter
123128
-tlscacert string
124129
path to TLS CA certificate file (default "~/.docker/machine/machines/default/ca.pem")
125130
-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
interval int
@@ -110,6 +111,8 @@ func initFlags() {
110111
flag.BoolVar(&onlyPublished, "only-published", false,
111112
"only include containers with published ports (implies -only-exposed)")
112113
flag.BoolVar(&includeStopped, "include-stopped", false, "include stopped containers")
114+
flag.Var(&containerFilter, "container-filter",
115+
"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")
113116
flag.BoolVar(&notifyOutput, "notify-output", false, "log the output(stdout/stderr) of notify command")
114117
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)")
115118
flag.Var(&sighupContainerID, "notify-sighup",
@@ -175,7 +178,7 @@ func main() {
175178
NotifyContainers: make(map[string]int),
176179
OnlyExposed: onlyExposed,
177180
OnlyPublished: onlyPublished,
178-
IncludeStopped: includeStopped,
181+
ContainerFilter: containerFilter,
179182
Interval: interval,
180183
KeepBlankLines: keepBlankLines,
181184
}
@@ -189,25 +192,20 @@ func main() {
189192
cfg.NotifyContainersFilter = notifyContainerFilter
190193
cfg.NotifyContainersSignal = notifyContainerSignal
191194
}
195+
if len(containerFilter) == 0 && !includeStopped {
196+
cfg.ContainerFilter = map[string][]string{"status": {"running"}}
197+
}
192198
configs = config.ConfigFile{
193199
Config: []config.Config{cfg},
194200
}
195201
}
196202

197-
all := false
198-
for _, config := range configs.Config {
199-
if config.IncludeStopped {
200-
all = true
201-
}
202-
}
203-
204203
generator, err := generator.NewGenerator(generator.GeneratorConfig{
205204
Endpoint: endpoint,
206205
TLSKey: tlsKey,
207206
TLSCert: tlsCert,
208207
TLSCACert: tlsCaCert,
209208
TLSVerify: tlsVerify,
210-
All: all,
211209
ConfigFile: configs,
212210
})
213211

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
@@ -38,7 +38,6 @@ type GeneratorConfig struct {
3838
TLSKey string
3939
TLSCACert string
4040
TLSVerify bool
41-
All bool
4241

4342
ConfigFile config.ConfigFile
4443
}
@@ -69,7 +68,6 @@ func NewGenerator(gc GeneratorConfig) (*generator, error) {
6968
TLSCert: gc.TLSCert,
7069
TLSCaCert: gc.TLSCACert,
7170
TLSKey: gc.TLSKey,
72-
All: gc.All,
7371
Configs: gc.ConfigFile,
7472
retry: true,
7573
}, nil
@@ -120,12 +118,13 @@ func (g *generator) generateFromSignals() {
120118
}
121119

122120
func (g *generator) generateFromContainers() {
123-
containers, err := g.getContainers()
124-
if err != nil {
125-
log.Printf("Error listing containers: %s\n", err)
126-
return
127-
}
128121
for _, config := range g.Configs.Config {
122+
containers, err := g.getContainers(config)
123+
if err != nil {
124+
log.Printf("Error listing containers: %s\n", err)
125+
return
126+
}
127+
129128
changed := template.GenerateFile(config, containers)
130129
if !changed {
131130
log.Printf("Contents of %s did not change. Skipping notification '%s'", config.Dest, config.NotifyCmd)
@@ -155,7 +154,7 @@ func (g *generator) generateAtInterval() {
155154
for {
156155
select {
157156
case <-ticker.C:
158-
containers, err := g.getContainers()
157+
containers, err := g.getContainers(cfg)
159158
if err != nil {
160159
log.Printf("Error listing containers: %s\n", err)
161160
continue
@@ -201,7 +200,7 @@ func (g *generator) generateFromEvents() {
201200
defer g.wg.Done()
202201
debouncedChan := newDebounceChannel(watcher, cfg.Wait)
203202
for range debouncedChan {
204-
containers, err := g.getContainers()
203+
containers, err := g.getContainers(cfg)
205204
if err != nil {
206205
log.Printf("Error listing containers: %s\n", err)
207206
continue
@@ -382,7 +381,7 @@ func (g *generator) sendSignalToFilteredContainers(config config.Config) {
382381
}
383382
}
384383

385-
func (g *generator) getContainers() ([]*context.RuntimeContainer, error) {
384+
func (g *generator) getContainers(config config.Config) ([]*context.RuntimeContainer, error) {
386385
apiInfo, err := g.Client.Info()
387386
if err != nil {
388387
log.Printf("Error retrieving docker server info: %s\n", err)
@@ -391,8 +390,9 @@ func (g *generator) getContainers() ([]*context.RuntimeContainer, error) {
391390
}
392391

393392
apiContainers, err := g.Client.ListContainers(docker.ListContainersOptions{
394-
All: g.All,
395-
Size: false,
393+
All: true,
394+
Size: false,
395+
Filters: config.ContainerFilter,
396396
})
397397
if err != nil {
398398
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)