Working with Java vibrant tooling ecosystem on Kubernetes nowadays is a blast and you should know about it!
Wait, wait, what’s going on???
Let’s go through the BOM (Bill of Material) of the used tools first:
🌐 Kubernetes extensions #
On Kubernetes, the Operator pattern has quickly become a popular way to provide “Kubernetes native” experience for various services and applications.
I work on the Keycloak project and we will use its CRD for this post.
Try out your own!
🔥 Fabric8 kubernetes-client #
In the Java world, there is a top-notch kubernetes-client that enables seamless interaction with a Kubernetes cluster.
In particular, the
fabric8 kubernetes-client provides a fully typed API for resources
that gives Java developers a huge kick for safety and productivity.
Starting from the upcoming version there is a new module called java-generator that takes CRDs as input spits output plain Java files that are directly usable from the previously mentioned
resources typed API.
❗ Jbang #
jbang is an awesome tool for running Java programs with ease. Not just that, it already evolved into a pretty comprehensive tool for managing/packaging/using Java programs from the command line with minimal to no setup.
🔁 JShell #
jshell is a Read-Evaluate-Print Loop (REPL) for Java that can be used for learning and prototyping Java code. Here we are (ab)using it to be our interactive shell.
🍡 All together #
Glue all of this with a little bit of
bash and you have a little shell script that can be executed as a one-liner.
The script takes one argument being the URL of the (
json) CRD we want to interact with, e.g.:
bash <(curl -sL https://raw.githubusercontent.com/andreaTP/playing-with-jbang/main/k8s-shell.sh) \ https://raw.githubusercontent.com/keycloak/keycloak-k8s-resources/18.0.0/kubernetes/keycloaks.k8s.keycloak.org-v1.yml
The script will, in a
- download the CRD
- generate the Java code for the CRD using the latest version (
-SNAPSHOT) of the
- generate a little
.javafile containing a basic dependencies/sources configuration for the following steps
- produce a single
jarcontaining everything needed using
- start a
✈️ now enjoy the fun of experimenting with Java code on the fly!
Quickly sort out the
import io.fabric8.kubernetes.client.*; import org.keycloak.k8s.v2alpha1.*;
Create a client for Kubernetes:
var client = new DefaultKubernetesClient();
You can already do pretty nifty things on your cluster with (typed!) commands like:
client.pods().list().getItems().stream().map(p -> p.getMetadata().getName()).collect(Collectors.joining(", "));
Create a client for the Keycloak CRD:
var kcClient = client.resources(Keycloak.class);
Play with your
keycloak CRs in the cluster using, again, a fully typed API!
var kc = kcClient.list().getItems().get(0); kc.getSpec().getInstances()
✌️ Conclusions #
Java ecosystem and tooling are growing, maturing, and improving at an incredible pace making it a first-tier choice for developing on top of Kubernetes as of today. In this post, we explored how to lively interact with a Kubernetes cluster and custom CRDs through a fully typed, generated on-the-fly, Java native API.
🏃♂️ Go and try it at home! 🏃♂️
Bear in mind that we are using some bleeding edge tech here but you can already play and take advantage of it as a developer. 😇