curl 命令参数上的 YAML 解析错误

nil*_*son 4 markdown curl yaml docker kubernetes

我正在尝试在 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
Run Code Online (Sandbox Code Playgroud)

然后我尝试运行该文件:

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

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

谢谢

Efr*_*tan 6

那是因为你的curl命令包含分号,所以 yaml 认为你正在尝试定义一个对象。修复错误:

  1. 用反斜杠转义 every "\"
  2. 将整行包裹起来"

所以-

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

应该转义到

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

http://www.yamllint.com/是追踪此类错误的好地方。