1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 09:26:54 +00:00
kyverno/documentation/writing-policies-generate.md

119 lines
3.7 KiB
Markdown
Raw Normal View History

2020-02-06 00:10:36 -08:00
<small>*[documentation](/README.md#documentation) / [Writing Policies](/documentation/writing-policies.md) / Generate Resources*</small>
2019-05-21 15:50:36 -07:00
# Generating Resources
2019-05-21 15:50:36 -07:00
2020-02-07 10:21:47 -08:00
The ```generate``` rule can used to create additional resources when a new resource is created. This is useful to create supporting resources, such as new role bindings for a new namespace.
The `generate` rule supports `match` and `exclude` blocks, like other rules. Hence, the trigger for applying this rule can be the creation of any resource and its possible to match or exclude API requests based on subjects, roles, etc.
2020-07-08 14:26:22 -07:00
The generate rule triggers during a API CREATE operation and does not support [background processing](/documentation/writing-policies-background.md). To keep resources synchronized across changes you can use `synchronize : true`, In this case user can't be able to delete/update generated resource directly. If `synchronize : false` then user can delete/update generated resource directly but on policy update old generated resource would not be able to sync with new generated policy
This policy sets the Zookeeper and Kafka connection strings for all namespaces.
```yaml
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: "zk-kafka-address"
spec:
rules:
- name: "zk-kafka-address"
match:
resources:
kinds:
- Namespace
generate:
synchronize: true
kind: ConfigMap
name: zk-kafka-address
# generate the resource in the new namespace
namespace: "{{request.object.metadata.name}}"
data:
kind: ConfigMap
data:
ZK_ADDRESS: "192.168.10.10:2181,192.168.10.11:2181,192.168.10.12:2181"
KAFKA_ADDRESS: "192.168.10.13:9092,192.168.10.14:9092,192.168.10.15:9092"
```
2019-05-21 15:50:36 -07:00
## Example 1
2020-02-06 22:51:16 -08:00
2019-05-22 18:14:10 +03:00
````yaml
2019-11-13 13:55:27 -08:00
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: basic-policy
spec:
2019-05-22 18:14:10 +03:00
rules:
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
- name: "Generate ConfigMap"
2019-08-21 15:49:34 -07:00
match:
resources:
kinds:
- Namespace
2019-05-22 18:14:10 +03:00
generate:
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
kind: ConfigMap # Kind of resource
name: default-config # Name of the new Resource
2020-02-06 22:51:16 -08:00
namespace: "{{request.object.metadata.name}}" # namespace that triggers this rule
synchronize : true
clone:
2019-05-22 18:14:10 +03:00
namespace: default
name: config-template
2020-02-06 22:51:16 -08:00
- name: "Generate Secret (insecure)"
2019-08-21 15:49:34 -07:00
match:
resources:
kinds:
- Namespace
generate:
kind: Secret
2019-05-22 18:14:10 +03:00
name: mongo-creds
2020-02-06 22:51:16 -08:00
namespace: "{{request.object.metadata.name}}" # namespace that triggers this rule
2019-05-22 18:14:10 +03:00
data:
data:
DB_USER: YWJyYWthZGFicmE=
DB_PASSWORD: YXBwc3dvcmQ=
metadata:
labels:
purpose: mongo
2019-05-22 18:14:10 +03:00
````
2020-02-06 22:51:16 -08:00
In this example new namespaces will receive 2 new resources after its creation:
2020-04-02 22:28:09 -07:00
* A `ConfigMap` cloned from `default/config-template`.
* A `Secret` with values `DB_USER` and `DB_PASSWORD`, and label `purpose: mongo`.
2019-05-22 18:15:35 +03:00
## Example 2
````yaml
2019-11-13 13:55:27 -08:00
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: "default"
spec:
rules:
- name: "deny-all-traffic"
2019-08-21 15:49:34 -07:00
match:
resources:
kinds:
- Namespace
name: "*"
generate:
kind: NetworkPolicy
name: deny-all-traffic
2020-02-06 22:51:16 -08:00
namespace: "{{request.object.metadata.name}}" # namespace that triggers this rule
data:
spec:
2020-02-07 12:03:53 -08:00
# select all pods in the namespace
podSelector: {}
policyTypes:
- Ingress
metadata:
labels:
policyname: "default"
````
2019-06-12 16:47:22 -07:00
2020-04-02 22:28:09 -07:00
In this example new namespaces will receive a `NetworkPolicy` that by default denies all inbound and outbound traffic.
2019-05-21 15:50:36 -07:00
---
2020-02-06 00:04:19 -08:00
<small>*Read Next >> [Variables](/documentation/writing-policies-variables.md)*</small>
2019-05-21 15:50:36 -07:00