Skip to content

Commit

Permalink
Refactor configuration of ClusterLogForwarder
Browse files Browse the repository at this point in the history
With this commit configuring the ClusterLogForwarder resource will be done with
a new parameter `openshift4_logging.clusterLogForwarder`, which will follow the
upstream convention / spec.

The old parameter `openshift4_logging.clusterLogForwarding` is deprecated, but
existing legacy config will be respected.
  • Loading branch information
DebakelOrakel committed Aug 10, 2024
1 parent 36a5147 commit 294d837
Show file tree
Hide file tree
Showing 19 changed files with 945 additions and 91 deletions.
15 changes: 1 addition & 14 deletions class/defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,7 @@ parameters:
memory: 128Mi

clusterLogging: {}

clusterLogForwarding:
enabled: false
forwarders: {}
namespace_groups: {}
application_logs: {}
audit_logs:
enabled: false
infrastructure_logs:
enabled: true
json:
enabled: false
typekey: 'kubernetes.labels.logFormat'
typename: 'nologformat'
clusterLogForwarder: {}

operatorResources:
clusterLogging:
Expand Down
146 changes: 88 additions & 58 deletions component/config_forwarding.libsonnet
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local com = import 'lib/commodore.libjsonnet';
local kap = import 'lib/kapitan.libjsonnet';
local lib = import 'lib/openshift4-logging.libsonnet';

Expand All @@ -8,70 +9,83 @@ local deployLokistack = params.components.lokistack.enabled;
local deployElasticsearch = params.components.elasticsearch.enabled;
local forwardingOnly = !deployLokistack && !deployElasticsearch;

local legacyConfig = std.get(params, 'clusterLogForwarding', {});
local hasLegacyConfig = std.length(legacyConfig) > 0;

local enableLogForwarder = std.length(params.clusterLogForwarder) > 0 || std.get(legacyConfig, 'enabled', false);

local pipelineOutputRefs(pipeline) =
local default = if forwardingOnly then [] else [ 'default' ];
std.get(pipeline, 'forwarders', []) + default;

// -----------------------------------------------------------------------------
// Legacy Rendering
// -----------------------------------------------------------------------------

// Apply default config for application logs.
local patchAppLogDefaults = {
local outputRefs = pipelineOutputRefs(params.clusterLogForwarding.application_logs),
local enablePipeline = std.length(outputRefs) > 0,
local patchLegacyAppLogDefaults = {
local pipeline = std.get(legacyConfig, 'application_logs', { enabled: true }),
local pipelineOutputs = pipelineOutputRefs(pipeline),
local pipelineEnabled = std.length(pipelineOutputs) > 0,

pipelines: {
[if enablePipeline then 'application-logs']: {
[if pipelineEnabled then 'application-logs']: {
inputRefs: [ 'application' ],
outputRefs: outputRefs,
outputRefs: pipelineOutputs,
},
},
};

// Apply default config for infra logs.
local patchInfraLogDefaults = {
local outputRefs = pipelineOutputRefs(params.clusterLogForwarding.infrastructure_logs),
local enablePipeline = params.clusterLogForwarding.infrastructure_logs.enabled && std.length(outputRefs) > 0,
local patchLegacyInfraLogDefaults = {
local pipeline = { enabled: true } + std.get(legacyConfig, 'infrastructure_logs', {}),
local pipelineOutputs = pipelineOutputRefs(pipeline),
local pipelineEnabled = pipeline.enabled && std.length(pipelineOutputs) > 0,

pipelines: {
[if enablePipeline then 'infrastructure-logs']: {
[if pipelineEnabled then 'infrastructure-logs']: {
inputRefs: [ 'infrastructure' ],
outputRefs: outputRefs,
outputRefs: pipelineOutputs,
},
},
};

// Apply default config for audit logs.
local patchAuditLogDefaults = {
local outputRefs = pipelineOutputRefs(params.clusterLogForwarding.audit_logs),
local enablePipeline = params.clusterLogForwarding.audit_logs.enabled && std.length(outputRefs) > 0,
local patchLegacyAuditLogDefaults = {
local pipeline = std.get(legacyConfig, 'audit_logs', { enabled: false }),
local pipelineOutputs = pipelineOutputRefs(pipeline),
local pipelineEnabled = pipeline.enabled && std.length(pipelineOutputs) > 0,

pipelines: {
[if enablePipeline then 'audit-logs']: {
[if pipelineEnabled then 'audit-logs']: {
inputRefs: [ 'audit' ],
outputRefs: outputRefs,
outputRefs: pipelineOutputs,
},
},
};

// Enable json parsing for default pipelines if configured.
local patchJsonLogging = {
local enableAppLogs = std.get(params.clusterLogForwarding.application_logs, 'json', false),
local enableInfraLogs = std.get(params.clusterLogForwarding.infrastructure_logs, 'json', false),
local legacyEnableJson = std.get(std.get(legacyConfig, 'json', {}), 'enabled', false);
local patchLegacyJsonLogging = {
local enableAppLogs = std.get(std.get(legacyConfig, 'application_logs', {}), 'json', false),
local enableInfraLogs = std.get(std.get(legacyConfig, 'infrastructure_logs', {}), 'json', false),

pipelines: {
[if enableAppLogs then 'application-logs']: { parse: 'json' },
[if enableInfraLogs then 'infrastructure-logs']: { parse: 'json' },
},
[if deployElasticsearch && params.clusterLogForwarding.json.enabled then 'outputDefaults']: {
[if deployElasticsearch && legacyEnableJson then 'outputDefaults']: {
elasticsearch: {
structuredTypeKey: params.clusterLogForwarding.json.typekey,
structuredTypeName: params.clusterLogForwarding.json.typename,
structuredTypeKey: std.get(legacyConfig.json, 'typekey', 'kubernetes.labels.logFormat'),
structuredTypeName: std.get(legacyConfig.json, 'typename', 'nologformat'),
},
},
};

// Enable detectMultilineErrors for default pipelines if configured.
local patchMultilineErrors = {
local enableAppLogs = std.get(params.clusterLogForwarding.application_logs, 'detectMultilineErrors', false),
local enableInfraLogs = std.get(params.clusterLogForwarding.infrastructure_logs, 'detectMultilineErrors', false),
local patchLegacyMultilineErrors = {
local enableAppLogs = std.get(std.get(legacyConfig, 'application_logs', {}), 'detectMultilineErrors', false),
local enableInfraLogs = std.get(std.get(legacyConfig, 'infrastructure_logs', {}), 'detectMultilineErrors', false),

pipelines: {
[if enableAppLogs then 'application-logs']: { detectMultilineErrors: true },
Expand All @@ -81,19 +95,19 @@ local patchMultilineErrors = {

// --- patch deprecated `clusterLogForwarding.namespace` config
local namespaceGroups = (
if std.objectHas(params.clusterLogForwarding, 'namespaces') then
if std.objectHas(legacyConfig, 'namespaces') then
{
[ns]: {
namespaces: [ ns ],
forwarders: [ params.clusterLogForwarding.namespaces[ns].forwarder ],
forwarders: [ legacyConfig.namespaces[ns].forwarder ],
}
for ns in std.objectFields(params.clusterLogForwarding.namespaces)
for ns in std.objectFields(legacyConfig.namespaces)
} else {}
) + params.clusterLogForwarding.namespace_groups;
) + std.get(legacyConfig, 'namespace_groups', {});
// --- patch end

// Add inputs entry for every namespace_group defined in `clusterLogForwarding.namespace_groups`.
local patchCustomInputs = {
local patchLegacyCustomInputs = {
[if std.length(namespaceGroups) > 0 then 'inputs']: {
[group]: {
application: {
Expand All @@ -105,7 +119,7 @@ local patchCustomInputs = {
};

// Add pipelines entry for every namespace_group defined in `clusterLogForwarding.namespace_groups`.
local patchCustomPipelines = {
local patchLegacyCustomPipelines = {
[if std.length(namespaceGroups) > 0 then 'pipelines']: {
local enableJson = std.get(namespaceGroups[group], 'json', false),
local enableMultilineError = std.get(namespaceGroups[group], 'detectMultilineErrors', false),
Expand All @@ -121,53 +135,69 @@ local patchCustomPipelines = {
};

// Add outputs entry for every forwarder defined in `clusterLogForwarding.forwarders`.
local patchCustomOutputs = {
[if std.length(params.clusterLogForwarding.forwarders) > 0 then 'outputs']: {
[name]: params.clusterLogForwarding.forwarders[name]
for name in std.objectFields(params.clusterLogForwarding.forwarders)
local patchLegacyCustomOutputs = {
[if std.length(std.get(legacyConfig, 'forwarders', {})) > 0 then 'outputs']: {
[name]: legacyConfig.forwarders[name]
for name in std.objectFields(legacyConfig.forwarders)
},
};

// ClusterLogForwarderSpecs:
// legacyLogForwarderSpec:
// Consecutively apply patches to result of previous apply.
local clusterLogForwarderSpec = std.foldl(
local legacyLogForwarderSpec = std.foldl(
// we use std.mergePatch here, because this way we don't need
// to make each patch object mergeable by suffixing all keys with a +.
function(manifest, patch) std.mergePatch(manifest, patch),
[
patchAppLogDefaults,
patchInfraLogDefaults,
patchAuditLogDefaults,
patchJsonLogging,
patchMultilineErrors,
patchCustomInputs,
patchCustomOutputs,
patchCustomPipelines,
patchLegacyAppLogDefaults,
patchLegacyInfraLogDefaults,
patchLegacyAuditLogDefaults,
patchLegacyJsonLogging,
patchLegacyMultilineErrors,
patchLegacyCustomInputs,
patchLegacyCustomOutputs,
patchLegacyCustomPipelines,
],
{
inputs: {},
outputs: {},
pipelines: {},
}
},
);

// -----------------------------------------------------------------------------
// End Legacy Rendering
// -----------------------------------------------------------------------------

// ClusterLogForwarderSpec:
local clusterLogForwarderSpec = legacyLogForwarderSpec + com.makeMergeable(params.clusterLogForwarder);

// ClusterLogForwarder:
// Create definitive ClusterLogForwarder resource from specs.
local clusterLogForwarder = lib.ClusterLogForwarder(params.namespace, 'instance') {
spec: {
// Unfold objects into array.
[if std.length(clusterLogForwarderSpec.inputs) > 0 then 'inputs']: [
{ name: name } + clusterLogForwarderSpec.inputs[name]
for name in std.objectFields(clusterLogForwarderSpec.inputs)
],
[if std.length(clusterLogForwarderSpec.outputs) > 0 then 'outputs']: [
{ name: name } + clusterLogForwarderSpec.outputs[name]
for name in std.objectFields(clusterLogForwarderSpec.outputs)
],
[if std.length(clusterLogForwarderSpec.pipelines) > 0 then 'pipelines']: [
{ name: name } + clusterLogForwarderSpec.pipelines[name]
for name in std.objectFields(clusterLogForwarderSpec.pipelines)
],
[if std.length(clusterLogForwarderSpec.inputs) > 0 then 'inputs']: std.trace(
'Parameter `clusterLogForwarding` is deprecated. Please update your config to use `clusterLogForwarder`',
[
{ name: name } + clusterLogForwarderSpec.inputs[name]
for name in std.objectFields(clusterLogForwarderSpec.inputs)
]
),
[if std.length(clusterLogForwarderSpec.outputs) > 0 then 'outputs']: std.trace(
'Parameter `clusterLogForwarding` is deprecated. Please update your config to use `clusterLogForwarder`',
[
{ name: name } + clusterLogForwarderSpec.outputs[name]
for name in std.objectFields(clusterLogForwarderSpec.outputs)
]
),
[if std.length(clusterLogForwarderSpec.pipelines) > 0 then 'pipelines']: std.trace(
'Parameter `clusterLogForwarding` is deprecated. Please update your config to use `clusterLogForwarder`',
[
{ name: name } + clusterLogForwarderSpec.pipelines[name]
for name in std.objectFields(clusterLogForwarderSpec.pipelines)
]
),
} + {
// Import remaining specs as is.
[key]: clusterLogForwarderSpec[key]
Expand All @@ -177,7 +207,7 @@ local clusterLogForwarder = lib.ClusterLogForwarder(params.namespace, 'instance'
};

// Define outputs below
if params.clusterLogForwarding.enabled then
if enableLogForwarder then
{
'31_cluster_logforwarding': clusterLogForwarder,
}
Expand Down
8 changes: 0 additions & 8 deletions tests/elasticsearch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,12 @@ parameters:
- type: https
source: https://raw.githubusercontent.com/appuio/component-openshift4-monitoring/v2.9.0/lib/openshift4-monitoring-alert-patching.libsonnet
output_path: vendor/lib/alert-patching.libsonnet
- type: https
source: https://raw.githubusercontent.com/projectsyn/component-patch-operator/v1.1.0/lib/patch-operator.libsonnet
output_path: vendor/lib/patch-operator.libsonnet
compile:
- input_type: jsonnet
input_paths:
- tests/console-patch.jsonnet
output_path: console-patching/

patch_operator:
namespace: syn-patch-operator
patch_serviceaccount:
name: syn-patch-operator

openshift4_operators:
defaultInstallPlanApproval: Automatic
defaultSource: openshift-operators-redhat
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
config:
plugins:
- logging-view-plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Namespace
metadata:
annotations:
openshift.io/node-selector: ''
labels:
name: openshift-logging
openshift.io/cluster-monitoring: 'true'
name: openshift-logging
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: operators.coreos.com/v1
kind: OperatorGroup
metadata:
annotations: {}
labels:
name: cluster-logging
name: cluster-logging
namespace: openshift-logging
spec:
targetNamespaces:
- openshift-logging
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
annotations: {}
labels:
name: cluster-logging
name: cluster-logging
namespace: openshift-logging
spec:
channel: stable-5.9
config:
resources:
limits:
memory: 256Mi
requests:
cpu: 10m
memory: 128Mi
installPlanApproval: Automatic
name: cluster-logging
source: redhat-operators
sourceNamespace: openshift-operators-redhat
---
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
annotations: {}
labels:
name: loki-operator
name: loki-operator
namespace: openshift-operators-redhat
spec:
channel: stable-5.9
config:
resources:
limits:
memory: 512Mi
requests:
cpu: 50m
memory: 381Mi
installPlanApproval: Automatic
name: loki-operator
source: openshift-operators-redhat
sourceNamespace: openshift-operators-redhat
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: logging.openshift.io/v1
kind: ClusterLogging
metadata:
annotations:
argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
labels:
name: instance
name: instance
namespace: openshift-logging
spec:
collection:
type: vector
logStore:
lokistack:
name: loki
type: lokistack
managementState: Managed
Loading

0 comments on commit 294d837

Please sign in to comment.