In order to further develop this application the following tools may be needed:
- Visual Studio Code or IntelliJ IDEA as Integrated Development Environment (IDE)
- Tanzu Developer Tools plugin for mentioned IDE
- Docker Desktop to execute integration tests or run the application locally
- Curl for local testing
- Pack CLI for local testing
To quickly test locally, run this command in the root directory.
pack build java-function --path . --builder ghcr.io/vmware-tanzu/function-buildpacks-for-knative/functions-builder:0.0.12
Where java-function is the name of your runnable function image, later used by Docker.
Then run via Docker:
docker run -it --rm -p 8080:8080 java-function
Check for a successful response with the commands in the Testing section below.
With our functions, you should see some HTML or sample text returned indicating a success.
After deploying your function, you can interact with the function by running:
curl -w'\n' localhost:8080/handler \
-H "Content-Type: application/json" \
-d '{"firstName":"John", "lastName":"Doe"}' -i
Where
/handleras a path invokes that specific function
If you'd like to test this function, you may use this CloudEvent saved as cloudevent.json:
{
"specversion" : "1.0",
"type" : "hire",
"source" : "https://spring.io/",
"id" : "A234-1234-1234",
"datacontenttype" : "application/json",
"data": {
"firstName": "John",
"lastName": "Doe"
}
}
NOTE: that you should change the contents of the CloudEvent you're testing against as you update the function.
After deploying your function as an image, you can test with:
curl -X POST -H "Content-Type: application/cloudevents+json" -d @cloudevent.json http://localhost:8080
Using the config/workload.yaml it is possible to build, test and deploy this application onto a
Kubernetes cluster that is provisioned with Tanzu Application Platform (https://tanzu.vmware.com/application-platform).
NOTE: The provided
config/workload.yamlfile uses the Git URL for this sample. When you want to modify the source, you must push the code to your own Git repository and then update thespec.source.gitinformation in theconfig/workload.yamlfile.
You need to select the accelerator option Include TAP deployment resources when generating the project for the steps below to function.
When you are done developing your app, you can simply deploy it using:
tanzu apps workload apply -f config/workload.yaml
If you would like deploy the code from your local working directory you can use the following command:
tanzu apps workload create java-function -f config/workload.yaml \
--local-path . \
--source-image <REPOSITORY-PREFIX>/java-function-source \
--type web
Determine the URL to use for the accessing the app by running:
tanzu apps workload get java-function
NOTE: This depends on the TAP installation having DNS configured for the Knative ingress.
After deploying your function, you can interact with the function by using:
NOTE: Replace the placeholder with the actual URL.
curl -w'\n' <URL>/handler \
-H "Content-Type: application/json" \
-d '{"firstName":"John", "lastName":"Doe"}' -i
Where
/handleras a path invokes that specific function
If you'd like to test this function, you may use this CloudEvent saved as cloudevent.json:
{
"specversion" : "1.0",
"type" : "hire",
"source" : "https://spring.io/",
"id" : "A234-1234-1234",
"datacontenttype" : "application/json",
"data": {
"firstName": "John",
"lastName": "Doe"
}
}
NOTE: that you should change the contents of the CloudEvent you're testing against as you update the function.
curl -X POST -H "Content-Type: application/cloudevents+json" -d @cloudevent.json <URL>
You may use tilt >v0.27.2 in combination with TAP's VS Code plugin to enable live development features including Application Live View and Live Update.
You will have to update some fields in the root directory's Tiltfile to connect your live session to Kubernetes.
Update the allow_k8s_contexts line of the Tiltfile to indicate the Kubernetes context to use.
Update the Tiltfile or set the SOURCE_IMAGE environment variable to indicate the registry path where TAP should store your image.
export SOURCE_IMAGE=registry/project/java-function
export K8S_TEST_CONTEXT="a-kubernetes-context"
tilt up
tilt down
There is an environment variable BP_FUNCTION. Use this to point to the Java class that contains your function.
Note that you can use this variable in the pack cli when building locally and in the config/workload.yaml file when using the Tanzu CLI (and also tilt up)
If you need to add dependencies to your Java function, use Maven or Gradle in the normal fashion. The Maven and Gradle build files default to building a fat runnable jar. This allows your additional dependencies to be included and available during runtime.
For example, you could add the following to pom.xml:
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
which would allow you to add import org.apache.commons.lang3.StringUtils; to your Java code and make use of StringUtils.