【发布时间】:2019-08-28 23:13:14
【问题描述】:
我正在尝试为 Kubernetes 创建我的角色 YAML 文件,但我遇到了所需 YAML 的这个特定部分:
rules:
- apiGroups: [""] # "" indicates the core API group
resources: ["pods"]
verbs: ["get", "watch", "list"]
我尝试将其添加为字典,然后将其添加为 -apiGroups 行中包含字典的列表,但这会导致规则的其余参数出现问题。即使我指定了default_flow_style=False
def create_role_yml(role_filename, team_name, group_user):
"""
https://kubernetes.io/docs/reference/
access-authn-authz/rbac/#role-and-clusterrole
"""
yml_file_kubernetes_data = dict(
apiVersion='rbac.authorization.k8s.io/v1',
kind='Role',
metadata=dict(
namespace=team_name,
name=group_user,
),
rules={
[{'apiGroups':""}],
'resourses': '[pods]',
'verbs':'[get, watch, list]'}
)
with open(role_filename, 'w') as outfile:
yaml.dump(yml_file_kubernetes_data, outfile,
default_flow_style=False)
我想打开 YAML,让它看起来与 Kubernetes 参考 YAML 完全一样:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""] # "" indicates the core API group
resources: ["pods"]
verbs: ["get", "watch", "list"]
但我将 [ ] 分开,而没有 - 用于 apiGroup。这是我的结果:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: default
name: pod-reader
rules:
apiGroups:
- "" # "" indicates the core API group
resources:
- "pods"
verbs:
-"get"
-"watch"
-"list"
【问题讨论】:
-
您确定在您的输出(帖子的最后三行)中的序列项指示符 (
-) 之后没有空格
标签: python-3.x kubernetes yaml pyyaml