Velero provides a generic ability to modify the resources during restore by specifying json patches. The json patches are applied to the resources before they are restored. The json patches are specified in a configmap and the configmap is referenced in the restore command.
Creating resource Modifiers
Below is the two-step of using resource modifiers to modify the resources during restore.
Creating resource modifiers configmap
You need to create one configmap in Velero install namespace from a YAML file that defined resource modifiers. The creating command would be like the below:
kubectl create cm <configmap-name> --from-file <yaml-file> -n velero
Creating a restore reference to the defined resource policies
You can create a restore with the flag --resource-modifier-configmap
, which will apply the defined resource modifiers to the current restore. The creating command would be like the below:
velero restore create --resource-modifier-configmap <configmap-name>
YAML template
version: v1
resourceModifierRules:
- conditions:
groupResource: persistentvolumeclaims
resourceNameRegex: "^mysql.*$"
namespaces:
- bar
- foo
labelSelector:
matchLabels:
foo: bar
patches:
- operation: replace
path: "/spec/storageClassName"
value: "premium"
- operation: remove
path: "/metadata/labels/test"
foo: bar
. The JSON Patch will replace the storageClassName with “premium” and remove the label “test” from the PVCs.The test
operation can be used to check if a particular value is present in the resource. If the value is present, the patch will be applied. If the value is not present, the patch will not be applied. This can be used to apply a patch only if a particular value is present in the resource. For example, if you wish to change the storage class of a PVC only if the PVC is using a particular storage class, you can use the following configmap.
version: v1
resourceModifierRules:
- conditions:
groupResource: persistentvolumeclaims
resourceNameRegex: ".*"
namespaces:
- bar
- foo
patches:
- operation: test
path: "/spec/storageClassName"
value: "premium"
- operation: replace
path: "/spec/storageClassName"
value: "standard"
version: v1
resourceModifierRules:
- conditions:
groupResource: deployments.apps
resourceNameRegex: "^test-.*$"
namespaces:
- bar
- foo
patches:
# Dealing with complex values by escaping the yaml
- operation: add
path: "/spec/template/spec/containers/0"
value: "{\"name\": \"nginx\", \"image\": \"nginx:1.14.2\", \"ports\": [{\"containerPort\": 80}]}"
# Copy Operator
- operation: copy
from: "/spec/template/spec/containers/0"
path: "/spec/template/spec/containers/1"
Note:
You can modify a resource using JSON Merge Patch
version: v1
resourceModifierRules:
- conditions:
groupResource: pods
namespaces:
- ns1
mergePatches:
- patchData: |
{
"metadata": {
"annotations": {
"foo": null
}
}
}
foo
from the pods.You can modify a resource using Strategic Merge Patch
version: v1
resourceModifierRules:
- conditions:
groupResource: pods
resourceNameRegex: "^my-pod$"
namespaces:
- ns1
strategicPatches:
- patchData: |
{
"spec": {
"containers": [
{
"name": "nginx",
"image": "repo2/nginx"
}
]
}
}
repo2/nginx
.A new field matches
is added in conditions to support conditional patches.
Example of matches in conditions
version: v1
resourceModifierRules:
- conditions:
groupResource: persistentvolumeclaims.storage.k8s.io
matches:
- path: "/spec/storageClassName"
value: "premium"
mergePatches:
- patchData: |
{
"metadata": {
"annotations": {
"foo": null
}
}
}
foo
from the PVCs.matches
list. The patch will be applied only if all the matches are satisfied.The user can specify a wildcard for groupResource in the conditions' struct. This will allow the user to apply the patches for all the resources of a particular group or all resources in all groups. For example, *.apps
will apply to all the resources in the apps
group, *
will apply to all the resources in core group, *.*
will apply to all the resources in all groups.
*.groupName
and namespaces
are specified, the patches will be applied to all the namespaced resources in this group in the specified namespaces and all the cluster resources in this group.To help you get started, see the documentation.