Skip to content

Commit 613bd8d

Browse files
committed
Add -only-published filter option
Adds a more restrictive form of -only-exposed where only contiainers that have an exposed port published to the host will be included in the template metadata.
1 parent dfc75c9 commit 613bd8d

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

docker-gen.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ import (
1515
)
1616

1717
var (
18-
watch bool
19-
notifyCmd string
20-
onlyExposed bool
21-
configFile string
22-
configs ConfigFile
23-
interval int
24-
endpoint string
25-
wg sync.WaitGroup
18+
watch bool
19+
notifyCmd string
20+
onlyExposed bool
21+
onlyPublished bool
22+
configFile string
23+
configs ConfigFile
24+
interval int
25+
endpoint string
26+
wg sync.WaitGroup
2627
)
2728

2829
type Event struct {
@@ -63,12 +64,13 @@ func (i *DockerImage) String() string {
6364
}
6465

6566
type Config struct {
66-
Template string
67-
Dest string
68-
Watch bool
69-
NotifyCmd string
70-
OnlyExposed bool
71-
Interval int
67+
Template string
68+
Dest string
69+
Watch bool
70+
NotifyCmd string
71+
OnlyExposed bool
72+
OnlyPublished bool
73+
Interval int
7274
}
7375

7476
type ConfigFile struct {
@@ -198,6 +200,7 @@ func generateFromEvents(client *docker.Client, configs ConfigFile) {
198200
func initFlags() {
199201
flag.BoolVar(&watch, "watch", false, "watch for container changes")
200202
flag.BoolVar(&onlyExposed, "only-exposed", false, "only include containers with exposed ports")
203+
flag.BoolVar(&onlyPublished, "only-published", false, "only include containers with published ports (implies -only-exposed)")
201204
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated")
202205
flag.StringVar(&configFile, "config", "", "config file with template directives")
203206
flag.IntVar(&interval, "interval", 0, "notify command interval (s)")
@@ -221,12 +224,13 @@ func main() {
221224
}
222225
} else {
223226
config := Config{
224-
Template: flag.Arg(0),
225-
Dest: flag.Arg(1),
226-
Watch: watch,
227-
NotifyCmd: notifyCmd,
228-
OnlyExposed: onlyExposed,
229-
Interval: interval,
227+
Template: flag.Arg(0),
228+
Dest: flag.Arg(1),
229+
Watch: watch,
230+
NotifyCmd: notifyCmd,
231+
OnlyExposed: onlyExposed,
232+
OnlyPublished: onlyPublished,
233+
Interval: interval,
230234
}
231235
configs = ConfigFile{
232236
Config: []Config{config}}

template.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,18 @@ func generateFile(config Config, containers []*RuntimeContainer) bool {
4141
}
4242

4343
filteredContainers := []*RuntimeContainer{}
44-
if config.OnlyExposed {
44+
if config.OnlyPublished {
4545
for _, container := range containers {
4646
if len(container.PublishedAddresses()) > 0 {
4747
filteredContainers = append(filteredContainers, container)
4848
}
4949
}
50+
} else if config.OnlyExposed {
51+
for _, container := range containers {
52+
if len(container.Addresses) > 0 {
53+
filteredContainers = append(filteredContainers, container)
54+
}
55+
}
5056
} else {
5157
filteredContainers = containers
5258
}

0 commit comments

Comments
 (0)