/
dag.go
351 lines (280 loc) · 8.75 KB
/
dag.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
// 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 dag provides a data model, in the form of a directed acyclic graph,
// of the relationship between Kubernetes Ingress, Service, and Secret objects.
package dag
import (
"fmt"
"time"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
ingressroutev1 "github.com/heptio/contour/apis/contour/v1beta1"
v1 "k8s.io/api/core/v1"
)
// A DAG represents a directed acylic graph of objects representing the relationship
// between Kubernetes Ingress objects, the backend Services, and Secret objects.
// The DAG models these relationships as Roots and Vertices.
type DAG struct {
// roots are the roots of this dag
roots []Vertex
// status computed while building this dag.
statuses map[Meta]Status
}
// Visit calls fn on each root of this DAG.
func (d *DAG) Visit(fn func(Vertex)) {
for _, r := range d.roots {
fn(r)
}
}
// Statuses returns a slice of Status objects associated with
// the computation of this DAG.
func (d *DAG) Statuses() map[Meta]Status {
return d.statuses
}
// PrefixRoute defines a Route that matches a path prefix.
type PrefixRoute struct {
// Prefix to match.
Prefix string
Route
}
// RegexRoute defines a Route that matches a regular expression.
type RegexRoute struct {
// Regex to match.
Regex string
Route
}
// Route defines the properties of a route to a Cluster.
type Route struct {
Clusters []*Cluster
// Should this route generate a 301 upgrade if accessed
// over HTTP?
HTTPSUpgrade bool
// Is this a websocket route?
// TODO(dfc) this should go on the service
Websocket bool
// TimeoutPolicy defines the timeout request/idle
TimeoutPolicy *TimeoutPolicy
// RetryPolicy defines the retry / number / timeout options for a route
RetryPolicy *RetryPolicy
// Indicates that during forwarding, the matched prefix (or path) should be swapped with this value
PrefixRewrite string
}
// TimeoutPolicy defines the timeout request/idle
type TimeoutPolicy struct {
// A timeout applied to requests on this route.
// A timeout of zero implies "use envoy's default"
// A timeout of -1 represents "infinity"
// TODO(dfc) should this move to service?
Timeout time.Duration
}
// RetryPolicy defines the retry / number / timeout options
type RetryPolicy struct {
// RetryOn specifies the conditions under which retry takes place.
// If empty, retries will not be performed.
RetryOn string
// NumRetries specifies the allowed number of retries.
// Ignored if RetryOn is blank, or defaults to 1 if RetryOn is set.
NumRetries int
// PerTryTimeout specifies the timeout per retry attempt.
// Ignored if RetryOn is blank.
PerTryTimeout time.Duration
}
// UpstreamValidation defines how to validate the certificate on the upstream service
type UpstreamValidation struct {
// CACertificate holds a reference to the Secret containing the CA to be used to
// verify the upstream connection.
CACertificate *Secret
// SubjectName holds an optional subject name which Envoy will check against the
// certificate presented by the upstream.
SubjectName string
}
func (r *Route) Visit(f func(Vertex)) {
for _, c := range r.Clusters {
f(c)
}
}
// A VirtualHost represents a named L4/L7 service.
type VirtualHost struct {
// Name is the fully qualified domain name of a network host,
// as defined by RFC 3986.
Name string
routes map[string]Vertex
// Service to TCP proxy all incoming connections.
*TCPProxy
}
func (v *VirtualHost) addRoute(route Vertex) {
if v.routes == nil {
v.routes = make(map[string]Vertex)
}
switch r := route.(type) {
case *PrefixRoute:
v.routes[r.Prefix] = r
case *RegexRoute:
v.routes[r.Regex] = r
default:
panic(fmt.Sprintf("unexpected route type: %T %#v", r, r))
}
}
func (v *VirtualHost) Visit(f func(Vertex)) {
for _, r := range v.routes {
f(r)
}
if v.TCPProxy != nil {
f(v.TCPProxy)
}
}
func (v *VirtualHost) Valid() bool {
// A VirtualHost is valid if it has at least one route,
// or tcp proxy is not nil.
return len(v.routes) > 0 || v.TCPProxy != nil
}
// A SecureVirtualHost represents a HTTP host protected by TLS.
type SecureVirtualHost struct {
VirtualHost
// TLS minimum protocol version. Defaults to auth.TlsParameters_TLS_AUTO
MinProtoVersion auth.TlsParameters_TlsProtocol
// The cert and key for this host.
*Secret
}
func (s *SecureVirtualHost) Visit(f func(Vertex)) {
s.VirtualHost.Visit(f)
if s.Secret != nil {
f(s.Secret) // secret is not required if vhost is using tls passthrough
}
}
func (s *SecureVirtualHost) Valid() bool {
// A SecureVirtualHost is valid if it has a Secret or a TCPProxy.
return s.Secret != nil || s.TCPProxy != nil
}
type Visitable interface {
Visit(func(Vertex))
}
type Vertex interface {
Visitable
}
type Service interface {
Vertex
toMeta() servicemeta
}
// A Listener represents a TCP socket that accepts
// incoming connections.
type Listener struct {
// Address is the TCP address to listen on.
// If blank 0.0.0.0, or ::/0 for IPv6, is assumed.
Address string
// Port is the TCP port to listen on.
Port int
VirtualHosts map[string]Vertex
}
func (l *Listener) Visit(f func(Vertex)) {
for _, vh := range l.VirtualHosts {
f(vh)
}
}
// TCPProxy represents a cluster of TCP endpoints.
type TCPProxy struct {
// Clusters is the, possibly weighted, set
// of upstream services to forward decrypted traffic.
Clusters []*Cluster
}
func (t *TCPProxy) Visit(f func(Vertex)) {
for _, s := range t.Clusters {
f(s)
}
}
// TCPService represents a Kuberentes Service that speaks TCP. That's all we know.
type TCPService struct {
Name, Namespace string
*v1.ServicePort
// Circuit breaking limits
// Max connections is maximum number of connections
// that Envoy will make to the upstream cluster.
MaxConnections int
// MaxPendingRequests is maximum number of pending
// requests that Envoy will allow to the upstream cluster.
MaxPendingRequests int
// MaxRequests is the maximum number of parallel requests that
// Envoy will make to the upstream cluster.
MaxRequests int
// MaxRetries is the maximum number of parallel retries that
// Envoy will allow to the upstream cluster.
MaxRetries int
// ExternalName is an optional field referencing a dns entry for Service type "ExternalName"
ExternalName string
}
type servicemeta struct {
name string
namespace string
port int32
}
func (s *TCPService) toMeta() servicemeta {
return servicemeta{
name: s.Name,
namespace: s.Namespace,
port: s.Port,
}
}
func (s *TCPService) Visit(func(Vertex)) {
// TCPServices are leaves in the DAG.
}
// Cluster holds the connetion specific parameters that apply to
// traffic routed to an upstream service.
type Cluster struct {
// Upstream is the backend Kubernetes service traffic arriving
// at this Cluster will be forwarded too.
Upstream Service
// The relative weight of this Cluster compared to its siblings.
Weight int
// UpstreamValidation defines how to verify the backend service's certificate
UpstreamValidation *UpstreamValidation
// The load balancer type to use when picking a host in the cluster.
// See https://www.envoyproxy.io/docs/envoy/latest/api-v2/api/v2/cds.proto#envoy-api-enum-cluster-lbpolicy
LoadBalancerStrategy string
HealthCheck *ingressroutev1.HealthCheck
}
func (c Cluster) Visit(f func(Vertex)) {
f(c.Upstream)
}
// HTTPService represents a Kuberneres Service object which speaks
// HTTP/1.1 or HTTP/2.0.
type HTTPService struct {
TCPService
// Protocol is the layer 7 protocol of this service
// One of "", "h2", "h2c", or "tls".
Protocol string
}
// Secret represents a K8s Secret for TLS usage as a DAG Vertex. A Secret is
// a leaf in the DAG.
type Secret struct {
Object *v1.Secret
}
func (s *Secret) Name() string { return s.Object.Name }
func (s *Secret) Namespace() string { return s.Object.Namespace }
func (s *Secret) Visit(func(Vertex)) {}
// Data returns the contents of the backing secret's map.
func (s *Secret) Data() map[string][]byte {
return s.Object.Data
}
// Cert returns the secret's tls certificate
func (s *Secret) Cert() []byte {
return s.Object.Data[v1.TLSCertKey]
}
// PrivateKey returns the secret's tls private key
func (s *Secret) PrivateKey() []byte {
return s.Object.Data[v1.TLSPrivateKeyKey]
}
func (s *Secret) toMeta() Meta {
return Meta{
name: s.Name(),
namespace: s.Namespace(),
}
}