-
Notifications
You must be signed in to change notification settings - Fork 155
/
resources.go
528 lines (474 loc) · 17.3 KB
/
resources.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
Copyright 2021 The Kubermatic Kubernetes Platform contributors.
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 mla
import (
"bytes"
"crypto/sha1"
"crypto/x509"
"encoding/json"
"fmt"
"net"
"strings"
"text/template"
"github.com/Masterminds/sprig/v3"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
"k8c.io/kubermatic/v2/pkg/controller/operator/common"
"k8c.io/kubermatic/v2/pkg/resources"
"k8c.io/kubermatic/v2/pkg/resources/certificates"
"k8c.io/kubermatic/v2/pkg/resources/nodeportproxy"
"k8c.io/kubermatic/v2/pkg/resources/registry"
"k8c.io/reconciler/pkg/reconciling"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
certutil "k8s.io/client-go/util/cert"
"k8s.io/utils/pointer"
)
const nginxConfig = `worker_processes 1;
error_log /dev/stderr;
pid /tmp/nginx.pid;
worker_rlimit_nofile 8192;
events {
worker_connections 1024;
}
http {
client_body_temp_path /tmp/client_temp;
proxy_temp_path /tmp/proxy_temp_path;
fastcgi_temp_path /tmp/fastcgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
scgi_temp_path /tmp/scgi_temp;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
resolver kube-dns.kube-system.svc.cluster.local;
{{ if .LokiWriteLimit }}
limit_req_zone $binary_remote_addr zone=loki_write_limit:1m rate={{ .LokiWriteLimit }}r/s;
{{ end }}
{{ if .LokiReadLimit }}
limit_req_zone $binary_remote_addr zone=loki_read_limit:1m rate={{ .LokiReadLimit }}r/s;
{{ end }}
{{ if .CortexReadLimit }}
limit_req_zone $binary_remote_addr zone=cortex_read_limit:1m rate={{ .CortexReadLimit }}r/s;
{{ end }}
# write path - exposed to user clusters
server {
listen 8080 ssl;
proxy_set_header X-Scope-OrgID {{ .TenantID }};
ssl_certificate {{ .SSLCertFile }};
ssl_certificate_key {{ .SSLKeyFile }};
ssl_verify_client on;
ssl_client_certificate {{ .SSLCACertFile }};
ssl_protocols TLSv1.3;
# Loki
location = /loki/api/v1/push {
{{ if .LokiWriteLimit }}
limit_req zone=loki_write_limit{{ if .LokiWriteLimitBurst }} burst={{ .LokiWriteLimitBurst }} nodelay{{ end }};
{{ end }}
proxy_pass http://loki-distributed-distributor.{{ .Namespace }}.svc.cluster.local:3100$request_uri;
}
# Cortex
location = /api/v1/push {
proxy_pass http://cortex-distributor.{{ .Namespace }}.svc.cluster.local:8080$request_uri;
}
}
# read path - cluster-local access only
server {
listen 8081;
proxy_set_header X-Scope-OrgID {{ .TenantID }};
# k8s probes
location = / {
return 200 'OK';
auth_basic off;
}
# Alertmanager Alerts
location ~ /api/prom/alertmanager/.* {
proxy_pass http://cortex-alertmanager.{{ .Namespace }}.svc.cluster.local:8080$request_uri;
}
# Alertmanager Config
location = /api/prom/api/v1/alerts {
proxy_pass http://cortex-alertmanager.{{ .Namespace }}.svc.cluster.local:8080/api/v1/alerts;
}
# Loki
location ~ /loki/api/.* {
{{ if .LokiReadLimit }}
limit_req zone=loki_read_limit{{ if .LokiReadLimitBurst }} burst={{ .LokiReadLimitBurst }} nodelay{{ end }};
{{ end }}
proxy_pass http://loki-distributed-query-frontend.{{ .Namespace }}.svc.cluster.local:3100$request_uri;
}
# Loki Ruler
location ~ /prometheus/.* {
proxy_pass http://loki-distributed-ruler.{{ .Namespace }}.svc.cluster.local:3100$request_uri;
}
# Cortex
location ~ /api/prom/.* {
{{ if .CortexReadLimit }}
limit_req zone=cortex_read_limit{{ if .CortexReadLimitBurst }} burst={{ .CortexReadLimitBurst }} nodelay{{ end }};
{{ end }}
proxy_pass http://cortex-query-frontend.{{ .Namespace }}.svc.cluster.local:8080$request_uri;
}
# Cortex Ruler, alternative for /api/v1/rules which is also used by loki)
location = /api/prom/api/v1/rules {
proxy_pass http://cortex-ruler.{{ .Namespace }}.svc.cluster.local:8080/prometheus/api/v1/rules;
}
}
}
`
const (
gatewayName = "mla-gateway"
gatewayExternalName = resources.MLAGatewayExternalServiceName
extPortName = "http-ext"
intPortName = "http-int"
configHashAnnotation = "mla.k8c.io/config-hash"
configVolumeName = "config"
configVolumePath = "/etc/nginx"
certificatesVolumeName = "gw-certificates"
certificatesVolumePath = "/etc/ssl/mla-gateway"
caCertificatesVolumeName = "ca-certificates"
caCertificatesVolumePath = "/etc/ssl/mla-gateway-ca"
)
type configTemplateData struct {
Namespace string
TenantID string
SSLCertFile string
SSLKeyFile string
SSLCACertFile string
CortexReadLimit int32
CortexReadLimitBurst int32
LokiWriteLimit int32
LokiWriteLimitBurst int32
LokiReadLimit int32
LokiReadLimitBurst int32
}
func renderTemplate(tpl string, data interface{}) (string, error) {
t, err := template.New("base").Funcs(sprig.TxtFuncMap()).Parse(tpl)
if err != nil {
return "", fmt.Errorf("failed to parse as Go template: %w", err)
}
output := bytes.Buffer{}
if err := t.Execute(&output, data); err != nil {
return "", fmt.Errorf("failed to render template: %w", err)
}
return strings.TrimSpace(output.String()), nil
}
func GatewayConfigMapReconciler(c *kubermaticv1.Cluster, mlaNamespace string, s *kubermaticv1.MLAAdminSetting) reconciling.NamedConfigMapReconcilerFactory {
return func() (string, reconciling.ConfigMapReconciler) {
return gatewayName, func(cm *corev1.ConfigMap) (*corev1.ConfigMap, error) {
if cm.Data == nil {
cm.Data = map[string]string{}
}
configData := configTemplateData{
Namespace: mlaNamespace,
TenantID: c.Name,
SSLCertFile: fmt.Sprintf("%s/%s", certificatesVolumePath, resources.MLAGatewayCertSecretKey),
SSLKeyFile: fmt.Sprintf("%s/%s", certificatesVolumePath, resources.MLAGatewayKeySecretKey),
SSLCACertFile: fmt.Sprintf("%s/%s", caCertificatesVolumePath, resources.MLAGatewayCACertKey),
}
if s != nil && s.Spec.MonitoringRateLimits != nil {
// NOTE: Cortex write path rate-limiting is implemented directly by Cortex configuration
configData.CortexReadLimit = s.Spec.MonitoringRateLimits.QueryRate
configData.CortexReadLimitBurst = s.Spec.MonitoringRateLimits.QueryBurstSize
}
if s != nil && s.Spec.LoggingRateLimits != nil {
configData.LokiWriteLimit = s.Spec.LoggingRateLimits.IngestionRate
configData.LokiWriteLimitBurst = s.Spec.LoggingRateLimits.IngestionBurstSize
configData.LokiReadLimit = s.Spec.LoggingRateLimits.QueryRate
configData.LokiReadLimitBurst = s.Spec.LoggingRateLimits.QueryBurstSize
}
config, err := renderTemplate(nginxConfig, configData)
if err != nil {
return nil, fmt.Errorf("failed to render Prometheus config: %w", err)
}
cm.Data["nginx.conf"] = config
return cm, nil
}
}
}
func GatewayInternalServiceReconciler() reconciling.NamedServiceReconcilerFactory {
return func() (string, reconciling.ServiceReconciler) {
return gatewayName, func(s *corev1.Service) (*corev1.Service, error) {
s.Spec.Type = corev1.ServiceTypeClusterIP
s.Spec.Selector = map[string]string{common.NameLabel: gatewayName}
if len(s.Spec.Ports) == 0 {
s.Spec.Ports = make([]corev1.ServicePort, 1)
}
s.Spec.Ports[0].Name = intPortName
s.Spec.Ports[0].Protocol = corev1.ProtocolTCP
s.Spec.Ports[0].Port = 80
s.Spec.Ports[0].TargetPort = intstr.FromString(intPortName)
return s, nil
}
}
}
func GatewayExternalServiceReconciler(c *kubermaticv1.Cluster) reconciling.NamedServiceReconcilerFactory {
return func() (string, reconciling.ServiceReconciler) {
return gatewayExternalName, func(s *corev1.Service) (*corev1.Service, error) {
if s.Annotations == nil {
s.Annotations = map[string]string{}
}
s.Spec.Selector = map[string]string{common.NameLabel: gatewayName}
switch c.Spec.ExposeStrategy {
case kubermaticv1.ExposeStrategyNodePort:
// Exposes MLA GW via NodePort.
s.Spec.Type = corev1.ServiceTypeNodePort
s.Annotations[nodeportproxy.DefaultExposeAnnotationKey] = nodeportproxy.NodePortType.String()
delete(s.Annotations, nodeportproxy.NodePortProxyExposeNamespacedAnnotationKey)
case kubermaticv1.ExposeStrategyLoadBalancer:
// When using exposeStrategy==LoadBalancer, only one LB service is used to expose multiple user cluster
// -related services (APIServer, OpenVPN, MLAGw). NodePortProxy in namespaced mode is used to redirect
// the traffic to the right service.
s.Spec.Type = corev1.ServiceTypeNodePort
s.Annotations[nodeportproxy.NodePortProxyExposeNamespacedAnnotationKey] = "true"
delete(s.Annotations, nodeportproxy.DefaultExposeAnnotationKey)
case kubermaticv1.ExposeStrategyTunneling:
// Exposes MLA GW via SNI.
s.Spec.Type = corev1.ServiceTypeClusterIP
s.Annotations[nodeportproxy.DefaultExposeAnnotationKey] = nodeportproxy.SNIType.String()
// Maps SNI host with the port name of this service.
s.Annotations[nodeportproxy.PortHostMappingAnnotationKey] =
fmt.Sprintf(`{%q: %q}`, extPortName, resources.MLAGatewaySNIPrefix+c.Status.Address.ExternalName)
delete(s.Annotations, nodeportproxy.NodePortProxyExposeNamespacedAnnotationKey)
default:
return nil, fmt.Errorf("unsupported expose strategy: %q", c.Spec.ExposeStrategy)
}
if len(s.Spec.Ports) == 0 {
s.Spec.Ports = make([]corev1.ServicePort, 1)
}
s.Spec.Ports[0].Name = extPortName
s.Spec.Ports[0].Protocol = corev1.ProtocolTCP
s.Spec.Ports[0].Port = 80
s.Spec.Ports[0].TargetPort = intstr.FromString(extPortName)
if c.Spec.ExposeStrategy == kubermaticv1.ExposeStrategyTunneling {
s.Spec.Ports[0].NodePort = 0 // allows switching from other expose strategies
}
return s, nil
}
}
}
const (
image = "nginxinc/nginx-unprivileged"
version = "1.20.1-alpine"
)
func GatewayDeploymentReconciler(data *resources.TemplateData, settings *kubermaticv1.MLAAdminSetting) reconciling.NamedDeploymentReconcilerFactory {
return func() (string, reconciling.DeploymentReconciler) {
return gatewayName, func(d *appsv1.Deployment) (*appsv1.Deployment, error) {
d.Spec.Replicas = pointer.Int32(1)
d.Spec.Selector = &metav1.LabelSelector{
MatchLabels: map[string]string{
common.NameLabel: gatewayName,
},
}
d.Spec.Template.Spec.ImagePullSecrets = []corev1.LocalObjectReference{{Name: resources.ImagePullSecretName}}
d.Spec.Template.Spec.SecurityContext = &corev1.PodSecurityContext{
FSGroup: pointer.Int64(1001),
RunAsGroup: pointer.Int64(2001),
RunAsUser: pointer.Int64(1001),
RunAsNonRoot: pointer.Bool(true),
}
d.Spec.Template.Labels = d.Spec.Selector.MatchLabels
// hash for the annotation used to force deployment rollout upon configuration change
configHash := sha1.New()
configData, err := json.Marshal(settings)
if err != nil {
return nil, fmt.Errorf("failed to encode MLAAdminSetting: %w", err)
}
configHash.Write(configData)
d.Spec.Template.Annotations = map[string]string{
configHashAnnotation: fmt.Sprintf("%x", configHash.Sum(nil)),
}
d.Spec.Template.Spec.Containers = []corev1.Container{
{
Name: "nginx",
Image: registry.Must(data.RewriteImage(resources.RegistryDocker + "/" + image + ":" + version)),
ImagePullPolicy: corev1.PullIfNotPresent,
Ports: []corev1.ContainerPort{
{
Name: extPortName,
ContainerPort: 8080,
Protocol: corev1.ProtocolTCP,
},
{
Name: intPortName,
ContainerPort: 8081,
Protocol: corev1.ProtocolTCP,
},
},
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/",
Port: intstr.FromString(intPortName),
Scheme: corev1.URISchemeHTTP,
},
},
InitialDelaySeconds: 15,
TimeoutSeconds: 1,
PeriodSeconds: 10,
SuccessThreshold: 1,
FailureThreshold: 3,
},
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
ReadOnlyRootFilesystem: pointer.Bool(true),
AllowPrivilegeEscalation: pointer.Bool(false),
},
VolumeMounts: []corev1.VolumeMount{
{
Name: configVolumeName,
MountPath: configVolumePath,
},
{
Name: certificatesVolumeName,
MountPath: certificatesVolumePath,
},
{
Name: caCertificatesVolumeName,
MountPath: caCertificatesVolumePath,
},
{
Name: "tmp",
MountPath: "/tmp",
},
{
Name: "docker-entrypoint-d-override",
MountPath: "/docker-entrypoint.d",
},
},
},
}
d.Spec.Template.Spec.Volumes = []corev1.Volume{
{
Name: configVolumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: gatewayName,
},
},
},
},
{
Name: certificatesVolumeName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: resources.MLAGatewayCertificatesSecretName,
DefaultMode: pointer.Int32(0400),
},
},
},
{
Name: caCertificatesVolumeName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: resources.MLAGatewayCASecretName,
Items: []corev1.KeyToPath{
{
Key: resources.MLAGatewayCACertKey,
Path: resources.MLAGatewayCACertKey,
},
},
DefaultMode: pointer.Int32(0400),
},
},
},
{
Name: "tmp",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
{
Name: "docker-entrypoint-d-override",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
},
}
return d, nil
}
}
}
// GatewayCAReconciler returns a function to create the ECDSA-based CA to be used for MLA Gateway.
func GatewayCAReconciler() reconciling.NamedSecretReconcilerFactory {
return func() (string, reconciling.SecretReconciler) {
return resources.MLAGatewayCASecretName, func(se *corev1.Secret) (*corev1.Secret, error) {
if se.Data == nil {
se.Data = map[string][]byte{}
}
if data, exists := se.Data[resources.MLAGatewayCACertKey]; exists {
certs, err := certutil.ParseCertsPEM(data)
if err != nil {
return nil, fmt.Errorf("failed to parse certificate %s from existing secret %s: %w",
resources.MLAGatewayCACertKey, resources.MLAGatewayCASecretName, err)
}
if !resources.CertWillExpireSoon(certs[0]) {
return se, nil
}
}
cert, key, err := certificates.GetECDSACACertAndKey()
if err != nil {
return nil, fmt.Errorf("failed to generate MLA CA: %w", err)
}
se.Data[resources.MLAGatewayCACertKey] = cert
se.Data[resources.MLAGatewayCAKeyKey] = key
return se, nil
}
}
}
// GatewayCertificateReconciler returns a function to create/update a secret with the MLA gateway TLS certificate.
func GatewayCertificateReconciler(c *kubermaticv1.Cluster, mlaGatewayCAGetter func() (*resources.ECDSAKeyPair, error)) reconciling.NamedSecretReconcilerFactory {
return func() (string, reconciling.SecretReconciler) {
return resources.MLAGatewayCertificatesSecretName, func(se *corev1.Secret) (*corev1.Secret, error) {
if se.Data == nil {
se.Data = map[string][]byte{}
}
ca, err := mlaGatewayCAGetter()
if err != nil {
return nil, fmt.Errorf("failed to get MLA Gateway ca: %w", err)
}
address := c.Status.Address
if address.ExternalName == "" {
return nil, fmt.Errorf("unable to issue MLA Gateway certificate: cluster ExternalName is empty")
}
commonName := resources.MLAGatewaySNIPrefix + address.ExternalName
altNames := certutil.AltNames{
DNSNames: []string{
commonName,
address.ExternalName, // required for NodePort expose strategy
},
}
cIP := net.ParseIP(address.IP)
if cIP != nil {
altNames.IPs = []net.IP{cIP} // required for LoadBalancer expose strategy
}
if b, exists := se.Data[resources.MLAGatewayCertSecretKey]; exists {
certs, err := certutil.ParseCertsPEM(b)
if err != nil {
return nil, fmt.Errorf("failed to parse certificate (key=%s) from existing secret: %w", resources.MLAGatewayCertSecretKey, err)
}
if resources.IsServerCertificateValidForAllOf(certs[0], commonName, altNames, ca.Cert) {
return se, nil
}
}
config := certutil.Config{
CommonName: commonName,
AltNames: altNames,
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
cert, key, err := certificates.GetSignedECDSACertAndKey(certificates.Duration365d, config, ca.Cert, ca.Key)
if err != nil {
return nil, fmt.Errorf("unable to sign the server certificate: %w", err)
}
se.Data[resources.MLAGatewayCertSecretKey] = cert
se.Data[resources.MLAGatewayKeySecretKey] = key
return se, nil
}
}
}