Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

k8s: Allow custom logging configuration via values files / ConfigMap (#2663)

Eric Bariauxbe18f016f106
diff --git a/kubernetes/README.md b/kubernetes/README.md
index a0a3bc70d..744a7794b 100644
--- a/kubernetes/README.md
+++ b/kubernetes/README.md
@@ -101,6 +101,42 @@ or:
value: "false"
```
+### Logging configuration
+
+The manager chart supports 3 modes for logging configuration:
+- no logging override in the values file: the manager uses the built in `logging.properties` or `logging-dev.properties` depending on `or.devMode`
+- `logging.config`: provide the full content of a custom `logging.properties` file directly in the values file
+- `logging.existingConfigMap`: reference an existing `ConfigMap` that contains a custom `logging.properties` entry
+
+Example using an inline logging configuration:
+```yaml
+logging:
+ config: |
+ handlers=java.util.logging.ConsoleHandler
+ .level=INFO
+```
+
+Example using an existing `ConfigMap`:
+```yaml
+logging:
+ existingConfigMap: "my-manager-logging"
+ existingConfigMapKey: "logging.properties"
+ restartToken: "v1"
+```
+
+When `logging.config` is used, Helm creates a `ConfigMap` and adds a checksum annotation on the manager pod template.
+Changing the logging configuration in the values file and running `helm upgrade` updates that checksum and causes the Deployment to roll out a new pod.
+
+When `logging.existingConfigMap` is used, Helm mounts the referenced `ConfigMap` but does not track changes made directly to it.
+If you edit that `ConfigMap` with `kubectl edit cm ...`, Kubernetes does not restart the pod because the Deployment specification has not changed.
+To trigger a restart through Helm after changing the external `ConfigMap`, bump the `logging.restartToken` value and run `helm upgrade`.
+`existingConfigMapKey` allows the manager chart to work with whatever structure that existing `ConfigMap` has.
+One handy use of this is defining multiple logging configuration in one `ConfigMap` under different keys and easily switching between them.
+
+Also note that the manager reads the logging configuration only once during startup.
+Kubernetes may refresh the mounted `ConfigMap` file on disk after a short delay, but the running JVM does not automatically reload the logging settings.
+In practice, any change to the logging configuration requires a pod restart to take effect.
+
### Metrics
Both the manager and HAProxy expose Prometheus metrics.
diff --git a/kubernetes/manager/templates/_helpers.tpl b/kubernetes/manager/templates/_helpers.tpl
index 67fcbe0d4..67034f6d9 100644
--- a/kubernetes/manager/templates/_helpers.tpl
+++ b/kubernetes/manager/templates/_helpers.tpl
@@ -89,3 +89,58 @@ app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
+
+{{/*
+Validate logging values.
+*/}}
+{{- define "manager.logging.validate" -}}
+{{- if and .Values.logging.config .Values.logging.existingConfigMap -}}
+{{- fail "manager chart values logging.config and logging.existingConfigMap are mutually exclusive" -}}
+{{- end -}}
+{{- if and (not .Values.logging.existingConfigMap) (ne (default "logging.properties" .Values.logging.existingConfigMapKey) "logging.properties") -}}
+{{- fail "manager chart value logging.existingConfigMapKey requires logging.existingConfigMap" -}}
+{{- end -}}
+{{- if or .Values.logging.config .Values.logging.existingConfigMap -}}
+ {{- range .Values.or.env -}}
+ {{- if eq .name "OR_LOGGING_CONFIG_FILE" -}}
+{{- fail "manager chart values logging.* manage OR_LOGGING_CONFIG_FILE; remove OR_LOGGING_CONFIG_FILE from or.env" -}}
+ {{- end -}}
+ {{- end -}}
+{{- end -}}
+{{- end }}
+
+{{/*
+Return the chart-managed logging ConfigMap name.
+*/}}
+{{- define "manager.logging.configMapName" -}}
+{{- printf "%s-%s" (include "manager.fullname" .) "logging" | trunc 63 | trimSuffix "-" -}}
+{{- end }}
+
+{{/*
+Return the referenced logging ConfigMap name.
+*/}}
+{{- define "manager.logging.configMapRefName" -}}
+{{- if .Values.logging.existingConfigMap -}}
+{{- .Values.logging.existingConfigMap -}}
+{{- else -}}
+{{- include "manager.logging.configMapName" . -}}
+{{- end -}}
+{{- end }}
+
+{{/*
+Return the referenced logging ConfigMap key.
+*/}}
+{{- define "manager.logging.configMapKey" -}}
+{{- if .Values.logging.existingConfigMap -}}
+{{- default "logging.properties" .Values.logging.existingConfigMapKey -}}
+{{- else -}}
+logging.properties
+{{- end -}}
+{{- end }}
+
+{{/*
+Return the mounted logging config path.
+*/}}
+{{- define "manager.logging.configFilePath" -}}
+/etc/openremote/logging/logging.properties
+{{- end }}
diff --git a/kubernetes/manager/templates/configmap.yaml b/kubernetes/manager/templates/configmap.yaml
new file mode 100644
index 000000000..585edeb46
--- /dev/null
+++ b/kubernetes/manager/templates/configmap.yaml
@@ -0,0 +1,11 @@
+{{- if .Values.logging.config }}
+apiVersion: v1
+kind: ConfigMap
+metadata:
+ name: {{ include "manager.logging.configMapName" . }}
+ labels:
+ {{- include "manager.labels" . | nindent 4 }}
+data:
+ logging.properties: |-
+{{- .Values.logging.config | nindent 4 }}
+{{- end }}
diff --git a/kubernetes/manager/templates/deployment.yaml b/kubernetes/manager/templates/deployment.yaml
index d6c37adb5..d67f2f2c2 100644
--- a/kubernetes/manager/templates/deployment.yaml
+++ b/kubernetes/manager/templates/deployment.yaml
@@ -1,3 +1,8 @@
+{{- include "manager.logging.validate" . }}
+{{- $loggingInlineConfig := .Values.logging.config -}}
+{{- $loggingExistingConfigMap := .Values.logging.existingConfigMap -}}
+{{- $loggingEnabled := or $loggingInlineConfig $loggingExistingConfigMap -}}
+{{- $loggingExternalConfig := $loggingExistingConfigMap -}}
apiVersion: apps/v1
kind: Deployment
metadata:
@@ -11,9 +16,17 @@ spec:
{{- include "manager.selectorLabels" . | nindent 6 }}
template:
metadata:
- {{- with .Values.podAnnotations }}
+ {{- if or .Values.podAnnotations $loggingEnabled }}
annotations:
+ {{- with .Values.podAnnotations }}
{{- toYaml . | nindent 8 }}
+ {{- end }}
+ {{- if $loggingInlineConfig }}
+ checksum/logging-config: {{ .Values.logging.config | sha256sum | quote }}
+ {{- end }}
+ {{- if and $loggingExternalConfig .Values.logging.restartToken }}
+ logging.openremote.io/restart-token: {{ .Values.logging.restartToken | quote }}
+ {{- end }}
{{- end }}
labels:
{{- include "manager.labels" . | nindent 8 }}
@@ -123,16 +136,34 @@ spec:
value: "{{ .Values.or.metricsEnabled }}"
- name: OR_METRICS_PORT
value: "{{ .Values.or.metricsPort }}"
+
+ {{- if $loggingEnabled }}
+ - name: OR_LOGGING_CONFIG_FILE
+ value: "{{ include "manager.logging.configFilePath" . }}"
+ {{- end }}
{{- with .Values.or.env }}
{{- toYaml . | nindent 12 }}
{{- end }}
volumeMounts:
+ {{- if $loggingEnabled }}
+ - name: volume-logging-config
+ mountPath: /etc/openremote/logging
+ readOnly: true
+ {{- end }}
- mountPath: /storage
name: manager-data
{{- with .Values.volumeMounts }}
{{- toYaml . | nindent 12 }}
{{- end }}
volumes:
+ {{- if $loggingEnabled }}
+ - name: volume-logging-config
+ configMap:
+ name: {{ include "manager.logging.configMapRefName" . }}
+ items:
+ - key: {{ include "manager.logging.configMapKey" . | quote }}
+ path: logging.properties
+ {{- end }}
- name: manager-data
persistentVolumeClaim:
claimName: {{ include "manager.fullname" . }}
diff --git a/kubernetes/manager/values.yaml b/kubernetes/manager/values.yaml
index 35470698f..5723b7f80 100644
--- a/kubernetes/manager/values.yaml
+++ b/kubernetes/manager/values.yaml
@@ -143,6 +143,28 @@ tolerations: []
affinity: {}
+logging:
+ # Full custom logging.properties content managed by this chart.
+ # When empty and existingConfigMap is also empty, the manager falls back to
+ # the built in logging.properties or logging-dev.properties based on or.devMode.
+ # Example:
+ # config: |
+ # handlers=java.util.logging.ConsoleHandler
+ # .level=INFO
+ config: ""
+
+ # Name of an existing ConfigMap containing a custom logging.properties file.
+ # Mutually exclusive with logging.config.
+ # Example: existingConfigMap: "my-manager-logging"
+ existingConfigMap: ""
+
+ # Key within logging.existingConfigMap to mount as logging.properties.
+ # Requires logging.existingConfigMap.
+ existingConfigMapKey: "logging.properties"
+
+ # Optional token to force a rollout when using an external ConfigMap.
+ restartToken: ""
+
or:
# Additional environment variables passed to the container
env: []