Skip to content

Automatically detect GCF environments and enable use of socket at /cloudsql #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public static Socket connect(Properties props, String socketPathFormat) throws I
"cloudSqlInstance property not set. Please specify this property in the JDBC URL or the "
+ "connection Properties with value in form \"project:region:instance\"");

// GAE Standard runtimes provide a connection path at "/cloudsql/<CONNECTION_NAME>"
if (forceUnixSocket || runningOnGaeStandard()) {
// GAE Standard + GCF provide a connection path at "/cloudsql/<CONNECTION_NAME>"
if (forceUnixSocket || runningOnGaeStandard() || runningOnGoogleCloudFunctions()) {
logger.info(
String.format(
"Connecting to Cloud SQL instance [%s] via unix socket.", csqlInstanceName));
Expand All @@ -195,6 +195,13 @@ private static boolean runningOnGaeStandard() {
&& ("Production".equals(runEnv) || "java11".equals(gaeRuntime));
}

/** Returns {@code true} if running in a Google Cloud Functions runtime. */
private static boolean runningOnGoogleCloudFunctions() {
// Functions automatically sets a few variables we can use to guess the env:
// See https://cloud.google.com/functions/docs/env-var#nodejs_10_and_subsequent_runtimes
return System.getenv("K_SERVICE") != null && System.getenv("K_REVISION") != null;
}

/**
* Creates a secure socket representing a connection to a Cloud SQL instance.
*
Expand Down