1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-01-20 18:52:16 +00:00
kyverno/documentation/writing-policies.md

66 lines
2.8 KiB
Markdown
Raw Normal View History

2019-05-21 15:50:36 -07:00
<small>*[documentation](/README.md#documentation) / Writing Policies*</small>
2019-05-21 14:44:04 -07:00
2019-05-21 11:06:03 -07:00
# Writing Policies
2019-05-21 14:44:04 -07:00
The following picture shows the structure of a Kyverno Policy:
2019-11-13 14:22:54 -08:00
![KyvernoPolicy](images/Kyverno-Policy-Structure.png)
2020-02-06 00:04:19 -08:00
Each Kyverno policy contains one or more rules. Each rule has a `match` clause, an optional `exclude` clause, and one of a `mutate`, `validate`, or `generate` clause.
The match / exclude clauses have the same structure, and can contain the following elements:
* resources: select resources by name, namespaces, kinds, and label selectors.
2020-02-06 00:42:01 -08:00
* subjects: select users, user groups, and service accounts
2020-02-06 00:04:19 -08:00
* roles: select namespaced roles
* clusterroles: select cluster wide roles
When Kyverno receives an admission controller request, i.e. a validation or mutation webhook, it first checks to see if the resource and user information matches or should be excluded from processing. If both checks pass, then the rule logic to mutate, validate, or generate resources is applied.
2020-02-06 00:04:19 -08:00
The following YAML provides an example for a match clause.
2019-05-21 14:44:04 -07:00
````yaml
2019-11-13 13:55:27 -08:00
apiVersion : kyverno.io/v1
kind : ClusterPolicy
2019-05-21 14:44:04 -07:00
metadata :
name : policy
spec :
# 'enforce' to block resource request if any rules fail
# 'audit' to allow resource request on failure of rules, but create policy violations to report them
validationFailureAction: enforce
2019-05-21 14:44:04 -07:00
# Each policy has a list of rules applied in declaration order
rules:
2019-06-12 13:50:08 -07:00
# Rules must have a unique name
- name: "check-pod-controller-labels"
2019-08-21 14:18:44 -07:00
# Each rule matches specific resource described by "match" field.
match:
resources:
2019-08-21 15:49:34 -07:00
kinds: # Required, list of kinds
2019-08-21 14:18:44 -07:00
- Deployment
- StatefulSet
2020-02-06 00:04:19 -08:00
name: "mongo*" # Optional, a resource name is optional. Name supports wildcards (* and ?)
namespaces: # Optional, list of namespaces. Supports wildcards (* and ?)
- "dev*"
- test
2020-02-06 00:04:19 -08:00
selector: # Optional, a resource selector is optional. Values support wildcards (* and ?)
2019-08-21 15:49:34 -07:00
matchLabels:
app: mongodb
matchExpressions:
- {key: tier, operator: In, values: [database]}
# Optional, subjects to be matched
subjects:
2019-11-14 13:34:41 -08:00
- kind: User
name: mary@somecorp.com
# Optional, roles to be matched
roles:
# Optional, clusterroles to be matched
2020-02-06 00:04:19 -08:00
clusterroles: cluster-admin
593 feature (#594) * initial commit * background policy validation * correct message * skip non-background policy process for add/update * add Generate Request CR * generate Request Generator Initial * test generate request CR generation * initial commit gr generator * generate controller initial framework * add crd for generate request * gr cleanup controller initial commit * cleanup controller initial * generate mid-commit * generate rule processing * create PV on generate error * embed resource type * testing phase 1- generate resources with variable substitution * fix tests * comment broken test #586 * add printer column for state * return if existing resource for clone * set resync time to 2 mins & remove resource version check in update handler for gr * generate events for reporting * fix logs * initial commit * fix trailing quote in patch * remove comments * initial condition (equal & notequal) * initial support for conditions * initial support fo conditions in generate * support precondition checks * cleanup * re-evaluate GR on namespace update using dynamic informers * add status for generated resources * display loaded variable SA * support delete cleanup of generate request main resources * fix log * remove namespace from SA username * support multiple variables per statement for scalar values * fix fail variables * add check for userInfo * validation checks for conditions * update policy * refactor logs * code review * add openapispec for clusterpolicy preconditions * Update documentation * CR fixes * documentation * CR fixes * update variable * fix logs * update policy * pre-defined variables (serviceAccountName & serviceAccountNamespace) * update test
2020-01-07 15:13:57 -08:00
2020-02-06 00:04:19 -08:00
...
2020-02-06 00:04:19 -08:00
````
2020-02-06 00:04:19 -08:00
Each rule can validate, mutate, or generate configurations of matching resources. A rule definition can contain only a single **mutate**, **validate**, or **generate** child node. These actions are applied to the resource in described order: mutation, validation and then generation.
2020-01-31 17:44:56 -08:00
2020-01-31 14:30:49 -08:00
---
2020-02-06 00:16:04 -08:00
<small>*Read Next >> [Validate Resources](/documentation/writing-policies-validate.md)*</small>