PinkElephantsOnParade
PinkElephantsOnParade

Reputation: 6592

Kubernetes Cronjob with env variables

I want to run a kuberenetes cronjob, but my command for the cronjob relies on environment variables to be defined or it will not work. When I set the env variables in the cronjob yaml it mentions that this is invalid YAML, with this message:

error: error parsing mapping_rake.yaml: error converting YAML to JSON: yaml: line 77: did not find expected key

Line 77 is the line defining the command (command: ["rake", "mapping:check"]). I am not sure why inclusion of env variables would invalidate/make impossible the command to be passed to the pod that will be instantiated to execute the cronjob. Here is my yaml body:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: bob-mapping-rake-cron
spec:
  schedule: "57 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
         volumes:
           - name: nfs-volume
             nfs:
              server: vcsnas.pvt # Change this!
              path: /ifs/ifs/AssetFlow
         containers:
              - env:
                  - name: ORIGIN_PATH
                    value: /mnt/Content/BobBarkerTesting/est_pricing
                  - name: FINAL_PATH
                    value: /mnt/Content/BobBarkerTesting/est_pricing/final
                  - name: SCANNED_PATH
                    value: /mnt/Content/BobBarkerTesting/est_pricing/scanned
                  - name: EST_BUNDLES
                    value: /mnt/Content/BobBarkerTesting/est_bundles
                  - name: SCANNED_BUNDLES
                    value: /mnt/Content/BobBarkerTesting/incoming_est_bundles/scanned
                  - name: INCOMING_BUNDLES
                    value: /mnt/Content/BobBarkerTesting/incoming_est_bundles
                  - name: INCOMING_SWAPS
                    value: /mnt/Content/BobBarkerTesting/locker_swap/ingest
                  - name: OUTPUT_FOLDER
                    value: /mnt/Content/BobBarkerTesting/locker_swap/output
                  - name: PROCESSED_FOLDER
                    value: /mnt/Content/BobBarkerTesting/locker_swap/processed
                  - name: FAILED_FOLDER
                    value: /mnt/Content/BobBarkerTesting/locker_swap/failed
                  - name: LDAP_HOST
                    value: 172.17.157.21
                  - name: LDAP_PORT
                    value: '636'
                  - name: LDAP_ATTRIBUTE
                    value: uid
                  - name: LDAP_BASE
                    value: ou=people,dc=cox,dc=net
                  - name: LDAP_USER
                    valueFrom:
                      secretKeyRef:
                        name: ldap
                        key: username
                  - name: LDAP_PASSWORD
                    valueFrom:
                      secretKeyRef:
                        name: ldap
                        key: password
                  - name: LDAP_SSL
                    value: simple_tls
                  - name: DB_HOST
                    value: mysql.bb.svc.cluster.local
                  - name: DB_USER
                    valueFrom:
                      secretKeyRef:
                        name: db
                        key: username
                  - name: DB_PW
                    valueFrom:
                      secretKeyRef:
                        name: db
                        key: password
                  - name: DB
                    value: pesto_prod
                volumeMounts:
                  - name: nfs-volume
                    mountPath: /mnt
                name: bob-barker-mapping-rake-cron
                image: repo.corp.cox.com:5005/vodcontent/bob-barker-mapping-rake:v2
                command: ["rake", "mapping:check"]
            restartPolicy: Never
  concurrencyPolicy: Replace

Are you allowed to define env variables for the containers that will execute a Kubernetes cronjob? Why or why not? Is there an alternative to do this by way of cronjob? If not I can employ a different way, but this is more idiomatic for my use case so I wanted to try it.

Upvotes: 1

Views: 4645

Answers (1)

Emruz Hossain
Emruz Hossain

Reputation: 5528

Your CronJob YAML has some indentation issues. Here, is the correctly indented YAML:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: bob-mapping-rake-cron
spec:
  schedule: "57 * * * *"
  concurrencyPolicy: Replace
  jobTemplate:
    spec:
      template:
        spec:
          volumes:
          - name: nfs-volume
            nfs:
              server: vcsnas.pvt # Change this!
              path: /ifs/ifs/AssetFlow
          containers:
          - name: bob-barker-mapping-rake-cron
            image: repo.corp.cox.com:5005/vodcontent/bob-barker-mapping-rake:v2
            command: ["rake", "mapping:check"]
            volumeMounts:
            - name: nfs-volume
              mountPath: /mnt
            env:
            - name: ORIGIN_PATH
              value: /mnt/Content/BobBarkerTesting/est_pricing
            - name: FINAL_PATH
              value: /mnt/Content/BobBarkerTesting/est_pricing/final
            - name: SCANNED_PATH
              value: /mnt/Content/BobBarkerTesting/est_pricing/scanned
            - name: EST_BUNDLES
              value: /mnt/Content/BobBarkerTesting/est_bundles
            - name: SCANNED_BUNDLES
              value: /mnt/Content/BobBarkerTesting/incoming_est_bundles/scanned
            - name: INCOMING_BUNDLES
              value: /mnt/Content/BobBarkerTesting/incoming_est_bundles
            - name: INCOMING_SWAPS
              value: /mnt/Content/BobBarkerTesting/locker_swap/ingest
            - name: OUTPUT_FOLDER
              value: /mnt/Content/BobBarkerTesting/locker_swap/output
            - name: PROCESSED_FOLDER
              value: /mnt/Content/BobBarkerTesting/locker_swap/processed
            - name: FAILED_FOLDER
              value: /mnt/Content/BobBarkerTesting/locker_swap/failed
            - name: LDAP_HOST
              value: 172.17.157.21
            - name: LDAP_PORT
              value: '636'
            - name: LDAP_ATTRIBUTE
              value: uid
            - name: LDAP_BASE
              value: ou=people,dc=cox,dc=net
            - name: LDAP_USER
              value: pesto_prod
            - name: LDAP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ldap
                  key: username
            - name: LDAP_SSL
              valueFrom:
                secretKeyRef:
                  name: ldap
                  key: password
            - name: DB_HOST
              value: simple_tls
            - name: DB_USER
              value: mysql.bb.svc.cluster.local
            - name: DB_PW
              valueFrom:
                secretKeyRef:
                  name: db
                  key: username
            - name: DB
              valueFrom:
                secretKeyRef:
                  name: db
                  key: password
          restartPolicy: Never

Upvotes: 7

Related Questions