-
Notifications
You must be signed in to change notification settings - Fork 131
/
manager_route_config.go
506 lines (412 loc) · 17.7 KB
/
manager_route_config.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Copyright (c) 2024 Tigera, Inc. All rights reserved.
// 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 manager
import (
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
operatorv1 "github.com/tigera/operator/api/v1"
rmeta "github.com/tigera/operator/pkg/render/common/meta"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
voltronRoutesConfigMapName = "voltron-routes"
voltronRoutesFolderPath = "/config_maps/voltron-routes"
configMapFolder = "/config_maps"
secretsFolder = "/secrets"
uiTLSTerminatedRoutesKey = "uiTLSTermRoutes.json"
upstreamTunnelTLSTerminatedRoutesKey = "upTunTLSTermRoutes.json"
upstreamTunnelTLSPassThroughRoutesKey = "upTunTLSPTRoutes.json"
routesAnnotationPrefix = "hash.operator.tigera.io/routeconf"
)
// tlsTerminatedRoute is need for the json translation from the TLSTerminatedRoute CR to the json voltron expects to
// see for a route.
type tlsTerminatedRoute struct {
// Destination is the destination URL
Destination string `json:"destination"`
// Path is the path portion of the URL based on which we proxy
Path string `json:"path"`
// CABundlePath is where we read the CA bundle from to authenticate the
// destination (if non-empty)
CABundlePath string `json:"caBundlePath,omitempty"`
// PathRegexp, if not nil, checks if Regexp matches the path
PathRegexp *string `json:"pathRegexp,omitempty"`
// PathReplace if not nil will be used to replace PathRegexp matches
PathReplace *string `json:"pathReplace,omitempty"`
// ClientCertPath and ClientKeyPath can be set for mTLS on the connection
// from Voltron to the destination.
ClientCertPath string `json:"clientCertPath,omitempty"`
ClientKeyPath string `json:"clientKeyPath,omitempty"`
Unauthenticated bool `json:"unauthenticated,omitempty"`
}
type tlsTerminatedRouteList []*tlsTerminatedRoute
func (r tlsTerminatedRouteList) Len() int {
return len(r)
}
func (r tlsTerminatedRouteList) Less(i, j int) bool {
return r[i].Path < r[j].Path
}
func (r tlsTerminatedRouteList) Swap(i, j int) {
swap(r, i, j)
}
// tlsPassThroughRoute is need for the json translation from the TLSPassThroughRoute CR to the json voltron expects to
// see for a route.
type tlsPassThroughRoute struct {
// Destination is the destination URL
Destination string `json:"destination"`
// ServerName
ServerName string `json:"serverName"`
}
type tlsPassThroughRouteList []*tlsPassThroughRoute
func (r tlsPassThroughRouteList) Len() int {
return len(r)
}
func (r tlsPassThroughRouteList) Less(i, j int) bool {
return (r)[i].ServerName < (r)[j].ServerName
}
func (r tlsPassThroughRouteList) Swap(i, j int) {
swap(r, i, j)
}
func swap[R any](list []R, i, j int) {
tmp := list[i]
list[i] = list[j]
list[j] = tmp
}
// VoltronRouteConfigBuilder is an interface that provides methods to build a VoltronRouteConfig.
// Implementations of this interface should provide methods to add different types of routes,
// such as TLSTerminatedRoute and TLSPassThroughRoute, as well as methods to add ConfigMaps
// and Secrets to the route configuration to generate annotations that change when the contents of
// objects change (used to restart Voltron). The Build method should be used to create the final
// VoltronRouteConfig object.
type VoltronRouteConfigBuilder interface {
// AddTLSTerminatedRoute adds TLSTerminatedRoutes to the config builder. When Build is called, the route is parsed
// and validated and the route is added to the ConfigMap mounted to voltron to configure the tls terminated routes.
//
// If CAs or MTLS certs / keys are referenced in the spec the Config Maps and Secrets containing
// those values must be added through AddConfigMap or AddSecret. This is so we can track when these values change.
AddTLSTerminatedRoute(routes operatorv1.TLSTerminatedRoute)
// AddTLSPassThroughRoute adds AddTLSPassThroughRoutes to the config builder. When Build is called, the route is parsed
// and validated and the route is added to the ConfigMap mounted to voltron to configure the tls pass through routes.
AddTLSPassThroughRoute(routes operatorv1.TLSPassThroughRoute)
// AddConfigMap accepts a Config Map referenced by a TLS terminated route. This is used to detect changes to ConfigMaps
// that will be mounted by the VoltronRouteConfig so Voltron can be restarted if the value changes.
AddConfigMap(configMap *corev1.ConfigMap)
// AddSecret accepts a Secret referenced by a TLS terminated route. This is used to detect changes to Secrets
// that will be mounted by the VoltronRouteConfig so Voltron can be restarted if the value changes.
AddSecret(secret *corev1.Secret)
Build() (*VoltronRouteConfig, error)
}
type voltronRouteConfigBuilder struct {
mountedConfigMaps map[string]struct{}
mountedSecrets map[string]struct{}
volumeMounts []corev1.VolumeMount
volumes []corev1.Volume
toAnnotate map[string]string
configMaps map[string]*corev1.ConfigMap
secrets map[string]*corev1.Secret
tlsTerminatedRoutes []operatorv1.TLSTerminatedRoute
tlsPassThroughRoutes []operatorv1.TLSPassThroughRoute
}
func NewVoltronRouteConfigBuilder() VoltronRouteConfigBuilder {
return &voltronRouteConfigBuilder{
configMaps: map[string]*corev1.ConfigMap{},
secrets: map[string]*corev1.Secret{},
mountedConfigMaps: map[string]struct{}{},
mountedSecrets: map[string]struct{}{},
toAnnotate: map[string]string{},
}
}
func (builder *voltronRouteConfigBuilder) AddConfigMap(configMap *corev1.ConfigMap) {
if _, ok := builder.configMaps[configMap.Name]; !ok {
builder.configMaps[configMap.Name] = configMap
}
}
func (builder *voltronRouteConfigBuilder) AddSecret(secret *corev1.Secret) {
if _, ok := builder.secrets[secret.Name]; !ok {
builder.secrets[secret.Name] = secret
}
}
func (builder *voltronRouteConfigBuilder) AddTLSTerminatedRoute(route operatorv1.TLSTerminatedRoute) {
builder.tlsTerminatedRoutes = append(builder.tlsTerminatedRoutes, route)
}
func (builder *voltronRouteConfigBuilder) AddTLSPassThroughRoute(route operatorv1.TLSPassThroughRoute) {
builder.tlsPassThroughRoutes = append(builder.tlsPassThroughRoutes, route)
}
func (builder *voltronRouteConfigBuilder) Build() (*VoltronRouteConfig, error) {
var uiTLSTerminatedRoutes tlsTerminatedRouteList
var tunnelTLSTerminatedRoutes tlsTerminatedRouteList
var tunnelTLSPassThroughRoutes tlsPassThroughRouteList
for _, route := range builder.tlsTerminatedRoutes {
if route.Spec.CABundle == nil {
return nil, fmt.Errorf("CABundle is required")
}
r := &tlsTerminatedRoute{
Destination: route.Spec.Destination,
Path: route.Spec.PathMatch.Path,
PathRegexp: route.Spec.PathMatch.PathRegexp,
PathReplace: route.Spec.PathMatch.PathReplace,
}
if route.Spec.Target == operatorv1.TargetTypeUI {
uiTLSTerminatedRoutes = append(uiTLSTerminatedRoutes, r)
if route.Spec.Unauthenticated != nil {
r.Unauthenticated = *route.Spec.Unauthenticated
}
} else if route.Spec.Target == operatorv1.TargetTypeUpstreamTunnel {
tunnelTLSTerminatedRoutes = append(tunnelTLSTerminatedRoutes, r)
} else {
return nil, fmt.Errorf("unknown Target value %s", route.Spec.Target)
}
if route.Spec.CABundle != nil {
path, err := builder.mountConfigMapReference(route.Spec.CABundle.Name, route.Spec.CABundle.Key)
if err != nil {
return nil, err
}
r.CABundlePath = path
}
// Require that either both MTLSCert and MTLSKey are set or neither are.
if (route.Spec.ForwardingMTLSCert != nil && route.Spec.ForwardingMTLSKey == nil) || (route.Spec.ForwardingMTLSKey != nil && route.Spec.ForwardingMTLSCert == nil) {
return nil, fmt.Errorf("must set both MTLSCert and MTLSKey, or neither for TLS terminated route %s", route.Name)
}
if route.Spec.ForwardingMTLSCert != nil {
path, err := builder.mountSecretReference(route.Spec.ForwardingMTLSCert.Name, route.Spec.ForwardingMTLSCert.Key)
if err != nil {
return nil, err
}
r.ClientCertPath = path
// At this point, if MTLSCert is set then MTLSKey will be set otherwise this if statement wouldn't be executed
// or an error would have already been returned.
path, err = builder.mountSecretReference(route.Spec.ForwardingMTLSKey.Name, route.Spec.ForwardingMTLSKey.Key)
if err != nil {
return nil, err
}
r.ClientKeyPath = path
}
}
for _, route := range builder.tlsPassThroughRoutes {
r := &tlsPassThroughRoute{
Destination: route.Spec.Destination,
ServerName: route.Spec.SNIMatch.ServerName,
}
tunnelTLSPassThroughRoutes = append(tunnelTLSPassThroughRoutes, r)
}
routesData := map[string]string{}
if len(uiTLSTerminatedRoutes) > 0 {
jsonBytes, err := marshalRouteList(uiTLSTerminatedRoutes)
if err != nil {
return nil, err
}
routesData[uiTLSTerminatedRoutesKey] = string(jsonBytes)
}
if len(tunnelTLSTerminatedRoutes) > 0 {
jsonBytes, err := marshalRouteList(tunnelTLSTerminatedRoutes)
if err != nil {
return nil, err
}
routesData[upstreamTunnelTLSTerminatedRoutesKey] = string(jsonBytes)
}
if len(tunnelTLSPassThroughRoutes) > 0 {
jsonBytes, err := marshalRouteList(tunnelTLSPassThroughRoutes)
if err != nil {
return nil, err
}
routesData[upstreamTunnelTLSPassThroughRoutesKey] = string(jsonBytes)
}
// Add the config map so mountConfigMap can add the annotation for the data.
builder.AddConfigMap(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: voltronRoutesConfigMapName,
},
Data: routesData,
})
if _, err := builder.mountConfigMapReference(voltronRoutesConfigMapName, ""); err != nil {
return nil, err
}
return &VoltronRouteConfig{
routesData: routesData,
volumeMounts: builder.volumeMounts,
volumes: builder.volumes,
annotations: builder.generateAnnotations(),
}, nil
}
func marshalRouteList[R sort.Interface](list R) ([]byte, error) {
sort.Sort(list)
jsonBytes, err := json.MarshalIndent(list, "", "\t")
if err != nil {
return nil, err
}
return jsonBytes, nil
}
func (builder *voltronRouteConfigBuilder) mountConfigMapReference(name, key string) (string, error) {
defaultMode := int32(420)
configMap := builder.configMaps[name]
if configMap == nil {
return "", fmt.Errorf("the contents for ConfigMap '%s' weren't provided, and are needed to generate annotations", name)
}
if _, ok := builder.mountedConfigMaps[name]; !ok {
// Prefix the volume name to avoid collisions with secrets that have the same name.
volumeName := fmt.Sprintf("cm-%s", name)
// Use a different folder for secrets and configmaps to avoid mounting collisions.
mountLocation := fmt.Sprintf("%s/%s", configMapFolder, name)
volumeMount := corev1.VolumeMount{Name: volumeName, MountPath: mountLocation, ReadOnly: true}
volume := corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: name},
DefaultMode: &defaultMode,
},
},
}
builder.volumeMounts = append(builder.volumeMounts, volumeMount)
builder.volumes = append(builder.volumes, volume)
var keys []string
for k := range configMap.Data {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
builder.addAnnotation(fmt.Sprintf("cm-%s-%s", configMap.Name, strings.ToLower(k)), configMap.Data[k])
}
builder.mountedConfigMaps[name] = struct{}{}
}
return fmt.Sprintf("%s/%s/%s", configMapFolder, name, key), nil
}
// Generate annotations takes the keys / values we need to add annotations for and ensures they're formatted correctly,
// i.e. are lower case and within the perceptible length. If the keys are too long, they are trimmed to 63 characters.
// If two trimmed keys result in a conflict, the last bytes of the conflicting key are replaced with an incremental number
// such that the length still remains 63 characters. This process continues, incrementing the prefix, until a non-conflicting
// key has been generated.
//
// The keys are sorted before we generate the annotations, ensuring that the conflict resolution logic is deterministic.
func (builder *voltronRouteConfigBuilder) generateAnnotations() map[string]string {
const maxAnnotationLen = 63
var keys []string
for key := range builder.toAnnotate {
keys = append(keys, key)
}
sort.Strings(keys)
annotations := map[string]string{}
for _, key := range keys {
formattedKey := fmt.Sprintf("%s-%s", routesAnnotationPrefix, strings.ToLower(key))
if len(formattedKey) > maxAnnotationLen {
formattedKey = formattedKey[0:maxAnnotationLen]
}
// Just in case there's a collision
for i := 1; ; i++ {
if _, ok := annotations[formattedKey]; !ok {
break
}
suffix := strconv.Itoa(i)
formattedKey = formattedKey[0:len(formattedKey)-len(suffix)] + suffix
}
annotations[formattedKey] = builder.toAnnotate[key]
}
return annotations
}
// addAnnotation adds the key and value to the annotation map. It ensures the key is lower case, and if it's length is
// greater than 55 characters it takes the first 55 characters and appends the first 6 characters of a hash of the key.
// This ensures that we're below the 63 character limit, but keys that have the same first 63 characters won't conflict
// with annotation keys.
func (builder *voltronRouteConfigBuilder) addAnnotation(key string, value string) {
builder.toAnnotate[key] = rmeta.AnnotationHash(value)
}
func (builder *voltronRouteConfigBuilder) mountSecretReference(name, key string) (string, error) {
defaultMode := int32(420)
secret := builder.secrets[name]
if secret == nil {
return "", fmt.Errorf("the contents for Secret '%s' weren't provided, and are needed to generate annotations", name)
}
if _, ok := builder.mountedSecrets[name]; !ok {
// Prefix the volume name to avoid collisions with secrets that have the same name.
volumeName := fmt.Sprintf("scrt-%s", name)
// Use a different folder for secrets and configmaps to avoid mounting collisions.
mountLocation := fmt.Sprintf("%s/%s", secretsFolder, name)
volumeMount := corev1.VolumeMount{Name: volumeName, MountPath: mountLocation, ReadOnly: true}
volume := corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: name,
DefaultMode: &defaultMode,
},
},
}
builder.volumeMounts = append(builder.volumeMounts, volumeMount)
builder.volumes = append(builder.volumes, volume)
var keys []string
for k := range secret.Data {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
builder.addAnnotation(fmt.Sprintf("s-%s-%s", secret.Name, k), string(secret.Data[k]))
}
builder.mountedSecrets[name] = struct{}{}
}
return fmt.Sprintf("%s/%s/%s", configMapFolder, name, key), nil
}
// VoltronRouteConfig contains everything needed to configure the voltron pod / container with routes via a mounted file.
// It contains the volumes and volume mounts needed to mount the config map with the routes, as well as the CA, certs,
// keys.
//
// It provides functions for getting an annotation based of the mounted config maps / secrets to detect changes, as well
// as the env variables needed to configure the file paths to tell voltron where to look for the routes.
type VoltronRouteConfig struct {
routesData map[string]string
volumeMounts []corev1.VolumeMount
volumes []corev1.Volume
annotations map[string]string
}
// Volumes returns the volumes that Voltron needs to be configured with (references to ConfigMaps and Secrets in the
// TLSTerminatedRoute CRs).
func (cfg *VoltronRouteConfig) Volumes() []corev1.Volume {
return cfg.volumes
}
// VolumeMounts returns the volume mounts that Voltron needs to be configured with (references to ConfigMaps and Secrets in the
// TLSTerminatedRoute CRs).
func (cfg *VoltronRouteConfig) VolumeMounts() []corev1.VolumeMount {
return cfg.volumeMounts
}
// RoutesConfigMap returns the config map the contains the routes that voltron is to be configured with. This has been
// parsed from the TLSTerminatedRoute and the TLSPassThroughRoute CRs.
//
// The namespace parameter is used to assign the namespace that the ConfigMap should be created in.
func (cfg *VoltronRouteConfig) RoutesConfigMap(namespace string) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: voltronRoutesConfigMapName,
Namespace: namespace,
},
Data: cfg.routesData,
}
}
// EnvVars returns a list of env vars that contain the paths to the route files that routes Config Map is mounted to.
func (cfg *VoltronRouteConfig) EnvVars() []corev1.EnvVar {
var envVars []corev1.EnvVar
if _, ok := cfg.routesData[uiTLSTerminatedRoutesKey]; ok {
envVars = append(envVars, corev1.EnvVar{Name: "VOLTRON_UI_TLS_TERMINATED_ROUTES_PATH", Value: fmt.Sprintf("%s/%s", voltronRoutesFolderPath, uiTLSTerminatedRoutesKey)})
}
if _, ok := cfg.routesData[upstreamTunnelTLSTerminatedRoutesKey]; ok {
envVars = append(envVars, corev1.EnvVar{Name: "VOLTRON_UPSTREAM_TUNNEL_TLS_TERMINATED_ROUTES_PATH", Value: fmt.Sprintf("%s/%s", voltronRoutesFolderPath, upstreamTunnelTLSTerminatedRoutesKey)})
}
if _, ok := cfg.routesData[upstreamTunnelTLSPassThroughRoutesKey]; ok {
envVars = append(envVars, corev1.EnvVar{Name: "VOLTRON_UPSTREAM_TUNNEL_TLS_PASS_THROUGH_ROUTES_PATH", Value: fmt.Sprintf("%s/%s", voltronRoutesFolderPath, upstreamTunnelTLSPassThroughRoutesKey)})
}
return envVars
}
func (cfg *VoltronRouteConfig) Annotations() map[string]string {
return cfg.annotations
}