forked from projectcontour/contour
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag.go
276 lines (221 loc) · 6.77 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
// 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 (
"time"
"k8s.io/api/core/v1"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
ingressroutev1 "github.com/heptio/contour/apis/contour/v1beta1"
)
// 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 []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() []Status {
return d.statuses
}
type Route struct {
Prefix string
object interface{} // one of Ingress or IngressRoute
httpServices map[servicemeta]*HTTPService
// 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
// 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
// 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
// Indicates that during forwarding, the matched prefix (or path) should be swapped with this value
PrefixRewrite string
}
func (r *Route) addHTTPService(s *HTTPService) {
if r.httpServices == nil {
r.httpServices = make(map[servicemeta]*HTTPService)
}
r.httpServices[s.toMeta()] = s
}
func (r *Route) Visit(f func(Vertex)) {
for _, c := range r.httpServices {
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]*Route
// Service to TCP proxy all incoming connections.
*TCPProxy
}
func (v *VirtualHost) addRoute(route *Route) {
if v.routes == nil {
v.routes = make(map[string]*Route)
}
v.routes[route.Prefix] = route
}
func (v *VirtualHost) Visit(f func(Vertex)) {
for _, r := range v.routes {
f(r)
}
if v.TCPProxy != nil {
f(v.TCPProxy)
}
}
// 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
}
}
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 {
// Services to proxy decrypted traffic to.
Services []*TCPService
}
func (t *TCPProxy) Visit(f func(Vertex)) {
for _, s := range t.Services {
f(s)
}
}
// TCPService represents a Kuberentes Service that speaks TCP. That's all we know.
type TCPService struct {
Name, Namespace string
*v1.ServicePort
Weight int
// 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
// 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
HealthCheck *ingressroutev1.HealthCheck
}
type servicemeta struct {
name string
namespace string
port int32
weight int
strategy string
healthcheck string // %#v of *ingressroutev1.HealthCheck
}
func (s *TCPService) toMeta() servicemeta {
return servicemeta{
name: s.Name,
namespace: s.Namespace,
port: s.Port,
weight: s.Weight,
strategy: s.LoadBalancerStrategy,
healthcheck: healthcheckToString(s.HealthCheck),
}
}
func (s *TCPService) Visit(func(Vertex)) {
// TCPServices are leaves in the DAG.
}
// 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", or "h2c".
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
}
func (s *Secret) toMeta() meta {
return meta{
name: s.Name(),
namespace: s.Namespace(),
}
}