问题描述
限时送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:关键词]
发布评论