diff --git a/packages/apps/kubernetes/templates/helmreleases/monitoring-agents.yaml b/packages/apps/kubernetes/templates/helmreleases/monitoring-agents.yaml index 616b83326..1268010bd 100644 --- a/packages/apps/kubernetes/templates/helmreleases/monitoring-agents.yaml +++ b/packages/apps/kubernetes/templates/helmreleases/monitoring-agents.yaml @@ -38,6 +38,9 @@ fluent-bit: json_date_format iso8601 header AccountID 0 header ProjectID 0 + {{ `{{- with .Values.config.extraOutputs }}` }} + {{ `{{ . }}` }} + {{ `{{- end }}` }} filters: | [FILTER] Name kubernetes @@ -65,6 +68,21 @@ fluent-bit: {{ end }} {{- if and .Values.addons.monitoringAgents.enabled .Values._namespace.etcd }} +{{- $values := deepCopy .Values.addons.monitoringAgents.valuesOverride | mergeOverwrite (fromYaml (include "cozystack.defaultMonitoringAgentsValues" .)) }} +{{- /* + The monitoring-agents chart refuses the same combination by rendering + outputs, but only once the HelmRelease reconciles inside the tenant + cluster. That render needs the subchart's context, which this chart does + not have, so the check here is textual: a with on extraOutputs whose + closing braces keep the newline, followed by {{ . }} on a line of its own. + A hook that is only mentioned, commented out or compacted fails here and + is reported on the application itself; anything subtler still fails in + the tenant cluster. +*/}} +{{- $fluentBitConfig := (index $values "fluent-bit").config | default dict }} +{{- if and $fluentBitConfig.extraOutputs (not (regexMatch "\\{\\{-?\\s*with\\s+\\.Values\\.config\\.extraOutputs\\s*\\}\\}[ \\t]*\\n[ \\t]*\\{\\{\\s*\\.\\s*-?\\}\\}" ($fluentBitConfig.outputs | default ""))) }} +{{- fail "addons.monitoringAgents.valuesOverride sets fluent-bit.config.extraOutputs but overrides fluent-bit.config.outputs without a hook that emits it on a line of its own, so the extra destinations would be dropped. Keep the hook from the default outputs at the end of yours, each part on a line of its own: {{- with .Values.config.extraOutputs }}, then {{ . }}, then {{- end }}. Or move the destinations into outputs and unset extraOutputs." }} +{{- end }} apiVersion: helm.toolkit.fluxcd.io/v2 kind: HelmRelease metadata: @@ -104,5 +122,5 @@ spec: - name: {{ .Release.Name }}-metrics-server namespace: {{ .Release.Namespace }} values: - {{- toYaml (deepCopy .Values.addons.monitoringAgents.valuesOverride | mergeOverwrite (fromYaml (include "cozystack.defaultMonitoringAgentsValues" .))) | nindent 4 }} + {{- toYaml $values | nindent 4 }} {{- end }} diff --git a/packages/apps/kubernetes/tests/monitoring_agents_test.yaml b/packages/apps/kubernetes/tests/monitoring_agents_test.yaml index 2ce5c79ff..6d2b45383 100644 --- a/packages/apps/kubernetes/tests/monitoring_agents_test.yaml +++ b/packages/apps/kubernetes/tests/monitoring_agents_test.yaml @@ -78,3 +78,110 @@ tests: - equal: path: spec.values.fluent-bit.daemonSetVolumes[0].name value: varlog + + - it: adds extra destinations through valuesOverride without dropping the tenant ones + set: + addons.monitoringAgents.valuesOverride: + vmagent: + extraRemoteWrite: + - url: https://metrics.example.org/api/v1/write + fluent-bit: + config: + extraOutputs: | + [OUTPUT] + Name stdout + Match * + asserts: + - equal: + path: spec.values.vmagent.remoteWrite.urls + value: + - http://vminsert-shortterm.tenant-root.svc.cozy.local:8480/insert/0/prometheus + - equal: + path: spec.values.vmagent.extraRemoteWrite + value: + - url: https://metrics.example.org/api/v1/write + - matchRegex: + path: spec.values.fluent-bit.config.outputs + pattern: 'Host vlinsert-generic\.tenant-root\.svc\.cozy\.local' + # The outputs string is rendered again by the fluent-bit subchart, + # which is where extraOutputs gets appended. + - matchRegex: + path: spec.values.fluent-bit.config.outputs + pattern: '\{\{- with \.Values\.config\.extraOutputs \}\}\n\{\{ \. \}\}\n\{\{- end \}\}' + + - it: refuses extraOutputs when valuesOverride replaces outputs without the hook + set: + addons.monitoringAgents.valuesOverride: + fluent-bit: + config: + outputs: | + [OUTPUT] + Name http + Host logs-a.example.org + extraOutputs: | + [OUTPUT] + Name http + Host logs-b.example.org + asserts: + - failedTemplate: + errorPattern: 'overrides fluent-bit\.config\.outputs without a hook that emits it on a line of its own' + + - it: refuses extraOutputs when the hook in an outputs override is commented out + set: + addons.monitoringAgents.valuesOverride: + fluent-bit: + config: + outputs: | + [OUTPUT] + Name http + Host logs-a.example.org + {{/* .Values.config.extraOutputs */}} + extraOutputs: | + [OUTPUT] + Name http + Host logs-b.example.org + asserts: + - failedTemplate: + errorPattern: 'without a hook that emits it on a line of its own' + + - it: refuses extraOutputs when the hook in an outputs override trims the newline + set: + addons.monitoringAgents.valuesOverride: + fluent-bit: + config: + outputs: | + [OUTPUT] + Name http + Host logs-a.example.org + {{- with .Values.config.extraOutputs }}{{ . }}{{- end }} + extraOutputs: | + [OUTPUT] + Name http + Host logs-b.example.org + asserts: + - failedTemplate: + errorPattern: 'without a hook that emits it on a line of its own' + + - it: passes both destinations through when valuesOverride replaces outputs and keeps the hook + set: + addons.monitoringAgents.valuesOverride: + fluent-bit: + config: + outputs: | + [OUTPUT] + Name http + Host logs-a.example.org + {{- with .Values.config.extraOutputs }} + {{ . }} + {{- end }} + extraOutputs: | + [OUTPUT] + Name http + Host logs-b.example.org + asserts: + - matchRegex: + path: spec.values.fluent-bit.config.outputs + pattern: 'Host logs-a\.example\.org\n\{\{- with \.Values\.config\.extraOutputs \}\}' + - matchRegex: + path: spec.values.fluent-bit.config.extraOutputs + pattern: 'Host logs-b\.example\.org' diff --git a/packages/system/monitoring-agents/templates/fluent-bit-extra-outputs-guard.yaml b/packages/system/monitoring-agents/templates/fluent-bit-extra-outputs-guard.yaml new file mode 100644 index 000000000..d4cbcea9e --- /dev/null +++ b/packages/system/monitoring-agents/templates/fluent-bit-extra-outputs-guard.yaml @@ -0,0 +1,21 @@ +{{- /* + extraOutputs is appended by a hook inside the outputs string, because the + vendored fluent-bit chart builds fluent-bit.conf from outputs alone and + make update re-pulls it unpatched. An outputs override without a working + hook would accept extraOutputs and drop it in silence, so refuse the render. + + outputs is rendered here the way the subchart renders it, and the check is + that extraOutputs comes out of it starting on a line of its own: a hook + commented out, or one that trims the newline before it, fails too. The + render only runs when extraOutputs is set, so the lookup in the default + outputs is not doubled otherwise. packages/apps/kubernetes carries an + earlier, textual check for tenant clusters. +*/}} +{{- $fluentBit := index .Values "fluent-bit" }} +{{- $config := $fluentBit.config | default dict }} +{{- if $config.extraOutputs }} +{{- $rendered := tpl ($config.outputs | default "") (dict "Values" $fluentBit "Release" .Release "Chart" .Chart "Capabilities" .Capabilities "Template" .Template "Files" .Files) }} +{{- if not (contains (printf "\n%s" (trim $config.extraOutputs)) (printf "\n%s" $rendered)) }} +{{- fail "fluent-bit.config.extraOutputs is set but fluent-bit.config.outputs does not emit it on a line of its own, so the extra destinations would be dropped. Keep the hook from the default outputs at the end of yours, each part on a line of its own: {{- with .Values.config.extraOutputs }}, then {{ . }}, then {{- end }}. Or move the destinations into outputs and unset extraOutputs." }} +{{- end }} +{{- end }} diff --git a/packages/system/monitoring-agents/templates/vmagent.yaml b/packages/system/monitoring-agents/templates/vmagent.yaml index 0eb6d9d80..51ed777ca 100644 --- a/packages/system/monitoring-agents/templates/vmagent.yaml +++ b/packages/system/monitoring-agents/templates/vmagent.yaml @@ -17,6 +17,9 @@ spec: {{- range .Values.vmagent.remoteWrite.urls }} - url: {{ tpl . $ | quote }} {{- end }} + {{- with .Values.vmagent.extraRemoteWrite }} + {{- toYaml . | nindent 2 }} + {{- end }} scrapeInterval: 30s selectAllByDefault: true diff --git a/packages/system/monitoring-agents/tests/fluent-bit_extra_outputs_guard_test.yaml b/packages/system/monitoring-agents/tests/fluent-bit_extra_outputs_guard_test.yaml new file mode 100644 index 000000000..e3c3a5ef4 --- /dev/null +++ b/packages/system/monitoring-agents/tests/fluent-bit_extra_outputs_guard_test.yaml @@ -0,0 +1,97 @@ +suite: fluent-bit extraOutputs is refused where outputs would drop it + +# extraOutputs only reaches fluent-bit.conf through the hook in outputs. An +# outputs override without a working hook would accept extraOutputs and drop +# it, so the render fails instead. The guard renders outputs, so a hook that +# is only mentioned, or that glues the extra section to the previous line, +# counts as missing. The tenant HelmRelease carries the same check; +# its counterpart is packages/apps/kubernetes/tests/monitoring_agents_test.yaml. +templates: + - templates/fluent-bit-extra-outputs-guard.yaml +release: + name: monitoring-agents + namespace: cozy-monitoring +set: + global: + target: tenant-root +tests: + - it: refuses extraOutputs when outputs is overridden without the hook + set: + fluent-bit.config.outputs: | + [OUTPUT] + Name http + Host logs-a.example.org + fluent-bit.config.extraOutputs: | + [OUTPUT] + Name http + Host logs-b.example.org + asserts: + - failedTemplate: + errorPattern: 'fluent-bit\.config\.outputs does not emit it on a line of its own' + + - it: refuses a hook that is commented out + set: + fluent-bit.config.outputs: | + [OUTPUT] + Name http + Host logs-a.example.org + {{/* .Values.config.extraOutputs */}} + fluent-bit.config.extraOutputs: | + [OUTPUT] + Name http + Host logs-b.example.org + asserts: + - failedTemplate: + errorPattern: 'does not emit it on a line of its own' + + - it: refuses a compact hook that trims the newline before the extra section + set: + fluent-bit.config.outputs: | + [OUTPUT] + Name http + Host logs-a.example.org + {{- with .Values.config.extraOutputs }}{{ . }}{{- end }} + fluent-bit.config.extraOutputs: | + [OUTPUT] + Name http + Host logs-b.example.org + asserts: + - failedTemplate: + errorPattern: 'does not emit it on a line of its own' + + - it: accepts an outputs override that keeps the three-line hook + set: + fluent-bit.config.outputs: | + [OUTPUT] + Name http + Host logs-a.example.org + {{- with .Values.config.extraOutputs }} + {{ . }} + {{- end }} + fluent-bit.config.extraOutputs: | + [OUTPUT] + Name http + Host logs-b.example.org + asserts: + - hasDocuments: + count: 0 + + - it: accepts extraOutputs with the default outputs + set: + fluent-bit.config.extraOutputs: | + [OUTPUT] + Name stdout + Match * + asserts: + - hasDocuments: + count: 0 + + - it: accepts an outputs override without extraOutputs + set: + fluent-bit.config.outputs: | + [OUTPUT] + Name http + Host logs-a.example.org + asserts: + - hasDocuments: + count: 0 diff --git a/packages/system/monitoring-agents/tests/fluent-bit_extra_outputs_test.yaml b/packages/system/monitoring-agents/tests/fluent-bit_extra_outputs_test.yaml new file mode 100644 index 000000000..b77f12ca2 --- /dev/null +++ b/packages/system/monitoring-agents/tests/fluent-bit_extra_outputs_test.yaml @@ -0,0 +1,56 @@ +suite: fluent-bit extraOutputs add destinations next to the default ones + +# outputs is one string, so a user who overrides it to add a destination has +# to restate the defaults or lose them. extraOutputs is appended by the +# outputs template itself. packages/apps/kubernetes replaces outputs for +# tenant clusters and carries the same hook; its counterpart is +# packages/apps/kubernetes/tests/monitoring_agents_test.yaml. +templates: + - charts/fluent-bit/templates/configmap.yaml +release: + name: monitoring-agents + namespace: cozy-monitoring +set: + global: + target: tenant-root +tests: + - it: appends extraOutputs after the default outputs, which stay + set: + fluent-bit.config.extraOutputs: | + [OUTPUT] + Name stdout + Match * + asserts: + - matchRegex: + path: data["fluent-bit.conf"] + pattern: '(?s)Match audit\.\*\n Host vlinsert-generic\.tenant-root\.svc.*\[OUTPUT\]\n Name stdout\n Match \*' + + - it: renders no extra output by default + asserts: + - notMatchRegex: + path: data["fluent-bit.conf"] + pattern: 'Name stdout' + + - it: renders each destination once when outputs is overridden and keeps the hook + set: + fluent-bit.config.outputs: | + [OUTPUT] + Name http + Host logs-a.example.org + {{- with .Values.config.extraOutputs }} + {{ . }} + {{- end }} + fluent-bit.config.extraOutputs: | + [OUTPUT] + Name http + Host logs-b.example.org + asserts: + - matchRegex: + path: data["fluent-bit.conf"] + pattern: '(?s)Host logs-a\.example\.org.*\[OUTPUT\]\n Name http\n Host logs-b\.example\.org' + - notMatchRegex: + path: data["fluent-bit.conf"] + pattern: '(?s)logs-a\.example\.org.*logs-a\.example\.org' + - notMatchRegex: + path: data["fluent-bit.conf"] + pattern: '(?s)logs-b\.example\.org.*logs-b\.example\.org' diff --git a/packages/system/monitoring-agents/tests/vmagent_remote_write_test.yaml b/packages/system/monitoring-agents/tests/vmagent_remote_write_test.yaml index 894a1c053..a1ee6c905 100644 --- a/packages/system/monitoring-agents/tests/vmagent_remote_write_test.yaml +++ b/packages/system/monitoring-agents/tests/vmagent_remote_write_test.yaml @@ -63,3 +63,23 @@ tests: - equal: path: spec.externalLabels.tenant value: "true" + + - it: appends extraRemoteWrite entries after the urls, as written + set: + global.target: tenant-root + vmagent: + extraRemoteWrite: + - url: https://metrics.example.org/api/v1/write + basicAuth: + username: {name: remote-write-auth, key: username} + password: {name: remote-write-auth, key: password} + asserts: + - equal: + path: spec.remoteWrite + value: + - url: http://vminsert-shortterm.tenant-root.svc:8480/insert/0/prometheus + - url: http://vminsert-longterm.tenant-root.svc:8480/insert/0/prometheus + - url: https://metrics.example.org/api/v1/write + basicAuth: + username: {name: remote-write-auth, key: username} + password: {name: remote-write-auth, key: password} diff --git a/packages/system/monitoring-agents/values.yaml b/packages/system/monitoring-agents/values.yaml index b6c046ea5..36c0fda5a 100644 --- a/packages/system/monitoring-agents/values.yaml +++ b/packages/system/monitoring-agents/values.yaml @@ -312,6 +312,12 @@ vmagent: urls: - http://vminsert-shortterm.{{ .Values.global.target }}.svc:8480/insert/0/prometheus - http://vminsert-longterm.{{ .Values.global.target }}.svc:8480/insert/0/prometheus + # VMAgent remoteWrite entries appended after the urls above, passed as is, + # so an entry can carry basicAuth, bearerTokenSecret, headers or tlsConfig. + # Adding a destination here leaves the default ones in place; overriding + # urls instead replaces them. Secrets an entry references must exist in + # this release's namespace. + extraRemoteWrite: [] extraArgs: {} # Replaces the global promscrape.maxScrapeSize (32MB) for the kube-apiserver # job only. Its /metrics grows with the number of CRDs and outgrows 32MB on a @@ -451,6 +457,15 @@ fluent-bit: header AccountID 0 header ProjectID 0 {{- end }} + {{- with .Values.config.extraOutputs }} + {{ . }} + {{- end }} + # [OUTPUT] sections appended after the outputs above, so a destination can + # be added without restating, and so without dropping, the default ones. + # Not passed through tpl. They are appended by the hook at the end of + # outputs, so an outputs override must keep that hook; the render fails + # when extraOutputs is set and outputs lacks it. + extraOutputs: "" filters: | [FILTER] Name kubernetes