Home > Back-end >  Kubernetes YAML file with if-else condition
Kubernetes YAML file with if-else condition

Time:03-12

I have yaml file which use to deploy my application in all the environments. I want to add some JVM args only for test environment . is there anyway i can do it in YAML file?

here is the yaml

apiVersion: v1
kind: Pod
metadata:
  name: rss-site
  labels:
    app: web
spec:
  containers:
    - name: front-end
      image: nginx
      ports:
        - containerPort: 80
    - name: rss-reader
      image: nickchase/rss-php-nginx:v1
      ports:
        - containerPort: 88
      env:
       - name: JAVA_OPTS
         value: "
                -Dlog4j.configurationFile=log4j2.xml
                -Denable.scan=true

                "

here i want -Denable.scan=true to be conditional and should add only for Test environment .

I tried following way but it not working and kubernete throwing error error converting YAML to JSON: yaml: line 53: did not find expected key

Tried:-

apiVersion: v1
kind: Pod
metadata:
  name: rss-site
  labels:
    app: web
spec:
  containers:
    - name: front-end
      image: nginx
      ports:
        - containerPort: 80
    - name: rss-reader
      image: nickchase/rss-php-nginx:v1
      ports:
        - containerPort: 88
      env:
       - name: JAVA_OPTS
         value: "
                -Dlog4j.configurationFile=log4j2.xml
                ${{if eq "TEST" "TEST" }} # just sample condition , it will change
                -Denable.scan=true
                 ${{end }}

                "

CodePudding user response:

helm will do that. In fact, the syntax is almost identical to what you've put, and would be something like this:

      env:
       - name: JAVA_OPTS
         value: "
                -Dlog4j.configurationFile=log4j2.xml
                 {{- if eq .Values.profile "TEST" }}, it will change
                -Denable.scan=true
                 {{- end }}

                "

And you declare via the install package (called a Chart) which profile you want to use (i.e. you set the .Values.profile value)

You can check out https://helm.sh/ for details and examples

  • Related