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.
I was trying to do this without repeating myself (once in the "replicas" setting and once in the setting of the environment variables).
The only real solution I could find of how to do this was using anchors and aliases as such (based on Kubernetes StatefulSet - obtain spec.replicas metadata and reference elsewhere in configuration):
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: solr
spec:
selector:
matchLabels:
app: solr
serviceName: solr-hs
replicas: &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
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:
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},
I tried casting to a string manually by writing:
value: !!str *numReplicas
But this also doesn't work and throws the following error:
error converting YAML to JSON: yaml: line 52: did not find expected key
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?