Creating Reusable Resource Modifiers for Kubernetes

You can use resource modifiers to add, delete, and modify fields in the Kubernetes resource YAMLs that you restore. Resource modifiers are useful when you need to modify the restore content to match the destination environment.

The modifier specifications are stored and maintained on Kubernetes clusters as CustomResource objects of the ‘RestoreModifiers’ kind. After you create a resource modifier on a cluster, you can apply the modifier to resources during full application restores and namespace-level restores.

Resource Modifier Definitions

Resource modifier definitions include the following:

  • One or more selectors

  • One or more actions

Selectors

  • Name: Matches resources that have a specific name.

  • Namespace: Matches resources that originate from a specific namespace.

  • Kind: Matches resources by kind.

  • Label: Matches resources that have one or more labels. You can enter multiple labels by separating key:value pairs with commas.

  • Field: Matches resources that contain a specified field.

Actions

  • Add: Adds a new value to a new or existing YAML path in the matched resources. Specify the path value in a Unix-style path in the YAML structure. If the path already exists, it is overwritten.

  • Delete: Deletes a path from the matched resources.

  • Modify: Modifies existing fields from matching YAML at a particular path.

Requirements and Bootstrapping

  • All access nodes for clusters that you want to use resource modifiers for must be on Commvault 2023E (11.32) or a more recent release.

  • Commvault needs the namespace ‘cv-config’ and a custom CRD 'restoremodifiers.k8s.cv.io' to be present on the cluster. If the namespace and CRD are not present, when you create your resource modifier, Commvault automatically creates a new namespace called 'cv-config' and deploys a new CustomResourceDefinition (CRD) to your cluster.

Managing Resource Modifiers for a Cluster Using YAMLs

Creating and updating RestoreModifier objects on the cluster using YAMLs with kubectl (or git-pipelines) is the best method for deploying modifier configurations on the cluster. (You can also create and update resource modifiers using the Command Center, as described in the following section.)

By deploying the RestoreModifier objects in the namespace “cv-config” using YAMLs, you can perform operations on advanced YAML paths, such as adding an entire YAML tree to a target resource. For example, you can add initContainers spec to a Pod. By using YAMLs, you can also specify multiple selectors and modifier actions on the same custom resource object. One selector is mapped to a modifier action using the selectorId field of a modifier action.

For a Commvault-provided RestoreModifier CRD, see Kubernetes Restore Modifiers on GitHub.

Note

If you create a RestoreModifier with multiple selectors and actions, only the first selector and action are displayed in the Command Center.

Example: Adding initContainers section to all Deployments of name “restoremodifier-deployment”

apiVersion: k8s.cv.io/v1
kind: RestoreModifier
metadata:
  name: add-init-containers
  namespace: cv-config
spec:
  selectors:
  - id: add-init-containers
    kind: Deployment
    name: restoremodifier-deployment
  modifiers:
  - selectorID: add-init-containers
    action: Add
    path: /spec/template/spec/initContainers
    value:
    - name: install-newname
    image: busybox
    command:
    - wget
    - "-0"
    - "/newdir/nginx.html"
    - https://www.nginx.com/
    volumeMounts:
    - name: data
      mountPath: "/newdir"

Example: Adding Container Arguments to All Deployments

apiVersion: k8s.cv.io/v1
kind: RestoreModifier
metadata:
  name: add-args-to-deployment
  namespace: cv-config
spec:
  modifiers:
  - action: Add
    parameters: Exact
    path: /spec/template/spec/containers/0/args
    selectorId: add-args-to-deployment
    value: [/bin/sh]     # Add the array value like this
  selectors:
  - id: add-args-to-deployment
    kind: Deployment

Procedure

  1. To apply the YAML on the cluster using kubectl, use the following command:

    kubectl create -f <YAML File> -n cv-config

    Example command:

    kubectl create -f sample-restoremodifier.yaml -n cv-config

    Example response:

    restoremodifier.k8s.cv.io/add-init-containers created

    If the following resources are not already present in your Kubernetes cluster, Commvault creates them:

    • A "cv-config" namespace

    • A "restoremodifiers.k8s.cv.io" CRD definition

  2. To use the resource modifier when running a full application restore or a namespace-level restores, selecting the resource modifier from the Modifiers list in the Command Center.

  3. To test the resource modifier, use the Test function in the Command Center.

Managing Resource Modifiers for a Cluster Using the Command Center

Creating a Resource Modifier

  1. From the navigation pane, go to Protect > Kubernetes.

    The Applications tab appears.

  2. On the Clusters tab, click the cluster.

    The cluster page appears.

  3. On the Configuration tab, in the Advanced options section, click Configure.

    The Configure modifiers page appears.

  4. For Modifier, on the right side of the page, click the add button add/plus button - gray - no border.

    The New modifier dialog box appears.

  5. Enter a name for the resource modifier.

  6. Click Continue.

  7. To specify a selector, choose a selector, enter the necessary values, and then click Add.

  8. To specify an action, choose an action, enter the necessary values, and then click Add.

  9. Click Save.

Modifying a Resource Modifier

  1. From the navigation pane, go to Protect > Kubernetes.

    The Applications tab appears.

  2. On the Clusters tab, click the cluster.

    The cluster page appears.

  3. On the Configuration tab, in the Advanced options section, click Configure.

    The Configure modifiers page appears.

  4. For Select a modifier, select the resource modifier to modify.

  5. Make the modifications.

  6. Click Save.

Deleting a Resource Modifier

  1. From the navigation pane, go to Protect > Kubernetes.

    The Applications tab appears.

  2. On the Clusters tab, click the cluster.

    The cluster page appears.

  3. On the Configuration tab, in the Advanced options section, for the resource modifier that you want to delete, click the delete button.

    A confirmation dialog box appears.

  4. Click Delete Modifier.

Testing a Resource Modifier

You can test resource modifiers to see how they will modify your YAML.

  1. From the navigation pane, go to Protect > Kubernetes.

    The Applications tab appears.

  2. On the Configuration tab, in the Advanced options section, click Configure.

    The Configure modifiers page appears.

  3. At the bottom of the page, click Test.

    The Test modifiers dialog box appears.

  4. For Selected modifiers, select the modifiers to include in the test.

  5. Paste your source YAML in the box.

  6. Click Test.

Examples

The following examples show how you can use resource modifiers:

Example: Add a ‘modified-by: commvault’ label to all PVC resources from a ‘mysql’ namespace

Selectors and Actions

  • Selector: Namespace | Value: mysql

  • Selector: Kind | Value: PersistentVolumeClaim

  • Action: Add | Path: /metadata/labels/modified-by | Value: commvault

YAMLs

Modifying Fields in Restored Kubernetes Resource YAMLs - example 1

Example: Change the host for the resources of kind “Route” with the name “ocp-route” after restore

Selectors and Actions

  • Selector: Kind | Value: Route

  • Selector: Name | Value: ocp-route

  • Action: Modify | Path: /spec/host | Value: ilocpbkp401.source | New value: ilocpde487.destination | Parameters: Exact

YAMLs

Modifying Fields in Restored Kubernetes Resource YAMLs - example 2

Example: Delete ownerReferences from all “Secret” resources that are owned by the “ServiceBinding” kind

Selectors and Actions

  • Selector: Kind | Value: Secret

  • Selector: Field | Value: ServiceBinding | Path: /metadata/ownerReferences/0/kind | Exact match: True | Criteria: Contains

  • Action: Delete | Path: /metadata/ownerReferences

YAMLs

Modifying Fields in Restored Kubernetes Resource YAMLs - example 3

Example: Remove ownerReferences from a resource YAML, if the resource is owned by a certain Kind

Selectors and Actions

  • Selector: Kind | Value: Deployment

  • Selector: Labels | Value: deployment:prod, operation:modify

  • Action: Modify | Path: /spec/template/spec/0/i... | Value: cvreg.commvault.com:8000 | New value: cvregnew.commvault.com:9000 | Parameters: Contains

YAMLs

Modifying Fields in Restored Kubernetes Resource YAMLs - example 4

Example: Change image of a Pod container to latest image

Selectors and Actions

  • Selector: Field | Value: nginx | Path: /spec/containers/0/image | Exact match: False | Criteria: Contains

  • Selector: Kind | Value: Pod

  • Action: Add | Path: /spec/containers/0/image | Value: nginx:latest | Parameters: Exact

YAMLs

Modifying Fields in Restored Kubernetes Resource YAMLs - example 5

Loading...