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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ commands:
type: string
consul-k8s-image:
type: string
default: "docker.mirror.hashicorp.services/hashicorpdev/consul-k8s:latest"
default: "kschoche/consul-k8s-dev-logging"
kschoche marked this conversation as resolved.
Show resolved Hide resolved
steps:
- when:
condition: << parameters.failfast >>
Expand Down
8 changes: 5 additions & 3 deletions templates/connect-inject-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ spec:
CONSUL_FULLNAME="{{template "consul.fullname" . }}"

consul-k8s inject-connect \
{{- if .Values.connectInject.logLevel }}
-log-level={{ .Values.connectInject.logLevel }} \
{{- else }}
-log-level={{ .Values.global.logLevel }} \
{{- end }}
kschoche marked this conversation as resolved.
Show resolved Hide resolved
-default-inject={{ .Values.connectInject.default }} \
-consul-image="{{ default .Values.global.image .Values.connectInject.imageConsul }}" \
-envoy-image="{{ .Values.global.imageEnvoy }}" \
Expand All @@ -106,9 +111,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
6 changes: 5 additions & 1 deletion templates/controller-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ spec:
- "-ec"
- |
consul-k8s controller \
{{- if .Values.controller.logLevel }}
-log-level={{ .Values.controller.logLevel }} \
{{- else }}
-log-level={{ .Values.global.logLevel }} \
{{- end }}
-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
24 changes: 13 additions & 11 deletions templates/create-federation-secret-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,19 @@ spec:
- "-ec"
- |
consul-k8s create-federation-secret \
{{- if (and .Values.global.gossipEncryption.secretName .Values.global.gossipEncryption.secretKey) }}
-gossip-key-file=/consul/gossip/gossip.key \
{{- end }}
{{- if .Values.global.acls.createReplicationToken }}
-export-replication-token=true \
{{- end }}
-mesh-gateway-service-name={{ .Values.meshGateway.consulServiceName }} \
-k8s-namespace="${NAMESPACE}" \
-resource-prefix="{{ template "consul.fullname" . }}" \
-server-ca-cert-file=/consul/tls/ca/tls.crt \
-server-ca-key-file=/consul/tls/server/ca/tls.key
-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 }}
{{- if .Values.global.acls.createReplicationToken }}
-export-replication-token=true \
{{- end }}
-mesh-gateway-service-name={{ .Values.meshGateway.consulServiceName }} \
-k8s-namespace="${NAMESPACE}" \
-resource-prefix="{{ template "consul.fullname" . }}" \
-server-ca-cert-file=/consul/tls/ca/tls.crt \
-server-ca-key-file=/consul/tls/server/ca/tls.key
kschoche marked this conversation as resolved.
Show resolved Hide resolved
resources:
requests:
memory: "50Mi"
Expand Down
30 changes: 18 additions & 12 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 @@ -368,11 +370,11 @@ spec:
- "/bin/sh"
- "-ec"
- |
/consul-bin/consul services deregister \
{{- if $root.Values.global.enableConsulNamespaces }}
-namespace={{ default $defaults.consulNamespace .consulNamespace }} \
{{- end }}
-id="${POD_NAME}"
/consul-bin/consul services deregister \
{{- if $root.Values.global.enableConsulNamespaces }}
-namespace={{ default $defaults.consulNamespace .consulNamespace }} \
{{- end }}
-id="${POD_NAME}"
kschoche marked this conversation as resolved.
Show resolved Hide resolved

# 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 @@ -417,13 +419,17 @@ spec:
value: http://$(HOST_IP):8500
{{- end }}
command:
- consul-k8s
- consul-sidecar
- -service-config=/consul/service/service.hcl
- -consul-binary=/consul-bin/consul
{{- if $root.Values.global.acls.manageSystemACLs }}
- -token-file=/consul/service/acl-token
{{- end }}
- "/bin/sh"
- "-ec"
kschoche marked this conversation as resolved.
Show resolved Hide resolved
- |
consul-k8s consul-sidecar \
-log-level={{ $root.Values.global.logLevel }} \
-log-json={{ $root.Values.global.logJSON }} \
-service-config=/consul/service/service.hcl \
{{- if $root.Values.global.acls.manageSystemACLs }}
-token-file=/consul/service/acl-token \
{{- end }}
-consul-binary=/consul-bin/consul
{{- if (default $defaults.priorityClassName .priorityClassName) }}
priorityClassName: {{ default $defaults.priorityClassName .priorityClassName | quote }}
{{- end }}
Expand Down
20 changes: 13 additions & 7 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 @@ -363,13 +365,17 @@ spec:
value: http://$(HOST_IP):8500
{{- end }}
command:
- consul-k8s
- consul-sidecar
- -service-config=/consul/service/service.hcl
- -consul-binary=/consul-bin/consul
{{- if .Values.global.acls.manageSystemACLs }}
- -token-file=/consul/service/acl-token
{{- end }}
- "/bin/sh"
- "-ec"
- |
consul-k8s consul-sidecar \
-log-level={{ .Values.global.logLevel }} \
-log-json={{ .Values.global.logJSON }} \
-service-config=/consul/service/service.hcl \
{{- if .Values.global.acls.manageSystemACLs }}
-token-file=/consul/service/acl-token \
{{- end }}
-consul-binary=/consul-bin/consul
{{- if .Values.meshGateway.priorityClassName }}
priorityClassName: {{ .Values.meshGateway.priorityClassName | quote }}
{{- end }}
Expand Down
13 changes: 8 additions & 5 deletions templates/server-acl-init-cleanup-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ spec:
- name: server-acl-init-cleanup
image: {{ .Values.global.imageK8S }}
command:
- consul-k8s
args:
- delete-completed-job
- -k8s-namespace={{ .Release.Namespace }}
- {{ template "consul.fullname" . }}-server-acl-init
- "/bin/sh"
- "-ec"
- |
consul-k8s delete-completed-job \
-log-level={{ .Values.global.logLevel }} \
-log-json={{ .Values.global.logJSON }} \
-k8s-namespace={{ .Release.Namespace }} \
{{ template "consul.fullname" . }}-server-acl-init
resources:
requests:
memory: "50Mi"
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
9 changes: 6 additions & 3 deletions templates/sync-catalog-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ spec:
- "-ec"
- |
consul-k8s sync-catalog \
{{- if .Values.syncCatalog.logLevel }}
-log-level={{ .Values.syncCatalog.logLevel }} \
{{- else }}
-log-level={{ .Values.global.logLevel }} \
{{- end }}
-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 +149,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
18 changes: 11 additions & 7 deletions templates/terminating-gateways-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,17 @@ spec:
value: http://$(HOST_IP):8500
{{- end }}
command:
- consul-k8s
- consul-sidecar
- -service-config=/consul/service/service.hcl
- -consul-binary=/consul-bin/consul
{{- if $root.Values.global.acls.manageSystemACLs }}
- -token-file=/consul/service/acl-token
{{- end }}
- "/bin/sh"
- "-ec"
- |
consul-k8s consul-sidecar \
-log-level={{ $root.Values.global.logLevel }} \
-log-json={{ $root.Values.global.logJSON }} \
-service-config=/consul/service/service.hcl \
{{- if $root.Values.global.acls.manageSystemACLs }}
-token-file=/consul/service/acl-token \
{{- end }}
-consul-binary=/consul-bin/consul
{{- if (default $defaults.priorityClassName .priorityClassName) }}
priorityClassName: {{ (default $defaults.priorityClassName .priorityClassName) | quote }}
{{- end }}
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=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" ]
}

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
9 changes: 7 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,13 @@ 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 -r '.spec.template.spec.containers[0].command[2]' | tee /dev/stderr)
exp='consul-k8s delete-completed-job \
-log-level=info \
-log-json=false \
-k8s-namespace=default \
RELEASE-NAME-consul-server-acl-init'
[ "${actual}" = "${exp}" ]
}

@test "serverACLInitCleanup/Job: enabled with externalServers.enabled=true and global.acls.manageSystemACLs=true, but server.enabled set to false" {
Expand Down
Loading