forked from openshift/cluster-logging-operator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforwarding.go
More file actions
351 lines (318 loc) · 12.8 KB
/
forwarding.go
File metadata and controls
351 lines (318 loc) · 12.8 KB
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
package k8shandler
import (
"fmt"
"strings"
"github.com/ViaQ/logerr/log"
logging "github.com/openshift/cluster-logging-operator/pkg/apis/logging/v1"
"github.com/openshift/cluster-logging-operator/pkg/constants"
"github.com/openshift/cluster-logging-operator/pkg/generators/forwarding"
"github.com/openshift/cluster-logging-operator/pkg/status"
"github.com/openshift/cluster-logging-operator/pkg/url"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
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 the CLO's CR")
return "", nil
}
switch clusterRequest.Cluster.Spec.Collection.Logs.Type {
case logging.LogCollectionTypeFluentd:
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
generator, err := forwarding.NewConfigGenerator(
clusterRequest.Cluster.Spec.Collection.Logs.Type,
clusterRequest.includeLegacyForwardConfig(),
clusterRequest.includeLegacySyslogConfig(),
clusterRequest.useOldRemoteSyslogPlugin(),
)
if err != nil {
log.Error(err, "Unable to create collector config generator")
return "",
clusterRequest.UpdateCondition(
logging.CollectorDeadEnd,
"Unable to generate collector configuration",
"No defined logstore destination",
corev1.ConditionTrue,
)
}
clfSpec := &clusterRequest.ForwarderSpec
fwSpec := clusterRequest.Cluster.Spec.Forwarder
generatedConfig, err := generator.Generate(clfSpec, clusterRequest.OutputSecrets, fwSpec)
if err != nil {
log.Error(err, "Unable to generate log configuration")
return "",
clusterRequest.UpdateCondition(
logging.CollectorDeadEnd,
"Collectors are defined but there is no defined LogStore or LogForward destinations",
"No defined logstore destination",
corev1.ConditionTrue,
)
}
// else
err = clusterRequest.UpdateCondition(
logging.CollectorDeadEnd,
"",
"",
corev1.ConditionFalse,
)
log.V(3).Info("ClusterLogForwarder generated config", generatedConfig)
return generatedConfig, err
}
// NormalizeForwarder normalizes the clusterRequest.ForwarderSpec, returns a normalized spec and status.
func (clusterRequest *ClusterLoggingRequest) NormalizeForwarder() (*logging.ClusterLogForwarderSpec, *logging.ClusterLogForwarderStatus) {
// Check for default configuration
if len(clusterRequest.ForwarderSpec.Pipelines) == 0 {
if clusterRequest.Cluster.Spec.LogStore != nil && clusterRequest.Cluster.Spec.LogStore.Type == logging.LogStoreTypeElasticsearch {
log.V(2).Info("ClusterLogForwarder forwarding to default store")
defaultPipeline := logging.PipelineSpec{
InputRefs: []string{logging.InputNameApplication, logging.InputNameInfrastructure},
OutputRefs: []string{logging.OutputNameDefault},
}
clusterRequest.ForwarderSpec.Pipelines = []logging.PipelineSpec{defaultPipeline}
if clusterRequest.includeLegacySyslogConfig() {
defaultPipeline.OutputRefs = append(defaultPipeline.OutputRefs, constants.LegacySyslog)
}
if clusterRequest.includeLegacyForwardConfig() {
defaultPipeline.OutputRefs = append(defaultPipeline.OutputRefs, constants.LegacySecureforward)
}
// 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{}
}
}
spec := &logging.ClusterLogForwarderSpec{}
status := &logging.ClusterLogForwarderStatus{}
clusterRequest.verifyInputs(spec, status)
clusterRequest.verifyOutputs(spec, status)
clusterRequest.verifyPipelines(spec, status)
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) {
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,
})
}
}
// 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)
}
}
}
func (clusterRequest *ClusterLoggingRequest) verifyOutputs(spec *logging.ClusterLogForwarderSpec, status *logging.ClusterLogForwarderStatus) {
status.Outputs = logging.NamedConditions{}
clusterRequest.OutputSecrets = make(map[string]*corev1.Secret, len(clusterRequest.ForwarderSpec.Outputs))
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 == "":
badName("output must have a name")
case logging.IsReservedOutputName(output.Name):
badName("output name %q is reserved", output.Name)
case names.Has(output.Name):
badName("duplicate name: %q", output.Name)
case !logging.IsOutputTypeName(output.Type):
status.Outputs.Set(output.Name, condInvalid("output %q: unknown output type %q", output.Name, output.Type))
case !clusterRequest.verifyOutputURL(&output, status.Outputs):
break
case !clusterRequest.verifyOutputSecret(&output, status.Outputs):
break
default:
status.Outputs.Set(output.Name, condReady)
spec.Outputs = append(spec.Outputs, output)
}
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 {
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)
}
}
}
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 == "kafka" {
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))
}
// Make sure we have secrets for a valid TLS configuration.
haveCert := len(secret.Data[constants.ClientCertKey]) > 0
haveKey := len(secret.Data[constants.ClientPrivateKey]) > 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))
}
clusterRequest.OutputSecrets[output.Name] = secret
return true
}