Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes ValidationError invalid type for io.k8s.api.core.v1.ContainerPort.containerPort: got "string", expected "integer";

Tags:

kubernetes

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.

like image 482
Niranga Sandaruwan Avatar asked Oct 23 '25 09:10

Niranga Sandaruwan


1 Answers

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
like image 176
Malathi Avatar answered Oct 25 '25 07:10

Malathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!