Accessing Knative REST API using Fabric8 Knative Client

Getting Fabric8 Knative Client:


Listing all Service object in a given namespace:

try (KnativeClient kn = new DefaultKnativeClient()) {
// Get all Service objects
ServiceList services =
// Iterate through list and print names
for (Service svc : services.getItems()) {

Getting Instance of Knative Client from Kubernetes Client:

try (KubernetesClient client = new DefaultKubernetesClient()) {
KnativeClient kn = null;
if (client.isAdaptable(KnativeClient.class)) {
System.out.println("Client is adaptable, adapting...");
kn = client.adapt(KnativeClient.class);

// ...
// Use kn for Knative operations
} else {
System.out.println("Sorry, could not adapt client");

Creating Knative Objects on the fly using Fabric8’s builders:

try (KnativeClient kn = new DefaultKnativeClient()) {
// Create Service object
Service service = new ServiceBuilder()
.addToContainers(new ContainerBuilder()
.withValue("Go Sample V1")

// Apply it onto Kubernetes Server"default").createOrReplace(service);

Loading Yaml manifests into Java objects:

Service svc =

Creating, Updating and Deleting resources:

get() // Get resource
patch(Route r) // Patch resource back to APIServer
edit() // Edit resource
delete() // Delete resource
try (KnativeClient kn = new DefaultKnativeClient()) {
// Load Route object from YAML
InputStream routeYamlIn = KnativeRouteCrudDemo.class
Route route = kn.routes().load(routeYamlIn).get();

// Create Route object into Kubernetes

// Get Route object from APIServer
String routeName = route.getMetadata().getName();
route = kn.routes().inNamespace(NAMESPACE)

// Edit Route object, add some dummy label
.addToAnnotations("context", "demo")

// Delete Route object
// Deletes with Foreground cascading deletion




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Pandemic Panic to Software Developer

[P2E] Puzzle Royale Summary

Cloning of ‘’ website

Wrangling Ruby Versions and Gems


How to get burned by moving to a microservice architecture.

GIT ‘er done!

WordPress Redirects Creation Guide

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rohan Kumar

Rohan Kumar

More from Medium

Cloud-native development with Quarkus

How to connect to multiple RabbitMQ instances using Spring Cloud Stream

Host & Secure your Java dependencies in 5 minutes with a Private Maven repository

REST API using Quarkus and Panache

Quarkus REST API