Skip to content
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

Add JVM/Kotlin example #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions jvm/kotlin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.project
.settings
.classpath
.idea/
target/
hello-world.iml
46 changes: 46 additions & 0 deletions jvm/kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Hello World in JVM/Kotlin on Fission

The `io.fission.HelloWorld` class is a very simple fission function that implements `io.fission.Function` and says "Hello, World!".

## Building locally and deploying with Fission

You can build the jar file in one of the two ways below based on your setup:

- You can use docker without the need to install JDK and Maven to build the jar file from source code:

```shell script
$ bash ./build.sh

```
- If you have JDK and Maven installed, you can directly (and much faster) build the JAR file using command:

```shell script
$ mvn clean package
```

Both of above steps will generate a target subdirectory which has the archive `target/hello-world-1.0-SNAPSHOT-jar-with-dependencies.jar` which will be used for creating function.

- The archive created above will be used as a deploy package when creating the function.

**Note:** The archive contains a Java 9 file, and the Fission environment currently only supports Java 8. To fix this,
run the following command every time after building the archive:

```shell script
$ zip -d target/hello-world-1.0-SNAPSHOT-jar-with-dependencies.jar 'META-INF/versions/*'
```

```shell script
$ fission env create --name jvm --image fission/jvm-env --version 2 --keeparchive=true
$ fission fn create --name hello --deploy target/hello-world-1.0-SNAPSHOT-jar-with-dependencies.jar --env jvm --entrypoint io.fission.HelloWorld
$ fission route create --function hello --url /hellop --method GET
$ fission fn test --name hello
Hello World! Greetings from function 'hello' delivered by Kotlin!
```

If you use Minikube for this, alternatively run the function using

```shell script
$ export FISSION=$(minikube ip):$(kubectl -n fission get svc router -o jsonpath='{...nodePort}')
$ curl http://$FISSION/hellop
Hello, World! Greetings from function 'hello' delivered by Kotlin!
```
5 changes: 5 additions & 0 deletions jvm/kotlin/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
# This script allows you to build the jar without needing Maven & JDK installed locally.
# You need docker, as it uses a Docker image to build source code
set -eou pipefail
docker run -it --rm -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.5-jdk-8 mvn clean package
139 changes: 139 additions & 0 deletions jvm/kotlin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>io.fission</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>hello-world</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<kotlin.version>1.5.20</kotlin.version>
<maven.test.skip>true</maven.test.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.fission</groupId>
<artifactId>fission-java-core</artifactId>
<version>0.0.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<!-- Adding Sonatype repository to pull snapshots -->
<repositories>
<repository>
<id>fission-java-core</id>
<name>fission-java-core-snapshot</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>

</project>
42 changes: 42 additions & 0 deletions jvm/kotlin/specs/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

Fission Specs
=============

This is a set of specifications for a Fission app. This includes functions,
environments, and triggers; we collectively call these things "resources".

How to use these specs
----------------------

These specs are handled with the 'fission spec' command. See 'fission spec --help'.

'fission spec apply' will "apply" all resources specified in this directory to your
cluster. That means it checks what resources exist on your cluster, what resources are
specified in the specs directory, and reconciles the difference by creating, updating or
deleting resources on the cluster.

'fission spec apply' will also package up your source code (or compiled binaries) and
upload the archives to the cluster if needed. It uses 'ArchiveUploadSpec' resources in
this directory to figure out which files to archive.

You can use 'fission spec apply --watch' to watch for file changes and continuously keep
the cluster updated.

You can add YAMLs to this directory by writing them manually, but it's easier to generate
them. Use 'fission function create --spec' to generate a function spec,
'fission environment create --spec' to generate an environment spec, and so on.

You can edit any of the files in this directory, except 'fission-deployment-config.yaml',
which contains a UID that you should never change. To apply your changes simply use
'fission spec apply'.

fission-deployment-config.yaml
------------------------------

fission-deployment-config.yaml contains a UID. This UID is what fission uses to correlate
resources on the cluster to resources in this directory.

All resources created by 'fission spec apply' are annotated with this UID. Resources on
the cluster that are _not_ annotated with this UID are never modified or deleted by
fission.

17 changes: 17 additions & 0 deletions jvm/kotlin/specs/env-java.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: fission.io/v1
kind: Environment
metadata:
creationTimestamp: null
name: java
namespace: default
spec:
builder:
command: build
image: fission/jvm-builder:1.7.1
imagepullsecret: ""
keeparchive: true
poolsize: 3
resources: {}
runtime:
image: fission/jvm-env:1.7.1
version: 2
7 changes: 7 additions & 0 deletions jvm/kotlin/specs/fission-deployment-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file is generated by the 'fission spec init' command.
# See the README in this directory for background and usage information.
# Do not edit the UID below: that will break 'fission spec apply'
apiVersion: fission.io/v1
kind: DeploymentConfig
name: java
uid: 908a303a-bf23-4bae-a22c-b1db9a1f71a9
27 changes: 27 additions & 0 deletions jvm/kotlin/specs/function-hello.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: fission.io/v1
kind: Function
metadata:
creationTimestamp: null
name: hello
namespace: default
spec:
InvokeStrategy:
ExecutionStrategy:
ExecutorType: poolmgr
MaxScale: 0
MinScale: 0
SpecializationTimeout: 120
TargetCPUPercent: 0
StrategyType: execution
configmaps: null
environment:
name: java
namespace: default
functionTimeout: 60
package:
functionName: io.fission.HelloWorld
packageref:
name: hellojava
namespace: default
resources: {}
secrets: null
26 changes: 26 additions & 0 deletions jvm/kotlin/specs/package-hellojava.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
include:
- src
- pom.xml
kind: ArchiveUploadSpec
name: src-URlE

---
apiVersion: fission.io/v1
kind: Package
metadata:
creationTimestamp: null
name: hellojava
namespace: default
spec:
deployment:
checksum: {}
environment:
name: java
namespace: default
source:
checksum: {}
type: url
url: archive://src-URlE
status:
buildstatus: pending
lastUpdateTimestamp: "2020-01-29T09:43:56Z"
11 changes: 11 additions & 0 deletions jvm/kotlin/src/main/kotlin/io/fission/HelloWorld.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.fission

import org.springframework.http.RequestEntity
import org.springframework.http.ResponseEntity

internal class HelloWorld : Function<Any?, Any?> {
override fun call(req: RequestEntity<*>, context: Context?): ResponseEntity<*> {
val functionName = req.headers["x-fission-function-name"]?.first() ?: "I do not know who I are?!?"
return ResponseEntity.ok("Hello, World! Greetings from function '$functionName' delivered by Kotlin!")
}
}