I have following pod manifest file.in that, I have defined some environment variables.
I want to assign an environment variable value to the container port as follow.
- containerPort: $(PORT_HTTP)
but this yaml trigger error when I try to create it: ValidationError(Pod.spec.containers[0].ports[0].containerPort): invalid type for io.k8s.api.core.v1.ContainerPort.containerPort: got "string", expected "integer"; if you choose to ignore these errors, turn validation off with --validate=false
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: webapp
name: webapp
spec:
containers:
- env:
- name: PORT_HTTP
value: 8080
- name: PORT_HTTPS
value: 8443
image: nginx
name: webapp
ports:
- containerPort: $(PORT_HTTP)
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
How to convert string value to integer value in yaml.
Environment variable substitution doesn't happen in kubernetes. To achieve this, you can use Helm. Or you can use shell command as follows,
( echo "cat <<EOF" ; cat pod.yaml; echo EOF ) | sh > pod-variable-resolved.yaml
And then use it to create pod in kubernetes.
kubectl apply -f pod-variable-resolved.yaml