forked from invzhi/caddy-docker-upstreams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupstreams.go
229 lines (188 loc) · 4.98 KB
/
upstreams.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 caddy_docker_upstreams
import (
"context"
"errors"
"net"
"net/http"
"sync"
"time"
"github.com/bep/debounce"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"go.uber.org/zap"
)
const (
LabelEnable = "com.caddyserver.http.enable"
LabelUpstreamPort = "com.caddyserver.http.upstream.port"
)
func init() {
caddy.RegisterModule(Upstreams{})
}
type candidate struct {
matchers caddyhttp.MatcherSet
upstream *reverseproxy.Upstream
}
var (
candidates []candidate
candidatesMu sync.RWMutex
)
var defaultFilters = filters.NewArgs(
filters.Arg("label", LabelEnable),
// types.ContainerState.Status
filters.Arg("status", "running"),
filters.Arg("health", types.Healthy),
filters.Arg("health", types.NoHealthcheck),
)
// Upstreams provides upstreams from the docker host.
type Upstreams struct {
logger *zap.Logger
}
func (Upstreams) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.reverse_proxy.upstreams.docker",
New: func() caddy.Module { return new(Upstreams) },
}
}
func (u *Upstreams) provisionCandidates(ctx caddy.Context, containers []types.Container) {
updated := make([]candidate, 0, len(containers))
for _, container := range containers {
// Check enable.
if enable, ok := container.Labels[LabelEnable]; !ok || enable != "true" {
continue
}
// Build matchers.
var matchers caddyhttp.MatcherSet
for key, producer := range producers {
value, ok := container.Labels[key]
if !ok {
continue
}
matcher, err := producer(value)
if err != nil {
u.logger.Error("unable to load matcher",
zap.String("key", key),
zap.String("value", value),
zap.Error(err),
)
continue
}
if prov, ok := matcher.(caddy.Provisioner); ok {
err = prov.Provision(ctx)
if err != nil {
u.logger.Error("unable to provision matcher",
zap.String("key", key),
zap.String("value", value),
zap.Error(err),
)
continue
}
}
matchers = append(matchers, matcher)
}
// Build upstream.
port, ok := container.Labels[LabelUpstreamPort]
if !ok {
u.logger.Error("unable to get port from container labels",
zap.String("container_id", container.ID),
)
continue
}
if len(container.NetworkSettings.Networks) == 0 {
u.logger.Error("unable to get ip address from container networks",
zap.String("container_id", container.ID),
)
continue
}
// Use the first network settings of container.
for _, settings := range container.NetworkSettings.Networks {
address := net.JoinHostPort(settings.IPAddress, port)
upstream := &reverseproxy.Upstream{Dial: address}
updated = append(updated, candidate{
matchers: matchers,
upstream: upstream,
})
break
}
}
candidatesMu.Lock()
candidates = updated
candidatesMu.Unlock()
}
func (u *Upstreams) keepUpdated(ctx caddy.Context, cli *client.Client) {
debounced := debounce.New(100 * time.Millisecond)
for {
messages, errs := cli.Events(ctx, types.EventsOptions{
Filters: filters.NewArgs(filters.Arg("type", events.ContainerEventType)),
})
selectLoop:
for {
select {
case <-messages:
debounced(func() {
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{
Filters: defaultFilters,
})
if err != nil {
u.logger.Error("unable to get the list of containers", zap.Error(err))
return
}
u.provisionCandidates(ctx, containers)
})
case err := <-errs:
if errors.Is(err, context.Canceled) {
return
}
u.logger.Warn("unable to monitor container events; will retry", zap.Error(err))
break selectLoop
}
}
select {
case <-ctx.Done():
return
case <-time.After(500 * time.Millisecond):
}
}
}
func (u *Upstreams) Provision(ctx caddy.Context) error {
u.logger = ctx.Logger()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
ping, err := cli.Ping(ctx)
if err != nil {
return err
}
u.logger.Info("docker engine is connected", zap.String("api_version", ping.APIVersion))
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{
Filters: defaultFilters,
})
if err != nil {
return err
}
u.provisionCandidates(ctx, containers)
go u.keepUpdated(ctx, cli)
return nil
}
func (u *Upstreams) GetUpstreams(r *http.Request) ([]*reverseproxy.Upstream, error) {
upstreams := make([]*reverseproxy.Upstream, 0, 1)
candidatesMu.RLock()
defer candidatesMu.RUnlock()
for _, container := range candidates {
if !container.matchers.Match(r) {
continue
}
upstreams = append(upstreams, container.upstream)
}
return upstreams, nil
}
// Interface guards
var (
_ caddy.Provisioner = (*Upstreams)(nil)
_ reverseproxy.UpstreamSource = (*Upstreams)(nil)
)