diff --git a/jvm/kotlin/.gitignore b/jvm/kotlin/.gitignore new file mode 100644 index 0000000..48f70a0 --- /dev/null +++ b/jvm/kotlin/.gitignore @@ -0,0 +1,6 @@ +.project +.settings +.classpath +.idea/ +target/ +hello-world.iml diff --git a/jvm/kotlin/README.md b/jvm/kotlin/README.md new file mode 100644 index 0000000..5f64554 --- /dev/null +++ b/jvm/kotlin/README.md @@ -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! +``` diff --git a/jvm/kotlin/build.sh b/jvm/kotlin/build.sh new file mode 100755 index 0000000..a9e7afe --- /dev/null +++ b/jvm/kotlin/build.sh @@ -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 diff --git a/jvm/kotlin/pom.xml b/jvm/kotlin/pom.xml new file mode 100644 index 0000000..332e1a0 --- /dev/null +++ b/jvm/kotlin/pom.xml @@ -0,0 +1,139 @@ + + + 4.0.0 + io.fission + hello-world + 1.0-SNAPSHOT + jar + + hello-world + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + 1.8 + 1.5.20 + true + + + + + org.springframework.boot + spring-boot-starter-web + 2.0.1.RELEASE + provided + + + io.fission + fission-java-core + 0.0.2-SNAPSHOT + provided + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} + + + + + ${project.basedir}/src/main/kotlin + + + maven-assembly-plugin + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + false + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + 1.8 + + + + org.apache.maven.plugins + maven-compiler-plugin + + + default-compile + none + + + default-testCompile + none + + + compile + compile + + compile + + + + testCompile + test-compile + + testCompile + + + + + 1.8 + 1.8 + + + + + + + + + fission-java-core + fission-java-core-snapshot + https://oss.sonatype.org/content/repositories/snapshots/ + + + + diff --git a/jvm/kotlin/specs/README b/jvm/kotlin/specs/README new file mode 100644 index 0000000..1db3f9a --- /dev/null +++ b/jvm/kotlin/specs/README @@ -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. + diff --git a/jvm/kotlin/specs/env-java.yaml b/jvm/kotlin/specs/env-java.yaml new file mode 100644 index 0000000..f3bbb58 --- /dev/null +++ b/jvm/kotlin/specs/env-java.yaml @@ -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 diff --git a/jvm/kotlin/specs/fission-deployment-config.yaml b/jvm/kotlin/specs/fission-deployment-config.yaml new file mode 100644 index 0000000..753e349 --- /dev/null +++ b/jvm/kotlin/specs/fission-deployment-config.yaml @@ -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 diff --git a/jvm/kotlin/specs/function-hello.yaml b/jvm/kotlin/specs/function-hello.yaml new file mode 100644 index 0000000..8188e5d --- /dev/null +++ b/jvm/kotlin/specs/function-hello.yaml @@ -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 diff --git a/jvm/kotlin/specs/package-hellojava.yaml b/jvm/kotlin/specs/package-hellojava.yaml new file mode 100644 index 0000000..0da0366 --- /dev/null +++ b/jvm/kotlin/specs/package-hellojava.yaml @@ -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" diff --git a/jvm/kotlin/src/main/kotlin/io/fission/HelloWorld.kt b/jvm/kotlin/src/main/kotlin/io/fission/HelloWorld.kt new file mode 100644 index 0000000..0c33039 --- /dev/null +++ b/jvm/kotlin/src/main/kotlin/io/fission/HelloWorld.kt @@ -0,0 +1,11 @@ +package io.fission + +import org.springframework.http.RequestEntity +import org.springframework.http.ResponseEntity + +internal class HelloWorld : Function { + 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!") + } +}