Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Add ability to set log level and enable json logging for all consul-k8s commands #980

Merged
merged 17 commits into from
Jun 30, 2021
Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Unreleased

IMPROVEMENTS:
* Allow setting global.logLevel and global.logJSON and propogate this to all consul-k8s commands. [[GH-980](https://github.com/hashicorp/consul-helm/pull/980)]

## 0.32.1 (June 29, 2021)

BUG FIXES:
Expand Down
5 changes: 2 additions & 3 deletions templates/connect-inject-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ spec:
CONSUL_FULLNAME="{{template "consul.fullname" . }}"

consul-k8s inject-connect \
-log-level={{ default .Values.global.logLevel .Values.connectInject.logLevel }} \
-log-json={{ .Values.global.logJSON }} \
-default-inject={{ .Values.connectInject.default }} \
-consul-image="{{ default .Values.global.image .Values.connectInject.imageConsul }}" \
-envoy-image="{{ .Values.global.imageEnvoy }}" \
Expand All @@ -106,9 +108,6 @@ spec:
{{- if .Values.global.openshift.enabled }}
-enable-openshift \
{{- end }}
{{- if .Values.connectInject.logLevel }}
-log-level={{ .Values.connectInject.logLevel }} \
{{- end }}
{{- if (or (and (ne (.Values.connectInject.metrics.defaultEnabled | toString) "-") .Values.connectInject.metrics.defaultEnabled) (and (eq (.Values.connectInject.metrics.defaultEnabled | toString) "-") .Values.global.metrics.enabled)) }}
-default-enable-metrics=true \
{{- else }}
Expand Down
3 changes: 2 additions & 1 deletion templates/controller-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ spec:
- "-ec"
- |
consul-k8s controller \
-log-level={{ default .Values.global.logLevel .Values.controller.logLevel }} \
-log-json={{ .Values.global.logJSON }} \
-webhook-tls-cert-dir=/tmp/controller-webhook/certs \
-datacenter={{ .Values.global.datacenter }} \
-enable-leader-election \
kschoche marked this conversation as resolved.
Show resolved Hide resolved
-log-level={{ .Values.controller.logLevel | quote }} \
{{- if .Values.global.enableConsulNamespaces }}
-enable-namespaces=true \
{{- if .Values.connectInject.consulNamespaces.consulDestinationNamespace }}
Expand Down
2 changes: 2 additions & 0 deletions templates/create-federation-secret-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ spec:
- "-ec"
- |
consul-k8s create-federation-secret \
-log-level={{ .Values.global.logLevel }} \
-log-json={{ .Values.global.logJSON }} \
{{- if (and .Values.global.gossipEncryption.secretName .Values.global.gossipEncryption.secretKey) }}
-gossip-key-file=/consul/gossip/gossip.key \
{{- end }}
Expand Down
12 changes: 8 additions & 4 deletions templates/ingress-gateways-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ spec:
WAN_ADDR="${HOST_IP}"
{{- else if (or (eq $serviceType "ClusterIP") (eq $serviceType "LoadBalancer")) }}
consul-k8s service-address \
-log-level={{ $root.Values.global.logLevel }} \
-log-json={{ $root.Values.global.logJSON }} \
-k8s-namespace={{ $root.Release.Namespace }} \
-name={{ template "consul.fullname" $root }}-{{ .name }} \
-output-file=/tmp/address.txt
Expand Down Expand Up @@ -369,10 +371,10 @@ spec:
- "-ec"
- |
/consul-bin/consul services deregister \
{{- if $root.Values.global.enableConsulNamespaces }}
-namespace={{ default $defaults.consulNamespace .consulNamespace }} \
{{- end }}
-id="${POD_NAME}"
{{- if $root.Values.global.enableConsulNamespaces }}
-namespace={{ default $defaults.consulNamespace .consulNamespace }} \
{{- end }}
-id="${POD_NAME}"

# consul-sidecar ensures the ingress gateway is always registered with
# the local Consul agent, even if it loses the initial registration.
Expand Down Expand Up @@ -419,6 +421,8 @@ spec:
command:
- consul-k8s
- consul-sidecar
- -log-level={{ $root.Values.global.logLevel }}
- -log-json={{ $root.Values.global.logJSON }}
- -service-config=/consul/service/service.hcl
- -consul-binary=/consul-bin/consul
{{- if $root.Values.global.acls.manageSystemACLs }}
Expand Down
4 changes: 4 additions & 0 deletions templates/mesh-gateway-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ spec:
WAN_ADDR="${NODE_NAME}"
{{- else if and (eq $source "Service") (or (eq $serviceType "ClusterIP") (eq $serviceType "LoadBalancer")) }}
consul-k8s service-address \
-log-level={{ .Values.global.logLevel }} \
-log-json={{ .Values.global.logJSON }} \
-k8s-namespace={{ .Release.Namespace }} \
-name={{ template "consul.fullname" . }}-mesh-gateway \
-output-file=/tmp/address.txt
Expand Down Expand Up @@ -365,6 +367,8 @@ spec:
command:
- consul-k8s
- consul-sidecar
- -log-level={{ .Values.global.logLevel }}
- -log-json={{ .Values.global.logJSON }}
- -service-config=/consul/service/service.hcl
- -consul-binary=/consul-bin/consul
{{- if .Values.global.acls.manageSystemACLs }}
Expand Down
2 changes: 2 additions & 0 deletions templates/server-acl-init-cleanup-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ spec:
- consul-k8s
args:
- delete-completed-job
- -log-level={{ .Values.global.logLevel }}
- -log-json={{ .Values.global.logJSON }}
- -k8s-namespace={{ .Release.Namespace }}
- {{ template "consul.fullname" . }}-server-acl-init
resources:
Expand Down
2 changes: 2 additions & 0 deletions templates/server-acl-init-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ spec:
CONSUL_FULLNAME="{{template "consul.fullname" . }}"

consul-k8s server-acl-init \
-log-level={{ .Values.global.logLevel }} \
-log-json={{ .Values.global.logJSON }} \
-resource-prefix=${CONSUL_FULLNAME} \
-k8s-namespace={{ .Release.Namespace }} \

Expand Down
5 changes: 2 additions & 3 deletions templates/sync-catalog-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ spec:
- "-ec"
- |
consul-k8s sync-catalog \
-log-level={{ default .Values.global.logLevel .Values.syncCatalog.logLevel }} \
-log-json={{ .Values.global.logJSON }} \
-k8s-default-sync={{ .Values.syncCatalog.default }} \
{{- if (not .Values.syncCatalog.toConsul) }}
-to-consul=false \
Expand Down Expand Up @@ -143,9 +145,6 @@ spec:
{{- if .Values.syncCatalog.consulWriteInterval }}
-consul-write-interval={{ .Values.syncCatalog.consulWriteInterval }} \
{{- end }}
{{- if .Values.syncCatalog.logLevel }}
-log-level={{ .Values.syncCatalog.logLevel }} \
{{- end }}
{{- if .Values.syncCatalog.k8sTag }}
-consul-k8s-tag={{ .Values.syncCatalog.k8sTag }} \
{{- end }}
Expand Down
2 changes: 2 additions & 0 deletions templates/terminating-gateways-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ spec:
command:
- consul-k8s
- consul-sidecar
- -log-level={{ $root.Values.global.logLevel }}
- -log-json={{ $root.Values.global.logJSON }}
- -service-config=/consul/service/service.hcl
- -consul-binary=/consul-bin/consul
{{- if $root.Values.global.acls.manageSystemACLs }}
Expand Down
2 changes: 2 additions & 0 deletions templates/tls-init-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ spec:
# and use * at the start of the dns name when setting -additional-dnsname.
set -o noglob
consul-k8s tls-init \
-log-level={{ .Values.global.logLevel }} \
-log-json={{ .Values.global.logJSON }} \
-domain={{ .Values.global.domain }} \
-days=730 \
-name-prefix={{ template "consul.fullname" . }} \
Expand Down
2 changes: 2 additions & 0 deletions templates/webhook-cert-manager-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ spec:
- "-ec"
- |
consul-k8s webhook-cert-manager \
-log-level={{ .Values.global.logLevel }} \
-log-json={{ .Values.global.logJSON }} \
-config-file=/bootstrap/config/webhook-config.json \
-deployment-name={{ template "consul.fullname" . }}-webhook-cert-manager \
-deployment-namespace={{ .Release.Namespace }}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/connect-inject-deployment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ EOF
#--------------------------------------------------------------------
# logLevel

@test "connectInject/Deployment: logLevel info by default" {
@test "connectInject/Deployment: logLevel info by default from global" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/connect-inject-deployment.yaml \
Expand All @@ -1359,7 +1359,7 @@ EOF
[ "${actual}" = "true" ]
}

@test "connectInject/Deployment: logLevel can be set" {
@test "connectInject/Deployment: logLevel can be overridden" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/connect-inject-deployment.yaml \
Expand Down
31 changes: 31 additions & 0 deletions test/unit/controller-deployment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,34 @@ load _helpers
yq '[.spec.template.spec.containers[0].env[].name] | any(contains("CONSUL_HTTP_TOKEN"))' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

#--------------------------------------------------------------------
# logLevel

@test "controller/Deployment: logLevel info by default from global" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/controller-deployment.yaml \
--set 'controller.enabled=true' \
. | tee /dev/stderr |
yq '.spec.template.spec.containers[0].command' | tee /dev/stderr)

local actual=$(echo "$cmd" |
yq 'any(contains("-log-level=info"))' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "controller/Deployment: logLevel can be overridden" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/controller-deployment.yaml \
--set 'controller.enabled=true' \
--set 'controller.logLevel=error' \
. | tee /dev/stderr |
yq '.spec.template.spec.containers[0].command' | tee /dev/stderr)

local actual=$(echo "$cmd" |
yq 'any(contains("-log-level=error"))' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

4 changes: 4 additions & 0 deletions test/unit/ingress-gateways-deployment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,8 @@ key2: value2' \
yq -s -r '.[0].spec.template.spec.initContainers | map(select(.name == "service-init"))[0] | .command[2]' | tee /dev/stderr)

exp='consul-k8s service-address \
-log-level=info \
-log-json=false \
-k8s-namespace=default \
-name=RELEASE-NAME-consul-ingress-gateway \
-output-file=/tmp/address.txt
Expand Down Expand Up @@ -1239,6 +1241,8 @@ EOF
-token-sink-file=/consul/service/acl-token

consul-k8s service-address \
-log-level=info \
-log-json=false \
-k8s-namespace=default \
-name=RELEASE-NAME-consul-ingress-gateway \
-output-file=/tmp/address.txt
Expand Down
12 changes: 12 additions & 0 deletions test/unit/mesh-gateway-deployment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,8 @@ key2: value2' \
yq -r '.spec.template.spec.initContainers | map(select(.name == "service-init"))[0] | .command[2]' | tee /dev/stderr)

exp='consul-k8s service-address \
-log-level=info \
-log-json=false \
-k8s-namespace=default \
-name=RELEASE-NAME-consul-mesh-gateway \
-output-file=/tmp/address.txt
Expand Down Expand Up @@ -871,6 +873,8 @@ EOF
-token-sink-file=/consul/service/acl-token

consul-k8s service-address \
-log-level=info \
-log-json=false \
-k8s-namespace=default \
-name=RELEASE-NAME-consul-mesh-gateway \
-output-file=/tmp/address.txt
Expand Down Expand Up @@ -923,6 +927,8 @@ EOF
yq -r '.spec.template.spec.initContainers | map(select(.name == "service-init"))[0] | .command[2]' | tee /dev/stderr)

exp='consul-k8s service-address \
-log-level=info \
-log-json=false \
-k8s-namespace=default \
-name=RELEASE-NAME-consul-mesh-gateway \
-output-file=/tmp/address.txt
Expand Down Expand Up @@ -1200,6 +1206,8 @@ EOF
yq -r '.spec.template.spec.initContainers | map(select(.name == "service-init"))[0] | .command[2]' | tee /dev/stderr)

exp='consul-k8s service-address \
-log-level=info \
-log-json=false \
-k8s-namespace=default \
-name=RELEASE-NAME-consul-mesh-gateway \
-output-file=/tmp/address.txt
Expand Down Expand Up @@ -1317,6 +1325,8 @@ EOF
yq -r '.spec.template.spec.initContainers | map(select(.name == "service-init"))[0] | .command[2]' | tee /dev/stderr)

exp='consul-k8s service-address \
-log-level=info \
-log-json=false \
-k8s-namespace=default \
-name=RELEASE-NAME-consul-mesh-gateway \
-output-file=/tmp/address.txt
Expand Down Expand Up @@ -1367,6 +1377,8 @@ EOF
yq -r '.spec.template.spec.initContainers | map(select(.name == "service-init"))[0] | .command[2]' | tee /dev/stderr)

exp='consul-k8s service-address \
-log-level=info \
-log-json=false \
-k8s-namespace=default \
-name=RELEASE-NAME-consul-mesh-gateway \
-output-file=/tmp/address.txt
Expand Down
4 changes: 2 additions & 2 deletions test/unit/server-acl-init-cleanup-job.bats
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ load _helpers
-s templates/server-acl-init-cleanup-job.yaml \
--set 'global.acls.manageSystemACLs=true' \
. | tee /dev/stderr |
yq -c '.spec.template.spec.containers[0].args' | tee /dev/stderr)
[ "${actual}" = '["delete-completed-job","-k8s-namespace=default","RELEASE-NAME-consul-server-acl-init"]' ]
yq -c '.spec.template.spec.containers[0].args' | tee /dev/stderr)
[ "${actual}" = '["delete-completed-job","-log-level=info","-log-json=false","-k8s-namespace=default","RELEASE-NAME-consul-server-acl-init"]' ]
}

@test "serverACLInitCleanup/Job: enabled with externalServers.enabled=true and global.acls.manageSystemACLs=true, but server.enabled set to false" {
Expand Down
31 changes: 31 additions & 0 deletions test/unit/sync-catalog-deployment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -947,3 +947,34 @@ load _helpers

[ "${actual}" = "bar" ]
}

#--------------------------------------------------------------------
# logLevel

@test "syncCatalog/Deployment: logLevel info by default from global" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/sync-catalog-deployment.yaml \
--set 'syncCatalog.enabled=true' \
. | tee /dev/stderr |
yq '.spec.template.spec.containers[0].command' | tee /dev/stderr)

local actual=$(echo "$cmd" |
yq 'any(contains("-log-level=info"))' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "syncCatalog/Deployment: logLevel can be overridden" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/sync-catalog-deployment.yaml \
--set 'syncCatalog.enabled=true' \
--set 'syncCatalog.logLevel=debug' \
. | tee /dev/stderr |
yq '.spec.template.spec.containers[0].command' | tee /dev/stderr)

local actual=$(echo "$cmd" |
yq 'any(contains("-log-level=debug"))' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

23 changes: 18 additions & 5 deletions values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ global:
# setting `server.enabled` to true.
enabled: true

# The default log level to apply to all components which do not otherwise override this setting.
# It is recommended to generally not set this below "info" unless actively debugging due to logging verbosity.
# One of "debug", "info", "warn", or "error".
# @type: string
logLevel: "info"

# Enable all component logs to be output in JSON format.
# @type: boolean
logJSON: false

# Set the prefix used for all resources in the Helm chart. If not set,
# the prefix will be `<helm release name>-consul`.
# @type: string
Expand Down Expand Up @@ -1330,8 +1340,9 @@ syncCatalog:
memory: "50Mi"
cpu: "50m"

# Log verbosity level. One of "trace", "debug", "info", "warn", or "error".
logLevel: info
# Override global log verbosity level. One of "debug", "info", "warn", or "error".
# @type: string
logLevel: ""

# Override the default interval to perform syncing operations creating Consul services.
# @type: string
Expand Down Expand Up @@ -1435,8 +1446,9 @@ connectInject:
# @type: string
imageConsul: null

# Log verbosity level. One of "debug", "info", "warn", or "error".
logLevel: info
# Override global log verbosity level. One of "debug", "info", "warn", or "error".
# @type: string
logLevel: ""

serviceAccount:
# This value defines additional annotations for the injector service account. This should be formatted as a
Expand Down Expand Up @@ -1635,7 +1647,8 @@ controller:
replicas: 1

# Log verbosity level. One of "debug", "info", "warn", or "error".
logLevel: info
# @type: string
logLevel: ""

serviceAccount:
# This value defines additional annotations for the controller service account. This should be formatted as a
Expand Down