-
Notifications
You must be signed in to change notification settings - Fork 178
/
cmd.go
283 lines (236 loc) · 8.74 KB
/
cmd.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package caddydockerproxy
import (
"flag"
"net"
"os"
"regexp"
"strings"
"time"
"github.com/caddyserver/caddy/v2"
caddycmd "github.com/caddyserver/caddy/v2/cmd"
"github.com/lucaslorentz/caddy-docker-proxy/v2/config"
"github.com/lucaslorentz/caddy-docker-proxy/v2/generator"
"go.uber.org/zap"
)
var isTrue = regexp.MustCompile("(?i)^(true|yes|1)$")
func init() {
caddycmd.RegisterCommand(caddycmd.Command{
Name: "docker-proxy",
Func: cmdFunc,
Usage: "<command>",
Short: "Run caddy as a docker proxy",
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("docker-proxy", flag.ExitOnError)
fs.Bool("mode", false,
"Which mode this instance should run: standalone | controller | server")
fs.String("docker-sockets", "",
"Docker sockets comma separate")
fs.String("docker-certs-path", "",
"Docker socket certs path comma separate")
fs.String("docker-apis-version", "",
"Docker socket apis version comma separate")
fs.String("controller-network", "",
"Network allowed to configure caddy server in CIDR notation. Ex: 10.200.200.0/24")
fs.String("ingress-networks", "",
"Comma separated name of ingress networks connecting caddy servers to containers.\n"+
"When not defined, networks attached to controller container are considered ingress networks")
fs.String("caddyfile-path", "",
"Path to a base Caddyfile that will be extended with docker sites")
fs.String("envfile", "",
"Environment file with environment variables in the KEY=VALUE format")
fs.String("label-prefix", generator.DefaultLabelPrefix,
"Prefix for Docker labels")
fs.Bool("proxy-service-tasks", true,
"Proxy to service tasks instead of service load balancer")
fs.Bool("process-caddyfile", true,
"Process Caddyfile before loading it, removing invalid servers")
fs.Bool("scan-stopped-containers", false,
"Scan stopped containers and use its labels for caddyfile generation")
fs.Duration("polling-interval", 30*time.Second,
"Interval caddy should manually check docker for a new caddyfile")
fs.Duration("event-throttle-interval", 100*time.Millisecond,
"Interval to throttle caddyfile updates triggered by docker events")
return fs
}(),
})
}
func cmdFunc(flags caddycmd.Flags) (int, error) {
caddy.TrapSignals()
options := createOptions(flags)
log := logger()
if options.Mode&config.Server == config.Server {
log.Info("Running caddy proxy server")
err := caddy.Run(&caddy.Config{
Admin: &caddy.AdminConfig{
Listen: getAdminListen(options),
},
})
if err != nil {
return 1, err
}
}
if options.Mode&config.Controller == config.Controller {
log.Info("Running caddy proxy controller")
loader := CreateDockerLoader(options)
if err := loader.Start(); err != nil {
if err := caddy.Stop(); err != nil {
return 1, err
}
return 1, err
}
}
select {}
}
func getAdminListen(options *config.Options) string {
if options.ControllerNetwork != nil {
ifaces, err := net.Interfaces()
log := logger()
if err != nil {
log.Error("Failed to get network interfaces", zap.Error(err))
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
log.Error("Failed to get network interface addresses", zap.Error(err))
continue
}
for _, a := range addrs {
switch v := a.(type) {
case *net.IPAddr:
if options.ControllerNetwork.Contains(v.IP) {
return "tcp/" + v.IP.String() + ":2019"
}
break
case *net.IPNet:
if options.ControllerNetwork.Contains(v.IP) {
return "tcp/" + v.IP.String() + ":2019"
}
break
}
}
}
}
return "tcp/localhost:2019"
}
func createOptions(flags caddycmd.Flags) *config.Options {
caddyfilePath := flags.String("caddyfile-path")
envFile := flags.String("envfile")
labelPrefixFlag := flags.String("label-prefix")
proxyServiceTasksFlag := flags.Bool("proxy-service-tasks")
processCaddyfileFlag := flags.Bool("process-caddyfile")
scanStoppedContainersFlag := flags.Bool("scan-stopped-containers")
pollingIntervalFlag := flags.Duration("polling-interval")
eventThrottleIntervalFlag := flags.Duration("event-throttle-interval")
modeFlag := flags.String("mode")
controllerSubnetFlag := flags.String("controller-network")
dockerSocketsFlag := flags.String("docker-sockets")
dockerCertsPathFlag := flags.String("docker-certs-path")
dockerAPIsVersionFlag := flags.String("docker-apis-version")
ingressNetworksFlag := flags.String("ingress-networks")
options := &config.Options{}
var mode string
if modeEnv := os.Getenv("CADDY_DOCKER_MODE"); modeEnv != "" {
mode = modeEnv
} else {
mode = modeFlag
}
switch mode {
case "controller":
options.Mode = config.Controller
break
case "server":
options.Mode = config.Server
default:
options.Mode = config.Standalone
}
log := logger()
if dockerSocketsEnv := os.Getenv("CADDY_DOCKER_SOCKETS"); dockerSocketsEnv != "" {
options.DockerSockets = strings.Split(dockerSocketsEnv, ",")
} else if dockerSocketsFlag != "" {
options.DockerSockets = strings.Split(dockerSocketsFlag, ",")
} else {
options.DockerSockets = nil
}
if dockerCertsPathEnv := os.Getenv("CADDY_DOCKER_CERTS_PATH"); dockerCertsPathEnv != "" {
options.DockerCertsPath = strings.Split(dockerCertsPathEnv, ",")
} else {
options.DockerCertsPath = strings.Split(dockerCertsPathFlag, ",")
}
if dockerAPIsVersionEnv := os.Getenv("CADDY_DOCKER_APIS_VERSION"); dockerAPIsVersionEnv != "" {
options.DockerAPIsVersion = strings.Split(dockerAPIsVersionEnv, ",")
} else {
options.DockerAPIsVersion = strings.Split(dockerAPIsVersionFlag, ",")
}
if controllerIPRangeEnv := os.Getenv("CADDY_CONTROLLER_NETWORK"); controllerIPRangeEnv != "" {
_, ipNet, err := net.ParseCIDR(controllerIPRangeEnv)
if err != nil {
log.Error("Failed to parse CADDY_CONTROLLER_NETWORK", zap.String("CADDY_CONTROLLER_NETWORK", controllerIPRangeEnv), zap.Error(err))
} else if ipNet != nil {
options.ControllerNetwork = ipNet
}
} else if controllerSubnetFlag != "" {
_, ipNet, err := net.ParseCIDR(controllerSubnetFlag)
if err != nil {
log.Error("Failed to parse controller-network", zap.String("controller-network", controllerSubnetFlag), zap.Error(err))
} else if ipNet != nil {
options.ControllerNetwork = ipNet
}
}
if ingressNetworksEnv := os.Getenv("CADDY_INGRESS_NETWORKS"); ingressNetworksEnv != "" {
options.IngressNetworks = strings.Split(ingressNetworksEnv, ",")
} else if ingressNetworksFlag != "" {
options.IngressNetworks = strings.Split(ingressNetworksFlag, ",")
}
if caddyfilePathEnv := os.Getenv("CADDY_DOCKER_CADDYFILE_PATH"); caddyfilePathEnv != "" {
options.CaddyfilePath = caddyfilePathEnv
} else {
options.CaddyfilePath = caddyfilePath
}
if envFileEnv := os.Getenv("CADDY_DOCKER_ENVFILE"); envFileEnv != "" {
options.EnvFile = envFileEnv
} else {
options.EnvFile = envFile
}
if labelPrefixEnv := os.Getenv("CADDY_DOCKER_LABEL_PREFIX"); labelPrefixEnv != "" {
options.LabelPrefix = labelPrefixEnv
} else {
options.LabelPrefix = labelPrefixFlag
}
options.ControlledServersLabel = options.LabelPrefix + "_controlled_server"
if proxyServiceTasksEnv := os.Getenv("CADDY_DOCKER_PROXY_SERVICE_TASKS"); proxyServiceTasksEnv != "" {
options.ProxyServiceTasks = isTrue.MatchString(proxyServiceTasksEnv)
} else {
options.ProxyServiceTasks = proxyServiceTasksFlag
}
if processCaddyfileEnv := os.Getenv("CADDY_DOCKER_PROCESS_CADDYFILE"); processCaddyfileEnv != "" {
options.ProcessCaddyfile = isTrue.MatchString(processCaddyfileEnv)
} else {
options.ProcessCaddyfile = processCaddyfileFlag
}
if scanStoppedContainersEnv := os.Getenv("CADDY_DOCKER_SCAN_STOPPED_CONTAINERS"); scanStoppedContainersEnv != "" {
options.ScanStoppedContainers = isTrue.MatchString(scanStoppedContainersEnv)
} else {
options.ScanStoppedContainers = scanStoppedContainersFlag
}
if pollingIntervalEnv := os.Getenv("CADDY_DOCKER_POLLING_INTERVAL"); pollingIntervalEnv != "" {
if p, err := time.ParseDuration(pollingIntervalEnv); err != nil {
log.Error("Failed to parse CADDY_DOCKER_POLLING_INTERVAL", zap.String("CADDY_DOCKER_POLLING_INTERVAL", pollingIntervalEnv), zap.Error(err))
options.PollingInterval = pollingIntervalFlag
} else {
options.PollingInterval = p
}
} else {
options.PollingInterval = pollingIntervalFlag
}
if eventThrottleIntervalEnv := os.Getenv("CADDY_DOCKER_EVENT_THROTTLE_INTERVAL"); eventThrottleIntervalEnv != "" {
if p, err := time.ParseDuration(eventThrottleIntervalEnv); err != nil {
log.Error("Failed to parse CADDY_DOCKER_EVENT_THROTTLE_INTERVAL", zap.String("CADDY_DOCKER_EVENT_THROTTLE_INTERVAL", eventThrottleIntervalEnv), zap.Error(err))
options.EventThrottleInterval = pollingIntervalFlag
} else {
options.EventThrottleInterval = p
}
} else {
options.EventThrottleInterval = eventThrottleIntervalFlag
}
return options
}