From 0f7a57cf90105f6ffc1ecf92bf6c9b6b9acf93d9 Mon Sep 17 00:00:00 2001 From: Pete Wall Date: Thu, 11 Jul 2024 11:46:50 -0500 Subject: [PATCH] Make the formatAsJson actually do something and add config validation. (#633) Also remove journal extraConfig which also was not used. Signed-off-by: Pete Wall --- charts/k8s-monitoring/README.md | 1 - .../templates/_config_validations.tpl | 18 +++++++++---- .../_journal_logs_discovery.alloy.txt | 3 ++- charts/k8s-monitoring/values.yaml | 7 ------ examples/logs-journal/logs.alloy | 3 ++- examples/logs-journal/output.yaml | 3 ++- ...non-daemonset-and-journal-logs_values.yaml | 25 +++++++++++++++++++ .../spec/invalid-logs-configurations_spec.sh | 12 +++++++++ 8 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 tests/spec/fixtures/invalid-logs-config-non-daemonset-and-journal-logs_values.yaml diff --git a/charts/k8s-monitoring/README.md b/charts/k8s-monitoring/README.md index 9859f56c0..f722e4ddb 100644 --- a/charts/k8s-monitoring/README.md +++ b/charts/k8s-monitoring/README.md @@ -380,7 +380,6 @@ The Prometheus and Loki services may be hosted on the same cluster, or remotely | Key | Type | Default | Description | |-----|------|---------|-------------| | logs.journal.enabled | bool | `false` | Scrape Kubernetes Worker Journal Logs event | -| logs.journal.extraConfig | string | `""` | Extra configuration that will be added to the Grafana Alloy for Journal configuration file. This value is templated so that you can refer to other values from this file. This cannot be used to modify the generated configuration values, only append new components. See [Adding custom Flow configuration](#adding-custom-flow-configuration) for an example. | | logs.journal.extraStageBlocks | string | `""` | Stage blocks to be added to the loki.process component for journal logs. ([docs](https://grafana.com/docs/alloy/latest/reference/components/loki.process/#blocks)) This value is templated so that you can refer to other values from this file. | | logs.journal.formatAsJson | bool | `false` | Whether to forward the original journal entry as JSON. | | logs.journal.jobLabel | string | `"integrations/kubernetes/journal"` | The value for the job label for journal logs | diff --git a/charts/k8s-monitoring/templates/_config_validations.tpl b/charts/k8s-monitoring/templates/_config_validations.tpl index 0a9d053e0..47f4a7f3d 100644 --- a/charts/k8s-monitoring/templates/_config_validations.tpl +++ b/charts/k8s-monitoring/templates/_config_validations.tpl @@ -56,9 +56,17 @@ alloy-logs: {{- end -}} {{- end -}} {{- end -}} -{{- end -}} - - - - +{{- if and .Values.logs.enabled .Values.logs.journal.enabled }} + {{- if ne (index .Values "alloy-logs").controller.type "daemonset" }} +{{/* +Invalid configuration for gathering journal logs! Grafana Alloy for Logs must be a Daemonset. Otherwise, journal logs will be missing! +Please set: +alloy-logs: + controller: + type: daemonset +*/}} + {{ fail "Invalid configuration for gathering journal logs! Grafana Alloy for Logs must be a Daemonset. Otherwise, journal logs will be missing!\nPlease set:\nalloy-logs:\n controller:\n type: daemonset"}} + {{- end }} +{{- end -}} +{{- end -}} diff --git a/charts/k8s-monitoring/templates/alloy_config/_journal_logs_discovery.alloy.txt b/charts/k8s-monitoring/templates/alloy_config/_journal_logs_discovery.alloy.txt index 82583c91e..0b05e092e 100644 --- a/charts/k8s-monitoring/templates/alloy_config/_journal_logs_discovery.alloy.txt +++ b/charts/k8s-monitoring/templates/alloy_config/_journal_logs_discovery.alloy.txt @@ -26,8 +26,9 @@ loki.relabel "journal" { } loki.source.journal "worker" { - max_age = {{ .Values.logs.journal.maxAge | default "8h" | quote }} path = {{ .Values.logs.journal.path | default "/var/logs/journal" | quote }} + format_as_json = {{ .Values.logs.journal.formatAsJson | quote }} + max_age = {{ .Values.logs.journal.maxAge | default "8h" | quote }} relabel_rules = loki.relabel.journal.rules labels = { job = {{ .Values.logs.journal.jobLabel | default "integrations/kubernetes/journal" | quote }}, diff --git a/charts/k8s-monitoring/values.yaml b/charts/k8s-monitoring/values.yaml index 61aead4c8..be70e1cab 100644 --- a/charts/k8s-monitoring/values.yaml +++ b/charts/k8s-monitoring/values.yaml @@ -1483,13 +1483,6 @@ logs: # @section -- Logs Scrape: Pod Logs extraRelabelingRules: "" - # -- Extra configuration that will be added to the Grafana Alloy for Journal configuration file. - # This value is templated so that you can refer to other values from this file. - # This cannot be used to modify the generated configuration values, only append new components. - # See [Adding custom Flow configuration](#adding-custom-flow-configuration) for an example. - # @section -- Logs Scrape: Journal - extraConfig: "" - # Settings related to logs ingested via receivers # @section -- Logs -> OTEL Receiver receiver: diff --git a/examples/logs-journal/logs.alloy b/examples/logs-journal/logs.alloy index ccbe0321d..8e7adff24 100644 --- a/examples/logs-journal/logs.alloy +++ b/examples/logs-journal/logs.alloy @@ -173,8 +173,9 @@ loki.relabel "journal" { } loki.source.journal "worker" { - max_age = "8h" path = "/var/log/journal" + format_as_json = "false" + max_age = "8h" relabel_rules = loki.relabel.journal.rules labels = { job = "integrations/kubernetes/journal", diff --git a/examples/logs-journal/output.yaml b/examples/logs-journal/output.yaml index 109fb5b44..b6477f64d 100644 --- a/examples/logs-journal/output.yaml +++ b/examples/logs-journal/output.yaml @@ -415,8 +415,9 @@ data: } loki.source.journal "worker" { - max_age = "8h" path = "/var/log/journal" + format_as_json = "false" + max_age = "8h" relabel_rules = loki.relabel.journal.rules labels = { job = "integrations/kubernetes/journal", diff --git a/tests/spec/fixtures/invalid-logs-config-non-daemonset-and-journal-logs_values.yaml b/tests/spec/fixtures/invalid-logs-config-non-daemonset-and-journal-logs_values.yaml new file mode 100644 index 000000000..e5b61c4ce --- /dev/null +++ b/tests/spec/fixtures/invalid-logs-config-non-daemonset-and-journal-logs_values.yaml @@ -0,0 +1,25 @@ +--- +cluster: + name: invalid-logs-config-non-daemonset-and-journal-logs + +externalServices: + prometheus: + host: https://prometheus.example.com + basicAuth: + username: 12345 + password: "It's a secret to everyone" + loki: + host: https://loki.example.com + basicAuth: + username: 12345 + password: "It's a secret to everyone" + +logs: + pod_logs: + gatherMethod: api + journal: + enabled: true + +alloy-logs: + controller: + type: deployment diff --git a/tests/spec/invalid-logs-configurations_spec.sh b/tests/spec/invalid-logs-configurations_spec.sh index a85f93f3e..582d64f01 100644 --- a/tests/spec/invalid-logs-configurations_spec.sh +++ b/tests/spec/invalid-logs-configurations_spec.sh @@ -48,6 +48,18 @@ logs: pod_logs: gatherMethod: api or +alloy-logs: + controller: + type: daemonset' + End + End + + Describe 'Using non-daemonset with journal log gathering' + It 'prints a friendly error message' + When call helm template k8smon ../charts/k8s-monitoring -f "spec/fixtures/invalid-logs-config-non-daemonset-and-journal-logs_values.yaml" + The status should be failure + The error should include 'Invalid configuration for gathering journal logs! Grafana Alloy for Logs must be a Daemonset. Otherwise, journal logs will be missing! +Please set: alloy-logs: controller: type: daemonset'