kddiji
kddiji

Reputation: 225

K8 yaml : How to properly parse long string as an argument

As simple it can sounds I am just trying to append these 2 commands below as arguments in my pod during run time. I tested those 2 commands by going inside the pod and running them manually and it worked like a charm. I am just trying to inject those commands inside the cronjob yaml and been scratching my head. Haven't found the right way.

Command 1

export TOKEN=`curl -H "Content-Type: application/json" -H "${API_VS_HD}" --request POST --data "{\"providerName\":\"local\",\"username\":\"admin\",\"password\":\"$PASSWORD\",\"deviceId\":\"$UUID\"}" https://$CONTROLLER_IP:444/admin/login --insecure | jq -r '.token'`

Command 2

curl -k -H "Content-Type: application/json" \
        -H "$API_VS_HD" \
        -H "Authorization: Bearer $TOKEN" \
        -X GET \
        https://$CONTROLLER_IP:444/admin/license/users

My final current cronjob.yaml file

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: device-cron-job
  namespace: device-purge
spec:
#Cron Time is set according to server time, ensure server time zone and set accordingly.
  schedule: "*/2 * * * *" # test
  jobTemplate:
    spec:
      template:
        spec:
          imagePullSecrets:
          - name: cron
          containers:
          - name: device-cron-pod
            image: harbor/ops5/private/:device-purge
            command: ["/bin/sh", "-c"]
            args:
             - export TOKEN="curl -H \"Content-Type: application/json\" -H \"${API_VS_HD}\" --request POST --data \"{\\\"providerName\\\":\\\"local\\\",\\\"username\\\":\\\"admin\\\",\\\"password\\\":\\\"$PASSWORD\\\",\\\"deviceId\\\":\\\"$UUID\\\"}\" https://$CONTROLLER_IP:444/admin/login --insecure | jq -r '.token'";
             - command 2; # should be the curl command
            env:
            - name: AWS_REGION
              value: "us-east-1"
            - name: API_VS_HD
              value: "Accept:application/vnd.appgate.peer-v13+json"
            - name: CONTROLLER_IP
              value: "52.##.###.###"
            - name: UUID
              value: "d2b78ec2-####-###-###-#########"
            - name: PASSWORD
              valueFrom:
                secretKeyRef: 
                  name: password
                  key: password
            imagePullPolicy: Always
          restartPolicy: OnFailure
      backoffLimit: 3

The current error without appending my second command:

error: error validating "cronjob.yaml": error validating data: ValidationError(CronJob.spec.jobTemplate.spec.template.spec.containers[0].args[0]): invalid type for io.k8s.api.core.v1.Container.args: got "map", expected "string"; if you choose to ignore these errors, turn validation off with --validate=false

Upvotes: 0

Views: 706

Answers (1)

Brenk
Brenk

Reputation: 26

You can use something like YAML multiline block scalars

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: device-cron-job
  namespace: default
spec:
#Cron Time is set according to server time, ensure server time zone and set accordingly.
  schedule: "*/2 * * * *" # test
  jobTemplate:
    spec:
      template:
        spec:
          imagePullSecrets:
          - name: cron
          containers:
          - name: device-cron-pod
            image: harbor/ops5/private/:device-purge
            command: 
             - "/bin/sh"
             - -c
             - |
               export TOKEN=`curl -H "Content-Type: application/json" -H "${API_VS_HD}" --request POST --data "{\"providerName\":\"local\",\"username\":\"admin\",\"password\":\"$PASSWORD\",\"deviceId\":\"$UUID\"}" https://$CONTROLLER_IP:444/admin/login --insecure | jq -r '.token'`
               curl -k -H "Content-Type: application/json" \
               -H "$API_VS_HD" \
               -H "Authorization: Bearer $TOKEN" \
               -X GET \
               https://$CONTROLLER_IP:444/admin/license/users
            env:
            - name: AWS_REGION
              value: "us-east-1"
            - name: API_VS_HD
              value: "Accept:application/vnd.appgate.peer-v13+json"
            - name: CONTROLLER_IP
              value: "52.##.###.###"
            - name: UUID
              value: "d2b78ec2-####-###-###-#########"
            - name: PASSWORD
              valueFrom:
                secretKeyRef: 
                  name: password
                  key: password
            imagePullPolicy: Always
          restartPolicy: OnFailure
      backoffLimit: 3

Otherwise the expectation is

command: ["/bin/sh","-c"]
args: ["command 1; command 2"]

instead of

command: ["/bin/sh", "-c"]
args:
- command 1
- command 2

hence kubernetes throwing the error invalid type for io.k8s.api.core.v1.Container.args: got "map", expected "string";

Upvotes: 1

Related Questions