<p>For my AKS container setup, I would like to pass the requested number of replicas of a given statefulset to each pod through environment variables.</p>

<p>I was trying to do this without repeating myself (once in the "replicas" setting and once in the setting of the environment variables).</p>

<p>The only real solution I could find of how to do this was using anchors and aliases as such (based on <a href="https://stackoverflow.com/questions/48632096/kubernetes-statefulset-obtain-spec-replicas-metadata-and-reference-elsewhere-i">Kubernetes StatefulSet - obtain spec.replicas metadata and reference elsewhere in configuration</a>):</p>

<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: solr
spec:
  selector:
    matchLabels:
      app: solr
  serviceName: solr-hs
  replicas: &amp;numReplicas 3
  updateStrategy:
    type: RollingUpdate
  # Make sure pods get created sequentially
  podManagementPolicy: OrderedReady
  template:
    metadata:
      labels:
        app: solr
    spec:
      containers:
      - name: kubernetes-solr
        imagePullPolicy: Always
        image: "..."
        resources:
          requests:
            memory: "8Gi"
            cpu: "0.5"
        ports:
        - containerPort: 8983
        env:
        - name: N_O_REPLICAS
          value: *numReplicas
</code></pre>

<p>Unfortunately, it seems like the "env" value has to be a string, and the integer value of "replicas" does not get cast or transformed. The following error gets thrown instead:</p>

<blockquote>
  <p>v1.EnvVar.v1.EnvVar.Value: ReadString: expects " or n, but found 3,
  error found in #10 byte of ...|,"value":3},{"name":|..., bigger
  context ...|:"solr-config"}}},{"name":"N_O_REPLICAS","value":3},</p>
</blockquote>

<p>I tried casting to a string manually by writing:</p>

<pre><code>value: !!str *numReplicas
</code></pre>

<p>But this also doesn't work and throws the following error:</p>

<blockquote>
  <p>error converting YAML to JSON: yaml: line 52: did not find expected key</p>
</blockquote>

<p>Is there any way to create a Kubernetes YAML file that allows the reusing of integer values as strings? Or is there another solution for this particular situation?</p>

<p>Although your approach is interesting, <code>!!str</code> is not a casting operator and the <a href="http://yaml.org/spec/1.2/spec.html#id2768011" rel="nofollow noreferrer">YAML specification</a> clearly indicates that what you tried is not going to work:</p>

<blockquote>
  <p>When a node has more than one occurrence (using aliases), tag resolution must depend only on the path to the first (anchored) occurrence of the node.</p>
</blockquote>

<p>So within YAML this is not possible, unless the parser/loader is non-conform. </p>

<p>The best solution for your problem, IMO, would be that  kubernetes explicitly casts all parameters that will be environment variables to a string before adding them to the environment. That way you could use booleans, dates, etc. as well. </p>

<p>You can also use any templating system that you like to generate the YAML input for kubernetes as long as such a system allows you to "stringify" your integer parameter.</p>
