Pass variable that includes double quotes (") in its value to a container from K8s deployment
Asked Answered
D

1

7

I am trying to deploy the statsd exporter (https://github.com/prometheus/statsd_exporter) software as a docker container in a K8s cluster. However, I want some parameters to be configurable. In order to do that, I am passing some arguments to the container via K8s deployment in a yaml format. When these arguments do not contain the double quotes character ("), everything works fine. However, if the desired value of the introduced variables contains double quotes, K8s interprets them in a wrong way (something similar is described in Pass json string to environment variable in a k8s deployment for Envoy). What I want to set is the --statsd.listen-tcp=":<port>" argument, and I am using command and args in K8s deployment:

- name: statsd-exporter
  image: prom/statsd-exporter:v0.12.2
    ...
  command: ["/bin/statsd_exporter"]
  args: ['--log.level="debug"', '--statsd.listen-tcp=":9999"']

When I deploy it in K8s and check the content of the "running" deployment, everything seems to be right:

command:
- /bin/statsd_exporter
args:
- --log.level="debug"
- --statsd.listen-tcp=":9999"

However, the container never starts, giving the following error:

time="..." level=fatal msg="Unable to resolve \": lookup \": no such host" source="main.go:64"

I think that K8s is trying to "scape" the double quotes and it is passing them adding the backslash to the container, so the latter cannot understand them. I have also tried to write the args as

args: ["--log.level=\"debug\"", "--statsd.listen-tcp=\":9999\""]

and the same happens. I have also tried to pass them as env variables, and all the times the same problem is happening: the double quotes are not parsed in the right way.

Any idea regarding some possible solution?

Thanks!

Duckboard answered 6/11, 2019 at 14:25 Comment(1)
What about replacing double quotes by single quotes or not using quotes at all ? It looks like " is passed as a string \" (escaped ") and application tries to resolve this string as a hostname. "When these arguments do not contain the double quotes character ("), everything works fine." Is there any particular reason you need to use double quotes ? Did you try using --statsd.listen-tcp= flag by providing full hostname with port without using any quotes ?Arita
A
2

According to the source code, statsd-exporter uses kingpin for command line and flag parser. If I am not mistaken, kingpin doesn't require values to be surrounded by double quotes.

I would suggest to try:

- name: statsd-exporter
  image: prom/statsd-exporter:v0.12.2
    ...
  command: ["/bin/statsd_exporter"]
  args:
  - --log.level=debug
  - --statsd.listen-tcp=:9999

Reason being is that according to the source code here, the input value for statsd.listen-tcp is split into host and port and it seems the the host per the error message gets the value of a double quote character ".

Aforethought answered 6/11, 2019 at 19:57 Comment(1)
@SalvadorForadada, does this answer your question ? Did you try this solution ?Arita

© 2022 - 2024 — McMap. All rights reserved.