Accessing Knative REST API using Fabric8 Knative Client

Getting Fabric8 Knative Client:

<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:

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());
}
}

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()
.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:

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

Creating, Updating and Deleting resources:

kn.routes().inNamespace("ns1").withName("r1").
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
.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();
}
// Deletes with Foreground cascading deletion
kn.routes()
.inNamespace(NAMESPACE)
.withName(routeName)
.withPropagationPolicy(DeletionPropagation.FOREGROUND)
.delete();

--

--

--

https://about.me/rohankanojia

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 ‘us.shein.com’ website

Wrangling Ruby Versions and Gems

BASIC GIT COMMANDS.

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

https://about.me/rohankanojia

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