<p>How can I add space in <code>--set</code> switch with helm command.
I need spaces between <code>"mongodb://datadog:'${DB_PASSWORD}'@%%host%%:%%port%%",</code> and <code>"replica_check": true,</code> and also between <code>"replica_check": true,</code> and  <code>"additional_metrics": ["metrics.commands","tcmalloc","top","collection"]}]'</code></p>

<p>It gives me error as :
 <code>that set cannot end with ,</code></p>

<pre><code>helm upgrade myservice helm/charts/myservice \
-f helm/charts/chat/values.yaml \
--set "mongodb-replicaset.podAnnotations.ad\.datadoghq\.com/mongodb-replicaset\.instances"='[{"server": "mongodb://datadog:'${DB_PASSWORD}'@%%host%%:%%port%%", "replica_check": true, "additional_metrics": ["metrics.commands","tcmalloc","top","collection"]}]' \
--wait --install```
</code></pre>

cloudbud
<p>If you're trying to pass something really complicated like that, it might be easier to package it up in a YAML file and pass it with the <code>helm upgrade -f</code> option.</p>

<pre class="lang-yaml prettyprint-override"><code>mongodb-replicaset.podAnnotations.ad.datadoghq.com/mongodb-replicaset.instances: &gt;-
  [
    {
      "server": "mongodb://datadog:passw0rd@%%host%%:%%port%%",
      "replica_check": true,
      "additional_metrics": ["metrics.commands","tcmalloc","top","collection"]
    }
  ]
</code></pre>

<p>(The <code>&gt;-</code> marker is YAML syntax causing the following indented block to be processed as a string, folding newlines into spaces, and trimming leading and trailing newlines.)</p>

<p>Since valid JSON is valid YAML, if you're trying to launch this from a script, you could use your language-native JSON serializer.</p>

<pre class="lang-python prettyprint-override"><code>import json, os
db_password = os.environ.get('DB_PASSWORD')
instances = [{
  "server": "mongodb://datadog:" + db_password + "@%%host:%%port",
  "replica_check": True,
  "additional_metrics": ["metrics.commands", "tcmalloc", "top", "collection"]
}]
annotations = {
  "mongodb-replicaset.podAnnotations.ad.datadoghq.com/mongodb-replicaset.instances": json.dumps(instances)
}
print(json.dumps(annotations))
</code></pre>

<p>Once you've done this you can pass the generated file to the <code>helm upgrade -f</code> option.  (You don't need to include the chart's own <code>values.yaml</code> file, Helm includes this by default and your values override it.)</p>

<pre><code>helm upgrade myservice helm/charts/myservice \
  -f mongo-dd.yaml \
  --wait --install
</code></pre>

<p>Yet another option is to generate this inside the chart itself.  Helm includes an undocumented <code>toJson</code> template function; you can construct objects using the Sprig <a href="http://masterminds.github.io/sprig/dicts.html" rel="nofollow noreferrer"><code>dict</code></a> function.</p>

<pre><code>{{- $server := printf "mongodb://datadog:%s@%%%%host%%%%:%%%%port%%%%" .Values.mongodbPassword -}}
{{- $metrics := list "metrics.commands" "tcmalloc" "top" "collection" -}}
{{- $instance := dict "server" $server "replica_check" true "additional_metrics" $metrics -}}
{{- $instances := list $instance -}}
mongodb-replicaset...instances: {{ toJson $instances }}
</code></pre>

<p>Your error message suggests your problem isn't the spaces <em>per se</em>, but that <code>helm --set</code> treats commas as separators between multiple key/value pairs.  You can <a href="https://helm.sh/docs/intro/using_helm/#the-format-and-limitations-of-set" rel="nofollow noreferrer">backslash-escape the commas</a></p>

<pre><code>... --set 'mongodb-replicaset..."replica_check": true\, "additional_metrics": ["metrics.commands\,"tcmalloc"\,...]}]'
</code></pre>
