【问题标题】:error converting YAML to JSON: yaml: line 10: mapping values are not allowed in this context将 YAML 转换为 JSON 时出错:yaml:第 10 行:此上下文中不允许映射值
【发布时间】:2020-06-04 17:58:55
【问题描述】:

我已经尝试了几个小时来解决这个问题。我看不出是什么导致了这个问题。它似乎与 YAML 的第 10 行有关。我已经尝试使用和不使用引号并启动一个新文件以防出现一些损坏的值。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: "foyer-api"
  namespace: "foyer-api"
spec:
   replicas: 3
   selector:
     matchLabels:
       app: "foyer-api"
    template:
      metadata:
        labels:
          app: "foyer-api"
      spec:
        containers:
        - image: foyer-api
         imagePullPolicy: Always
          name: "foyer-api"
          ports:
          - containerPort: 80

【问题讨论】:

  • 标识好像有点不对?由于元数据和规范处于同一级别,因此它们的子级也应该处于相同的缩进:)
  • 我已经更新了缩进,但在第 10 行仍然出现同样的错误。这是 matchLabels 命令之后的行。

标签: amazon-web-services kubernetes amazon-eks aws-fargate


【解决方案1】:

此错误主要发生在复制粘贴到 yaml 中时,这可能会导致语法错误。

考虑将 yaml 粘贴到 YAML Linters(例如 this 之一)中,这在某些情况下有助于更快地识别问题。

【讨论】:

    【解决方案2】:

    在我更正缩进后,

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: "foyer-api"
      namespace: "foyer-api"
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: "foyer-api"
        template:
          metadata:
            labels:
              app: "foyer-api"
          spec:
            containers:
            - image: foyer-api
              imagePullPolicy: Always
              name: "foyer-api"
              ports:
              - containerPort: 80
    

    可以毫无问题地转成JSON

    {
      "apiVersion": "apps/v1",
      "kind": "Deployment",
      "metadata": {
        "name": "foyer-api",
        "namespace": "foyer-api"
      },
      "spec": {
        "replicas": 3,
        "selector": {
          "matchLabels": {
            "app": "foyer-api"
          },
          "template": {
            "metadata": {
              "labels": {
                "app": "foyer-api"
              }
            },
            "spec": {
              "containers": [
                {
                  "image": "foyer-api",
                  "imagePullPolicy": "Always",
                  "name": "foyer-api",
                  "ports": [
                    {
                      "containerPort": 80
                    }
                  ]
                }
              ]
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-23
      • 2019-09-12
      • 1970-01-01
      • 2021-06-04
      • 2020-04-21
      • 1970-01-01
      • 2023-03-16
      • 2020-12-01
      相关资源
      最近更新 更多