1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00
kyverno/documentation/writing-policies-mutate.md

183 lines
5.2 KiB
Markdown
Raw Normal View History

2019-05-21 22:56:01 +00:00
<small>*[documentation](/README.md#documentation) / [Writing Policies](/documentation/writing-policies.md) / Mutate*</small>
2019-05-21 22:50:36 +00:00
# Mutate Configurations
2019-05-21 22:50:36 +00:00
2019-06-12 20:50:08 +00:00
The ```mutate``` rule contains actions that will be applied to matching resource before their creation. A mutate rule can be written as a JSON Patch or as an overlay. By using a ```patch``` in the (JSONPatch - RFC 6902)[http://jsonpatch.com/] format, you can make precise changes to the resource being created. Using an ```overlay``` is convenient for describing the desired state of the resource.
2019-05-21 22:50:36 +00:00
2019-06-12 20:50:08 +00:00
Resource mutation occurs before validation, so the validation rules should not contradict the changes performed by the mutation section.
2019-05-21 22:50:36 +00:00
2019-05-22 17:38:55 +00:00
## Patches
2019-06-12 20:50:08 +00:00
This patch adds an init container to all deployments.
2019-05-22 17:38:55 +00:00
````yaml
apiVersion : kyverno.io/v1alpha1
kind : Policy
metadata :
name : policy-v1
spec :
rules:
2019-06-12 20:50:08 +00:00
- name: "add-init-secrets"
2019-08-21 22:49:34 +00:00
match:
resources:
kinds:
- Deployment
2019-05-22 17:38:55 +00:00
mutate:
patches:
2019-06-12 20:50:08 +00:00
- path: "/spec/template/spec/initContainers/0/"
2019-05-22 17:38:55 +00:00
op: add
value:
2019-06-12 20:50:08 +00:00
- image: "nirmata.io/kube-vault-client:v2"
name: "init-secrets"
2019-05-22 17:38:55 +00:00
````
2019-06-12 20:50:08 +00:00
[JSONPatch](http://jsonpatch.com/) supports the following operations (in the 'op' field):
2019-05-22 17:38:55 +00:00
* **add**
* **replace**
* **remove**
2019-06-12 20:50:08 +00:00
With Kyverno, the add and replace have the same behavior i.e. both operations will add or replace the target element.
Here is the example of a patch that removes a label from the secret:
2019-05-22 17:38:55 +00:00
````yaml
apiVersion : kyverno.io/v1alpha1
kind : Policy
metadata :
name : policy-remove-label
spec :
rules:
- name: "Remove unwanted label"
2019-08-21 22:49:34 +00:00
match:
resources:
kinds:
- Secret
2019-05-22 17:38:55 +00:00
mutate:
patches:
- path: "/metadata/labels/purpose"
op: remove
````
Note, that if **remove** operation cannot be applied, then this **remove** operation will be skipped with no error.
## Overlay
2019-06-21 14:19:48 +00:00
A mutation overlay describes the desired form of resource. The existing resource values are replaced with the values specified in the overlay. If a value is specified in the overlay but not present in the target resource, then it will be added to the resource. The overlay cannot be used to delete values in a resource: use **patches** for this purpose.
2019-05-22 17:38:55 +00:00
2019-06-12 20:50:08 +00:00
The following mutation overlay will add (or replace) the memory request and limit to 10Gi for every Pod with a label ```memory: high```:
2019-05-22 17:38:55 +00:00
````yaml
apiVersion : kyverno.io/v1alpha1
kind : Policy
metadata :
name : policy-change-memory-limit
spec :
rules:
- name: "Set hard memory limit to 2Gi"
2019-08-21 22:49:34 +00:00
match:
resources:
kinds:
- Pod
selector:
matchLabels:
memory: high
2019-05-22 17:38:55 +00:00
mutate:
overlay:
spec:
2019-06-12 20:50:08 +00:00
containers:
# the wildcard * will match all containers in the list
- (name): "*"
2019-06-12 20:50:08 +00:00
resources:
requests:
memory: "10Gi"
limits:
memory: "10Gi"
2019-05-22 17:38:55 +00:00
````
### Working with lists
2019-06-21 14:20:57 +00:00
Applying overlays to a list type is fairly straightforward: new items will be added to the list, unless they already exist. For example, the next overlay will add IP "192.168.10.172" to all addresses in all Endpoints:
2019-05-22 17:38:55 +00:00
````yaml
apiVersion: kyverno.io/v1alpha1
2019-05-22 17:38:55 +00:00
kind: Policy
metadata:
2019-06-12 23:47:22 +00:00
name: policy-endpoints
2019-05-22 17:38:55 +00:00
spec:
rules:
- name: "Add IP to subsets"
2019-08-21 22:49:34 +00:00
match:
resources:
kinds :
- Endpoints
2019-05-22 17:38:55 +00:00
mutate:
overlay:
subsets:
- addresses:
- ip: 192.168.42.172
2019-05-22 17:38:55 +00:00
````
2019-06-12 20:50:08 +00:00
### Conditional logic using anchors
An **anchor** field, marked by parentheses, allows conditional processing of configurations. Processing stops when the anchor value does not match. Once processing stops, any child elements or any remaining siblings in a list, will not be processed.
2019-06-12 20:50:08 +00:00
For example, this overlay will add or replace the value 6443 for the port field, for all ports with a name value that starts with "secure":
2019-05-22 17:38:55 +00:00
````yaml
apiVersion: kyverno.io/v1alpha1
2019-05-22 17:38:55 +00:00
kind : Policy
metadata :
2019-06-12 20:50:08 +00:00
name : policy-set-port
2019-05-22 17:38:55 +00:00
spec :
rules:
- name: "Set port"
2019-08-21 22:49:34 +00:00
match:
resources:
kinds :
- Endpoints
2019-05-22 17:38:55 +00:00
mutate:
overlay:
subsets:
- ports:
- (name): "secure*"
port: 6443
````
2019-06-12 20:50:08 +00:00
The **anchors** values support **wildcards**:
2019-05-22 17:38:55 +00:00
1. `*` - matches zero or more alphanumeric characters
2. `?` - matches a single alphanumeric character
2019-06-12 20:50:08 +00:00
### Add if not present
A variation of an anchor, is to add a field value if it is not already defined. This is done by using the ````+(...)```` notation for the field.
For example, this overlay will set the port to 6443, if a port is not already defined:
````yaml
apiVersion: kyverno.io/v1alpha1
2019-06-12 20:50:08 +00:00
kind : Policy
metadata :
name : policy-set-port
spec :
rules:
- name: "Set port"
2019-08-21 22:49:34 +00:00
match:
resources:
kinds :
- Endpoints
2019-06-12 20:50:08 +00:00
mutate:
overlay:
subsets:
- (ports):
2019-06-12 20:50:08 +00:00
+(port): 6443
````
## Additional Details
Additional details on mutation overlay behaviors are available on the wiki: [Mutation Overlay](https://github.com/nirmata/kyverno/wiki/Mutation-Overlay)
2019-05-21 22:50:36 +00:00
---
2019-06-12 20:50:08 +00:00
<small>*Read Next >> [Generate](/documentation/writing-policies-generate.md)*</small>