-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
229 lines (204 loc) · 4.68 KB
/
main.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"bytes"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"regexp"
"sync"
"time"
log "github.com/sirupsen/logrus"
"github.com/rjeczalik/notify"
"github.com/urfave/cli"
)
const (
version = "0.3"
)
var resolvContent []byte
var rcl sync.RWMutex
func main() {
app := cli.NewApp()
app.Name = "undocker-dns"
app.Usage = "Stop Docker from screwing up resolv.conf"
app.Version = version
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, d",
Usage: "Enable debugging.",
},
cli.IntFlag{
Name: "refresh, r",
Usage: "Refresh resolv.conf every n seconds. 0 to disable.",
Value: 0,
},
cli.StringFlag{
Name: "resolvconf, rc",
Usage: "resolv.conf file location. Will first try /run/systemd/resolve/resolve.conf then /etc/resolv.conf",
},
}
app.Action = Run
err := app.Run(os.Args)
if err != nil {
panic(err)
}
}
var tryResolvs = []string{
"/run/systemd/resolve/resolv.conf",
"/etc/resolv.conf",
}
// Run runs the app
func Run(ctx *cli.Context) error {
log.SetFormatter(&log.TextFormatter{
//ForceColors: false,
//DisableColors: true,
DisableTimestamp: false,
FullTimestamp: true,
})
if ctx.Bool("debug") {
log.SetLevel(log.DebugLevel)
log.Info("Debug logging enabled")
}
resolvconf := ctx.String("resolvconf")
if resolvconf == "" {
for _, rc := range tryResolvs {
resolvconf = rc
if _, err := os.Stat(rc); err == nil {
break
}
}
}
err := refreshAll(resolvconf, true)
if err != nil {
log.WithError(err).Error("Error doing initial refresh")
return err
}
resolvEvents := make(chan notify.EventInfo, 8)
err = notify.Watch(resolvconf, resolvEvents, notify.Write)
if err != nil {
log.WithError(err).Error("Failed to resolv.conf notifications")
return err
}
dkrResolvEvents := make(chan notify.EventInfo, 256)
err = notify.Watch("/var/lib/docker/containers/...", dkrResolvEvents, notify.Write)
if err != nil {
log.WithError(err).Error("Failed to container notifications")
return err
}
stop := make(chan struct{})
done := make(chan struct{})
// This lets me just return err and have nice cleanup
defer func() {
select {
case <-stop:
default:
close(stop)
}
<-done
}()
refresh := ctx.Int("refresh")
go func() {
dockerResolv := regexp.MustCompile("^/var/lib/docker/containers/[A-Za-z0-9]*/resolv.conf$")
t := time.NewTicker(time.Duration(max(refresh, 1)) * time.Second)
if refresh <= 0 {
t.Stop()
}
for {
select {
case <-t.C:
err = refreshAll(resolvconf, true)
if err != nil {
log.WithError(err).Error("Error during scheduled refresh")
}
continue
case ev := <-dkrResolvEvents:
if !dockerResolv.MatchString(ev.Path()) {
continue
}
go fixResolvConf(ev.Path())
case <-resolvEvents:
go func() {
err = refreshAll(resolvconf, false)
if err != nil {
log.WithError(err).Error("Error refreshing after resolv.conf changed")
}
}()
case <-stop:
notify.Stop(dkrResolvEvents)
notify.Stop(resolvEvents)
rcl.Lock() // Ensure all writing is done by locking rcl
close(done)
return
}
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
defer close(c)
go func() {
<-c
close(stop)
}()
<-done
return nil
}
func refreshAll(resolvconf string, force bool) error {
newResolvContent, err := ioutil.ReadFile(resolvconf)
if err != nil {
log.WithError(err).Error("Failed to read resolv.conf")
return err
}
rcl.RLock()
if bytes.Equal(newResolvContent, resolvContent) {
log.Debug("resolv.conf unchanged")
if !force {
rcl.RUnlock()
return nil
}
}
rcl.RUnlock()
rcl.Lock()
resolvContent = newResolvContent
log.WithField("content", string(resolvContent)).Debug("/etc/resov.conf content retrieved")
rcl.Unlock()
resolvs, err := filepath.Glob("/var/lib/docker/containers/*/resolv.conf")
if err != nil {
log.WithError(err).Error("Failed to list existing resolv.conf files")
return err
}
if resolvs != nil {
for _, r := range resolvs {
go fixResolvConf(r)
}
}
return nil
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func fixResolvConf(path string) {
_, err := os.Stat(path)
if err != nil {
log.WithField("Path", path).Debug("File does not exist")
return
}
c, err := ioutil.ReadFile(path)
if err != nil {
log.WithError(err).WithField("Path", path).Error("Failed to read file")
return
}
rcl.RLock()
defer rcl.RUnlock()
if bytes.Equal(c, resolvContent) {
log.WithField("Path", path).Debug("File already has correct content")
return
}
err = ioutil.WriteFile(path, resolvContent, 0644)
if err != nil {
log.WithError(err).WithField("Path", path).Error("Failed to write to file")
}
log.WithField("Path", path).Debug("File content updated")
}