forked from mewil/tailscale-ingress-controller
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontroller_http.go
439 lines (403 loc) · 12 KB
/
controller_http.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package main
import (
"context"
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/bep/debounce"
v1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"tailscale.com/ipn/store/kubestore"
"tailscale.com/tsnet"
)
const INGRESS_CLASS_NAME = "tailscale"
// HttpController state
type HttpController struct {
// Tailscale authentication key
tsAuthKey string
// Mutex for shared hosts map
mu sync.RWMutex
// HTTP proxies for each Ingress host
hosts map[string]*HttpHost
}
// State of the HTTP proxy
type HttpHost struct {
// Tailscale leg of the proxy
tsServer *tsnet.Server
// HTTP connection to backoffice service
httpServer *http.Server
// Path prefixes to match this host
pathPrefixes []*HttpHostPath
// Path map to direct to this host
pathMap map[string]*HttpHostPath
// Host state
started, deleted bool
// If Tailscale TLS will be requested for the service
useTls bool
// If Tailscale Funnel will be requested for the service
useFunnel bool
// If TIC will log the requests passing
enableLogging bool
// Version of the HTTP setup to track changes
generation int64
}
// A path associated with the host
type HttpHostPath struct {
// A matching part of the specification
value string
// If it is an exact match
exact bool
// Reference to the backend service
backend *url.URL
}
// Create a new HTTP controller with a specified Tailscale auth key
func NewHttpController(tsAuthKey string) *HttpController {
return &HttpController{
tsAuthKey: tsAuthKey,
mu: sync.RWMutex{},
hosts: make(map[string]*HttpHost),
}
}
// Find a backend target for the specific host and incoming request
func (c *HttpController) getBackendUrl(host, path string, rawquery string) (*url.URL, error) {
c.mu.RLock()
defer c.mu.RUnlock()
h, ok := c.hosts[host]
if !ok {
return nil, fmt.Errorf("host not found")
}
if _, ok = h.pathMap[path]; ok {
return h.pathMap[path].backend, nil
}
for _, p := range h.pathPrefixes {
if strings.HasPrefix(path, p.value) {
return &url.URL{
Scheme: p.backend.Scheme,
Host: p.backend.Host,
Path: path,
RawQuery: rawquery,
}, nil
}
}
return nil, fmt.Errorf("path not found")
}
// Generate a tsnet state folder name at the specific prefix and host
func generateTsDir(prefix, host string) (*string, error) {
confDir, err := os.UserConfigDir()
if err != nil {
return nil, fmt.Errorf("failed to get user config dir: %s", err.Error())
}
dir := filepath.Join(confDir, prefix, host)
if err = os.MkdirAll(dir, 0755); err != nil {
return nil, fmt.Errorf("failed to create config dir: %s", err.Error())
}
return &dir, nil
}
// Turn K8s service name into an actual port number (if necessary)
func resolveTargetAddress(targetAddress, targetPort string) (*string, error) {
var fullTargetAddress string
// check if targetPort is number or service name
if targetPortNumber, err := strconv.Atoi(targetPort); err == nil {
fullTargetAddress = fmt.Sprintf("%s:%d", targetAddress, targetPortNumber)
} else {
// targetPort is a service name, must resolve
_, addrs, err := net.LookupSRV(targetPort, "tcp", targetAddress)
var port int16
if err == nil {
for _, service := range addrs {
// XXX: is there a possibility of multiple answers for the k8s SRV request?
port = int16(service.Port)
break
}
} else {
log.Printf("TIC: Unable to resolve service to port number: %s: %s", targetPort, err.Error())
return nil, fmt.Errorf("unable to resolve service to port number %s: %s", targetPort, err.Error())
}
fullTargetAddress = fmt.Sprintf("%s:%d", targetAddress, port)
}
return &fullTargetAddress, nil
}
// Refresh controller state from the set of Ingress objects
func (c *HttpController) update(payload *update) {
c.mu.Lock()
defer c.mu.Unlock()
for h := range c.hosts {
c.hosts[h].deleted = true
}
for _, ingress := range payload.ingresses {
ingressClassName := ""
if ingress.Spec.IngressClassName != nil {
ingressClassName = *ingress.Spec.IngressClassName
}
if ingressClassName != INGRESS_CLASS_NAME {
log.Printf("TIC: skipping %s as the ingressClassName %s is not for TIC", ingress.Name, ingressClassName)
continue
}
tlsHosts := make(map[string]struct{})
_, useFunnel := ingress.Labels["tailscale.com/funnel"]
_, enableLogging := ingress.Labels["tailscale.com/logging"]
_, enableWebClient := ingress.Labels["tailscale.com/webclient"]
for _, t := range ingress.Spec.TLS {
for _, h := range t.Hosts {
tlsHosts[h] = struct{}{}
}
}
for _, rule := range ingress.Spec.Rules {
if rule.Host == "" {
log.Println("TIC: ignoring ingress rule without host")
continue
}
if strings.Contains(rule.Host, "*") {
log.Println("TIC: ignoring ingress rule with wildcard host")
continue
}
if rule.HTTP == nil {
log.Println("TIC: ignoring ingress rule without http")
continue
}
existingHost, ok := c.hosts[rule.Host]
if !ok || existingHost.generation < ingress.Generation {
if ok {
// We already have a host with the same name but now the resource configuration
// is updated. We need to re-create the host with any new settings.
log.Printf("TIC: Ingress definition for host %s changed from %d to %d, restarting Tailscale host",
rule.Host,
existingHost.generation,
ingress.Generation,
)
existingHost.tsServer.Close()
delete(c.hosts, rule.Host)
}
dir, err := generateTsDir("ts", rule.Host)
if err != nil {
log.Printf("TIC: unable to create dir for tsnet: %s", err.Error())
continue
}
_, useTls := tlsHosts[rule.Host]
kubeStore, err := kubestore.New(log.Printf, fmt.Sprintf("ts-%s", rule.Host))
if err != nil {
log.Printf("TIC: unable to create kubestore: %s", err.Error())
}
c.hosts[rule.Host] = &HttpHost{
tsServer: &tsnet.Server{
Dir: *dir,
Store: kubeStore,
Hostname: rule.Host,
Ephemeral: true,
AuthKey: c.tsAuthKey,
Logf: nil,
RunWebClient: enableWebClient,
},
useTls: useTls,
useFunnel: useFunnel,
enableLogging: enableLogging,
generation: ingress.Generation,
}
}
c.hosts[rule.Host].deleted = false
if ingress.Spec.DefaultBackend != nil {
log.Println("TIC: ignoring ingress default backend")
continue
}
for _, path := range rule.HTTP.Paths {
if _, ok = c.hosts[rule.Host].pathMap[path.Path]; !ok {
c.hosts[rule.Host].pathMap = make(map[string]*HttpHostPath, 0)
}
if path.PathType == nil {
log.Println("TIC: ignoring ingress path without path type")
continue
}
var fullTargetAddress string
// port can be given as a service name or as a number
if path.Backend.Service.Port.Name != "" {
resolvedAddress, err := resolveTargetAddress(
fmt.Sprintf("%s.%s.svc.cluster.local", path.Backend.Service.Name, ingress.Namespace),
path.Backend.Service.Port.Name,
)
if err != nil {
log.Printf("TIC: Unable to resolve target address: %v", err.Error())
continue
}
fullTargetAddress = *resolvedAddress
} else {
fullTargetAddress = fmt.Sprintf(
"%s.%s.svc.cluster.local:%d",
path.Backend.Service.Name,
ingress.Namespace,
path.Backend.Service.Port.Number,
)
}
p := &HttpHostPath{
value: path.Path,
exact: *path.PathType == v1.PathTypeExact,
backend: &url.URL{
Scheme: "http",
Host: fullTargetAddress,
},
}
c.hosts[rule.Host].pathMap[p.value] = p
if !p.exact {
appendSorted := func(l []*HttpHostPath, e *HttpHostPath) []*HttpHostPath {
i := sort.Search(len(l), func(i int) bool {
return len(l[i].value) < len(e.value)
})
if i == len(l) {
return append(l, e)
}
l = append(l, &HttpHostPath{})
copy(l[i+1:], l[i:])
l[i] = e
return l
}
c.hosts[rule.Host].pathPrefixes = appendSorted(c.hosts[rule.Host].pathPrefixes, p)
}
}
}
}
for n, h := range c.hosts {
if h.deleted {
log.Println("TIC: deleting host ", n)
if err := h.httpServer.Close(); err != nil {
log.Printf("TIC: failed to close http server: %v", err)
}
if err := h.tsServer.Close(); err != nil {
log.Printf("TIC: failed to close ts server: %v", err)
}
delete(c.hosts, n)
continue
}
if h.started {
log.Printf("TIC: host %s already started", n)
continue
}
var ln net.Listener
var err error
if h.useFunnel {
ln, err = h.tsServer.ListenFunnel("tcp", ":443")
} else if h.useTls {
ln, err = h.tsServer.Listen("tcp", ":443")
} else {
ln, err = h.tsServer.Listen("tcp", ":80")
}
if err != nil {
log.Println("TIC: failed to listen: ", err)
continue
}
lc, err := h.tsServer.LocalClient()
if err != nil {
log.Println("TIC: failed to get local client: ", err)
continue
}
if h.useTls {
ln = tls.NewListener(ln, &tls.Config{
GetCertificate: lc.GetCertificate,
})
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Hack since the host will include a tailnet name when using TLS.
rh, _, _ := strings.Cut(r.Host, ".")
backendURL, err := c.getBackendUrl(rh, r.URL.Path, r.URL.RawQuery)
if err != nil {
log.Printf("TIC: upstream server %s not found: %s", rh, err.Error())
http.Error(w, fmt.Sprintf("upstream server %s not found", rh), http.StatusNotFound)
return
}
// TODO: optional request logging
director := func(req *http.Request) {
req.URL = backendURL
who, err := lc.WhoIs(req.Context(), req.RemoteAddr)
if err != nil {
log.Println("TIC: failed to get the owner of the request")
return
}
if who.UserProfile == nil {
log.Println("TIC: user profile is nil")
return
}
req.Header.Set("X-Webauth-User", who.UserProfile.LoginName)
req.Header.Set("X-Webauth-Name", who.UserProfile.DisplayName)
if h.enableLogging {
log.Printf("TIC: Proxying HTTP request for host %s to [%s]", r.Host, backendURL)
}
}
proxy := &httputil.ReverseProxy{Director: director}
proxy.ServeHTTP(w, r)
})
srv := http.Server{Handler: handler}
c.hosts[n].httpServer = &srv
go func() {
log.Printf("TIC: Started HTTP proxy for host [%s]", n)
if err := srv.Serve(ln); err != nil {
log.Println("TIC: failed to serve: ", err)
}
}()
c.hosts[n].started = true
}
}
// Shutdown HTTP reverse proxies
func (c *HttpController) shutdown() {
c.mu.Lock()
defer c.mu.Unlock()
// shutdown HTTP proxies
for n, h := range c.hosts {
if h.started {
log.Printf("TIC: deleting host %s", n)
if err := h.httpServer.Close(); err != nil {
log.Printf("TIC: failed to close http server: %v", err)
}
if err := h.tsServer.Close(); err != nil {
log.Printf("TIC: failed to close ts server: %v", err)
}
delete(c.hosts, n)
}
}
}
type update struct {
ingresses []*v1.Ingress
}
// Listen to updates on the Ingress objects
// @param ctx Go context to operate in
// @param client a K8s client interface
func (c *HttpController) listen(ctx context.Context, client kubernetes.Interface) {
factory := informers.NewSharedInformerFactory(client, time.Minute)
ingressLister := factory.Networking().V1().Ingresses().Lister()
onChange := func() {
ingresses, err := ingressLister.List(labels.Everything())
if err != nil {
log.Printf("TIC: failed to list ingresses: %s", err)
return
}
log.Printf("TIC: Ingress items to review=%d", len(ingresses))
c.update(&update{ingresses})
}
debounced := debounce.New(time.Second)
eventHandler := cache.ResourceEventHandlerFuncs{
AddFunc: func(any) { debounced(onChange) },
UpdateFunc: func(any, any) { debounced(onChange) },
DeleteFunc: func(any) { debounced(onChange) },
}
go func() {
i := factory.Networking().V1().Ingresses().Informer()
i.AddEventHandler(eventHandler)
i.Run(ctx.Done())
}()
go func() {
i := factory.Core().V1().Services().Informer()
i.AddEventHandler(eventHandler)
i.Run(ctx.Done())
}()
}