-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
consul_http_sock_hook.go
210 lines (173 loc) · 4.76 KB
/
consul_http_sock_hook.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
package allocrunner
import (
"context"
"net"
"os"
"path/filepath"
"sync"
"time"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/nomad/structs/config"
"github.com/pkg/errors"
)
func tgFirstNetworkIsBridge(tg *structs.TaskGroup) bool {
if len(tg.Networks) < 1 || tg.Networks[0].Mode != "bridge" {
return false
}
return true
}
const (
consulHTTPSocketHookName = "consul_http_socket"
)
type consulHTTPSockHook struct {
logger hclog.Logger
// lock synchronizes proxy and alloc which may be mutated and read concurrently
// via Prerun, Update, and Postrun.
lock sync.Mutex
alloc *structs.Allocation
proxy *httpSocketProxy
}
func newConsulHTTPSocketHook(logger hclog.Logger, alloc *structs.Allocation, allocDir *allocdir.AllocDir, config *config.ConsulConfig) *consulHTTPSockHook {
return &consulHTTPSockHook{
alloc: alloc,
proxy: newHTTPSocketProxy(logger, allocDir, config),
logger: logger.Named(consulHTTPSocketHookName),
}
}
func (*consulHTTPSockHook) Name() string {
return consulHTTPSocketHookName
}
// shouldRun returns true if the alloc contains at least one connect native
// task and has a network configured in bridge mode
//
// todo(shoenig): what about CNI networks?
func (h *consulHTTPSockHook) shouldRun() bool {
tg := h.alloc.Job.LookupTaskGroup(h.alloc.TaskGroup)
// we must be in bridge networking and at least one connect native task
if !tgFirstNetworkIsBridge(tg) {
return false
}
for _, service := range tg.Services {
if service.Connect.IsNative() {
return true
}
}
return false
}
func (h *consulHTTPSockHook) Prerun() error {
h.lock.Lock()
defer h.lock.Unlock()
if !h.shouldRun() {
return nil
}
return h.proxy.run(h.alloc)
}
func (h *consulHTTPSockHook) Update(req *interfaces.RunnerUpdateRequest) error {
h.lock.Lock()
defer h.lock.Unlock()
h.alloc = req.Alloc
if !h.shouldRun() {
return nil
}
return h.proxy.run(h.alloc)
}
func (h *consulHTTPSockHook) Postrun() error {
h.lock.Lock()
defer h.lock.Unlock()
if err := h.proxy.stop(); err != nil {
// Only log a failure to stop, worst case is the proxy leaks a goroutine.
h.logger.Warn("error stopping Consul HTTP proxy", "error", err)
}
return nil
}
type httpSocketProxy struct {
logger hclog.Logger
allocDir *allocdir.AllocDir
config *config.ConsulConfig
ctx context.Context
cancel func()
doneCh chan struct{}
runOnce bool
}
func newHTTPSocketProxy(logger hclog.Logger, allocDir *allocdir.AllocDir, config *config.ConsulConfig) *httpSocketProxy {
ctx, cancel := context.WithCancel(context.Background())
return &httpSocketProxy{
logger: logger,
allocDir: allocDir,
config: config,
ctx: ctx,
cancel: cancel,
doneCh: make(chan struct{}),
}
}
// run the httpSocketProxy for the given allocation.
//
// Assumes locking done by the calling alloc runner.
func (p *httpSocketProxy) run(alloc *structs.Allocation) error {
// Only run once.
if p.runOnce {
return nil
}
// Never restart.
select {
case <-p.doneCh:
p.logger.Trace("consul http socket proxy already shutdown; exiting")
return nil
case <-p.ctx.Done():
p.logger.Trace("consul http socket proxy already done; exiting")
return nil
default:
}
// consul http dest addr
destAddr := p.config.Addr
if destAddr == "" {
return errors.New("consul address must be set on nomad client")
}
hostHTTPSockPath := filepath.Join(p.allocDir.AllocDir, allocdir.AllocHTTPSocket)
if err := maybeRemoveOldSocket(hostHTTPSockPath); err != nil {
return err
}
listener, err := net.Listen("unix", hostHTTPSockPath)
if err != nil {
return errors.Wrap(err, "unable to create unix socket for Consul HTTP endpoint")
}
// The Consul HTTP socket should be usable by all users in case a task is
// running as a non-privileged user. Unix does not allow setting domain
// socket permissions when creating the file, so we must manually call
// chmod afterwards.
if err := os.Chmod(hostHTTPSockPath, os.ModePerm); err != nil {
return errors.Wrap(err, "unable to set permissions on unix socket")
}
go func() {
proxy(p.ctx, p.logger, destAddr, listener)
p.cancel()
close(p.doneCh)
}()
p.runOnce = true
return nil
}
func (p *httpSocketProxy) stop() error {
p.cancel()
// if proxy was never run, no need to wait before shutdown
if !p.runOnce {
return nil
}
select {
case <-p.doneCh:
case <-time.After(socketProxyStopWaitTime):
return errSocketProxyTimeout
}
return nil
}
func maybeRemoveOldSocket(socketPath string) error {
_, err := os.Stat(socketPath)
if err == nil {
if err = os.Remove(socketPath); err != nil {
return errors.Wrap(err, "unable to remove existing unix socket")
}
}
return nil
}