基于角色的访问控制
最佳实践指南的这一部分讨论在图表清单中创建和格式化 RBAC 资源。
RBAC 资源是
- ServiceAccount(命名空间)
- 角色(命名空间)
- 集群角色
- 角色绑定(命名空间)
- 集群角色绑定
YAML 配置
RBAC 和 ServiceAccount 配置应该在单独的键下进行。它们是独立的事物。在 YAML 中将这两个概念分开,可以消除歧义并使其更清晰。
rbac:
# Specifies whether RBAC resources should be created
create: true
serviceAccount:
# Specifies whether a ServiceAccount should be created
create: true
# The name of the ServiceAccount to use.
# If not set and create is true, a name is generated using the fullname template
name:
此结构可以扩展到需要多个 ServiceAccount 的更复杂的图表。
someComponent:
serviceAccount:
create: true
name:
anotherComponent:
serviceAccount:
create: true
name:
RBAC 资源应默认创建
rbac.create
应该是一个布尔值,用于控制是否创建 RBAC 资源。默认值应为 true
。希望自行管理 RBAC 访问控制的用户可以将此值设置为 false
(在这种情况下,请参见下文)。
使用 RBAC 资源
serviceAccount.name
应设置为图表创建的受访问控制资源要使用的 ServiceAccount 的名称。如果 serviceAccount.create
为 true,则应创建具有此名称的 ServiceAccount。如果未设置名称,则使用 fullname
模板生成名称。如果 serviceAccount.create
为 false,则不应创建它,但仍应与相同的资源关联,以便稍后创建的引用它的手动创建的 RBAC 资源能够正常工作。如果 serviceAccount.create
为 false 且未指定名称,则使用默认 ServiceAccount。
应使用以下辅助模板来创建 ServiceAccount。
{{/*
Create the name of the service account to use
*/}}
{{- define "mychart.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
{{ default (include "mychart.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
{{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}