2019-05-21 22:56:01 +00:00
< small > *[documentation](/README.md#documentation) / [Writing Policies ](/documentation/writing-policies.md ) / Generate*</ small >
2019-05-21 22:50:36 +00:00
2019-05-21 22:56:01 +00:00
# Generate Configurations
2019-05-21 22:50:36 +00:00
2019-06-12 20:50:08 +00:00
```generate``` is used to create default resources for a namespace. This feature is useful for managing resources that are required in each namespace.
2019-05-21 22:50:36 +00:00
2019-06-03 23:02:34 +00:00
## Example 1
2019-05-21 22:50:36 +00:00
2019-05-22 15:14:10 +00:00
````yaml
2019-06-03 23:02:34 +00:00
apiVersion: kyverno.io/v1alpha1
kind: Policy
metadata:
name: basic-policy
spec:
2019-05-22 15:14:10 +00:00
rules:
2019-06-03 23:02:34 +00:00
- name: "Basic config generator for all namespaces"
2019-05-22 15:14:10 +00:00
resource:
2019-06-03 23:02:34 +00:00
kinds:
- Namespace
selector:
matchLabels:
LabelForSelector : "namespace2"
2019-05-22 15:14:10 +00:00
generate:
2019-06-03 23:02:34 +00:00
kind: ConfigMap
2019-05-22 15:14:10 +00:00
name: default-config
2019-06-03 23:02:34 +00:00
clone:
2019-05-22 15:14:10 +00:00
namespace: default
name: config-template
2019-06-03 23:02:34 +00:00
- name: "Basic config generator for all namespaces"
resource:
kinds:
- Namespace
selector:
matchLabels:
LabelForSelector : "namespace2"
generate:
kind: Secret
2019-05-22 15:14:10 +00:00
name: mongo-creds
data:
2019-06-03 23:02:34 +00:00
data:
DB_USER: YWJyYWthZGFicmE=
DB_PASSWORD: YXBwc3dvcmQ=
metadata:
labels:
purpose: mongo
2019-05-22 15:14:10 +00:00
````
2019-06-03 23:02:34 +00:00
In this example, when this policy is applied, any new namespace that satisfies the label selector will receive 2 new resources after its creation:
2019-06-12 23:47:22 +00:00
* ConfigMap copied from default/config-template.
* Secret with values DB_USER and DB_PASSWORD, and label ```purpose: mongo```.
2019-06-03 23:02:34 +00:00
2019-05-22 15:15:35 +00:00
2019-06-03 23:02:34 +00:00
## Example 2
````yaml
apiVersion: kyverno.io/v1alpha1
kind: Policy
metadata:
name: "default"
spec:
rules:
- name: "deny-all-traffic"
resource:
kinds:
- Namespace
name: "*"
generate:
kind: NetworkPolicy
name: deny-all-traffic
data:
spec:
podSelector:
matchLabels: {}
matchExpressions: []
policyTypes: []
metadata:
labels:
policyname: "default"
````
2019-06-12 23:47:22 +00:00
In this example, when the policy is applied, any new namespace will receive a nNtworkPolicy based on the specified template that by default denies all inbound and outbound traffic.
2019-05-21 22:50:36 +00:00
2019-05-22 07:09:45 +00:00
---
2019-05-21 22:56:01 +00:00
< small > *Read Next >> [Testing Policies ](/documentation/testing-policies.md )*</ small >
2019-05-21 22:50:36 +00:00