为什么明显格式错误的 yaml 是验证的那个?

编程入门 行业动态 更新时间:2024-10-25 20:20:42
本文介绍了为什么明显格式错误的 yaml 是验证的那个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在下面的第一个 yaml 中,第二个 podSelector 子句(在 to 下)似乎格式正确,matchLabels 缩进了两个空格,一致使用标准和 yaml 的其余部分.

In the first yaml below, the second podSelector clause (under to) seems correctly formatted, with two spaces indent for matchLabels, consistent with standards and the rest of the yaml.

第二个 yaml 是相同的,但 matchLabels 有四个空格.此格式遵循 Kubernetes 文档.(没有选项卡.)

The second yaml is identical, but matchLabels has four spaces. This format follows the Kubernetes documentation. (There are no tabs.)

然而第一个 yaml 失败 kubectl 验证,错误验证p.yaml":错误验证数据:ValidationError(NetworkPolicy.spec.egress[0].to[0]):io.k8s.apiworking.v1.NetworkPolicyPeer 中的未知字段matchLabels",第二次通过验证.

Yet the first yaml fails kubectl validation with error validating "p.yaml": error validating data: ValidationError(NetworkPolicy.spec.egress[0].to[0]): unknown field "matchLabels" in io.k8s.apiworking.v1.NetworkPolicyPeer, and the second passes validation.

这没有通过验证:

 apiVersion: networking.k8s.io/v1
 kind: NetworkPolicy
 metadata:
   name: internal-policy
 spec:
   podSelector:
     matchLabels:
       name: internal
   policyTypes:
   - Egress
   egress:
   - to:
     - podSelector:
       matchLabels:
         name: mysql

这通过验证:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: internal-policy
spec:
  podSelector:
    matchLabels:
      name: internal
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels:
          name: mysql

推荐答案

显然 matchLabels 应该是 podSelector 的映射值中的一个键,因此它必须更多缩进.这:

Well apparently matchLabels should be a key in the mapping value of podSelector, hence it must be more indented. This:

- podSelector:
  matchLabels:

matchLabels 置于与 podSelector 相同的缩进级别,因为根据 YAML 规范,初始 - 被视为缩进的一部分.基本上,这里定义了两个缩进级别:

Places matchLabels on the same indentation level as podSelector, since the initial - is treated as part of the indentation as per YAML spec. Basically, there are two indentation levels defined here:

序列的级别,以-开头.所有后续序列项必须具有相同级别的 -.映射的级别,它是序列的值,以 p 开头.映射的所有后续键都必须从同一级别开始. The level of the sequence, starting with -. All subsequent sequence items must have their - at the same level. The level of the mapping which is a value of the sequence, starting with p. All subsequent keys of the mapping must start at the same level.

因此,如果你想让 matchLabels 嵌套在 podSelector 中,你必须把它缩进更多:

Therefore, if you want matchLabels to be nested in podSelector, you must indent it more:

- podSelector:
    matchLabels:

这篇关于为什么明显格式错误的 yaml 是验证的那个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 10:51:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408865.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   格式   yaml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!