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 ci-create-operator-database-kubernetes.sh in the Kubernetes Operator Patterns and Best Practices repository.

    #Backup
    IMAGE_NAME="$REGISTRY/$ORG/$IMAGE_DATABASE_BACKUP"
    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"
    IMAGE_NAME="$REGISTRY/$ORG/$IMAGE_DATABASE_SERVICE"

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.

apiVersion: database.sample.third.party/v1alpha1
kind: DatabaseBackup
metadata:
  name: databasebackup-manual
  namespace: database
spec:
  image: DATABASE_BACKUP_IMAGE
  repos:
  - name: ibmcos-repo
    type: ibmcos
    secretName: ibmcos-repo
    serviceEndpoint: s3.eu.cloud-object-storage.appdomain.cloud
    cosRegion: eu-geo
    bucketNamePrefix: "database-backup-"
  manualTrigger:
    time: "2022-04-20T02:59:43.1Z"
    repo: ibmcos-repo
  scheduledTrigger:
    schedule: "0 0 * * *"
    repo: ibmcos-repo

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

Greetings,

Thomas

#bash, #sed, #automation

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

Add yours

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.

Blog at WordPress.com.

Up ↑

%d bloggers like this: