Skip to content

Commit

Permalink
Merge pull request #2 from fnproject/master
Browse files Browse the repository at this point in the history
Origin repo changes sync
  • Loading branch information
denis256 authored Mar 22, 2018
2 parents df5707d + 52ac503 commit 3130cc2
Show file tree
Hide file tree
Showing 30 changed files with 669 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ jobs:

- run:
name: Login to Docker
command: docker login -u $DOCKER_USER -p $DOCKER_PASS
command: |
if [[ "${CIRCLE_BRANCH}" == "master" && -z "${CIRCLE_PR_REPONAME}" ]]; then
docker login -u $DOCKER_USER -p $DOCKER_PASS
fi
- run:
name: Build fn-java-fdk Docker build image
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ target/
logs/
/headrevtag.txt
*.bak
*.versionsBackup
*.versionsBackup
.gradle
examples/gradle-build/build
25 changes: 25 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.netbeans.tools</groupId>
<artifactId>sigtest-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<sigfile>src/main/api/snapshot.sigfile</sigfile>
<action>strictcheck</action>
<packages>com.fnproject.fn.api,com.fnproject.fn.api.exception,com.fnproject.fn.api.flow</packages>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
467 changes: 467 additions & 0 deletions api/src/main/api/snapshot.sigfile

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions api/src/main/java/com/fnproject/fn/api/TypeWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public interface TypeWrapper {
*
* For example, take the following classes:
* <pre>{@code
* class GenericParent&lt;T&gt; {
* class GenericParent<T> {
* public void someMethod(T t) { // do something with t }
* }
*
* class ConcreteClass extends GenericParent&lt;String&gt; { }
* class ConcreteClass extends GenericParent<String> { }
* }</pre>
*
* A {@link TypeWrapper} representing the first argument of {@code someMethod} would return {@code String.class}
Expand Down
4 changes: 3 additions & 1 deletion api/src/main/java/com/fnproject/fn/api/flow/Flows.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
/**
* Fn Flow API entry point class - this provides access to the current {@link Flow} for the current function invocation.
*/
public class Flows {
public final class Flows {
private Flows() {
}

private static FlowSource flowSource;

Expand Down
23 changes: 23 additions & 0 deletions api/src/test/java/com/fnproject/fn/api/flow/FlowsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.fnproject.fn.api.flow;

import java.lang.reflect.Modifier;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

public class FlowsTest {
public FlowsTest() {
}

/** People shall not be allowed to create subclasses of {@code Flow}:
* <pre>
* static class MyFlows extends Flows {
* }
* </pre>
*/
@Test
public void dontSubclassFlows() {
assertTrue("Flows is final", Modifier.isFinal(Flows.class.getModifiers()));
assertEquals("No visible constructors", 0, Flows.class.getConstructors().length);
}
}
2 changes: 1 addition & 1 deletion build-image/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM maven:3-jdk-8-slim

ARG FN_REPO_URL
ADD pom.xml /tmp/cache-deps/pom.xml
ADD cache-deps.sh /tmp/cache-deps/cache-deps.sh
ADD src /tmp/cache-deps/src
Expand Down
1 change: 1 addition & 0 deletions build-image/Dockerfile-jdk9
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM maven:3-jdk-9-slim
ARG FN_REPO_URL

ADD pom.xml /tmp/cache-deps/pom.xml
ADD cache-deps.sh /tmp/cache-deps/cache-deps.sh
Expand Down
10 changes: 9 additions & 1 deletion build-image/cache-deps.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#!/bin/bash -ex

cd /tmp/cache-deps && mvn test package dependency:copy-dependencies -Dmaven.repo.local=/usr/share/maven/ref/repository -Dmdep.prependGroupId=true -DoutputDirectory=target
set -x
set -e

if [ -n "$FN_REPO_URL" ]; then
REPO_DFN="-Dfn.repo.url=$FN_REPO_URL"
fi


cd /tmp/cache-deps && mvn test package dependency:copy-dependencies -Dmaven.repo.local=/usr/share/maven/ref/repository -Dmdep.prependGroupId=true -DoutputDirectory=target $REPO_DFN
cd / && rm -fr /tmp/cache-deps
7 changes: 6 additions & 1 deletion build-image/docker-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
cd /tmp/staging-repository && python -mSimpleHTTPServer 18080 1>>/tmp/http-logs 2>&1 &
SRV_PROCESS=$!

docker build $*
if [ -n "$DOCKER_LOCALHOST" ]; then
REPO_ENV="--build-arg FN_REPO_URL=http://$DOCKER_LOCALHOST:18080"
fi

docker build $REPO_ENV $*

kill $SRV_PROCESS
3 changes: 2 additions & 1 deletion build-image/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<fnproject.version>1.0.0-SNAPSHOT</fnproject.version>
<fn.repo.url>http://172.17.0.1:18080</fn.repo.url>
</properties>

<!-- The artifact is irrelevant, this pom is only used to download dependencies. -->
Expand Down Expand Up @@ -64,7 +65,7 @@
<repositories>
<repository>
<id>fn-maven-releases</id>
<url>http://172.17.0.1:18080</url>
<url>${fn.repo.url}</url>
</repository>
</repositories>
</project>
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ access to data entering the `fn` Java FDK.
This example showcases the Fn Flow asynchronous execution API, by
creating a workflow that takes an image and asynchronously generates three
thumbnails for it, then uploads them to an object storage.

## 5. Gradle build
This shows how to use Gradle to build functions using the Java FDK.
4 changes: 4 additions & 0 deletions examples/gradle-build/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.gradle
.idea
.git
build
20 changes: 20 additions & 0 deletions examples/gradle-build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM gradle:4.5.1-jdk8 as build-stage
WORKDIR /function
# needed for gradle?
USER root
ENV GRADLE_USER_HOME /function/.gradle

# Code build
# Copy any build files into the build container and build
COPY *.gradle /function/
RUN ["gradle", "-s", "--no-daemon","--console","plain","cacheDeps"]

# Copies build source into build container
COPY src /function/src

RUN ["gradle", "-s", "--no-daemon","--console","plain","build"]
# Container build
FROM fnproject/fn-java-fdk:1.0.56
WORKDIR /function
COPY --from=build-stage /function/build/libs/*.jar /function/build/deps/*.jar /function/app/
CMD ["com.example.fn.HelloFunction::handleRequest"]
14 changes: 14 additions & 0 deletions examples/gradle-build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Fn Gradle + Java fdk example

Fn uses Maven by default for builds. This is an example that uses Fn's `docker` runtime format to build a Java function using the [Fn Java FDK](https://github.com/fnproject/fdk-java).

The example consists of a `Dockerfile` that builds the function using gradle and copies the function's dependencies to `build/deps` and a func.yaml that uses that `Dockerfile` to build the function.

Note that FDK versions are hard-coded in this example, you may need to update them manually to more recent version.

Key points:

* [Dockerfile](Dockerfile) - contains the containerised docker build (based on dockerhub library/gradle images) and image build - this includes the gradle invocation
* The `cacheDeps` task in `build.gradle` is invoked within the Dockerfile - The task pulls down dependencies into the container gradle cache to speed up docker builds.
* The `copyDeps` task in `build.gradle` copies the functions compile deps
* This uses JDK 8 by default - you can change this to Java 9 by changing : `FROM gradle:4.5.1-jdk8 as build-stage` to `FROM gradle:4.5.1-jdk9 as build-stage` and `FROM fnproject/fn-java-fdk:1.0.56` to `FROM fnproject/fn-java-fdk:jdk9-1.0.56`
32 changes: 32 additions & 0 deletions examples/gradle-build/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apply plugin: 'java'

ext {
fdkVersion = '1.0.56'
}

repositories {
mavenCentral()
maven {
url 'https://dl.bintray.com/fnproject/fnproject'
}
}

dependencies {
runtime "com.fnproject.fn:api:$fdkVersion"
// runtime "com.fnproject.fn:runtime:$fdkVersion" // this is optional and included with its deps in the base image to reduce layer size

testCompile "junit:junit:4.12"
testCompile "com.fnproject.fn:testing:$fdkVersion"
}

task cacheDeps(type: Exec) {
configurations.testRuntime.files
commandLine 'echo', 'Downloaded all dependencies'
}

task copyDeps(type: Copy) {
from configurations.compile
into "${project.buildDir}/deps"
}

build.dependsOn copyDeps
4 changes: 4 additions & 0 deletions examples/gradle-build/func.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: gradle_build
version: 0.0.3
runtime: docker
format: http
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.fn;

public class HelloFunction {

public String handleRequest(String input) {
String name = (input == null || input.isEmpty()) ? "world" : input;
return "Hello, " + name + "!";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.fn;

import com.fnproject.fn.testing.*;
import org.junit.*;

import static org.junit.Assert.*;

public class HelloFunctionTest {

@Rule
public final FnTestingRule testing = FnTestingRule.createDefault();

@Test
public void shouldReturnGreeting() {
testing.givenEvent().enqueue();
testing.thenRun(HelloFunction.class, "handleRequest");

FnResult result = testing.getOnlyResult();
assertEquals("Hello, world!", result.getBodyAsString());
}

}
6 changes: 4 additions & 2 deletions integration-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@ To run locally you will need to deploy the fn artifacts to a local repository:
(in top-level dir)
```bash
export REPOSITORY_LOCATION=/tmp/staging-repository
# on OSX:
export DOCKER_LOCALHOST=docker.for.mac.localhost

mvn deploy -DaltDeploymentRepository=localStagingDir::default::file://"$REPOSITORY_LOCATION"
```

You may also want to/need build local copies of the build images:
```bash
cd build-images
cd build-image
./docker-build.sh -t fnproject/fn-java-fdk-build .
./docker-build.sh -f Dockerfile-jdk9 -t fnproject/fn-java-fdk-build:jdk9-latest .
```

and runtime images:
```
cd runtime
docker build -t fnproject/fn-java-fdk .
docker build -f Dockerfile-jdk9 -t fnproject/fn-java-fdk:jdk9-latest .
```
Expand Down
1 change: 1 addition & 0 deletions integration-tests/main/test-1-jdk8/func.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ runtime: java8
cmd: com.fnproject.fn.integration.test_1.CompleterFunction::handleRequest
format: http
timeout: 120
name: test-1-jdk8
3 changes: 2 additions & 1 deletion integration-tests/main/test-1/func.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: 0.0.1
name: test-1
version: 0.0.5
runtime: java
cmd: com.fnproject.fn.integration.test_1.CompleterFunction::handleRequest
format: http
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/main/test-2/func.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: fn-test/test-2
name: test-2
version: 0.0.1
runtime: java9
cmd: com.fnproject.fn.integration.test2.PlainFunction::handleRequest
Expand Down
1 change: 1 addition & 0 deletions integration-tests/main/test-4/func.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ build:
-Dmdep.prependGroupId=true -DoutputDirectory=target
format: http
timeout: 120
name: test-4
1 change: 1 addition & 0 deletions integration-tests/main/test-5/func.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ runtime: java9
cmd: com.fnproject.fn.integration.test_5.CompleterFunction::handleRequest
format: http
timeout: 120
name: test-5
1 change: 1 addition & 0 deletions integration-tests/main/test-6/func.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ runtime: java9
cmd: com.fnproject.fn.integration.test_6.CompleterFunction::handleRequest
format: http
timeout: 120
name: test-6
2 changes: 1 addition & 1 deletion integration-tests/run-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ docker pull $FUNCTIONS_DOCKER_IMAGE

FUNCTIONS_CONTAINER_ID=$(
docker run -d \
-p 8080 \
-p 8080:8080 \
-v /var/run/docker.sock:/var/run/docker.sock \
--name functions-$SUFFIX \
-e FN_LOG_LEVEL=debug \
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/smoke-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fi

[[ -n "$PRE_BUILD_HOOK" ]] && $PRE_BUILD_HOOK

fn build --no-cache >build-output 2>&1 || {
fn -v build --no-cache >build-output 2>&1 || {
echo "Test function build failed:"
cat build-output
exit 1
Expand Down
2 changes: 1 addition & 1 deletion release.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.57
1.0.60

0 comments on commit 3130cc2

Please sign in to comment.