Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

RBAC

这些标记会生成一个 RBAC ClusterRole。这使您能够在使用这些权限的代码旁边描述您的控制器所需的权限。

// +kubebuilder:rbac
groups
string
namespace
string
resourceNames
string
resources
string
roleName
string
urls
string
verbs
string
specifies an RBAC rule to all access to some resources or non-resource URLs.

RBAC markers 用于生成 ClusterRole 或 Role manifests。可组合多个 markers 以构建完整的 RBAC 策略。

Examples:

// Basic resource access
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch

// Core API group (use empty string)
// +kubebuilder:rbac:groups="",resources=pods;services,verbs=get;list;watch

// Multiple API groups and resources
// +kubebuilder:rbac:groups=apps;batch,resources=deployments;jobs,verbs=get;list;watch;create;update;patch;delete

// Access to resource status or scale subresources
// +kubebuilder:rbac:groups=apps,resources=deployments/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=apps,resources=deployments/scale,verbs=get;update

// Access to specific resource instances by name
// +kubebuilder:rbac:groups="",resources=configmaps,resourceNames=my-config,verbs=get

// Non-resource URLs (for metrics, healthz, etc.)
// +kubebuilder:rbac:urls=/metrics;/healthz,verbs=get

// Namespace-scoped Role instead of ClusterRole
// +kubebuilder:rbac:groups="",namespace=my-namespace,resources=secrets,verbs=get;list;watch

// Custom role name
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list,roleName=deployment-reader
groups
string
specifies the API groups that this rule encompasses.

核心 API group 使用空字符串(“”)。可用分号分隔指定多个 group。示例:“apps;batch” 或 “”(表示核心 group)。

namespace
string
specifies the scope of the Rule.

若未设置,Rule 属于生成的 ClusterRole。若设置,Rule 属于 Role,其命名空间由此字段指定。示例:“my-namespace”。

resourceNames
string
specifies the names of the API resources that this rule encompasses.

Create 请求不能按 resourcename 限制,因为授权时对象名称尚未知。可用分号分隔指定多个名称。示例:“my-config;my-secret”。

resources
string
specifies the API resources that this rule encompasses.

可用分号分隔指定多个资源。子资源可用斜杠指定(例如 “deployments/status”)。示例:“deployments;pods” 或 “deployments/status”。

roleName
string
specifies a custom name for the Role or ClusterRole.

若未设置,使用生成器的默认 roleName。在多个命名空间使用相同 roleName 时,有助于避免名称冲突。

示例:当将命名空间作用域的 RBAC markers 与 kustomize 的全局命名空间转换一起使用时,多个 Role 可能最终出现在同一命名空间且名称相同,导致 “ID conflict” 错误。使用 roleName 确保每个 Role 有唯一名称:

// +kubebuilder:rbac:groups=apps,namespace=infrastructure,roleName=infra-manager,resources=deployments,verbs=get;list // +kubebuilder:rbac:groups=“”,namespace=users,roleName=user-secrets,resources=secrets,verbs=get

这将生成名为 “infra-manager” 和 “user-secrets” 的 Role,而不是都叫 “manager-role”。

urls
string
URL specifies the non-resource URLs that this rule encompasses.

非资源 URL 是不表示资源的路径,如 “/metrics” 或 “/healthz”。可用分号分隔指定多个 URL。示例:“/metrics;/healthz”。

verbs
string
specifies the (lowercase) kubernetes API verbs that this rule encompasses.

常见动词:“get”、“list”、“watch”、“create”、“update”、“patch”、“delete”。使用 “*” 表示全部动词。多个动词必须用分号分隔。示例:“get;list;watch”。