#1 Global Leader in Data Protection & Ransomware Recovery

How to ‘Transform’ Multiple Resources with Regex

KB ID: 4593
Product: Veeam Kasten for Kubernetes
Published: 2024-06-07
Last Modified: 2024-06-07
mailbox
Get weekly article updates
By subscribing, you are agreeing to have your personal information managed in accordance with the terms of Veeam's Privacy Notice.

Cheers for trusting us with the spot in your mailbox!

Now you’re less likely to miss what’s been brewing in our knowledge base with this weekly digest

error icon

Oops! Something went wrong.

Please, try again later.

Purpose

Veeam Kasten makes use of the ‘Transforms’ feature to migrate applications between different clusters, between different storage backends, and many other use cases. Transforms enable modifications to Kubernetes resources on restore.

The ability to move an application across clusters is an extremely powerful feature that enables a variety of use cases including Disaster Recovery (DR), Test/Dev with realistic data sets, and performance testing in isolated environments.

Transforms are loosely modeled on JSON patch and support regex to modify resources with a pattern. 

The Veeam Kasten for Kubernetes documentation provides a simple example of Transformation. This article provides an example to showcase the use of regex and transforms for modifying ingress/route hostnames.

Solution

Transforming the Routes/Ingresses Hostnames During Restores

‘Transformation replace’ operations can be used to replace the hostname in the routes/ingresses. However, if multiple ingress/route hostnames follow a particular pattern, regex can be used to match the text and replace them.

Example Scenario

There are 4 ingresses with the hostnames as shown below:

#  kubectl get ingress -n mysql
NAME         CLASS    HOSTS                             ADDRESS   PORTS     AGE
apigateway   <none>   prd-apigateway.apps.example.com             80, 443   4h58m
backend      <none>   cn-backend.apps.example.com                 80        4h57m
frontend     <none>   cn-frontend.apps.example.com                80        4h56m
pgdb         <none>   prd-pgdb.apps.example.com                   80        4h56m

While restoring these ingresses to another cluster, there is a requirement to change the prefixes [prd-] and [cn-] from the ingress hostnames to [dr-]. The restored ingresses/routes are expected to have the following hostnames.

dr-apigateway.apps.example.com
dr-pgdb.apps.example.com
dr-backend.apps.example.com
dr-frontend.apps.example.com

Multiple transformation operations can be used to achieve this, which isn’t efficient! Veeam Kasten for Kubernetes has inbuilt support for matching patterns using regex for transformation.

Following regex expression and the JSONpath expression can be used to achieve the desired transformation outcome.

  • JSONPath for ingresses: /spec/rules/0/host
  • JSONPath for routes: /spec/host
  • Regex: (prd|cn)(.*)(\.apps\.example\.com)
  • Value: dr${2}${3}

The above expression matches the pattern as shown in the image below

regexr Note: https://regex101.com/ can be used to test and try out regular expressions.

Adding Transforms Operation From the UI

While performing restore or creating an import policy with restore, in UI, under Optional Restore Settings:

  1. Enable Apply transforms to restored resources.
  2. Select Add new transform to start creating a transformation.
  1. In the ‘New Transform’ form, assign the transform a Display Name.
  2. Add a New Operation intended with the transform.
    Operations that are supported include Test, Add, Remove, Copy, Move, and Replace without or with Regex pattern matching.

    In this example scenario, Replace with Regex was selected.
    (Replace element at path with JSON value. Match the target text with regex and substitute regex capturing groups with value.)
new transform
This operation can be tested from the UI by comparing the original and transformed resource.

Adding Transforms Operation From the Policy/RestoreAction API

Transform operations can also be accomplished by editing the Policy CR using ‘kubectl edit’ to add the transform field, shown below, under restoreParameters while creating an import+restore policy.

This can also be added to a restoreAction (https://docs.kasten.io/latest/api/actions.html#restoreaction). 

transforms:
- subject:
    resource: ingresses
  name: replace-ingress-host
  json:
  - op: replace
    path: /spec/rules/0/host
    value: dr${2}${3}
    regex: (prd|cn)(.*)(\.apps\.example\.com)

Trigger the import policy and verify the ingress hostnames after the restore.

#  kubectl get ingress -n mysql-clone
NAME         CLASS    HOSTS                            ADDRESS   PORTS     AGE
apigateway   <none>   dr-apigateway.apps.example.com             80, 443   20m
backend      <none>   dr-backend.apps.example.com                80        20m
frontend     <none>   dr-frontend.apps.example.com               80        20m
pgdb         <none>   dr-pgdb.apps.example.com                   80        20m
Veeam Kasten for Kubernetes also supports saving these transforms with a resource called Transformsets and these transformsets can be referenced during restore in the restore form. 
To submit feedback regarding this article, please click this link: Send Article Feedback
To report a typo on this page, highlight the typo with your mouse and press CTRL + Enter.

Spelling error in text

This site is protected by hCaptcha and its Privacy Policy and Terms of Service apply except as noted in our Privacy Policy.
Thank you!

Thank you!

Your feedback has been received and will be reviewed.

Oops! Something went wrong.

Please, try again later.

You have selected too large block!

Please try select less.

KB Feedback/Suggestion

This form is only for KB Feedback/Suggestions, if you need help with the software open a support case

By submitting, you are agreeing to have your personal information managed in accordance with the terms of Veeam's Privacy Notice.
This site is protected by hCaptcha and its Privacy Policy and Terms of Service apply except as noted in our Privacy Policy.
Verify your email to continue your product download
We've sent a verification code to:
  • Incorrect verification code. Please try again.
An email with a verification code was just sent to
Didn't receive the code? Click to resend in sec
Didn't receive the code? Click to resend
Thank you!

Thank you!

Your feedback has been received and will be reviewed.

error icon

Oops! Something went wrong.

Please, try again later.