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
In addition a short example to change two values at the same time:
LikeLike