Skip to content

Commit aa28c43

Browse files
committed
add WorkloadWriter and COnfigMapWriter
1 parent 99e5e89 commit aa28c43

9 files changed

Lines changed: 498 additions & 11 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<properties>
1515
<jackson.version>2.15.2</jackson.version>
1616
<httpclient.version>5.2.1</httpclient.version>
17+
1718
<junit.version>5.9.3</junit.version>
1819
<maven.assembly.version>3.6.0</maven.assembly.version>
1920
<maven.dependency.version>3.6.0</maven.dependency.version>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (2023, ) Institute of Software, Chinese Academy of Sciences
3+
*/
4+
package io.github.kubesys.client.writers;
5+
6+
import com.fasterxml.jackson.databind.node.ObjectNode;
7+
8+
/**
9+
* @author wuheng@iscas.ac.cn
10+
* @since 2023/07/26
11+
* @version 1.0.2
12+
*
13+
*/
14+
public class ConfigMapWriter extends KindWriter {
15+
16+
static final String TEMPLATE = "apiVersion: v1\r\n"
17+
+ "kind: ConfigMap\r\n"
18+
+ "metadata:\r\n"
19+
+ " name: #NAME#\r\n"
20+
+ " namespace: #NAMESPACE#";
21+
22+
public ConfigMapWriter(String name, String namespace) throws Exception {
23+
super(name, namespace);
24+
}
25+
26+
public ConfigMapWriter withData(String key, String value) {
27+
ObjectNode data = getObjectValue("data");
28+
data.put(key, value);
29+
return this;
30+
}
31+
32+
@Override
33+
public String getTemplate() {
34+
return TEMPLATE;
35+
}
36+
37+
public static void main(String[] args) throws Exception {
38+
ConfigMapWriter writer = new ConfigMapWriter("kube-database", "kube-system");
39+
String value = "|-\r\n"
40+
+ " @test \"Test Health\" {\r\n"
41+
+ " url=\"http://grafana/api/health\"\r\n"
42+
+ "\r\n"
43+
+ " code=$(wget --server-response --spider --timeout 10 --tries 1 ${url} 2>&1 | awk '/^ HTTP/{print $2}')\r\n"
44+
+ " [ \"$code\" == \"200\" ]\r\n"
45+
+ " }";
46+
writer.withData("username", "onceas").withData("run.sh", value.replaceAll("\r\n", "")).stream(System.out);
47+
}
48+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright (2023, ) Institute of Software, Chinese Academy of Sciences
3+
*/
4+
package io.github.kubesys.client.writers;
5+
6+
import io.github.kubesys.client.writers.WorkloadWriter.Env.ValueFrom;
7+
import io.github.kubesys.client.writers.WorkloadWriter.Env.ValueFrom.SecretKeyRef;
8+
9+
/**
10+
* @author wuheng@iscas.ac.cn
11+
* @since 2023/08/01
12+
* @version 1.0.2
13+
*
14+
*/
15+
public class DeploymentWriter extends WorkloadWriter {
16+
17+
public DeploymentWriter(String name, String namespace) throws Exception {
18+
this(name, namespace, 1);
19+
}
20+
21+
public DeploymentWriter(String name, String namespace, int number) throws Exception {
22+
super(name, namespace, new String[] {
23+
"#APIVERSION#", "apps/v1",
24+
"#KIND#", "Deployment",
25+
"#NAME#", name,
26+
"#NAMESPACE#", namespace,
27+
"#NUMBER#", String.valueOf(number)});
28+
}
29+
30+
public static void main(String[] args) throws Exception {
31+
DeploymentWriter writer = new DeploymentWriter("kube-database", "kube-system");
32+
writer
33+
// .withMasterEnbale()
34+
.withContainer(
35+
new Container("postgres", "postgres:14.5-alpine",
36+
new Env[] {
37+
new Env("POSTGRES_PASSWORD", new ValueFrom(new SecretKeyRef("kube-database", "password")))
38+
},
39+
new Port[] {
40+
new Port(5432)
41+
},
42+
new VolumeMount[] {
43+
new VolumeMount("data", "/var/lib/postgresql")
44+
}))
45+
.withContainer(
46+
new Container("adminer", "adminer:4.8.1-standalone",
47+
null,
48+
new Port[] {
49+
new Port(8080)
50+
},
51+
null))
52+
.withVolume("data", "kube-database")
53+
.stream(System.out);
54+
}
55+
}

src/main/java/io/github/kubesys/client/writers/KindWriter.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.fasterxml.jackson.databind.JsonNode;
99
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.node.ArrayNode;
1011
import com.fasterxml.jackson.databind.node.ObjectNode;
1112
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
1213
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
@@ -21,9 +22,9 @@
2122
*/
2223
public abstract class KindWriter {
2324

24-
protected final KubernetesWriter writer;
25+
protected KubernetesWriter writer;
2526

26-
protected final ObjectNode json;
27+
protected ObjectNode json;
2728

2829
public KindWriter(String name) throws Exception {
2930
this.writer = new KubernetesWriter();
@@ -35,6 +36,11 @@ public KindWriter(String name, String namespace) throws Exception {
3536
this.json = toObjectNode(getTemplate(), new String[] {"#NAME#", name, "#NAMESPACE#", namespace});
3637
}
3738

39+
public KindWriter(String name, String namespace, String[] kvs) throws Exception {
40+
this.writer = new KubernetesWriter();
41+
this.json = toObjectNode(getTemplate(), kvs);
42+
}
43+
3844
public void stream(PrintStream ps) throws Exception {
3945
ps.println(new YAMLMapper().writeValueAsString(json));
4046
}
@@ -47,6 +53,30 @@ public void yaml(String path) {
4753
writer.writeAsYaml(path, json);
4854
}
4955

56+
public ObjectNode getObjectValue(String key) {
57+
if (!json.has(key)) {
58+
ObjectNode val = new ObjectMapper().createObjectNode();
59+
json.set(key, val);
60+
}
61+
return (ObjectNode) json.get(key);
62+
}
63+
64+
public ObjectNode getObjectValue(ObjectNode node, String key) {
65+
if (!node.has(key)) {
66+
ObjectNode val = new ObjectMapper().createObjectNode();
67+
node.set(key, val);
68+
}
69+
return (ObjectNode) node.get(key);
70+
}
71+
72+
public ArrayNode getArrayValue(ObjectNode node, String key) {
73+
if (!node.has(key)) {
74+
ArrayNode val = new ObjectMapper().createArrayNode();
75+
node.set(key, val);
76+
}
77+
return (ArrayNode) node.get(key);
78+
}
79+
5080
public ObjectNode toObjectNode(String str, String[] list) throws Exception {
5181
for (int i = 0; i < list.length; i = i + 2) {
5282
str = str.replaceAll(list[i], list[i + 1]);
@@ -60,5 +90,15 @@ public ObjectNode toObjectNode(String key, JsonNode json) throws Exception {
6090
return (ObjectNode) new ObjectMapper(new YAMLFactory()).readTree(val.toPrettyString());
6191
}
6292

93+
public ObjectNode toObjectNode(Object obj) throws Exception {
94+
return (ObjectNode) new ObjectMapper(new YAMLFactory()).readTree(
95+
new ObjectMapper().writeValueAsString(obj));
96+
}
97+
98+
public ObjectNode toObjectNode(String json) throws Exception {
99+
return (ObjectNode) new ObjectMapper(new YAMLFactory()).readTree(json);
100+
}
101+
63102
public abstract String getTemplate();
103+
64104
}

src/main/java/io/github/kubesys/client/writers/PVCWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public PVCWriter(String name, String namespace) throws Exception {
3333
public PVCWriter withCapacity(String gb) throws Exception {
3434
ObjectNode size = toObjectNode(CAPACITY, new String[] {"#SIZE#", gb});
3535
ObjectNode requests = toObjectNode("requests", size);
36-
ObjectNode spec = (ObjectNode) json.get("spec");
36+
ObjectNode spec = getObjectValue("spec");
3737
spec.set("resources", requests);
3838
return this;
3939
}

src/main/java/io/github/kubesys/client/writers/PVWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public PVWriter withCapacity(String gb) throws Exception {
4141
+ "namespace: #PV_NAMESPACE#";
4242

4343
public PVWriter withPVC(String name, String namespace) throws Exception {
44-
ObjectNode spec = (ObjectNode) json.get("spec");
44+
ObjectNode spec = getObjectValue("spec");
4545
spec.set("claimRef", toObjectNode(CLAIMREF, new String[] {"#PV_NAME#", name, "#PV_NAMESPACE#", namespace}));
4646
return this;
4747
}
4848

4949
static final String HOSTPATH = "path: #PATH#";
5050

5151
public PVWriter withPath(String hostpath) throws Exception {
52-
ObjectNode spec = (ObjectNode) json.get("spec");
52+
ObjectNode spec = getObjectValue("spec");
5353
spec.set("hostPath", toObjectNode(HOSTPATH, new String[] {"#PATH#", hostpath}));
5454
return this;
5555
}

src/main/java/io/github/kubesys/client/writers/SecretWriter.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44
package io.github.kubesys.client.writers;
55

6-
import com.fasterxml.jackson.databind.ObjectMapper;
76
import com.fasterxml.jackson.databind.node.ObjectNode;
87

98
/**
@@ -26,11 +25,7 @@ public SecretWriter(String name, String namespace) throws Exception {
2625
}
2726

2827
public SecretWriter withData(String key, String value) {
29-
if (!json.has("data")) {
30-
json.set("data", new ObjectMapper().createObjectNode());
31-
}
32-
33-
ObjectNode data = (ObjectNode) json.get("data");
28+
ObjectNode data = getObjectValue("data");
3429
data.put(key, value);
3530
return this;
3631
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright (2023, ) Institute of Software, Chinese Academy of Sciences
3+
*/
4+
package io.github.kubesys.client.writers;
5+
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.node.ArrayNode;
8+
import com.fasterxml.jackson.databind.node.ObjectNode;
9+
10+
/**
11+
* @author wuheng@iscas.ac.cn
12+
* @since 2023/07/26
13+
* @version 1.0.2
14+
*
15+
*/
16+
public class ServiceWriter extends KindWriter {
17+
18+
static final String TEMPLATE = "apiVersion: v1\r\n"
19+
+ "kind: Service\r\n"
20+
+ "metadata:\r\n"
21+
+ " name: #NAME#\r\n"
22+
+ " namespace: #NAMESPACE#\r\n"
23+
+ " labels: \r\n"
24+
+ " name: #NAME#\r\n";
25+
26+
public ServiceWriter(String name, String namespace) throws Exception {
27+
super(name, namespace);
28+
}
29+
30+
static final String TYPE = "type: #TYPE#";
31+
32+
public ServiceWriter withType(String type) throws Exception {
33+
ObjectNode spec = getObjectValue("spec");
34+
spec.set("type", toObjectNode(TYPE, new String[] {"#TYPE#", type}).get("type"));
35+
return this;
36+
}
37+
38+
static final String SELECTOR = "name: #NAME#";
39+
40+
public ServiceWriter withSelector(String name) throws Exception {
41+
ObjectNode spec = getObjectValue("spec");
42+
spec.set("selector", toObjectNode(SELECTOR, new String[] {"#NAME#", name}));
43+
return this;
44+
}
45+
46+
static final String PORTS = "name: #NAME#";
47+
48+
public ServiceWriter withPort(int port, int targetPort, int nodePort, String protocol, String name) throws Exception {
49+
ArrayNode ports = getArrayValue(getObjectValue("spec"), "ports");
50+
51+
ObjectNode portNode = new ObjectMapper().createObjectNode();
52+
portNode.put("port", port);
53+
portNode.put("targetPort", targetPort);
54+
portNode.put("nodePort", nodePort);
55+
portNode.put("protocol", protocol);
56+
portNode.put("name", name);
57+
58+
ports.add(portNode);
59+
return this;
60+
}
61+
62+
@Override
63+
public String getTemplate() {
64+
return TEMPLATE;
65+
}
66+
67+
public static void main(String[] args) throws Exception {
68+
ServiceWriter writer = new ServiceWriter("kube-database", "kube-system");
69+
writer.withType("NodePort").withSelector("kube-database")
70+
.withPort(5432, 5432, 30306, "TCP", "postgre-port")
71+
.withPort(8080, 8080, 30307, "TCP", "adminer-port").stream(System.out);
72+
}
73+
}

0 commit comments

Comments
 (0)