How to use environment variable in kubernetes container command?

6/20/2019

I am trying to deploy cloudsql proxy as sidecar contaier like this:

        - name: cloudsql-proxy
          image: gcr.io/cloudsql-docker/gce-proxy:1.11
          command: ["/cloud_sql_proxy",
              "-instances=${CLOUDSQL_INSTANCE}=tcp:5432",
              "-credential_file=/secrets/cloudsql/google_application_credentials.json"]
          env:
            - name: CLOUDSQL_INSTANCE
              valueFrom:
                secretKeyRef:
                  name: persistence-cloudsql-instance-creds
                  key: instance_name
          volumeMounts:
            - name: my-secrets-volume
              mountPath: /secrets/cloudsql
              readOnly: true

But when I deploy this, I get following error in logs:

2019/06/20 13:42:38 couldn't connect to "${CLOUDSQL_INSTANCE}": googleapi: Error 400: Missing parameter: project., required

How could I use environment variable in command that runs inside kubernetes container?

-- Bunyk
cloud-sql-proxy
kubernetes

1 Answer

6/20/2019

If you want to reference environment variables in the command you need to put them in parentheses, something like: $(CLOUDSQL_INSTANCE).

-- Adam Otto
Source: StackOverflow