From 51fc5647f06c757c671f1a283d531799b5fd4316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BUISSON?= Date: Thu, 11 Jul 2024 15:00:04 +0200 Subject: [PATCH] feat: handle log filePath and noColor --- traefik/templates/_podtemplate.tpl | 64 ++++++++++++++++++------------ traefik/tests/pod-config_test.yaml | 53 +++++++++++++++++++++++++ traefik/values.yaml | 5 ++- 3 files changed, 95 insertions(+), 27 deletions(-) diff --git a/traefik/templates/_podtemplate.tpl b/traefik/templates/_podtemplate.tpl index af0207b41..ff363fd74 100644 --- a/traefik/templates/_podtemplate.tpl +++ b/traefik/templates/_podtemplate.tpl @@ -621,46 +621,58 @@ {{- end }} {{- end }} {{- with .Values.logs }} - {{- if .general.format }} + {{- if and .general.format (not (has .general.format (list "common" "json"))) }} + {{- fail "ERROR: .Values.logs.general.format must be either common or json" }} + {{- end }} + {{- if .general.format }} - "--log.format={{ .general.format }}" - {{- end }} - {{- if ne .general.level "ERROR" }} + {{- end }} + {{- if .general.filePath }} + - "--log.filePath={{ .general.filePath }}" + {{- end }} + {{- if and (or (eq .general.format "common") (not .general.format)) (eq .general.noColor true) }} + - "--log.noColor={{ .general.noColor }}" + {{- end }} + {{- if and .general.level (not (has (.general.level | upper) (list "DEBUG" "PANIC" "FATAL" "ERROR" "WARN" "INFO"))) }} + {{- fail "ERROR: .Values.logs.level must be DEBUG, PANIC, FATAL, ERROR, WARN, and INFO" }} + {{- end }} + {{- if .general.level }} - "--log.level={{ .general.level | upper }}" - {{- end }} - {{- if .access.enabled }} + {{- end }} + {{- if .access.enabled }} - "--accesslog=true" - {{- with .access.format }} + {{- with .access.format }} - "--accesslog.format={{ . }}" - {{- end }} - {{- with .access.filePath }} + {{- end }} + {{- with .access.filePath }} - "--accesslog.filepath={{ . }}" - {{- end }} - {{- if .access.addInternals }} + {{- end }} + {{- if .access.addInternals }} - "--accesslog.addinternals" - {{- end }} - {{- with .access.bufferingSize }} + {{- end }} + {{- with .access.bufferingSize }} - "--accesslog.bufferingsize={{ . }}" - {{- end }} - {{- with .access.filters }} - {{- with .statuscodes }} + {{- end }} + {{- with .access.filters }} + {{- with .statuscodes }} - "--accesslog.filters.statuscodes={{ . }}" - {{- end }} - {{- if .retryattempts }} + {{- end }} + {{- if .retryattempts }} - "--accesslog.filters.retryattempts" - {{- end }} - {{- with .minduration }} + {{- end }} + {{- with .minduration }} - "--accesslog.filters.minduration={{ . }}" - {{- end }} - {{- end }} + {{- end }} + {{- end }} - "--accesslog.fields.defaultmode={{ .access.fields.general.defaultmode }}" - {{- range $fieldname, $fieldaction := .access.fields.general.names }} + {{- range $fieldname, $fieldaction := .access.fields.general.names }} - "--accesslog.fields.names.{{ $fieldname }}={{ $fieldaction }}" - {{- end }} + {{- end }} - "--accesslog.fields.headers.defaultmode={{ .access.fields.headers.defaultmode }}" - {{- range $fieldname, $fieldaction := .access.fields.headers.names }} + {{- range $fieldname, $fieldaction := .access.fields.headers.names }} - "--accesslog.fields.headers.names.{{ $fieldname }}={{ $fieldaction }}" - {{- end }} - {{- end }} + {{- end }} + {{- end }} {{- end }} {{- range $resolver, $config := $.Values.certResolvers }} {{- range $option, $setting := $config }} diff --git a/traefik/tests/pod-config_test.yaml b/traefik/tests/pod-config_test.yaml index bdb25c045..04e848c27 100644 --- a/traefik/tests/pod-config_test.yaml +++ b/traefik/tests/pod-config_test.yaml @@ -623,3 +623,56 @@ tests: - notContains: path: spec.template.spec.containers[0].args content: "--core.defaultRuleSyntax=v2" + - it: should fail when providing bad log format + set: + logs: + general: + format: "test" + asserts: + - failedTemplate: + errorMessage: "ERROR: .Values.logs.general.format must be either common or json" + - it: should set log filePath when configured + set: + logs: + general: + filePath: "/var/log/traefik.log" + asserts: + - contains: + path: spec.template.spec.containers[0].args + content: "--log.filePath=/var/log/traefik.log" + - it: should set log noColor when configured + set: + logs: + general: + noColor: true + asserts: + - contains: + path: spec.template.spec.containers[0].args + content: "--log.noColor=true" + - it: should set log noColor when configured to false as it's default + set: + logs: + general: + noColor: false + asserts: + - notContains: + path: spec.template.spec.containers[0].args + content: "--log.noColor=false" + - it: should not set log noColor when configured and log format is not common + set: + logs: + general: + format: json + noColor: true + asserts: + - notContains: + path: spec.template.spec.containers[0].args + content: "--log.noColor=true" + - it: should fail when setting unknown log level + set: + logs: + general: + level: test + asserts: + - failedTemplate: + errorMessage: "ERROR: .Values.logs.level must be DEBUG, PANIC, FATAL, ERROR, WARN, and INFO" diff --git a/traefik/values.yaml b/traefik/values.yaml index c8bfd5be1..f1352799d 100644 --- a/traefik/values.yaml +++ b/traefik/values.yaml @@ -307,9 +307,12 @@ logs: # -- Set [logs format](https://doc.traefik.io/traefik/observability/logs/#format) # @default common format: - # By default, the level is set to ERROR. + # By default, the level is set to INFO. # -- Alternative logging levels are DEBUG, PANIC, FATAL, ERROR, WARN, and INFO. level: INFO + # + # filePath: "/var/log/traefik/traefik.log + # noColor: true access: # -- To enable access logs enabled: false