Managing Kubernetes Ingress JSON Log Format with Sanity
The nginx ingress supports a custom log format. The option can be set via the log_format
field in the ingress configmap.
As you can see, the log format must be a single line. I wanted to implement the Elasticsearch Common Schema
for our nginx ingress access log. Writing the ECS JSON for our access log results in:
|
|
This 34 lines of JSON must be condensed to one line. Doing this manually is error prone. Moreover, maintaining this JSON
struct in one line is nearly impossible. Since we manage the deployment of our ingress in a helm chart, I used the
nospace
sprig function. This function removes every whitespace from the input string.
Be aware, this approach works only if your JSON struct contains no whitespace. The whitespaces from your JSON key and values
will be stripped as well.
I saved the JSON struct to the file config/logpattern.json
in the helm chart.
My templates/configmap.yml
looks like this:
|
|
The .Files.Get
loads the JSON log pattern from the log pattern file and pipes that into the nospace
function.
This results in the following rendered configmap:
|
|
Chart directory layout:
|
|