Home > Software design >  Is it possible to add dependent environment variables in Google Cloud Run?
Is it possible to add dependent environment variables in Google Cloud Run?

Time:09-26

I would like to specify dependent environment variables on a Cloud Run service.

If the environment variables have been defined in a .env file it would look like this

DATABASE_NAME=my-database
DATABASE_USER=root
DATABASE_PASSWORD=P4SSw0rd!
DATABASE_PORT=5432
DATABASE_HOST="/socket/my-database-socket"
DATABASE_URL="user=${DATABASE_USER} password=${DATABASE_PASSWORD} dbname=${DATABASE_NAME} host=${DATABASE_HOST}"

In this example, DATABASE_URL depends on every other environment variables.

To deploy the service I run the following command:

gcloud run deploy my-service \
--image gcr.io/my-project/my-image:latest \
--region europe-west1 \
--port 80 \
--platform managed \
--allow-unauthenticated \
--set-env-vars 'DATABASE_NAME=my-database' \
--set-env-vars 'DATABASE_USER=root' \
--set-env-vars 'DATABASE_PASSWORD=P4SSw0rd!' \
--set-env-vars 'DATABASE_PORT=5432' \
--set-env-vars 'DATABASE_HOST="/socket/my-database-socket"' \
--set-env-vars 'DATABASE_URL="user=$(DATABASE_USER) password=$(DATABASE_PASSWORD) dbname=$(DATABASE_NAME) host=$(DATABASE_HOST)"'

Here is the created YAML definition of the service (some values are omitted)

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: my-service
spec:
  template:
    metadata:
      name: ...
    spec:
      containerConcurrency: 80
      timeoutSeconds: 300
      containers:
      - image: ...
        ports:
        - name: http1
          containerPort: 80
        env:
        - name: DATABASE_NAME
          value: my-database
        - name: DATABASE_USER
          value: root
        - name: DATABASE_PASSWORD
          value: P4SSw0rd!
        - name: DATABASE_HOST
          value: /socket/my-database-socket
        - name: DATABASE_URL
          value: user=$(DATABASE_USER) password=$(DATABASE_PASSWORD) dbname=$(DATABASE_NAME) host=$(DATABASE_HOST)

The problem is that when the service is running, the env vars in DATABASE_URL seem not interpolated.

I read that Kubernetes supports dependent env vars but I can't figure out how to make this run in Cloud Run.

I am wondering if it is supported in Cloud Run in the end.

CodePudding user response:

It's likely this may work in Knative open source (which uses Kubernetes to execute pods) but not on Google Cloud Run (fully hosted), which runs on a proprietary execution engine.

  • Related