-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
var.go
612 lines (510 loc) · 18.7 KB
/
var.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
package manifests
import (
"fmt"
"path"
"time"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
lokiv1 "github.com/grafana/loki/operator/api/loki/v1"
"github.com/grafana/loki/operator/internal/manifests/openshift"
)
const (
gossipPort = 7946
httpPort = 3100
internalHTTPPort = 3101
grpcPort = 9095
protocolTCP = "TCP"
gossipInstanceAddrEnvVarName = "HASH_RING_INSTANCE_ADDR"
gossipInstanceAddrEnvVarTemplate = "${" + gossipInstanceAddrEnvVarName + "}"
lokiHTTPPortName = "metrics"
lokiInternalHTTPPortName = "healthchecks"
lokiGRPCPortName = "grpclb"
lokiGossipPortName = "gossip-ring"
lokiLivenessPath = "/loki/api/v1/status/buildinfo"
lokiReadinessPath = "/ready"
lokiFrontendContainerName = "loki-query-frontend"
gatewayContainerName = "gateway"
gatewayHTTPPort = 8080
gatewayInternalPort = 8081
gatewayHTTPPortName = "public"
gatewayInternalPortName = "metrics"
walVolumeName = "wal"
configVolumeName = "config"
rulesStorageVolumeName = "rules"
storageVolumeName = "storage"
rulePartsSeparator = "___"
walDirectory = "/tmp/wal"
dataDirectory = "/tmp/loki"
rulesStorageDirectory = "/tmp/rules"
rulerContainerName = "loki-ruler"
// EnvRelatedImageLoki is the environment variable to fetch the Loki image pullspec.
EnvRelatedImageLoki = "RELATED_IMAGE_LOKI"
// EnvRelatedImageGateway is the environment variable to fetch the Gateway image pullspec.
EnvRelatedImageGateway = "RELATED_IMAGE_GATEWAY"
// DefaultContainerImage declares the default fallback for loki image.
DefaultContainerImage = "docker.io/grafana/loki:3.2.1"
// DefaultLokiStackGatewayImage declares the default image for lokiStack-gateway.
DefaultLokiStackGatewayImage = "quay.io/observatorium/api:latest"
// PrometheusCAFile declares the path for prometheus CA file for service monitors.
PrometheusCAFile string = "/etc/prometheus/configmaps/serving-certs-ca-bundle/service-ca.crt"
// BearerTokenFile declares the path for bearer token file for service monitors.
BearerTokenFile string = "/var/run/secrets/kubernetes.io/serviceaccount/token" //#nosec G101 -- False positive
// labelJobComponent is a ServiceMonitor.Spec.JobLabel.
labelJobComponent string = "loki.grafana.com/component"
// AnnotationCertRotationRequiredAt stores the point in time the last cert rotation happened
AnnotationCertRotationRequiredAt string = "loki.grafana.com/certRotationRequiredAt"
// AnnotationLokiConfigHash stores the last SHA1 hash of the loki configuration
AnnotationLokiConfigHash string = "loki.grafana.com/config-hash"
// AnnotationLokiObjectStoreHash stores the last SHA1 hash of the loki object storage credetials.
AnnotationLokiObjectStoreHash string = "loki.grafana.com/object-store-hash"
// AnnotationLokiTokenCCOAuthHash stores the SHA1 hash of the secret generated by the Cloud Credential Operator.
AnnotationLokiTokenCCOAuthHash string = "loki.grafana.com/token-cco-auth-hash" //#nosec G101 -- False positive
// LabelCompactorComponent is the label value for the compactor component
LabelCompactorComponent string = "compactor"
// LabelDistributorComponent is the label value for the distributor component
LabelDistributorComponent string = "distributor"
// LabelIngesterComponent is the label value for the ingester component
LabelIngesterComponent string = "ingester"
// LabelQuerierComponent is the label value for the querier component
LabelQuerierComponent string = "querier"
// LabelQueryFrontendComponent is the label value for the query frontend component
LabelQueryFrontendComponent string = "query-frontend"
// LabelIndexGatewayComponent is the label value for the lokiStack-index-gateway component
LabelIndexGatewayComponent string = "index-gateway"
// LabelRulerComponent is the label value for the lokiStack-ruler component
LabelRulerComponent string = "ruler"
// LabelGatewayComponent is the label value for the lokiStack-gateway component
LabelGatewayComponent string = "lokistack-gateway"
// httpTLSDir is the path that is mounted from the secret for TLS
httpTLSDir = "/var/run/tls/http"
// grpcTLSDir is the path that is mounted from the secret for TLS
grpcTLSDir = "/var/run/tls/grpc"
// tenantCAsDir is the path that is mounted from the configmaps for mTLS
tenantCAsDir = "/var/run/tenants-ca"
// LokiStackCABundleDir is the path that is mounted from the configmap for TLS
caBundleDir = "/var/run/ca"
// caFile is the file name of the certificate authority file
caFile = "service-ca.crt"
kubernetesNodeOSLabel = "kubernetes.io/os"
kubernetesNodeOSLinux = "linux"
kubernetesNodeHostnameLabel = "kubernetes.io/hostname"
kubernetesComponentLabel = "app.kubernetes.io/component"
kubernetesInstanceLabel = "app.kubernetes.io/instance"
)
const (
// lokiDefaultQueryTimeout contains the default query timeout. It should match the value mentioned in the CRD
// definition and also the default in the `sizes.go`.
lokiDefaultQueryTimeout = 3 * time.Minute
lokiDefaultHTTPIdleTimeout = 30 * time.Second
lokiQueryWriteDuration = 1 * time.Minute
gatewayReadDuration = 30 * time.Second
gatewayWriteDuration = 2 * time.Minute
)
var (
defaultTimeoutConfig = calculateHTTPTimeouts(lokiDefaultQueryTimeout)
defaultRevHistoryLimit int32 = 10
defaultConfigMapMode int32 = 420
volumeFileSystemMode = corev1.PersistentVolumeFilesystem
)
func commonAnnotations(opts Options) map[string]string {
a := map[string]string{
AnnotationLokiConfigHash: opts.ConfigSHA1,
AnnotationCertRotationRequiredAt: opts.CertRotationRequiredAt,
}
if opts.ObjectStorage.SecretSHA1 != "" {
a[AnnotationLokiObjectStoreHash] = opts.ObjectStorage.SecretSHA1
}
if opts.ObjectStorage.OpenShift.CloudCredentials.SHA1 != "" {
a[AnnotationLokiTokenCCOAuthHash] = opts.ObjectStorage.OpenShift.CloudCredentials.SHA1
}
return a
}
func gatewayAnnotations(configSHA1, certRotationRequiredAt string) map[string]string {
return map[string]string{
AnnotationLokiConfigHash: configSHA1,
AnnotationCertRotationRequiredAt: certRotationRequiredAt,
}
}
func commonLabels(stackName string) map[string]string {
return map[string]string{
"app.kubernetes.io/name": "lokistack",
kubernetesInstanceLabel: stackName,
"app.kubernetes.io/managed-by": "lokistack-controller",
"app.kubernetes.io/created-by": "lokistack-controller",
}
}
func componentInstanceLabels(component string, stackName string) map[string]string {
return map[string]string{
kubernetesInstanceLabel: stackName,
kubernetesComponentLabel: component,
}
}
func serviceAnnotations(serviceName string, enableSigningService bool) map[string]string {
annotations := map[string]string{}
if enableSigningService {
annotations[openshift.ServingCertKey] = serviceName
}
return annotations
}
// ComponentLabels is a list of all commonLabels including the app.kubernetes.io/component:<component> label
func ComponentLabels(component, stackName string) labels.Set {
return labels.Merge(commonLabels(stackName), map[string]string{
kubernetesComponentLabel: component,
})
}
// GossipLabels is the list of labels that should be assigned to components using the gossip ring
func GossipLabels() map[string]string {
return map[string]string{
"loki.grafana.com/gossip": "true",
}
}
// CompactorName is the name of the compactor statefulset
func CompactorName(stackName string) string {
return fmt.Sprintf("%s-compactor", stackName)
}
// DistributorName is the name of the distributor deployment
func DistributorName(stackName string) string {
return fmt.Sprintf("%s-distributor", stackName)
}
// IngesterName is the name of the compactor statefulset
func IngesterName(stackName string) string {
return fmt.Sprintf("%s-ingester", stackName)
}
// QuerierName is the name of the querier deployment
func QuerierName(stackName string) string {
return fmt.Sprintf("%s-querier", stackName)
}
// QueryFrontendName is the name of the query-frontend statefulset
func QueryFrontendName(stackName string) string {
return fmt.Sprintf("%s-query-frontend", stackName)
}
// IndexGatewayName is the name of the index-gateway statefulset
func IndexGatewayName(stackName string) string {
return fmt.Sprintf("%s-index-gateway", stackName)
}
// RulerName is the name of the ruler statefulset
func RulerName(stackName string) string {
return fmt.Sprintf("%s-ruler", stackName)
}
// RulesConfigMapName is the name of the alerting/recording rules configmap
func RulesConfigMapName(stackName string) string {
return fmt.Sprintf("%s-rules", stackName)
}
// RulesStorageVolumeName is the name of the rules volume
func RulesStorageVolumeName() string {
return rulesStorageVolumeName
}
// GatewayName is the name of the lokiStack-gateway statefulset
func GatewayName(stackName string) string {
return fmt.Sprintf("%s-gateway", stackName)
}
// PrometheusRuleName is the name of the loki-prometheus-rule
func PrometheusRuleName(stackName string) string {
return fmt.Sprintf("%s-prometheus-rule", stackName)
}
func lokiConfigMapName(stackName string) string {
return fmt.Sprintf("%s-config", stackName)
}
func lokiServerGRPCTLSDir() string {
return path.Join(grpcTLSDir, "server")
}
func lokiServerGRPCTLSCert() string {
return path.Join(lokiServerGRPCTLSDir(), corev1.TLSCertKey)
}
func lokiServerGRPCTLSKey() string {
return path.Join(lokiServerGRPCTLSDir(), corev1.TLSPrivateKeyKey)
}
func lokiServerHTTPTLSDir() string {
return path.Join(httpTLSDir, "server")
}
func lokiServerHTTPTLSCert() string {
return path.Join(lokiServerHTTPTLSDir(), corev1.TLSCertKey)
}
func lokiServerHTTPTLSKey() string {
return path.Join(lokiServerHTTPTLSDir(), corev1.TLSPrivateKeyKey)
}
func gatewayServerHTTPTLSDir() string {
return path.Join(httpTLSDir, "server")
}
func gatewayServerHTTPTLSCert() string {
return path.Join(gatewayServerHTTPTLSDir(), corev1.TLSCertKey)
}
func gatewayServerHTTPTLSKey() string {
return path.Join(gatewayServerHTTPTLSDir(), corev1.TLSPrivateKeyKey)
}
func gatewayUpstreamHTTPTLSDir() string {
return path.Join(httpTLSDir, "upstream")
}
func gatewayUpstreamHTTPTLSCert() string {
return path.Join(gatewayUpstreamHTTPTLSDir(), corev1.TLSCertKey)
}
func gatewayUpstreamHTTPTLSKey() string {
return path.Join(gatewayUpstreamHTTPTLSDir(), corev1.TLSPrivateKeyKey)
}
func tenantCAVolumeName(tenantName string) string {
return fmt.Sprintf("%s-ca-bundle", tenantName)
}
func tenantCADir(tennantName string) string {
return path.Join(tenantCAsDir, tennantName)
}
func TenantCAPath(tennantName, key string) string {
return path.Join(tenantCAsDir, tennantName, key)
}
func gatewayClientSecretName(stackName string) string {
return fmt.Sprintf("%s-gateway-client-http", stackName)
}
func serviceNameQuerierHTTP(stackName string) string {
return fmt.Sprintf("%s-querier-http", stackName)
}
func serviceNameQuerierGRPC(stackName string) string {
return fmt.Sprintf("%s-querier-grpc", stackName)
}
func serviceNameIngesterGRPC(stackName string) string {
return fmt.Sprintf("%s-ingester-grpc", stackName)
}
func serviceNameIngesterHTTP(stackName string) string {
return fmt.Sprintf("%s-ingester-http", stackName)
}
func serviceNameDistributorGRPC(stackName string) string {
return fmt.Sprintf("%s-distributor-grpc", stackName)
}
func serviceNameDistributorHTTP(stackName string) string {
return fmt.Sprintf("%s-distributor-http", stackName)
}
func serviceNameCompactorGRPC(stackName string) string {
return fmt.Sprintf("%s-compactor-grpc", stackName)
}
func serviceNameCompactorHTTP(stackName string) string {
return fmt.Sprintf("%s-compactor-http", stackName)
}
func serviceNameQueryFrontendGRPC(stackName string) string {
return fmt.Sprintf("%s-query-frontend-grpc", stackName)
}
func serviceNameQueryFrontendHTTP(stackName string) string {
return fmt.Sprintf("%s-query-frontend-http", stackName)
}
func serviceNameIndexGatewayHTTP(stackName string) string {
return fmt.Sprintf("%s-index-gateway-http", stackName)
}
func serviceNameIndexGatewayGRPC(stackName string) string {
return fmt.Sprintf("%s-index-gateway-grpc", stackName)
}
func serviceNameRulerHTTP(stackName string) string {
return fmt.Sprintf("%s-ruler-http", stackName)
}
func serviceNameRulerGRPC(stackName string) string {
return fmt.Sprintf("%s-ruler-grpc", stackName)
}
func serviceNameGatewayHTTP(stackName string) string {
return fmt.Sprintf("%s-gateway-http", stackName)
}
func serviceMonitorName(componentName string) string {
return fmt.Sprintf("%s-monitor", componentName)
}
func signingCABundleName(stackName string) string {
return fmt.Sprintf("%s-ca-bundle", stackName)
}
func gatewaySigningCABundleName(gwName string) string {
return fmt.Sprintf("%s-ca-bundle", gwName)
}
func gatewaySigningCADir() string {
return path.Join(caBundleDir, "server")
}
func gatewaySigningCAPath() string {
return path.Join(gatewaySigningCADir(), caFile)
}
func gatewayUpstreamCADir() string {
return path.Join(caBundleDir, "upstream")
}
func gatewayUpstreamCAPath() string {
return path.Join(gatewayUpstreamCADir(), caFile)
}
func gatewayTokenSecretName(gwName string) string {
return fmt.Sprintf("%s-token", gwName)
}
func alertmanagerSigningCABundleName(rulerName string) string {
return fmt.Sprintf("%s-ca-bundle", rulerName)
}
func alertmanagerUpstreamCADir() string {
return path.Join(caBundleDir, "alertmanager")
}
func alertmanagerUpstreamCAPath() string {
return path.Join(alertmanagerUpstreamCADir(), caFile)
}
func signingCAPath() string {
return path.Join(caBundleDir, caFile)
}
func fqdn(serviceName, namespace string) string {
return fmt.Sprintf("%s.%s.svc.cluster.local", serviceName, namespace)
}
// lokiServiceMonitorEndpoint returns the lokistack endpoint for service monitors.
func lokiServiceMonitorEndpoint(stackName, portName, serviceName, namespace string, enableTLS bool) monitoringv1.Endpoint {
if enableTLS {
tlsConfig := monitoringv1.TLSConfig{
SafeTLSConfig: monitoringv1.SafeTLSConfig{
CA: monitoringv1.SecretOrConfigMap{
ConfigMap: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: signingCABundleName(stackName),
},
Key: caFile,
},
},
Cert: monitoringv1.SecretOrConfigMap{
Secret: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: serviceName,
},
Key: corev1.TLSCertKey,
},
},
KeySecret: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: serviceName,
},
Key: corev1.TLSPrivateKeyKey,
},
// ServerName can be e.g. loki-distributor-http.openshift-logging.svc.cluster.local
ServerName: ptr.To(fqdn(serviceName, namespace)),
},
}
return monitoringv1.Endpoint{
Port: portName,
Path: "/metrics",
Scheme: "https",
TLSConfig: &tlsConfig,
}
}
return monitoringv1.Endpoint{
Port: portName,
Path: "/metrics",
Scheme: "http",
}
}
// gatewayServiceMonitorEndpoint returns the lokistack endpoint for service monitors.
func gatewayServiceMonitorEndpoint(gatewayName, portName, serviceName, namespace string, enableTLS bool) monitoringv1.Endpoint {
if enableTLS {
tlsConfig := monitoringv1.TLSConfig{
SafeTLSConfig: monitoringv1.SafeTLSConfig{
CA: monitoringv1.SecretOrConfigMap{
ConfigMap: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: gatewaySigningCABundleName(gatewayName),
},
Key: caFile,
},
},
// ServerName can be e.g. lokistack-dev-gateway-http.openshift-logging.svc.cluster.local
ServerName: ptr.To(fqdn(serviceName, namespace)),
},
}
return monitoringv1.Endpoint{
Port: portName,
Path: "/metrics",
Scheme: "https",
Authorization: &monitoringv1.SafeAuthorization{
Type: "Bearer",
Credentials: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: gatewayTokenSecretName(gatewayName),
},
Key: corev1.ServiceAccountTokenKey,
},
},
TLSConfig: &tlsConfig,
}
}
return monitoringv1.Endpoint{
Port: portName,
Path: "/metrics",
Scheme: "http",
}
}
// configureAffinity returns an Affinity struture that can be used directly
// in a Deployment/StatefulSet. Parameters will affected configuration of the
// different fields in Affinity (NodeAffinity, PodAffinity, PodAntiAffinity).
func configureAffinity(componentLabel, stackName string, enableNodeAffinity bool, cSpec *lokiv1.LokiComponentSpec) *corev1.Affinity {
affinity := &corev1.Affinity{
NodeAffinity: defaultNodeAffinity(enableNodeAffinity),
PodAntiAffinity: defaultPodAntiAffinity(componentLabel, stackName),
}
if cSpec.PodAntiAffinity != nil {
affinity.PodAntiAffinity = cSpec.PodAntiAffinity
}
return affinity
}
// defaultNodeAffinity if enabled will require pods to run on Linux nodes
func defaultNodeAffinity(enableNodeAffinity bool) *corev1.NodeAffinity {
if !enableNodeAffinity {
return nil
}
return &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: []corev1.NodeSelectorTerm{
{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: kubernetesNodeOSLabel,
Operator: corev1.NodeSelectorOpIn,
Values: []string{
kubernetesNodeOSLinux,
},
},
},
},
},
},
}
}
// defaultPodAntiAffinity for components in podAntiAffinityComponents will
// configure pods, of a LokiStack, to preferably not run on the same node
func defaultPodAntiAffinity(componentLabel, stackName string) *corev1.PodAntiAffinity {
return &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
{
Weight: 100,
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: componentInstanceLabels(componentLabel, stackName),
},
TopologyKey: kubernetesNodeHostnameLabel,
},
},
},
}
}
func lokiLivenessProbe() *corev1.Probe {
return &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: lokiLivenessPath,
Port: intstr.FromInt(httpPort),
Scheme: corev1.URISchemeHTTP,
},
},
TimeoutSeconds: 2,
PeriodSeconds: 30,
FailureThreshold: 10,
SuccessThreshold: 1,
}
}
func lokiReadinessProbe() *corev1.Probe {
return &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: lokiReadinessPath,
Port: intstr.FromInt(httpPort),
Scheme: corev1.URISchemeHTTP,
},
},
PeriodSeconds: 10,
InitialDelaySeconds: 15,
TimeoutSeconds: 1,
SuccessThreshold: 1,
FailureThreshold: 3,
}
}