2

Similar to this issue. (I will describe the link in more detail below)

Problem: When running my Google Cloud Build I get an error stating:

django.db.utils.OperationalError: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/cloudsql/sample-kubernetes-268320:us-west1:cloud-build-staging/.s.PGSQL.3306"

I am following the solution posted here by collaborator. They provide a sample cloudbuild.yaml that I followed closely without any luck.

Working cloudbuild.yaml

steps:
  - id: proxy-install
    name: alpine:3.10
    entrypoint: sh
    args:
      - -c
      - 'wget -O /workspace/cloud_sql_proxy https://storage.googleapis.com/cloudsql-proxy/v1.16/cloud_sql_proxy.linux.386 &&  chmod +x /workspace/cloud_sql_proxy'
    waitFor: ['-']
  - id: execute-with-proxy
    name: python:3.7
    timeout: 100s
    entrypoint: sh
    args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=[INSTANCE_CONNECTION_NAME] & sleep 2) && (pip install -r requirements.txt && python test_sql.py)'
    waitFor: ['proxy-install']

My cloudbuild.yaml

steps:
  - id: proxy-install
    name: alpine:3.10
    entrypoint: sh
    args:
      - -c
      - 'wget -O /workspace/cloud_sql_proxy https://storage.googleapis.com/cloudsql-proxy/v1.16/cloud_sql_proxy.linux.386 &&  chmod +x /workspace/cloud_sql_proxy'
    waitFor: ['-']
  - id: Test
    name: 'python:3.7.6-buster'
    env:
      - "CLOUDBUILD_TEST=True"
    timeout: 100s
    entrypoint: /bin/sh
    args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'
    waitFor: ['proxy-install']

Steps I've taken to debug this:

  1. I have added the Cloud Build Service as the Cloud SQL Admin
  2. I have ensured my instance name is correct through using gcloud sqlinstances describe cloud-build-staging and copying the "connectionname"

Edit: I changed my cloudbuild.yaml file from

    args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'

to:

    args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/cloudsql -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'

With no effect.

Austin Peña
  • 300
  • 4
  • 11
  • It looks like you're mounting the socket on `/workspace`, I believe that should be `/cloudsql`? e.g: ` - '(/workspace/cloud_sql_proxy -dir=/cloudsql -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)' ` – Jerp Dertin Feb 18 '20 at 23:34
  • @JerpDertin I changed that without any effect. My edit above reflects that. However, I think that I am doing something wrong as both you and Kurtis came to that same conclusion. – Austin Peña Feb 19 '20 at 00:33
  • 1
    Thanks for providing that github link – Aseem Jun 22 '21 at 22:58

1 Answers1

2

Looks like you started the proxy with -dir=/workspace but then tried to connect at /cloudsql. You'll need either update the proxy or update the path your app uses to connect.

The line:

args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'

Should Read:

args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/cloudsql -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'

Other mistakes include:

  1. Listening at the wrong port. In your logs you will see the port that your database listens on. The default for postgres is 5432, yours is 3306.

  2. Verify your Cloud Build service has the correct permissions. Add [projectnumber]@cloudbuild.gserviceaccount.com as a "Cloud SQL Admin" through the IAM dashboard.

Austin Peña
  • 300
  • 4
  • 11
kurtisvg
  • 3,412
  • 1
  • 8
  • 24
  • I updated the path in my args to ```-dir=/cloudsql``` from ```-dir=/workspace``` to no effect. Is this what you were wanting me to do? Please see my update for a little more context. – Austin Peña Feb 19 '20 at 00:34
  • 1
    Next I world verify that the proxy has started correctly - you should see logs saying that it started or if it failed for some reason. – kurtisvg Feb 19 '20 at 00:47
  • It looks like I am getting an error during the Test phase (after pulling the python 3.7.6 buster) that says the following. Do you have an idea what API I need to enable? ```Status: Downloaded newer image for python:3.7.6-buster docker.io/library/python:3.7.6-buster 2020/02/19 00:23:23 current FDs rlimit set to 1048576, wanted limit is 8500. Nothing to do here. 2020/02/19 00:23:24 errors parsing config: googleapi: Error 403: The client is not authorized to make this request., notAuthorized``` – Austin Peña Feb 19 '20 at 15:30
  • 1
    You need to apply the "Cloud SQL Client" IAM role to whatever service account the proxy is using (probably the Cloud Build default?) – kurtisvg Feb 19 '20 at 17:16