Skip to content

Commit f9ca13f

Browse files
Merge pull request #2 from learnk8s/java
Java
2 parents 722bda3 + bdaf4f8 commit f9ca13f

File tree

5 files changed

+287
-1
lines changed

5 files changed

+287
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ npm-debug.log
44
*.env
55
*.dev
66
.DS_Store
7-
.vscode/
7+
.vscode/
8+
.idea/
9+
target/
10+
*.iml

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ This repository is meant to demonstrate how to create definitions for Kubernetes
55
You can find examples in the following languages:
66

77
- [Javascript](javascript/README.md)
8+
- [Java](java/README.md)
89
- [Go](go/README.md)

java/README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Templating Kubernetes resources with Java
2+
3+
In this section you will learn how to use Java and Maven to:
4+
5+
- create JSON (and YAML) definition for a Kubernetes Pod
6+
- submit a Pod definition to the cluster with code
7+
8+
Let's get started.
9+
10+
## Prerequisites
11+
12+
To your Maven application you need to add the `io.fabric8:kubernetes-client` dependency inside the `pom.xml` file
13+
14+
```xml
15+
<dependency>
16+
<groupId>io.fabric8</groupId>
17+
<artifactId>kubernetes-client</artifactId>
18+
<version>4.6.4</version>
19+
</dependency>
20+
```
21+
22+
## Generating Pod definitions
23+
24+
The code is short:
25+
26+
```java
27+
public static void main(String[] args) throws Exception {
28+
String environment = "production";
29+
Pod pod = createPod(namespace);
30+
31+
if (args[0].equals("yaml")) {
32+
System.out.println(SerializationUtils.dumpAsYaml(pod));
33+
} else {
34+
System.out.println(mapper.writeValueAsString(pod));
35+
}
36+
37+
}
38+
39+
public static Pod createPod( String environment ) {
40+
return new PodBuilder().withNewMetadata()
41+
.withName("test-pod")
42+
.endMetadata()
43+
.withNewSpec()
44+
.addNewContainer()
45+
.withName("test-container")
46+
.withImage("k8s.gcr.io/busybox")
47+
.withEnv(new EnvVarBuilder().withName("ENV").withValue(environment).build())
48+
.endContainer()
49+
.endSpec()
50+
.build();
51+
}
52+
```
53+
54+
You can compile and package the Maven application with:
55+
```shell
56+
mvn clean package
57+
```
58+
59+
You can execute the the application with:
60+
61+
```shell
62+
java -jar target/k8s-client-1.0.jar --dry-run
63+
```
64+
65+
The output is a JSON object for the Pod.
66+
67+
```json
68+
{
69+
"apiVersion" : "v1",
70+
"kind" : "Pod",
71+
"metadata" : {
72+
"annotations" : { },
73+
"labels" : { },
74+
"name" : "test-pod"
75+
},
76+
"spec" : {
77+
"containers" : [ {
78+
"env" : [ {
79+
"name" : "ENV",
80+
"value" : "production"
81+
} ],
82+
"image" : "k8s.gcr.io/busybox",
83+
"name" : "test-container"
84+
} ],
85+
"nodeSelector" : { }
86+
}
87+
}
88+
```
89+
90+
_But isn't Kubernetes accepting only YAML?_
91+
92+
YAML is a superset of JSON and any JSON file is also a valid YAML file.
93+
94+
You can create the Pod in the cluster with the following commands:
95+
96+
```shell
97+
java -jar target/k8s-client-1.0.jar yaml --dry-run
98+
kubectl apply -f pod.yaml
99+
```
100+
101+
## Creating a custom Kubectl
102+
103+
Instead of exporting the JSON and feeding it to kubectl, you can send the payload to the cluster directly.
104+
105+
You can use the [Fabric8 Kubernetes API](https://github.com/fabric8io/kubernetes-client) to send the Pod definition to the cluster.
106+
107+
Here's the code:
108+
109+
```java
110+
Config config = new ConfigBuilder().build();
111+
112+
try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
113+
if (namespace == null) {
114+
namespace = client.getNamespace();
115+
}
116+
117+
boolean dryRun = false;
118+
for (String arg : args) {
119+
if (arg.equals("--dry-run")) {
120+
dryRun = true;
121+
}
122+
}
123+
if (!dryRun) {
124+
client.pods().inNamespace(namespace).create(pod);
125+
System.out.println("Pod created!");
126+
}
127+
128+
129+
}
130+
131+
```
132+
133+
Assuming you are connected to a running cluster, you can execute the script with:
134+
135+
```shell
136+
java -jar target/k8s-client-1.0.jar yaml
137+
```
138+
139+
And you can verify that the Pod was created with:
140+
141+
```shell
142+
kubectl get pods
143+
```
144+
145+
## What's next
146+
147+
As you can imagine, this is a short demo and you can build more complex objects and use the power of Java and the Fabric8 Kubernetes API to compose large objects from smaller ones.

java/pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>io.learnk8s</groupId>
7+
<artifactId>k8s-client</artifactId>
8+
<version>1.0</version>
9+
10+
<name>k8s-client</name>
11+
12+
13+
<properties>
14+
<java.version>11</java.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>io.fabric8</groupId>
20+
<artifactId>kubernetes-client</artifactId>
21+
<version>4.6.4</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.slf4j</groupId>
25+
<artifactId>slf4j-api</artifactId>
26+
<version>1.7.25</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.slf4j</groupId>
30+
<artifactId>slf4j-jdk14</artifactId>
31+
<version>1.7.25</version>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-compiler-plugin</artifactId>
40+
<configuration>
41+
<source>11</source>
42+
<target>11</target>
43+
</configuration>
44+
</plugin>
45+
<plugin>
46+
<artifactId>maven-assembly-plugin</artifactId>
47+
<executions>
48+
<execution>
49+
<phase>package</phase>
50+
<goals>
51+
<goal>single</goal>
52+
</goals>
53+
</execution>
54+
</executions>
55+
<configuration>
56+
<archive>
57+
<manifest>
58+
<addClasspath>true</addClasspath>
59+
<mainClass>io.learnk8s.KubernetesClientCLI</mainClass>
60+
</manifest>
61+
</archive>
62+
<descriptorRefs>
63+
<descriptorRef>jar-with-dependencies</descriptorRef>
64+
</descriptorRefs>
65+
<finalName>${project.name}-${project.version}</finalName>
66+
<appendAssemblyId>false</appendAssemblyId>
67+
</configuration>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
</project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.learnk8s;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.SerializationFeature;
5+
import io.fabric8.kubernetes.api.model.*;
6+
import io.fabric8.kubernetes.client.*;
7+
import io.fabric8.kubernetes.client.Config;
8+
import io.fabric8.kubernetes.client.ConfigBuilder;
9+
import io.fabric8.kubernetes.client.internal.SerializationUtils;
10+
11+
12+
public class KubernetesClientCLI {
13+
14+
15+
private static final ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
16+
private static String namespace = null;
17+
18+
public static void main(String[] args) throws Exception {
19+
String environment = "production";
20+
Pod pod = createPod(environment);
21+
22+
if (args.length > 0 && args[0].equals("yaml")) {
23+
System.out.println(SerializationUtils.dumpAsYaml(pod));
24+
} else {
25+
System.out.println(mapper.writeValueAsString(pod));
26+
}
27+
28+
Config config = new ConfigBuilder().build();
29+
30+
try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
31+
if (namespace == null) {
32+
namespace = client.getNamespace();
33+
}
34+
35+
boolean dryRun = false;
36+
for (String arg : args) {
37+
if (arg.equals("--dry-run")) {
38+
dryRun = true;
39+
}
40+
}
41+
if (!dryRun) {
42+
client.pods().inNamespace(namespace).create(pod);
43+
System.out.println("Pod created!");
44+
}
45+
46+
47+
}
48+
}
49+
50+
public static Pod createPod(String environment){
51+
return new PodBuilder().withNewMetadata()
52+
.withName("test-pod")
53+
.endMetadata()
54+
.withNewSpec()
55+
.addNewContainer()
56+
.withName("test-container")
57+
.withImage("k8s.gcr.io/busybox")
58+
.withEnv(new EnvVarBuilder().withName("ENV").withValue(environment).build())
59+
.endContainer()
60+
.endSpec()
61+
.build();
62+
}
63+
64+
}

0 commit comments

Comments
 (0)