forked from projectcontour/contour
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
273 lines (235 loc) · 7.77 KB
/
listener.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
// Copyright © 2018 Heptio
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package contour
import (
"sync"
"github.com/envoyproxy/go-control-plane/envoy/api/v2"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/listener"
"github.com/gogo/protobuf/proto"
"github.com/heptio/contour/internal/dag"
"github.com/heptio/contour/internal/envoy"
"k8s.io/api/core/v1"
)
const (
ENVOY_HTTP_LISTENER = "ingress_http"
ENVOY_HTTPS_LISTENER = "ingress_https"
DEFAULT_HTTP_ACCESS_LOG = "/dev/stdout"
DEFAULT_HTTP_LISTENER_ADDRESS = "0.0.0.0"
DEFAULT_HTTP_LISTENER_PORT = 8080
DEFAULT_HTTPS_ACCESS_LOG = "/dev/stdout"
DEFAULT_HTTPS_LISTENER_ADDRESS = DEFAULT_HTTP_LISTENER_ADDRESS
DEFAULT_HTTPS_LISTENER_PORT = 8443
)
// ListenerVisitorConfig holds configuration parameters for visitListeners.
type ListenerVisitorConfig struct {
// Envoy's HTTP (non TLS) listener address.
// If not set, defaults to DEFAULT_HTTP_LISTENER_ADDRESS.
HTTPAddress string
// Envoy's HTTP (non TLS) listener port.
// If not set, defaults to DEFAULT_HTTP_LISTENER_PORT.
HTTPPort int
// Envoy's HTTP (non TLS) access log path.
// If not set, defaults to DEFAULT_HTTP_ACCESS_LOG.
HTTPAccessLog string
// Envoy's HTTPS (TLS) listener address.
// If not set, defaults to DEFAULT_HTTPS_LISTENER_ADDRESS.
HTTPSAddress string
// Envoy's HTTPS (TLS) listener port.
// If not set, defaults to DEFAULT_HTTPS_LISTENER_PORT.
HTTPSPort int
// Envoy's HTTPS (TLS) access log path.
// If not set, defaults to DEFAULT_HTTPS_ACCESS_LOG.
HTTPSAccessLog string
// UseProxyProto configurs all listeners to expect a PROXY
// V1 or V2 preamble.
// If not set, defaults to false.
UseProxyProto bool
}
// httpAddress returns the port for the HTTP (non TLS)
// listener or DEFAULT_HTTP_LISTENER_ADDRESS if not configured.
func (lvc *ListenerVisitorConfig) httpAddress() string {
if lvc.HTTPAddress != "" {
return lvc.HTTPAddress
}
return DEFAULT_HTTP_LISTENER_ADDRESS
}
// httpPort returns the port for the HTTP (non TLS)
// listener or DEFAULT_HTTP_LISTENER_PORT if not configured.
func (lvc *ListenerVisitorConfig) httpPort() int {
if lvc.HTTPPort != 0 {
return lvc.HTTPPort
}
return DEFAULT_HTTP_LISTENER_PORT
}
// httpAccessLog returns the access log for the HTTP (non TLS)
// listener or DEFAULT_HTTP_ACCESS_LOG if not configured.
func (lvc *ListenerVisitorConfig) httpAccessLog() string {
if lvc.HTTPAccessLog != "" {
return lvc.HTTPAccessLog
}
return DEFAULT_HTTP_ACCESS_LOG
}
// httpsAddress returns the port for the HTTPS (TLS)
// listener or DEFAULT_HTTPS_LISTENER_ADDRESS if not configured.
func (lvc *ListenerVisitorConfig) httpsAddress() string {
if lvc.HTTPSAddress != "" {
return lvc.HTTPSAddress
}
return DEFAULT_HTTPS_LISTENER_ADDRESS
}
// httpsPort returns the port for the HTTPS (TLS) listener
// or DEFAULT_HTTPS_LISTENER_PORT if not configured.
func (lvc *ListenerVisitorConfig) httpsPort() int {
if lvc.HTTPSPort != 0 {
return lvc.HTTPSPort
}
return DEFAULT_HTTPS_LISTENER_PORT
}
// httpsAccessLog returns the access log for the HTTPS (TLS)
// listener or DEFAULT_HTTPS_ACCESS_LOG if not configured.
func (lvc *ListenerVisitorConfig) httpsAccessLog() string {
if lvc.HTTPSAccessLog != "" {
return lvc.HTTPSAccessLog
}
return DEFAULT_HTTPS_ACCESS_LOG
}
// ListenerCache manages the contents of the gRPC LDS cache.
type ListenerCache struct {
mu sync.Mutex
values map[string]*v2.Listener
waiters []chan int
last int
}
// Register registers ch to receive a value when Notify is called.
// The value of last is the count of the times Notify has been called on this Cache.
// It functions of a sequence counter, if the value of last supplied to Register
// is less than the Cache's internal counter, then the caller has missed at least
// one notification and will fire immediately.
//
// Sends by the broadcaster to ch must not block, therefor ch must have a capacity
// of at least 1.
func (c *ListenerCache) Register(ch chan int, last int) {
c.mu.Lock()
defer c.mu.Unlock()
if last < c.last {
// notify this channel immediately
ch <- c.last
return
}
c.waiters = append(c.waiters, ch)
}
// Update replaces the contents of the cache with the supplied map.
func (c *ListenerCache) Update(v map[string]*v2.Listener) {
c.mu.Lock()
defer c.mu.Unlock()
c.values = v
c.notify()
}
// notify notifies all registered waiters that an event has occurred.
func (c *ListenerCache) notify() {
c.last++
for _, ch := range c.waiters {
ch <- c.last
}
c.waiters = c.waiters[:0]
}
// Values returns a slice of the value stored in the cache.
func (c *ListenerCache) Values(filter func(string) bool) []proto.Message {
c.mu.Lock()
values := make([]proto.Message, 0, len(c.values))
for _, v := range c.values {
if filter(v.Name) {
values = append(values, v)
}
}
c.mu.Unlock()
return values
}
type listenerVisitor struct {
*ListenerVisitorConfig
listeners map[string]*v2.Listener
http bool // at least one dag.VirtualHost encountered
}
func visitListeners(root dag.Vertex, lvc *ListenerVisitorConfig) map[string]*v2.Listener {
lv := listenerVisitor{
ListenerVisitorConfig: lvc,
listeners: map[string]*v2.Listener{
ENVOY_HTTPS_LISTENER: envoy.Listener(
ENVOY_HTTPS_LISTENER,
lvc.httpsAddress(), lvc.httpsPort(),
secureProxyProtocol(lvc.UseProxyProto),
),
},
}
lv.visit(root)
// add a listener if there are vhosts bound to http.
if lv.http {
lv.listeners[ENVOY_HTTP_LISTENER] = envoy.Listener(
ENVOY_HTTP_LISTENER,
lvc.httpAddress(), lvc.httpPort(),
proxyProtocol(lvc.UseProxyProto),
envoy.HTTPConnectionManager(ENVOY_HTTP_LISTENER, lvc.httpAccessLog()),
)
}
// remove the https listener if there are no vhosts bound to it.
if len(lv.listeners[ENVOY_HTTPS_LISTENER].FilterChains) == 0 {
delete(lv.listeners, ENVOY_HTTPS_LISTENER)
}
return lv.listeners
}
func proxyProtocol(useProxy bool) []listener.ListenerFilter {
if useProxy {
return []listener.ListenerFilter{
envoy.ProxyProtocol(),
}
}
return nil
}
func secureProxyProtocol(useProxy bool) []listener.ListenerFilter {
return append(proxyProtocol(useProxy), envoy.TLSInspector())
}
func (v *listenerVisitor) visit(vertex dag.Vertex) {
switch vh := vertex.(type) {
case *dag.VirtualHost:
// we only create on http listener so record the fact
// that we need to then double back at the end and add
// the listener properly.
v.http = true
case *dag.SecureVirtualHost:
filters := []listener.Filter{
envoy.HTTPConnectionManager(ENVOY_HTTPS_LISTENER, v.httpsAccessLog()),
}
alpnProtos := []string{"h2", "http/1.1"}
if vh.VirtualHost.TCPProxy != nil {
filters = []listener.Filter{
envoy.TCPProxy(ENVOY_HTTPS_LISTENER, vh.VirtualHost.TCPProxy, v.httpsAccessLog()),
}
alpnProtos = nil // do not offer ALPN
}
fc := listener.FilterChain{
FilterChainMatch: &listener.FilterChainMatch{
ServerNames: []string{vh.VirtualHost.Name},
},
Filters: filters,
}
// attach certificate data to this listener if provided.
if vh.Secret != nil {
data := vh.Secret.Data()
fc.TlsContext = envoy.DownstreamTLSContext(data[v1.TLSCertKey], data[v1.TLSPrivateKeyKey], vh.MinProtoVersion, alpnProtos...)
}
v.listeners[ENVOY_HTTPS_LISTENER].FilterChains = append(v.listeners[ENVOY_HTTPS_LISTENER].FilterChains, fc)
default:
// recurse
vertex.Visit(v.visit)
}
}