diff --git a/docs/examples/job-mysql.yaml b/docs/examples/job-mysql.yaml new file mode 100644 index 00000000..1e7da3ce --- /dev/null +++ b/docs/examples/job-mysql.yaml @@ -0,0 +1,85 @@ +# Copyright 2023 Google LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +### +# This demonstrates how to configure a batch job so that it shuts down +# the proxy containers when it has finished processing. +# +# The operator will set an environment variable called CSQL_QUIT_URLS +# with a space-separated list of URLs to each proxy container's /quitquitquit +# endpoint. In most cases, your job will only have one proxy container attached, +# so the value of CSQL_QUIT_URLS will be something like +# "http://localhost:9091/quitquitquit" +# +# The main job container should send a POST request each URL when the job +# container process finishes. This will cause the proxy side-car containers +# to shut down. +# +# In Kubernetes 1.28, side-car containers will be properly supported. When +# That happens, we will update the operator to use the built-in side-car +# mechanism for workloads on clusters version 1.28 and higher. +# +# See https://github.com/kubernetes/enhancements/issues/753 +# and https://github.com/GoogleCloudPlatform/cloud-sql-proxy-operator/issues/381 +apiVersion: batch/v1 +kind: Job +metadata: + name: job + labels: + app: busybox +spec: + template: + metadata: + creationTimestamp: null + labels: + app: busybox + spec: + containers: + - name: busybox + # Run your batch job command. + # Then, Iterate through the attached proxy shutdown hooks + # set by the operator in $CSQL_QUIT_URLS. Call each proxy's shutdown + # hook HTTP endpoint. + command: + - sh + - -c + - > + psql --host=$DB_HOST --port=$DB_PORT --username=$DB_USER '--command=select 1' --echo-queries --dbname=$DB_NAME + + for url in $CSQL_QUIT_URLS ; do + wget --post-data '' $url + done + image: busybox + imagePullPolicy: IfNotPresent + resources: {} + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + restartPolicy: Never + terminationGracePeriodSeconds: 30 + +--- +apiVersion: cloudsql.cloud.google.com/v1 +kind: AuthProxyWorkload +metadata: + name: job-proxy +spec: + instances: + - connectionString: project:region:instancename + hostEnvName: DB_HOST + portEnvName: DB_PORT + workloadSelector: + kind: Job + selector: + matchLabels: + app: busybox \ No newline at end of file