I have the following Kubernets CronJob definition:
apiVersion: batch/v1
kind: CronJob
metadata:
name: myCronJob
spec:
schedule: "*/1 * * * *"
failedJobsHistoryLimit: 1
successfulJobsHistoryLimit: 1
jobTemplate:
spec:
backoffLimit: 2
parallelism: 1
template:
metadata:
annotations:
linkerd.io/inject: disabled
spec:
containers:
- name: myCronJob
image: curlimages/curl:7.72.0
env:
- name: CURRENT_TIME
value: $(date +"%Y-%m-%dT%H:%M:%S.%sZ")
args:
- /bin/sh
- -ec
- "curl --request POST --header 'Content-type: application/json' -d '{\"Label\":\"myServiceBusLabel\",\"Data\":\"{\'TimeStamp\':\'$(echo $CURRENT_TIME)\'}\",\"QueueName\":\"myServiceBusQueue\"}' https://mycronjobproxy.net/api/HttpTrigger?code=mySecretCode"
restartPolicy: OnFailure
Notice I pass a dynamic date to curl
via an environment variable, as described here.
However, this produces an error at runtime (copied from K9s):
curl: (3) unmatched close brace/bracket in URL position 26:
+"%Y-%m-%dT%H:%M:%S.%sZ")}","QueueName":"myServiceBusQueue"}
^
I suspect this is likely an issue with combining double- and single quotes and escape characters. The curl
command runs fine locally on macOS, but not when deployed using the curlimages/curl:7.72.0
. There seems to be some difference in behavior.
On macOS, on my local dev machine, I can run the command like so:
curl --request POST --header "Content-type: application/json" -d "{'Label':'myServiceBusLabel','Data':{'TimeStamp':'$(echo $CURRENT_TIME)'},'QueueName':'myServiceBusQueue'}" https://mycronjobproxy.net/api/HttpTrigger?code=mySecretCode
Output:
Message was successfully sent using the following params: Label = myServiceBusLabel | Data = {"TimeStamp": "2023-05-15T15:44:45.1684158285Z"} | QueueName = myServiceBusQueue%
But when I use that version in my K8s CronJob YAML file, my IDE (JetBrains Rider) says: "Scalar value expected." It seems like the whole command must be enclosed in double quotes.
What is the correct way to quote/escape this curl
command?