Answer a question

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

Answers

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)

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐