首页 > 解决方案 > curl命令参数上的YAML解析错误

问题描述

我正在尝试在 k8s 中创建一个小型 cronjob,它只是在busybox 容器中使用 curl 执行 HTTP Post。格式是错误的,我不知道我必须改变什么。

我试过用谷歌搜索我收到的错误消息,并以各种方式更改 curl 命令的格式,但没有任何成功。

apiVersion: batch/v1beta1
kind: CronJob
#namespace: test
metadata:
  name: test-cron
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: test-cron
            image: busybox
            args:
            - /bin/sh
            - -c
            - curl "https://test-es01.test.svc.clu01.test.local:9200/logs_write/_rollover" -H 'Content-Type: application/json' -d '{"conditions: {"max_size": "5gb"}}'
          restartPolicy: OnFailure

然后我尝试运行该文件:

kubectl -n test apply -f test-cron.yaml

并得到以下错误:

error: error parsing test-cron.yaml: error converting YAML to JSON: yaml: line 20: mapping values are not allowed in this context

有谁知道格式的问题是什么?

谢谢

标签: dockercurlkubernetesyamlmarkdown

解决方案


thats because your curl command contains semicolon so yaml thinks you are trying to define an object. to fix the error:

  1. escape every " with backlslash: \"
  2. wrap the entire line with "

therefore-

 - curl "https://test-es01.test.svc.clu01.test.local:9200/logs_write/_rollover" -H 'Content-Type: application/json' -d '{"conditions: {"max_size": "5gb"}}'

should be escaped to

- "curl \"https://test-es01.test.svc.clu01.test.local:9200/logs_write/_rollover\" -H 'Content-Type: application/json' -d '{\"conditions: {\"max_size\": \"5gb\"}}'\n"

http://www.yamllint.com/ is a great place to track down such bugs.


推荐阅读