Sub topic 16
Least-privilege RBAC for operators
This chapter assumes you know Kubernetes RBAC basics (Roles, ClusterRoles, bindings, ServiceAccounts) and have read the reconcile chapters. It uses the WebApp operator from earlier.
Why operators need their own permissions
An operator runs as a Pod. Every API call it makes is authorized as its ServiceAccount. If you grant it cluster-admin, a bug or a compromised image can read every Secret in the cluster. Least privilege means granting only the verbs the code actually uses, on only the resources it touches.
Derive permissions from the reconciler
Read the reconcile function and list each API call:
| Code | Permission needed |
|---|---|
| `r.Get` on a WebApp, and the watch from `For(&WebApp{})` | `get`, `list`, `watch` on `webapps` |
| `r.Status().Update` | `update` on `webapps/status` |
| `CreateOrUpdate` on a Deployment | `get`, `create`, `update`, `patch` on `deployments` |
| `Owns(&Deployment{})` | `list`, `watch` on `deployments` |
| `SetControllerReference` | `update` on `webapps/finalizers` |
The status subresource is authorized separately from the main resource. Without webapps/status, the operator can read the object but its status writes fail with Forbidden.
The last row surprises many people. When the OwnerReferencesPermissionEnforcement admission plugin is on, which is common on OpenShift, setting blockOwnerDeletion on a child requires permission to update the owner's finalizers subresource.
Generate rules with kubebuilder markers
Kubebuilder reads comment markers above Reconcile and writes a ClusterRole for you with make manifests.
// +kubebuilder:rbac:groups=example.com,resources=webapps,verbs=get;list;watch
// +kubebuilder:rbac:groups=example.com,resources=webapps/status,verbs=update;patch
// +kubebuilder:rbac:groups=example.com,resources=webapps/finalizers,verbs=update
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch
func (r *WebAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return ctrl.Result{}, nil
}Keeping the markers next to the code means a new API call and its permission change in the same diff.
The resulting manifests
The generated rules look like this. Add the binding yourself if you deploy without the kubebuilder kustomize setup.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: webapp-operator
rules:
- apiGroups: ["example.com"]
resources: ["webapps"]
verbs: ["get", "list", "watch"]
- apiGroups: ["example.com"]
resources: ["webapps/status"]
verbs: ["update", "patch"]
- apiGroups: ["example.com"]
resources: ["webapps/finalizers"]
verbs: ["update"]
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: webapp-operator
subjects:
- kind: ServiceAccount
name: webapp-operator
namespace: webapp-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: webapp-operatorNarrow the scope
If the operator manages one namespace, use a Role and RoleBinding and restrict the manager's cache to that namespace. Leader election needs a separate namespaced Role with get, list, watch, create, update, patch and delete on leases in coordination.k8s.io. If the operator emits events, it also needs create and patch on events.
Common errors
- The log shows
webapps.example.com is forbidden: User "system:serviceaccount:..." cannot list resource. A watch needslistandwatch, not onlyget. - Wildcards such as
resources: ["*"]orverbs: ["*"]hide mistakes and grant access to future resource types. - Reading Secrets cluster-wide is rarely necessary. Grant it in specific namespaces instead.
- Test the rules with
kubectl auth can-i update webapps/status --as=system:serviceaccount:webapp-system:webapp-operator.
Exercise
The WebApp operator reads WebApps fine, but every status write fails with Forbidden even though the ClusterRole grants update on webapps. What is missing, and why?
Show answer
The role needs update (and usually patch) on webapps/status. The status subresource is authorized separately, so permission on webapps does not cover it.