-
Notifications
You must be signed in to change notification settings - Fork 684
/
watch.go
173 lines (153 loc) · 3.19 KB
/
watch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package docker
import (
"bufio"
"io"
"log"
"os/exec"
"strings"
"time"
"github.com/datawire/ambassador/pkg/tpu"
)
type empty struct{}
type Watcher struct {
Containers map[string]string
stop chan empty
done chan empty
}
func NewWatcher() *Watcher {
return &Watcher{
Containers: make(map[string]string),
stop: make(chan empty),
done: make(chan empty),
}
}
func (w *Watcher) log(line string, args ...interface{}) {
log.Printf("DKR: "+line, args...)
}
func (w *Watcher) Start(listener func(w *Watcher)) {
go func() {
wakeup := w.waiter()
defer close(w.done)
for {
select {
case <-w.stop:
return
case <-wakeup:
containers, err := w.containers()
if err == nil {
updated := false
for key := range w.Containers {
if containers[key] == "" {
delete(w.Containers, key)
updated = true
}
}
for key, value := range containers {
prev := w.Containers[key]
if value != prev {
w.Containers[key] = value
updated = true
}
}
if updated {
listener(w)
}
}
}
}
}()
}
func (w *Watcher) Stop() {
close(w.stop)
<-w.done
}
func (w *Watcher) containers() (result map[string]string, err error) {
ids, err := tpu.CmdLogf([]string{"docker", "container", "list", "-q"}, w.log)
if err != nil {
return
}
lines := ""
if ids != "" {
lines, err = tpu.CmdLogf(append([]string{"docker", "inspect", "--format={{.Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}", "--"}, strings.Fields(ids)...), w.log)
if err != nil {
return
}
}
result = make(map[string]string)
for _, line := range strings.Split(lines, "\n") {
parts := strings.Fields(line)
if len(parts) == 2 {
name := strings.TrimLeft(parts[0], "/")
ip := parts[1]
result[name] = ip
} else if len(parts) > 2 {
w.log("error parsing: %s", line)
}
}
return
}
func (w *Watcher) checkDocker(warn bool) bool {
output, err := tpu.Cmd("docker", "version")
if err != nil {
if warn {
w.log(output)
w.log(err.Error())
w.log("docker is required for docker bridge functionality")
}
return false
}
return true
}
func (w *Watcher) waiter() chan empty {
result := make(chan empty)
go func() {
var pipe io.ReadCloser
var events *bufio.Reader
for {
for count := 0; true; count += 1 {
if w.checkDocker((count % 60) == 0) {
break
} else {
time.Sleep(1 * time.Second)
}
}
result <- empty{}
if pipe == nil {
pipe = w.containerEvents()
events = bufio.NewReader(pipe)
}
st, err := events.ReadString('\n')
if st != "" {
w.log(st)
}
if err != nil {
if err != io.EOF {
log.Println(err)
}
pipe.Close()
pipe = nil
time.Sleep(1 * time.Second)
}
}
}()
return result
}
func (w *Watcher) containerEvents() io.ReadCloser {
command := []string{"docker", "events",
"--filter", "type=container",
"--filter", "event=start",
"--filter", "event=die"}
w.log(strings.Join(command, " "))
cmd := exec.Command(command[0], command[1:]...)
events, err := cmd.StdoutPipe()
if err != nil {
w.log(err.Error())
return nil
}
err = cmd.Start()
if err != nil {
w.log(err.Error())
return nil
}
return events
}