How to dynamically set values containing URLs (with slashes) in Kubernetes yaml files & prevent: bad flag in substitute command

1/28/2022

We're using Tekton as our CI/CD solution and want to replace the value of {{DASHBOARD_HOST}} inside our pipeline-run.yml, which looks like this:

apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  generateName: buildpacks-test-pipeline-run-
spec:
  serviceAccountName: buildpacks-service-account-gitlab # Only needed if you set up authorization
  pipelineRef:
    name: buildpacks-test-pipeline
  workspaces:
    - name: source-workspace
      subPath: source
      persistentVolumeClaim:
        claimName: buildpacks-source-pvc
    - name: cache-workspace
      subPath: cache
      persistentVolumeClaim:
        claimName: buildpacks-source-pvc
    - name: maven-repo-cache
      subPath: maven-repo-cache
      persistentVolumeClaim:
        claimName: buildpacks-source-pvc
  params:
    - name: IMAGE
      value: registry.gitlab.com/jonashackt/microservice-api-spring-boot # This defines the name of output image
    - name: REPO_PATH_ONLY
      value: jonashackt/microservice-api-spring-boot
    - name: SOURCE_REVISION
      value: 3c4131f8566ef157244881bacc474543ef96755d
    - name: DASHBOARD_PORT
      value: 8765
    - name: DASHBOARD_HOST
      value: {{DASHBOARD_HOST}}

We tried to use the approach with sed described in this so answer:

DASHBOARD_HOST=http://abd1c6f-123246.eu-central-1.elb.amazonaws.com
sed "s/{{DASHBOARD_HOST}}/$DASHBOARD_HOST/g" pipeline-run.yml | kubectl apply -f -

but get the following error:

bad flag in substitute command: '/'

Any idea on how to use sed to substitute the {{DASHBOARD_HOST}} variable?

-- jonashackt
amazon-web-services
config
kubernetes
templates
yaml

1 Answer

1/28/2022

The variable you want to replace contains slashes - and sed "s/{{DASHBOARD_HOST}}/$DASHBOARD_HOST/g" tells sed to use / as the delimiter. This produces the error. But as sed s command can use any character as a delimiter, we could optimize the solution using s# instead of s/ like this:

sed "s#{{DASHBOARD_HOST}}#$DASHBOARD_HOST#g" app-deployment.yaml | kubectl apply -f -

We can also ommit the cat as stated by gertvdijk since sed is able to read files on it's own. The variable we want to replace inside the app-deployment.yaml could look somehow like this:

...
params:
  - name: DASHBOARD_HOST
    value: {{DASHBOARD_HOST}}
...

Multiple variable substitution

Using sed you can even replace multiple variables in your yaml file. Let's assume your app-deployment.yaml has the following contents:

...
params:
  - name: DASHBOARD_HOST
    value: {{DASHBOARD_HOST}}
  - name: DASHBOARD_PORT
    value: {{DASHBOARD_PORT}}
...

Now set both variables inside your shell:

DASHBOARD_HOST=http://abd1c6f-123246.eu-central-1.elb.amazonaws.com
DASHBOARD_PORT=9785

And then chain the sed s# commands using ; like this:

sed "s#{{DASHBOARD_HOST}}#$DASHBOARD_HOST#g;s#{{DASHBOARD_PORT}}#$DASHBOARD_PORT#g" app-deployment.yaml | kubectl apply -f -
-- jonashackt
Source: StackOverflow