Last week we do an example of a Kotlin Serverless Function using Apache OpenWhisk, however since we using Kotlin we could make our example a native application that will not run in the JVM.
We will need as the week before to have OpenWhisk, and we will require again Vagrant and Virtual Box, so install those first.
To setup the environment just:
Now we could ssh to the machine with:
To test the action directly just using the docker that I’ve upload to docker hub:
To test the new action just run:
This will output something like:
OpenWhisk allow us to use a docker that contain a binary that will be execute when an action is invoke, to do it so the binary will receive 1 argument with a JSON string and it need to return a JSON string in a single line.
So to create our function first we need to have kotlin-native installed and configured:
The last line will raise and error since we haven provide any parameters to the compiler but we just do that in order to download the compiler dependencies and toolchain.
Now we will create our project, kotlin native use a gradle plugin that we initial setup as:
We need as well to specify where is kotlin-native installed using the gradle.properties:
now lets just create one simple main in the folder src/main.kt
Now to compile simple :
We use clean because sometimes gradle will not compile the files even with the files changed.
Then we could run it with:
Now we need to use JSON but since we don’t have the JVM neither we could use any Java code we are just going to use a C JSON library, I’m choose parson since is perfect for the task.
First clone it, or create a submodule in your git:
No I’ve create a shell script to compile the C code into a library that we could latter link:
This script will create a library in LLVM format, the architecture used by Kotlin Native.
Now we need to update our gradle script to compile the C code, and to link it to our program.
Finally, we need to add a definition file for the C code so Kotlin could create and stub for it.
The file should be place in src/c_interop/Parson.def :
Now we could just build as before to check that everything is ok:
This should generate some files including:
build/clang/parson.bc : The library that could be linked.
build/konan/interopStubs/genParsonInteropStubs/Parson/Parson.kt : The Kotin Stub for calling the library.
No we modify our program to use parson:
We have create a couple of functions that uses the Kotlin/Native interoperability so we could invoke Parson.
After building the program again we could just use it like this:
No we need to create a docker for it, OpenWhisk have a docker base image that is based on alpine linux, I’ve not been able to make Kotlin-Native to work on alpine so I’ve create a base docker image that any one could use for making Kotlin Native Serverless functions.
The docker file for that image is:
I’ve use openjdk 8 since we are going to use gradle in our build and it use the actionProxy python3 script that is provide by OpenWhisk to create a flask server that will serve our action trough HTTP, our action should be place in the folder /action and be named exec.
So now we could create a docker image for our fibonacci function:
Finally, I create a simple scripts to publish the images in docker hub, remember to login before.
In the way that the files are added to the docker we can just run that command to get update the image only doing the parts that actually change.
So now we could put all things together to do:
This has been a great example to learn Kotlin Native, so sure I’ll do more things with it in a future.
Note: Many things could be change in this example, for example I could create a builder image that generate the binary and use for creating another image that has only that binary, since we do not need the whole compiler infrastructure and the image could be much smaller, but for demonstration is just OK.
About Juan Medina I'm just a normal geek that code all kind of stuff, from complex corporate applications to games.
Games, music, movies and traveling are my escape pods.