Sub topic 12
Composing ClusterRoles with aggregation
This chapter assumes you can write a ClusterRole and a ClusterRoleBinding, and that you know how labels and selectors work. Here you will learn how Kubernetes builds one ClusterRole out of several smaller ones.
Why aggregation exists
A ClusterRole sometimes needs to grow over time. Suppose you install a new custom resource definition (CRD) and want the built-in view role to cover it. Editing view by hand is fragile, because the API server's default roles are reconciled and your edits can be overwritten.
Aggregation solves this. You define an aggregated ClusterRole with a label selector instead of rules. A controller in the control plane finds every ClusterRole whose labels match the selector and copies their rules into the aggregated role. Add a matching ClusterRole later and the aggregated role picks it up automatically.
How it works
The aggregated ClusterRole has an aggregationRule field containing clusterRoleSelectors. Each selector is a standard label selector. The controller owns the rules field of that role, so anything you write there is overwritten.
A worked example
The first ClusterRole below is the aggregated one. It has an empty rules list, and the controller fills it in. The second ClusterRole carries the matching label and supplies the rules.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: monitoring
aggregationRule:
clusterRoleSelectors:
- matchLabels:
rbac.example.com/aggregate-to-monitoring: "true"
rules: []
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: monitoring-endpoints
labels:
rbac.example.com/aggregate-to-monitoring: "true"
rules:
- apiGroups: [""]
resources: ["services", "endpoints", "pods"]
verbs: ["get", "list", "watch"]After you apply both, inspect the aggregated role to see the merged rules:
kubectl get clusterrole monitoring -o yamlThe rules section now lists the services, endpoints, and pods rule. Bind monitoring with a ClusterRoleBinding, as in the earlier chapters, and the subject receives those permissions.
Extending the default roles
The default roles admin, edit, and view are themselves aggregated. They select ClusterRoles carrying these labels:
| Label | Role it extends |
|---|---|
| `rbac.authorization.k8s.io/aggregate-to-view: "true"` | `view` |
| `rbac.authorization.k8s.io/aggregate-to-edit: "true"` | `edit` |
| `rbac.authorization.k8s.io/aggregate-to-admin: "true"` | `admin` |
This is how a CRD can grant namespace users access to its custom resources. Ship a small ClusterRole with the right label, and the default roles include it. Many operators and CRD bundles do this.
Gotchas
- Do not edit
ruleson an aggregated role. The controller replaces them. - A typo in the label means the rules are silently not merged. Check the result with
kubectl get clusterrole <name> -o yaml. - Aggregation is transitive in effect: anyone who can create or label a ClusterRole can widen every role that selects that label. Restrict who can create ClusterRoles.
Exercise
You apply an aggregated ClusterRole named reporting with a selector for the label team/aggregate-to-reporting: "true", and you then create a ClusterRole reporting-jobs with the label team/aggregate-to-reporting: "True". The aggregated role shows no rules from reporting-jobs. Why, and how do you fix it?
Show answer
Label values are case-sensitive, so "True" does not match "true". Change the label on reporting-jobs to "true". The controller then merges its rules into reporting, which you can confirm with kubectl get clusterrole reporting -o yaml.