forked from openshift/cluster-logging-operator
/
forwarding.go
524 lines (472 loc) · 20.5 KB
/
forwarding.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
package k8shandler
import (
"context"
"errors"
"fmt"
"strings"
forwardergenerator "github.com/openshift/cluster-logging-operator/internal/generator/forwarder"
"github.com/openshift/cluster-logging-operator/internal/generator/helpers"
log "github.com/ViaQ/logerr/v2/log/static"
configv1 "github.com/openshift/api/config/v1"
logging "github.com/openshift/cluster-logging-operator/apis/logging/v1"
"github.com/openshift/cluster-logging-operator/internal/constants"
"github.com/openshift/cluster-logging-operator/internal/generator"
"github.com/openshift/cluster-logging-operator/internal/generator/fluentd/output/cloudwatch"
"github.com/openshift/cluster-logging-operator/internal/generator/fluentd/output/security"
"github.com/openshift/cluster-logging-operator/internal/status"
"github.com/openshift/cluster-logging-operator/internal/url"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
client "sigs.k8s.io/controller-runtime/pkg/client"
)
func applyOutputDefaults(outputDefaults *logging.OutputDefaults, out logging.OutputSpec) logging.OutputSpec {
if outputDefaults != nil && outputDefaults.Elasticsearch != nil {
if out.Type == logging.OutputTypeElasticsearch && out.Elasticsearch == nil {
out.Elasticsearch = outputDefaults.Elasticsearch
}
}
return out
}
func (clusterRequest *ClusterLoggingRequest) generateCollectorConfig() (config string, err error) {
if clusterRequest.Cluster == nil || clusterRequest.Cluster.Spec.Collection == nil {
log.V(2).Info("skipping collection config generation as 'collection' section is not specified in CLO's CR")
return "", nil
}
switch clusterRequest.Cluster.Spec.Collection.Type {
case logging.LogCollectionTypeFluentd:
break
case logging.LogCollectionTypeVector:
break
default:
return "", fmt.Errorf("%s collector does not support pipelines feature", clusterRequest.Cluster.Spec.Collection.Logs.Type)
}
if clusterRequest.ForwarderRequest == nil {
clusterRequest.ForwarderRequest = &logging.ClusterLogForwarder{}
}
spec, status := clusterRequest.NormalizeForwarder()
clusterRequest.ForwarderSpec = *spec
clusterRequest.ForwarderRequest.Status = *status
tokenSecret, err := clusterRequest.getLogCollectorServiceAccountTokenSecret()
if err == nil {
clusterRequest.OutputSecrets[constants.LogCollectorToken] = tokenSecret
}
op := generator.Options{}
if clusterRequest.useOldRemoteSyslogPlugin() {
op[generator.UseOldRemoteSyslogPlugin] = ""
}
if debug, ok := clusterRequest.ForwarderRequest.Annotations[AnnotationDebugOutput]; ok && strings.ToLower(debug) == "true" {
op[helpers.EnableDebugOutput] = "true"
}
var collectorType = clusterRequest.Cluster.Spec.Collection.Type
g := forwardergenerator.New(collectorType)
err = g.Verify(clusterRequest.Cluster.Spec.Collection, clusterRequest.OutputSecrets, &clusterRequest.ForwarderSpec, op)
if err != nil {
log.Error(err, "Unable to generate log configuration")
if updateError := clusterRequest.UpdateCondition(
logging.CollectorDeadEnd,
"ClusterLogForwarder input validation error",
"ClusterLogForwarder input validation error",
corev1.ConditionTrue,
); updateError != nil {
log.Error(updateError, "Unable to update the clusterlogging status", "conditionType", logging.CollectorDeadEnd)
}
return "", err
}
generatedConfig, err := g.GenerateConf(clusterRequest.Cluster.Spec.Collection, clusterRequest.OutputSecrets, &clusterRequest.ForwarderSpec, op)
if err != nil {
log.Error(err, "Unable to generate log configuration")
if updateError := clusterRequest.UpdateCondition(
logging.CollectorDeadEnd,
"Collectors are defined but there is no defined LogStore or LogForward destinations",
"No defined logstore destination",
corev1.ConditionTrue,
); updateError != nil {
log.Error(updateError, "Unable to update the clusterlogging status", "conditionType", logging.CollectorDeadEnd)
}
return "", err
}
// else
err = clusterRequest.UpdateCondition(
logging.CollectorDeadEnd,
"",
"",
corev1.ConditionFalse,
)
log.V(3).Info("ClusterLogForwarder generated config", generatedConfig)
return generatedConfig, err
}
func (clusterRequest *ClusterLoggingRequest) readClusterName() (string, error) {
infra := configv1.Infrastructure{}
err := clusterRequest.Client.Get(context.Background(), client.ObjectKey{Name: constants.ClusterInfrastructureInstance}, &infra)
if err != nil {
return "", err
}
return infra.Status.InfrastructureName, nil
}
// NormalizeForwarder normalizes the clusterRequest.ForwarderSpec, returns a normalized spec and status.
func (clusterRequest *ClusterLoggingRequest) NormalizeForwarder() (*logging.ClusterLogForwarderSpec, *logging.ClusterLogForwarderStatus) {
clusterRequest.OutputSecrets = make(map[string]*corev1.Secret, len(clusterRequest.ForwarderSpec.Outputs))
// Check for default configuration
logStore := clusterRequest.Cluster.Spec.LogStore
if len(clusterRequest.ForwarderSpec.Pipelines) == 0 {
if logStore != nil && logStore.Type != "" {
log.V(2).Info("ClusterLogForwarder forwarding to default store")
clusterRequest.ForwarderSpec.Pipelines = []logging.PipelineSpec{
{
InputRefs: []string{logging.InputNameApplication, logging.InputNameInfrastructure},
OutputRefs: []string{logging.OutputNameDefault},
},
}
// Continue with normalization to fill out spec and status.
} else if clusterRequest.ForwarderRequest == nil {
log.V(3).Info("ClusterLogForwarder disabled")
return &logging.ClusterLogForwarderSpec{}, &logging.ClusterLogForwarderStatus{}
}
}
if logStore != nil && logStore.Type == logging.LogStoreTypeLokiStack {
outputs, pipelines := clusterRequest.processPipelinesForLokiStack(clusterRequest.ForwarderSpec.Pipelines)
clusterRequest.ForwarderSpec.Outputs = outputs
clusterRequest.ForwarderSpec.Pipelines = pipelines
}
spec := &logging.ClusterLogForwarderSpec{}
status := &logging.ClusterLogForwarderStatus{}
clusterRequest.verifyInputs(spec, status)
if !status.Inputs.IsAllReady() {
log.V(3).Info("Input not Ready", "inputs", status.Inputs)
}
clusterRequest.verifyOutputs(spec, status)
if !status.Outputs.IsAllReady() {
log.V(3).Info("Output not Ready", "outputs", status.Outputs)
}
clusterRequest.verifyPipelines(spec, status)
if !status.Pipelines.IsAllReady() {
log.V(3).Info("Pipeline not Ready", "pipelines", status.Pipelines)
}
routes := logging.NewRoutes(spec.Pipelines) // Compute used inputs/outputs
// Add Ready=true status for all surviving inputs.
status.Inputs = logging.NamedConditions{}
inRefs := sets.StringKeySet(routes.ByInput).List()
for _, inRef := range inRefs {
status.Inputs.Set(inRef, condReady)
}
// Determine overall health
degraded := []string{}
unready := []string{}
for name, conds := range status.Pipelines {
if !conds.IsTrueFor(logging.ConditionReady) {
unready = append(unready, name)
}
if conds.IsTrueFor(logging.ConditionDegraded) {
degraded = append(degraded, name)
}
}
if len(unready) == len(status.Pipelines) {
log.V(3).Info("NormalizeForwarder. All pipelines invalid", "ForwarderSpec", clusterRequest.ForwarderSpec)
status.Conditions.SetCondition(condInvalid("all pipelines invalid: %v", unready))
} else {
if len(unready)+len(degraded) > 0 {
status.Conditions.SetCondition(condDegraded(logging.ReasonInvalid, "degraded pipelines: invalid %v, degraded %v", unready, degraded))
}
status.Conditions.SetCondition(condReady)
}
return spec, status
}
func condNotReady(r status.ConditionReason, format string, args ...interface{}) status.Condition {
return logging.NewCondition(logging.ConditionReady, corev1.ConditionFalse, r, format, args...)
}
func condDegraded(r status.ConditionReason, format string, args ...interface{}) status.Condition {
return logging.NewCondition(logging.ConditionDegraded, corev1.ConditionTrue, r, format, args...)
}
func condInvalid(format string, args ...interface{}) status.Condition {
return condNotReady(logging.ReasonInvalid, format, args...)
}
func condMissing(format string, args ...interface{}) status.Condition {
return condNotReady(logging.ReasonMissingResource, format, args...)
}
var condReady = status.Condition{Type: logging.ConditionReady, Status: corev1.ConditionTrue}
// verifyRefs returns the set of valid refs and a slice of error messages for bad refs.
func verifyRefs(what string, refs []string, allowed sets.String) (sets.String, []string) {
good, bad := sets.NewString(), sets.NewString()
for _, ref := range refs {
if allowed.Has(ref) {
good.Insert(ref)
} else {
bad.Insert(ref)
}
}
msg := []string{}
if len(bad) > 0 {
msg = append(msg, fmt.Sprintf("unrecognized %s: %v", what, bad.List()))
}
if len(good) == 0 {
msg = append(msg, fmt.Sprintf("no valid %s", what))
}
return good, msg
}
func (clusterRequest *ClusterLoggingRequest) verifyPipelines(spec *logging.ClusterLogForwarderSpec, status *logging.ClusterLogForwarderStatus) {
// Validate each pipeline and add a status object.
status.Pipelines = logging.NamedConditions{}
names := sets.NewString() // Collect pipeline names
// Known output names, note if "default" is enabled it will already be in the OutputMap()
outputs := sets.StringKeySet(spec.OutputMap())
// Known input names, reserved names not in InputMap() we don't expose default inputs.
inputs := sets.StringKeySet(spec.InputMap()).Union(logging.ReservedInputNames)
for i, pipeline := range clusterRequest.ForwarderSpec.Pipelines {
if pipeline.Name == "" {
pipeline.Name = fmt.Sprintf("pipeline_%v_", i)
}
if names.Has(pipeline.Name) {
original := pipeline.Name
pipeline.Name = fmt.Sprintf("pipeline_%v_", i)
status.Pipelines.Set(pipeline.Name, condInvalid("duplicate name %q", original))
continue
}
names.Insert(pipeline.Name)
goodIn, msgIn := verifyRefs("inputs", pipeline.InputRefs, inputs)
goodOut, msgOut := verifyRefs("outputs", pipeline.OutputRefs, outputs)
if msgs := append(msgIn, msgOut...); len(msgs) > 0 { // Something wrong
msg := strings.Join(msgs, ", ")
if len(goodIn) == 0 || len(goodOut) == 0 { // All bad, disabled
status.Pipelines.Set(pipeline.Name, condInvalid("invalid: %v", msg))
continue
} else { // Some good some bad, degrade the pipeline.
status.Pipelines.Set(pipeline.Name, condDegraded(logging.ReasonInvalid, "invalid: %v", msg))
}
}
status.Pipelines.Set(pipeline.Name, condReady) // Ready, possibly degraded.
spec.Pipelines = append(spec.Pipelines, logging.PipelineSpec{
Name: pipeline.Name,
InputRefs: goodIn.List(),
OutputRefs: goodOut.List(),
Labels: pipeline.Labels,
Parse: pipeline.Parse,
DetectMultilineErrors: pipeline.DetectMultilineErrors,
})
}
}
// verifyInputs and set status.Inputs conditions
func (clusterRequest *ClusterLoggingRequest) verifyInputs(spec *logging.ClusterLogForwarderSpec, status *logging.ClusterLogForwarderStatus) {
// Collect input conditions
status.Inputs = logging.NamedConditions{}
for i, input := range clusterRequest.ForwarderSpec.Inputs {
i, input := i, input // Don't bind range variables.
badName := func(format string, args ...interface{}) {
input.Name = fmt.Sprintf("input_%v_", i)
status.Inputs.Set(input.Name, condInvalid(format, args...))
}
switch {
case input.Name == "":
badName("input must have a name")
case logging.ReservedInputNames.Has(input.Name):
badName("input name %q is reserved", input.Name)
case len(status.Inputs[input.Name]) > 0:
badName("duplicate name: %q", input.Name)
default:
spec.Inputs = append(spec.Inputs, input)
status.Inputs.Set(input.Name, condReady)
}
}
}
// LokiStackGatewayService returns the name of LokiStack gateway service.
// Returns an empty string if ClusterLogging is not configured for a LokiStack log store.
func (clusterRequest *ClusterLoggingRequest) LokiStackGatewayService() string {
logStore := clusterRequest.Cluster.Spec.LogStore
if logStore == nil || logStore.LokiStack.Name == "" {
return ""
}
return fmt.Sprintf("%s-gateway-http", logStore.LokiStack.Name)
}
// LokiStackURL returns the URL of the LokiStack API for a specific tenant.
// Returns an empty string if ClusterLogging is not configured for a LokiStack log store.
func (clusterRequest *ClusterLoggingRequest) LokiStackURL(tenant string) string {
service := clusterRequest.LokiStackGatewayService()
if service == "" {
return ""
}
return fmt.Sprintf("https://%s.%s.svc:8080/api/logs/v1/%s", service, clusterRequest.Cluster.Namespace, tenant)
}
func (clusterRequest *ClusterLoggingRequest) verifyOutputs(spec *logging.ClusterLogForwarderSpec, status *logging.ClusterLogForwarderStatus) {
status.Outputs = logging.NamedConditions{}
names := sets.NewString() // Collect pipeline names
for i, output := range clusterRequest.ForwarderSpec.Outputs {
i, output := i, output // Don't bind range variable.
badName := func(format string, args ...interface{}) {
output.Name = fmt.Sprintf("output_%v_", i)
status.Outputs.Set(output.Name, condInvalid(format, args...))
}
log.V(3).Info("Verifying", "outputs", output)
switch {
case output.Name == "":
log.V(3).Info("verifyOutputs failed", "reason", "output must have a name")
badName("output must have a name")
case logging.IsReservedOutputName(output.Name):
log.V(3).Info("verifyOutputs failed", "reason", "output name is reserved", "output name", output.Name)
badName("output name %q is reserved", output.Name)
case names.Has(output.Name):
log.V(3).Info("verifyOutputs failed", "reason", "output name is duplicated", "output name", output.Name)
badName("duplicate name: %q", output.Name)
case !logging.IsOutputTypeName(output.Type):
log.V(3).Info("verifyOutputs failed", "reason", "output type is invalid", "output name", output.Name, "output type", output.Type)
status.Outputs.Set(output.Name, condInvalid("output %q: unknown output type %q", output.Name, output.Type))
case !clusterRequest.verifyOutputURL(&output, status.Outputs):
log.V(3).Info("verifyOutputs failed", "reason", "output URL is invalid", "output URL", output.URL)
case !clusterRequest.verifyOutputSecret(&output, status.Outputs):
log.V(3).Info("verifyOutputs failed", "reason", "output secret is invalid")
case output.Type == logging.OutputTypeCloudwatch && output.Cloudwatch == nil:
log.V(3).Info("verifyOutputs failed", "reason", "Cloudwatch output requires type spec", "output name", output.Name)
status.Outputs.Set(output.Name, condInvalid("output %q: Cloudwatch output requires type spec", output.Name))
default:
status.Outputs.Set(output.Name, condReady)
spec.Outputs = append(spec.Outputs, output)
}
if output.Type == logging.OutputTypeCloudwatch {
if output.Cloudwatch != nil && output.Cloudwatch.GroupPrefix == nil {
clusterName, err := clusterRequest.readClusterName()
if err != nil {
badName("outputprefix is not set and it can't be fetched from the cluster. Error: %s", err.Error())
} else {
output.Cloudwatch.GroupPrefix = &clusterName
}
}
}
names.Insert(output.Name)
}
// Add the default output if required and available.
routes := logging.NewRoutes(clusterRequest.ForwarderSpec.Pipelines)
name := logging.OutputNameDefault
if _, ok := routes.ByOutput[name]; ok {
if clusterRequest.Cluster.Spec.LogStore == nil {
status.Outputs.Set(name, condMissing("no default log store specified"))
} else {
logStore := clusterRequest.Cluster.Spec.LogStore
switch logStore.Type {
case logging.LogStoreTypeElasticsearch:
spec.Outputs = append(spec.Outputs, logging.OutputSpec{
Name: logging.OutputNameDefault,
Type: logging.OutputTypeElasticsearch,
URL: constants.LogStoreURL,
Secret: &logging.OutputSecretSpec{Name: constants.CollectorSecretName},
})
status.Outputs.Set(name, condReady)
case logging.LogStoreTypeLokiStack:
// The outputs for LokiStack will already have been added at this point
default:
status.Outputs.Set(name, condInvalid(fmt.Sprintf("unknown log store type: %s", clusterRequest.Cluster.Spec.LogStore.Type)))
}
}
}
for i, out := range spec.Outputs {
out = applyOutputDefaults(clusterRequest.ForwarderSpec.OutputDefaults, out)
spec.Outputs[i] = out
}
if clusterRequest.ForwarderSpec.OutputDefaults != nil {
spec.OutputDefaults = clusterRequest.ForwarderSpec.OutputDefaults
}
}
func (clusterRequest *ClusterLoggingRequest) verifyOutputURL(output *logging.OutputSpec, conds logging.NamedConditions) bool {
fail := func(c status.Condition) bool {
conds.Set(output.Name, c)
return false
}
if output.URL == "" {
// Some output types (currently just kafka) allow a missing URL
// TODO (alanconway) move output-specific valiation to the output implementation.
if output.Type == logging.OutputTypeKafka || output.Type == logging.OutputTypeCloudwatch {
return true
} else {
return fail(condInvalid("URL is required for output type %v", output.Type))
}
}
u, err := url.Parse(output.URL)
if err != nil {
return fail(condInvalid("invalid URL: %v", err))
}
if err := url.CheckAbsolute(u); err != nil {
return fail(condInvalid("invalid URL: %v", err))
}
return true
}
func (clusterRequest *ClusterLoggingRequest) verifyOutputSecret(output *logging.OutputSpec, conds logging.NamedConditions) bool {
fail := func(c status.Condition) bool {
conds.Set(output.Name, c)
return false
}
if output.Secret == nil {
return true
}
if output.Secret.Name == "" {
conds.Set(output.Name, condInvalid("secret has empty name"))
return false
}
log.V(3).Info("getting output secret", "output", output.Name, "secret", output.Secret.Name)
secret, err := clusterRequest.GetSecret(output.Secret.Name)
if err != nil {
return fail(condMissing("secret %q not found", output.Secret.Name))
}
verifySecret := verifySecretKeysForTLS
if output.Type == logging.OutputTypeCloudwatch {
verifySecret = verifySecretKeysForCloudwatch
}
if !verifySecret(output, conds, secret) {
return false
}
clusterRequest.OutputSecrets[output.Name] = secret
return true
}
func verifySecretKeysForTLS(output *logging.OutputSpec, conds logging.NamedConditions, secret *corev1.Secret) bool {
fail := func(c status.Condition) bool {
conds.Set(output.Name, c)
return false
}
// Make sure we have secrets for a valid TLS configuration.
haveCert := len(secret.Data[constants.ClientCertKey]) > 0
haveKey := len(secret.Data[constants.ClientPrivateKey]) > 0
haveUsername := len(secret.Data[constants.ClientUsername]) > 0
havePassword := len(secret.Data[constants.ClientPassword]) > 0
switch {
case haveCert && !haveKey:
return fail(condMissing("cannot have %v without %v", constants.ClientCertKey, constants.ClientPrivateKey))
case !haveCert && haveKey:
return fail(condMissing("cannot have %v without %v", constants.ClientPrivateKey, constants.ClientCertKey))
case haveUsername && !havePassword:
return fail(condMissing("cannot have %v without %v", constants.ClientUsername, constants.ClientPassword))
case !haveUsername && havePassword:
return fail(condMissing("cannot have %v without %v", constants.ClientPassword, constants.ClientUsername))
}
return true
}
func verifySecretKeysForCloudwatch(output *logging.OutputSpec, conds logging.NamedConditions, secret *corev1.Secret) bool {
log.V(3).Info("V")
fail := func(c status.Condition) bool {
conds.Set(output.Name, c)
return false
}
// Ensure we have secrets for valid cloudwatch config
hasKeyID := len(secret.Data[constants.AWSAccessKeyID]) > 0
hasSecretKey := len(secret.Data[constants.AWSSecretAccessKey]) > 0
hasRoleArnKey := security.HasAwsRoleArnKey(secret)
hasCredentialsKey := security.HasAwsCredentialsKey(secret)
hasValidRoleArn := len(cloudwatch.ParseRoleArn(secret)) > 0
switch {
case hasValidRoleArn: // Sts secret format is the first check
return true
case hasRoleArnKey && !hasValidRoleArn, hasCredentialsKey && !hasValidRoleArn:
return fail(condMissing("auth keys: a 'role_arn' or 'credentials' key is required containing a valid arn value"))
case !hasKeyID || !hasSecretKey:
return fail(condMissing("auth keys: " + constants.AWSAccessKeyID + " and " + constants.AWSSecretAccessKey + " are required"))
}
return true
}
func (clusterRequest *ClusterLoggingRequest) getLogCollectorServiceAccountTokenSecret() (*corev1.Secret, error) {
s := &corev1.Secret{}
log.V(9).Info("Fetching Secret", "Name", constants.LogCollectorToken)
if err := clusterRequest.Get(constants.LogCollectorToken, s); err != nil {
log.V(3).Error(err, "Could not find Secret", "Name", constants.LogCollectorToken)
return nil, errors.New("Could not retrieve ServiceAccount token")
}
if _, ok := s.Data[constants.TokenKey]; !ok {
log.V(9).Info("did not find token in secret", "Name", s.Name)
return nil, errors.New("logcollector secret is missing token")
}
return s, nil
}