Bash scripting: How to create a new custom resource file from a template file using sed?

In this blog post we use an existing template file, that we created for a custom resource to insert the needed value of an URL to point to a container image in a container registry, to create a custom resource yaml file, we use this file to deploy a custom resource to a cluster. The template file contains a string which we will replace with the content of URL for that container image.

Note: We haven’t used helm, kustomize to do that in this situation, because we did a custom way for the automation.

With the Linux sed command, we will search for the string value DATABASE_BACKUP_IMAGE in the the template file and replace this value with the content of the $IMAGE_NAME variable.

Then we save the changes in with operator >> to a new file. You can find the following code as a part of the bash script in the Kubernetes Operator Patterns and Best Practices repository.

    echo $IMAGE_NAME
    sed "s+DATABASE_BACKUP_IMAGE+$IMAGE_NAME+g" "$DATABASE_TEMPLATE_FOLDER/database.sample_v1alpha1_databasebackup-TEMPLATE.yaml" > "$ROOT_FOLDER/operator-database/config/samples/database.sample_v1alpha1_databasebackup.yaml"
    cat $ROOT_FOLDER/operator-database/config/samples/database.sample_v1alpha1_databasecluster.yaml | grep "$IMAGE_DATABASE_BACKUP"

In the code below you see the custom resource to create a DatabaseBackup instance on a cluster.
You find related code in the Kubernetes Operator Patterns and Best Practices repository.

kind: DatabaseBackup
  name: databasebackup-manual
  namespace: database
  - name: ibmcos-repo
    type: ibmcos
    secretName: ibmcos-repo
    cosRegion: eu-geo
    bucketNamePrefix: "database-backup-"
    time: "2022-04-20T02:59:43.1Z"
    repo: ibmcos-repo
    schedule: "0 0 * * *"
    repo: ibmcos-repo

I hope this was useful for you and let’s see what’s next?



#bash, #sed, #automation

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.