Sub topic 4
Anatomy of a rule: API groups, resources, and verbs
This chapter assumes you know basic Kubernetes objects and can apply YAML with kubectl. It also assumes you know that the API server exposes resources over REST. Here you will read and write the rules list that sits inside every Role and ClusterRole.
What a rule contains
A rule is one entry in the rules list. It answers one question: which actions on which resources are allowed? A request is permitted if at least one rule matches all of its parts.
| Field | Meaning | Example |
|---|---|---|
| `apiGroups` | The API group that owns the resource | `[""]`, `["apps"]` |
| `resources` | The resource types, in plural lowercase | `["pods", "pods/log"]` |
| `verbs` | The actions allowed | `["get", "list"]` |
| `resourceNames` | Optional. Limits the rule to named objects | `["app-config"]` |
A request must match the group, the resource, and the verb. If resourceNames is set, it must match that too.
API groups
Resources belong to API groups. The core group has an empty name, written "". It holds pods, services, secrets, configmaps, and namespaces. Other resources live in named groups. Deployments are in apps, and Jobs are in batch.
You can look up the group of any resource with kubectl:
kubectl api-resources --api-group=apps
kubectl api-resources --api-group=""The APIVERSION column shows the group before the slash. A value of v1 alone means the core group.
Verbs
Verbs map to HTTP operations on the API.
getreads one object by name.listreads a collection. It also returns the full contents of each object, soliston secrets exposes secret values.watchstreams changes.create,update,patch, anddeletechange one object.deletecollectiondeletes many objects in one call.
Most read-only tools need get, list, and watch together.
Subresources and resource names
Some actions target a subresource, written as resource/subresource. Reading logs needs pods/log. Running kubectl exec needs pods/exec. Granting pods does not grant its subresources.
Use resourceNames to limit a rule to specific objects:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: app-operator
rules:
- apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["configmaps"]
resourceNames: ["app-config"]
verbs: ["get", "update"]
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "patch"]This Role lets a subject read pods and their logs. It also lets them read and update only the ConfigMap app-config, and read and patch Deployments.
Wildcards and non-resource URLs
The value "*" matches everything in apiGroups, resources, or verbs. Wildcards also cover resources added later, such as custom resource definitions. Avoid them outside deliberate administrator roles.
A ClusterRole can also grant nonResourceURLs, such as /healthz. These are paths that are not Kubernetes objects. A namespaced Role cannot grant them.
Common mistakes
- Writing
apiGroups: ["apps"]for pods. Pods are in the core group"". - Using the singular
podinstead ofpods. - Forgetting
pods/logorpods/exec. - Combining rules and expecting an AND. Rules are additive. Each rule is checked on its own, and any match allows the request.
Exercise
A Role grants verbs get and list on resources deployments with apiGroups [""]. A user still gets Forbidden when listing deployments. What is wrong, and what is the fix?
Show answer
Deployments are in the apps API group, not the core group. The rule's apiGroups must be ["apps"] so the group matches. Running kubectl api-resources shows the group for any resource.
Exercise
Write a rule that lets a subject read the logs of pods but not run commands in them.
Show answer
Use apiGroups [""], resources ["pods/log"], and verbs ["get"]. Do not include pods/exec. Add "pods" with get and list if the subject must also find pod names.