Paul Bakker bio photo

Paul Bakker

Software architect, author of Modular Cloud Apps with OSGi and Java 9 Modularity

Java 9 Modularity Email Twitter Google+ LinkedIn Github Stackoverflow

We are working a lot with Kubernetes lately and were looking for a Java API to integrate our dashboards with Kubernetes. There used to be a few libraries to choose from, but most are not actively maintained and are still using the depricated beta APIs.

The library which is part of Fabric8 seems to be the best/only option, but unfortunatly the library’s dependency graph is huge mess. This is always bad, you should be very weary if Maven/Gradle tries to pull in half of Maven central, but it’s even worse when working in a modular environemnt like OSGi. We started out as we always do with these kind of libraries: wrapping them in a self contained bundle to at least hide the mess. This didn’t work out very well, there is just no end to the list of (transitive) dependencies.

So we decided to build something ourselves, and decided to open source it as part of Amdatu. As with every new Amdatu component it will first live in the “labs” incubator until we decide it’s stable and useful enough to make it to amdatu.org.

Introducing Amdatu Kubernetes

The Amdatu Kubernetes API makes it easy to use Kubernetes from OSGi or Java in general.

@Component
public class Example {
	@ServiceDependency	
	private volatile Kubernetes m_kubernetes;

	public void test() {
		kubernetes.listNodes().subscribe(nodes -> {
			nodes.getItems().forEach(System.out::println);
		});
	}
}

In a non-OSGi Java application you pass the Kubernetes API url to the constructor of the KubernetesRestClient.

public class PlainJavaExample {
	private Kubernetes m_kubernetes;
	
	@Before
	public void setup() {
		m_kubernetes = new KubernetesRestClient("http://[kubernetes-api]:8080");
	}
	
	@Test
	public void listNodes() {
		TestSubscriber<NodeList> testSubscriber = new TestSubscriber<>();
		m_kubernetes.listNodes().subscribe(testSubscriber);

		testSubscriber.assertCompleted();
		testSubscriber.assertNoErrors();
	}
}

The API is based on RX Observables. This makes it easier to deal with the asynchronous nature of the API.

The model classes used by Amdatu Kubernetes are from the Fabric8 project. These are generated from the JSON schema provided by Kubernetes, so there was no point in re-doing this.

Learn more

The integration tests in the project are the best place to learn how to use the API. Configure the test suite with a Kubernets API url and it will create a test namespace with a ReplicationController and Pods and executes several operations on them.

Of course also take a look at the JavaDocs and get the latest binary. You can also build the sources yourself using Gradle.

The Kubernetes API is not (yet) fully covered by Amdatu Kubernetes, but the most important methods are, including watches. Of course pull requests are welcome!