Skip to content

Commit

Permalink
Merge pull request #10293 from loicmathieu/gcp/http-functions-improve…
Browse files Browse the repository at this point in the history
…ments

Gcp HTTP functions improvements
  • Loading branch information
gsmet authored Jun 28, 2020
2 parents 2b9ae02 + 66a349a commit 2810852
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 22 deletions.
27 changes: 7 additions & 20 deletions docs/src/main/asciidoc/gcp-functions-http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ and pull requests should be submitted there:
https://github.com/quarkusio/quarkus/tree/master/docs/src/main/asciidoc
////
= Quarkus - Google Cloud Functions (Serverless) with RESTEasy, Undertow, or Vert.x Web
:extension-status: preview
:extension-status: experimental

include::./attributes.adoc[]

Expand All @@ -13,6 +13,8 @@ Undertow (servlet) or Vert.x Web, and make these microservices deployable to the

One Google Cloud Functions deployment can represent any number of JAX-RS, servlet, or Vert.x Web endpoints.

As the Google Cloud Function Java engine is a new Beta feature of Google Cloud, this extension is flagged as experimental.

include::./status-include.adoc[]

== Prerequisites
Expand Down Expand Up @@ -140,33 +142,18 @@ public class GreetingRoutes {

== Build and Deploy to Google Cloud

To build your application, you first need to define a packaging of type `uber-jar` via your `application.properties`.

[source]
----
quarkus.package.uber-jar=true
----
NOTE: Quarkus forces a packaging of type `uber-jar` for your function as Google Cloud Function deployment requires a single JAR.

Package your application using the standard `mvn clean package` command.
The result of the previous command is a single JAR file inside the `target` repository that contains classes and dependencies of the project.

To deploy your JAR to Google Cloud, you need to pass a directory with only this JAR inside it, to the `gcloud` utility.

So first, create a `deployment` directory and copy the generated artifact inside it.

[source]
----
mkdir deployment
cp target/google-cloud-functions-http-1.0-SNAPSHOT-runner.jar deployment/
----
The result of the previous command is a single JAR file inside the `target/deployment` directory that contains the classes and the dependencies of the project.

Then you will be able to use `gcloud` to deploy your function to Google Cloud.

[source]
----
gcloud beta functions deploy quarkus-example-http \
--entry-point=io.quarkus.gcp.functions.http.QuarkusHttpFunction \
--runtime=java11 --trigger-http --source=deployment
--runtime=java11 --trigger-http --source=target/deployment
----

[IMPORTANT]
Expand Down Expand Up @@ -211,7 +198,7 @@ Then you can use it to launch your function locally.
[source]
----
java -jar java-function-invoker-1.0.0-beta1.jar \
--classpath target/google-cloud-functions-http-1.0-SNAPSHOT-runner.jar \
--classpath target/deployment/google-cloud-functions-http-1.0-SNAPSHOT-runner.jar \
--target io.quarkus.gcp.functions.http.QuarkusHttpFunction
----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.quarkus.gcp.functions.http.deployment;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;

import io.quarkus.builder.BuildException;
import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.pkg.builditem.ArtifactResultBuildItem;
import io.quarkus.deployment.pkg.builditem.JarBuildItem;
import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem;
import io.quarkus.deployment.pkg.builditem.UberJarRequiredBuildItem;
import io.quarkus.deployment.pkg.steps.NativeBuild;

public class CloudFunctionsDeploymentBuildStep {
@BuildStep
public UberJarRequiredBuildItem forceUberJar() {
// Google Cloud Function needs a single JAR inside a dedicated directory
return new UberJarRequiredBuildItem();
}

/**
* Creates a target/deployment dir and copy the uber jar in it.
* This facilitates the usage of the 'glcoud' command.
*/
@BuildStep(onlyIf = IsNormal.class, onlyIfNot = NativeBuild.class)
public ArtifactResultBuildItem functionDeployment(OutputTargetBuildItem target, JarBuildItem jar)
throws BuildException, IOException {
if (!jar.isUberJar()) {
throw new BuildException("Google Cloud Function deployment need to use a uberjar, " +
"please set 'quarkus.package.uber-jar=true' inside your application.properties",
Collections.EMPTY_LIST);
}

Path deployment = target.getOutputDirectory().resolve("deployment");
if (Files.notExists(deployment)) {
Files.createDirectory(deployment);
}

Path jarPath = jar.getPath();
Path targetJarPath = deployment.resolve(jarPath.getFileName());
Files.deleteIfExists(targetJarPath);
Files.copy(jarPath, targetJarPath);

return new ArtifactResultBuildItem(targetJarPath, "function", Collections.EMPTY_MAP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ metadata:
categories:
- "cloud"
guide: "https://quarkus.io/guides/gcp-functions-http"
status: "preview"
status: "experimental"
5 changes: 5 additions & 0 deletions integration-tests/google-cloud-functions-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
quarkus.package.uber-jar=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.it.gcp.functions.http;

import static io.restassured.RestAssured.when;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class GreetingTest {

@Test
public void testGreeting() {
when().get("/hello").then().statusCode(200);

when().get("/servlet/hello").then().statusCode(200);

when().get("/vertx/hello").then().statusCode(200);
}
}

0 comments on commit 2810852

Please sign in to comment.