GoogleCloudPlatform / pgadapter

PostgreSQL wire-protocol proxy for Cloud Spanner
https://cloud.google.com/spanner/docs/postgresql-interface#postgresql-client-support
Apache License 2.0
51 stars 19 forks source link

Exit PGAdapter Sidecar when main container exits #2040

Open hiradp opened 4 days ago

hiradp commented 4 days ago

Thank you in advance for your time!

I have a kubernetes job that's using pgadapter as a side car. When the main container finishes, the pod get stuck in NotReady because pg adpter is running.

Is there native way to handle this properly?

I've seen this stackoverflow post https://stackoverflow.com/questions/41679364/kubernetes-stop-cloudsql-proxy-sidecar-container-in-multi-container-pod-job but this looks a bit of a hack.

olavloite commented 2 days ago

@hiradp Thanks for reporting this. I'm afraid there isn't any really better methods available at the moment. Sending PGAdapter a SIGINT or SIGTERM is currently the only way to shut it down.

We are considering an alternative, though. Normal PostgreSQL can be shut down by sending it a command like COPY (SELECT 1) TO PROGRAM 'pg_ctl stop -m smart --no-wait';. For PGAdapter, we're considering making this even simpler by just adding a custom SQL statement SHUTDOWN [SMART|FAST|IMMEDIATE]. This statement would only be enabled if PGAdapter is started with a command line argument that allows it to be executed, which would typically be used in a situation where it is running as a side-car container. Would that work in your case?

See https://www.postgresql.org/docs/current/server-shutdown.html for more information on the shutdown modes of PostgreSQL.

hiradp commented 2 days ago

Yep that would work in our case. In fact, I wanted to suggest using the same pattern as session management to conduct this.

For anybody else that come across this. We managed to get it working with the following:

              command:
                - /bin/bash
                - -c
              args:
                - |
                  trap "touch /cache/kill" EXIT
                  ./command
              command:
                - /bin/bash
                - -c
              args:
                - |
                  /home/pgadapter/startup.sh -p project -i instance -d db &
                  PID=$!
                  while [ ! -e "/cache/kill" ]; do
                      sleep 1;
                  done
                  kill $PID
hiradp commented 2 days ago

Also cloud-sql-proxy exposes a /quitquitquit endpoint but that's probably super involved if you don't have that infra setup.