Accessing Knative REST API using Fabric8 Knative Client

Rohan Kumar
3 min readJun 7, 2020

--

Fabric8 Kubernetes Client also offers very useful extensions for Knative, Tekton and Service Catalog. It was added by Ioannis Cannellos (main author of Fabric8 Kubernetes Client) in this PR. Since then, it has evolved a lot. Today I would be giving a quick overview of Fabric8 Knative extension.

Getting Fabric8 Knative Client:

You can find Knative Client on Maven Central as usual. You can start using Knative client by adding it as a dependency in your pom.xml :

<dependencies>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>knative-client</artifactId>
<version>${knative-client.version}</version>
</dependency>
</dependencies>

Listing all Service object in a given namespace:

Once added as a dependency, you can start using Fabric8 Knative Client. Let’s start by listing all the Service objects. Here is how you would do it with Fabric8 Knative client:

try (KnativeClient kn = new DefaultKnativeClient()) {
// Get all Service objects
ServiceList services = kn.services()
.inNamespace("default")
.list();
// Iterate through list and print names
for (Service svc : services.getItems()) {
System.out.println(svc.getMetadata().getName());
}
}

You can clearly see usage syntax of Fabric8 Knative Client is very similar to Fabric8 Kubernetes Client. It used Fabric8 Kubernetes Client underneath for all it’s basic operations(i.e interacting with REST API etc).

Getting Instance of Knative Client from Kubernetes Client:

Since Knative Client is just an extension of Fabric8 Kubernetes Client, it’s also possible to get an instance of KnativeClient from KubernetesClient . Here is how you would do it:

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:

Since Knative Client is based on Knative Model which is formed via parsing go structs inside knative source into a JSON schema and then using sundrio on top of it. We get very useful builder objects with which we can quickly build up Knative resources without loading anything from any Yaml manifest. Here is an example of using Service with builders:

try (KnativeClient kn = new DefaultKnativeClient()) {
// Create Service object
Service service = new ServiceBuilder()
.withNewMetadata()
.withName("helloworld-go")
.endMetadata()
.withNewSpec()
.withNewTemplate()
.withNewSpec()
.addToContainers(new ContainerBuilder()
.withImage("gcr.io/knative-samples/helloworld-go")
.addNewEnv()
.withName("TARGET")
.withValue("Go Sample V1")
.endEnv()
.build())
.endSpec()
.endTemplate()
.endSpec()
.build();

// Apply it onto Kubernetes Server
kn.services().inNamespace("default").createOrReplace(service);
}

Loading Yaml manifests into Java objects:

Fabric8 Knative Client, just like Fabric8 Kubernetes Client provides a seamless developer experience when it comes to serializing/de-serializing Yaml manifests to/from Java objects. You can easily load :

Service svc = kn.services()
.load(getClass().getResourceAsStream("/knative-svc.yml"))
.get();

Creating, Updating and Deleting resources:

Doing CRUD operations is no big deal if you’re already familiar with Fabric8 Kubernetes Client API. If you’re accessing some resource (for example, Route ). You can access it using:

kn.routes().inNamespace("ns1").withName("r1").
get() // Get resource
patch(Route r) // Patch resource back to APIServer
edit() // Edit resource
delete() // Delete resource

let’s have a look at a working example of CRUD operations using Fabric8 Knative Client. We would be loading, editing and finally deleting a Route object. Here is code for these series of operations:

try (KnativeClient kn = new DefaultKnativeClient()) {
// Load Route object from YAML
InputStream routeYamlIn = KnativeRouteCrudDemo.class
.getResourceAsStream("/knative-route.yml");
Route route = kn.routes().load(routeYamlIn).get();

// Create Route object into Kubernetes
kn.routes().inNamespace(NAMESPACE).createOrReplace(route);

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

// Edit Route object, add some dummy label
kn.routes().inNamespace(NAMESPACE).withName(routeName).edit()
.editOrNewMetadata()
.addToAnnotations("context", "demo")
.endMetadata()
.done();

// Delete Route object
kn.routes().inNamespace(NAMESPACE).withName(routeName).delete();
}

If you want to delete resources with more fine grained control, you can also provide deletion propagation policy like this:

// Deletes with Foreground cascading deletion
kn.routes()
.inNamespace(NAMESPACE)
.withName(routeName)
.withPropagationPolicy(DeletionPropagation.FOREGROUND)
.delete();

This concludes my short overview of Fabric8 Knative Client. I hope you liked it. Please try it out and provide feedback. If you face any problems, please reach out to us on our Gitter Channel or via Github issues. Source code of this blog is provided in this Github repository:

If you like our work, please help us out by spreading the word about the project and 🌟 our Github page.

Thanks for taking time to read my blog 🙏🏾

--

--

No responses yet