ostrokach
ostrokach

Reputation: 19992

Set values in a knative service.yaml file using environment variables

Is there a way to set the values of some keys in a Knative service.yaml file using environment variables?


More detail

I am trying to deploy a Knative service to a Kubernetes cluster using GitLab CI. Some of the variables in my service.yaml file depend on the project and environment of the GitLab CI pipeline. Is there a way I can seamlessly plug those values into my service.yaml file without resorting to hacks like sed -i ...?

For example, given the following script, I want the $(KUBE_NAMESPACE), $(CI_ENVIRONMENT_SLUG), and $(CI_PROJECT_PATH_SLUG) values to be replaced by accordingly-named environment variables.

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: design
  namespace: "$(KUBE_NAMESPACE)"
spec:
  template:
    metadata:
      name: design-v1
      annotations:
        app.gitlab.com/env: "$(CI_ENVIRONMENT_SLUG)"
        app.gitlab.com/app: "$(CI_PROJECT_PATH_SLUG)"
    spec:
      containers:
        - name: user-container
          image: ...
      timeoutSeconds: 600
      containerConcurrency: 8

Upvotes: 3

Views: 1313

Answers (2)

Rico
Rico

Reputation: 61669

More than a Knative issue this is more of Kubernetes limitation. Kubernetes allows some expansion but not in annotations or namespace definitions. For example, you can do it in container env definitions:

containers:
- env:
  - name: PODID
    valueFrom: ...
  - name: LOG_PATH
    value: /var/log/$(PODID)

If this is a CI/CD system like Gitlab the environment variables should be in a shell environment, so a simple shell expansion will do. For example.

#!/bin/bash

echo -e "
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: design
  namespace: "${KUBE_NAMESPACE}"
spec:
  template:
    metadata:
      name: design-v1
      annotations:
        app.gitlab.com/env: "${CI_ENVIRONMENT_SLUG}"
        app.gitlab.com/app: "${CI_PROJECT_PATH_SLUG}"
    spec:
      containers:
        - name: user-container
          image: ...
      timeoutSeconds: 600
      containerConcurrency: 8
" | kubectl apply -f -

You can also use envsubst as a helper like mentioned in the other answer.

Upvotes: 2

Brian Pursley
Brian Pursley

Reputation: 1196

I don't think there is a great way to expand environment variables inside of an existing yaml, but if you don't want to use sed, you might be able to use envsubst:

envsubst < original.yaml > modified.yaml

You would just run this command before you use the yaml to expand the environment variables contained within it.

Also I think you'll need your variables to use curly braces, instead of parentheses, like this: ${KUBE_NAMESPACE}.

EDIT: You might also be able to use this inline like this: kubectl apply -f <(envsubst < service.yaml)

Upvotes: 2

Related Questions