/
ingress_translator.go
355 lines (303 loc) · 9.1 KB
/
ingress_translator.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
package envoy
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"fmt"
"regexp"
"sort"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/uswitch/yggdrasil/pkg/k8s"
v1 "k8s.io/api/core/v1"
)
func sortCluster(clusters []*cluster) {
sort.Slice(clusters, func(i int, j int) bool {
return clusters[i].identity() < clusters[j].identity()
})
}
func ClustersEquals(a, b []*cluster) bool {
if len(a) != len(b) {
return false
}
sortCluster(a)
sortCluster(b)
for idx, cluster := range a {
if !cluster.Equals(b[idx]) {
return false
}
}
return true
}
func sortVirtualHosts(hosts []*virtualHost) {
sort.Slice(hosts, func(i int, j int) bool {
return hosts[i].Host < hosts[j].Host
})
}
func VirtualHostsEquals(a, b []*virtualHost) bool {
if len(a) != len(b) {
return false
}
sortVirtualHosts(a)
sortVirtualHosts(b)
for idx, hosts := range a {
if !hosts.Equals(b[idx]) {
return false
}
}
return true
}
type envoyConfiguration struct {
VirtualHosts []*virtualHost
Clusters []*cluster
}
type virtualHost struct {
Host string
UpstreamCluster string
Timeout time.Duration
PerTryTimeout time.Duration
TlsKey string
TlsCert string
RetryOn string
}
func (v *virtualHost) Equals(other *virtualHost) bool {
if other == nil {
return false
}
return v.Host == other.Host &&
v.Timeout == other.Timeout &&
v.UpstreamCluster == other.UpstreamCluster &&
v.PerTryTimeout == other.PerTryTimeout &&
v.TlsKey == other.TlsKey &&
v.TlsCert == other.TlsCert &&
v.RetryOn == other.RetryOn
}
type cluster struct {
Name string
VirtualHost string
HealthCheckPath string
Timeout time.Duration
Hosts []string
}
func (c *cluster) identity() string {
return c.Name
}
func (c *cluster) Equals(other *cluster) bool {
if other == nil {
return false
}
if c.Name != other.Name {
return false
}
if c.Timeout != other.Timeout {
return false
}
if c.VirtualHost != other.VirtualHost {
return false
}
if c.HealthCheckPath != other.HealthCheckPath {
return false
}
if len(c.Hosts) != len(other.Hosts) {
return false
}
sort.Strings(c.Hosts)
sort.Strings(other.Hosts)
for i, host := range c.Hosts {
if host != other.Hosts[i] {
return false
}
}
return true
}
func (cfg *envoyConfiguration) equals(oldCfg *envoyConfiguration) (vmatch bool, cmatch bool) {
if oldCfg == nil {
return false, false
}
return VirtualHostsEquals(cfg.VirtualHosts, oldCfg.VirtualHosts), ClustersEquals(cfg.Clusters, oldCfg.Clusters)
}
func classFilter(ingresses []*k8s.Ingress, ingressClass []string) (is []*k8s.Ingress) {
for _, i := range ingresses {
for _, class := range ingressClass {
if i.Annotations["kubernetes.io/ingress.class"] == class ||
(i.Class != nil && *i.Class == class) {
is = append(is, i)
}
}
}
matchingIngresses.Set(float64(len(is)))
return is
}
func validIngressFilter(ingresses []*k8s.Ingress) (vi []*k8s.Ingress) {
Ingress:
for _, i := range ingresses {
for _, u := range i.Upstreams {
if u != "" {
for _, h := range i.RulesHosts {
if h != "" {
vi = append(vi, i)
continue Ingress
}
}
logrus.Debugf("no host found in ingress config for: %+v in namespace: %+v", i.Name, i.Namespace)
continue Ingress
}
}
logrus.Debugf("no hostname or ip for loadbalancer found in ingress config for: %+v in namespace: %+v", i.Name, i.Namespace)
}
return vi
}
type envoyIngress struct {
vhost *virtualHost
cluster *cluster
}
func newEnvoyIngress(host string) *envoyIngress {
clusterName := strings.Replace(host, ".", "_", -1)
return &envoyIngress{
vhost: &virtualHost{
Host: host,
UpstreamCluster: clusterName,
Timeout: (15 * time.Second),
PerTryTimeout: (5 * time.Second),
},
cluster: &cluster{
Name: clusterName,
VirtualHost: host,
Hosts: []string{},
Timeout: (30 * time.Second),
HealthCheckPath: "",
},
}
}
func (ing *envoyIngress) addUpstream(host string) {
ing.cluster.Hosts = append(ing.cluster.Hosts, host)
}
func (ing *envoyIngress) addHealthCheckPath(path string) {
ing.cluster.HealthCheckPath = path
}
func (ing *envoyIngress) addTimeout(timeout time.Duration) {
ing.cluster.Timeout = timeout
ing.vhost.Timeout = timeout
ing.vhost.PerTryTimeout = timeout
}
// hostMatch returns true if tlsHost and ruleHost match, with wildcard support
//
// *.a.b ruleHost accepts tlsHost *.a.b but not a.a.b or a.b or a.a.a.b
// a.a.b ruleHost accepts tlsHost a.a.b and *.a.b but not *.a.a.b
func hostMatch(ruleHost, tlsHost string) bool {
// TODO maybe cache the results for speedup
pattern := strings.ReplaceAll(strings.ReplaceAll(tlsHost, ".", "\\."), "*", "(?:\\*|[a-z0-9][a-z0-9-_]*)")
matched, err := regexp.MatchString("^"+pattern+"$", ruleHost)
if err != nil {
logrus.Errorf("error in ingress hostname comparison: %s", err.Error())
return false
}
return matched
}
// getHostTlsSecret returns the tls secret configured for a given ingress host
func getHostTlsSecret(ingress *k8s.Ingress, host string, secrets []*v1.Secret) (*v1.Secret, error) {
for _, tls := range ingress.TLS {
// TODO prefer a.a.b tls secret over *.a.b for host a.a.b when both are configured
if hostMatch(host, tls.Host) {
for _, secret := range secrets {
if secret.Namespace == ingress.Namespace &&
secret.Name == tls.SecretName {
return secret, nil
}
}
return nil, fmt.Errorf("secret %s/%s not found for host '%s'", ingress.Namespace, tls.SecretName, host)
}
}
return nil, fmt.Errorf("ingress %s/%s - %s has no tls secret configured", ingress.Namespace, ingress.Name, host)
}
// validateTlsSecret checks that the given secret holds valid tls certificate and key
func validateTlsSecret(secret *v1.Secret) (bool, error) {
tlsCert, certOk := secret.Data["tls.crt"]
tlsKey, keyOk := secret.Data["tls.key"]
if !certOk || !keyOk {
logrus.Infof("skipping certificate %s/%s: missing 'tls.crt' or 'tls.key'", secret.Namespace, secret.Name)
return false, nil
}
if len(tlsCert) == 0 || len(tlsKey) == 0 {
logrus.Infof("skipping certificate %s/%s: empty 'tls.crt' or 'tls.key'", secret.Namespace, secret.Name)
return false, nil
}
// discard P-384 EC private keys
// see https://github.com/envoyproxy/envoy/issues/10855
block, _ := pem.Decode(tlsCert)
if block == nil {
return false, fmt.Errorf("error parsing x509 certificate - no PEM block found")
}
x509crt, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return false, fmt.Errorf("error parsing x509 certificate: %s", err.Error())
}
if x509crt.PublicKeyAlgorithm == x509.ECDSA {
ecdsaPub, ok := x509crt.PublicKey.(*ecdsa.PublicKey)
if !ok {
return false, fmt.Errorf("error in *ecdsa.PublicKey type assertion")
}
if ecdsaPub.Curve.Params().BitSize > 256 {
logrus.Infof("skipping ECDSA %s certificate %s/%s: only P-256 certificates are supported", ecdsaPub.Curve.Params().Name, secret.Namespace, secret.Name)
return false, nil
}
}
return true, nil
}
func (envoyIng *envoyIngress) addRetryOn(ingress *k8s.Ingress) {
if ingress.Annotations["yggdrasil.uswitch.com/retry-on"] != "" {
retryOn := ingress.Annotations["yggdrasil.uswitch.com/retry-on"]
if !ValidateEnvoyRetryOn(retryOn) {
logrus.Warnf("invalid retry-on parameter for ingress %s/%s: %s", ingress.Namespace, ingress.Name, retryOn)
return
}
envoyIng.vhost.RetryOn = retryOn
}
}
func translateIngresses(ingresses []*k8s.Ingress, syncSecrets bool, secrets []*v1.Secret) *envoyConfiguration {
cfg := &envoyConfiguration{}
envoyIngresses := map[string]*envoyIngress{}
for _, i := range ingresses {
for _, j := range i.Upstreams {
for _, ruleHost := range i.RulesHosts {
_, ok := envoyIngresses[ruleHost]
if !ok {
envoyIngresses[ruleHost] = newEnvoyIngress(ruleHost)
}
envoyIngress := envoyIngresses[ruleHost]
envoyIngress.addUpstream(j)
if i.Annotations["yggdrasil.uswitch.com/healthcheck-path"] != "" {
envoyIngress.addHealthCheckPath(i.Annotations["yggdrasil.uswitch.com/healthcheck-path"])
}
if i.Annotations["yggdrasil.uswitch.com/timeout"] != "" {
timeout, err := time.ParseDuration(i.Annotations["yggdrasil.uswitch.com/timeout"])
if err == nil {
envoyIngress.addTimeout(timeout)
}
}
envoyIngress.addRetryOn(i)
if syncSecrets && envoyIngress.vhost.TlsKey == "" && envoyIngress.vhost.TlsCert == "" {
if hostTlsSecret, err := getHostTlsSecret(i, ruleHost, secrets); err != nil {
logrus.Infof(err.Error())
} else {
valid, err := validateTlsSecret(hostTlsSecret)
if err != nil {
logrus.Warnf("secret %s/%s is not valid: %s", hostTlsSecret.Namespace, hostTlsSecret.Name, err.Error())
} else if valid {
envoyIngress.vhost.TlsKey = string(hostTlsSecret.Data["tls.key"])
envoyIngress.vhost.TlsCert = string(hostTlsSecret.Data["tls.crt"])
}
}
}
}
}
}
for _, ingress := range envoyIngresses {
cfg.Clusters = append(cfg.Clusters, ingress.cluster)
cfg.VirtualHosts = append(cfg.VirtualHosts, ingress.vhost)
}
numVhosts.Set(float64(len(cfg.VirtualHosts)))
numClusters.Set(float64(len(cfg.Clusters)))
return cfg
}