Skip to content

Commit 99e5e89

Browse files
committed
add SecrectWriter, PVWriter and PVCWriter
1 parent 28b8ebc commit 99e5e89

4 files changed

Lines changed: 227 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright (2023, ) Institute of Software, Chinese Academy of Sciences
3+
*/
4+
package io.github.kubesys.client.writers;
5+
6+
import java.io.PrintStream;
7+
8+
import com.fasterxml.jackson.databind.JsonNode;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.node.ObjectNode;
11+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
12+
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
13+
14+
import io.github.kubesys.client.addons.KubernetesWriter;
15+
16+
/**
17+
* @author wuheng@iscas.ac.cn
18+
* @since 2023/07/26
19+
* @version 1.0.2
20+
*
21+
*/
22+
public abstract class KindWriter {
23+
24+
protected final KubernetesWriter writer;
25+
26+
protected final ObjectNode json;
27+
28+
public KindWriter(String name) throws Exception {
29+
this.writer = new KubernetesWriter();
30+
this.json = toObjectNode(getTemplate(), new String[] {"#NAME#", name});
31+
}
32+
33+
public KindWriter(String name, String namespace) throws Exception {
34+
this.writer = new KubernetesWriter();
35+
this.json = toObjectNode(getTemplate(), new String[] {"#NAME#", name, "#NAMESPACE#", namespace});
36+
}
37+
38+
public void stream(PrintStream ps) throws Exception {
39+
ps.println(new YAMLMapper().writeValueAsString(json));
40+
}
41+
42+
public void json(String path) {
43+
writer.writeAsJson(path, json);
44+
}
45+
46+
public void yaml(String path) {
47+
writer.writeAsYaml(path, json);
48+
}
49+
50+
public ObjectNode toObjectNode(String str, String[] list) throws Exception {
51+
for (int i = 0; i < list.length; i = i + 2) {
52+
str = str.replaceAll(list[i], list[i + 1]);
53+
}
54+
return (ObjectNode) new ObjectMapper(new YAMLFactory()).readTree(str);
55+
}
56+
57+
public ObjectNode toObjectNode(String key, JsonNode json) throws Exception {
58+
ObjectNode val = new ObjectMapper().createObjectNode();
59+
val.set(key, json);
60+
return (ObjectNode) new ObjectMapper(new YAMLFactory()).readTree(val.toPrettyString());
61+
}
62+
63+
public abstract String getTemplate();
64+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 PVCWriter extends KindWriter {
15+
16+
static final String TEMPLATE = "apiVersion: v1\r\n"
17+
+ "kind: PersistentVolumeClaim\r\n"
18+
+ "metadata:\r\n"
19+
+ " name: #NAME#\r\n"
20+
+ " namespace: #NAMESPACE#\r\n"
21+
+ " labels:\r\n"
22+
+ " name: \"#NAME#\"\r\n"
23+
+ "spec:\r\n"
24+
+ " accessModes:\r\n"
25+
+ " - ReadWriteOnce";
26+
27+
public PVCWriter(String name, String namespace) throws Exception {
28+
super(name, namespace);
29+
}
30+
31+
static final String CAPACITY = "storage: #SIZE#Gi";
32+
33+
public PVCWriter withCapacity(String gb) throws Exception {
34+
ObjectNode size = toObjectNode(CAPACITY, new String[] {"#SIZE#", gb});
35+
ObjectNode requests = toObjectNode("requests", size);
36+
ObjectNode spec = (ObjectNode) json.get("spec");
37+
spec.set("resources", requests);
38+
return this;
39+
}
40+
41+
@Override
42+
public String getTemplate() {
43+
return TEMPLATE;
44+
}
45+
46+
public static void main(String[] args) throws Exception {
47+
PVCWriter writer = new PVCWriter("kube-database", "kube-system");
48+
writer.withCapacity("2").stream(System.out);
49+
}
50+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 PVWriter extends KindWriter {
15+
16+
static final String TEMPLATE = "apiVersion: v1\r\n"
17+
+ "kind: PersistentVolume\r\n"
18+
+ "metadata:\r\n"
19+
+ " name: #NAME#\r\n"
20+
+ "spec:\r\n"
21+
+ " accessModes:\r\n"
22+
+ " - ReadWriteOnce\r\n"
23+
+ " persistentVolumeReclaimPolicy: Retain\r\n"
24+
+ " volumeMode: Filesystem";
25+
26+
public PVWriter(String name) throws Exception {
27+
super(name);
28+
}
29+
30+
static final String CAPACITY = "storage: #SIZE#Gi";
31+
32+
public PVWriter withCapacity(String gb) throws Exception {
33+
ObjectNode spec = (ObjectNode) json.get("spec");
34+
spec.set("capacity", toObjectNode(CAPACITY, new String[] {"#SIZE#", gb}));
35+
return this;
36+
}
37+
38+
static final String CLAIMREF = "apiVersion: v1\r\n"
39+
+ "kind: PersistentVolumeClaim\r\n"
40+
+ "name: #PV_NAME#\r\n"
41+
+ "namespace: #PV_NAMESPACE#";
42+
43+
public PVWriter withPVC(String name, String namespace) throws Exception {
44+
ObjectNode spec = (ObjectNode) json.get("spec");
45+
spec.set("claimRef", toObjectNode(CLAIMREF, new String[] {"#PV_NAME#", name, "#PV_NAMESPACE#", namespace}));
46+
return this;
47+
}
48+
49+
static final String HOSTPATH = "path: #PATH#";
50+
51+
public PVWriter withPath(String hostpath) throws Exception {
52+
ObjectNode spec = (ObjectNode) json.get("spec");
53+
spec.set("hostPath", toObjectNode(HOSTPATH, new String[] {"#PATH#", hostpath}));
54+
return this;
55+
}
56+
57+
@Override
58+
public String getTemplate() {
59+
return TEMPLATE;
60+
}
61+
62+
public static void main(String[] args) throws Exception {
63+
PVWriter writer = new PVWriter("kube-database");
64+
writer.withCapacity("2").withPVC("kube-database", "kube-system").withPath("/var/lib/doslab/kube/postgres").stream(System.out);
65+
}
66+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.ObjectNode;
8+
9+
/**
10+
* @author wuheng@iscas.ac.cn
11+
* @since 2023/07/26
12+
* @version 1.0.2
13+
*
14+
*/
15+
public class SecretWriter extends KindWriter {
16+
17+
static final String TEMPLATE = "apiVersion: v1\r\n"
18+
+ "kind: Secret\r\n"
19+
+ "metadata:\r\n"
20+
+ " name: #NAME#\r\n"
21+
+ " namespace: #NAMESPACE#\r\n"
22+
+ "type: Opaque";
23+
24+
public SecretWriter(String name, String namespace) throws Exception {
25+
super(name, namespace);
26+
}
27+
28+
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");
34+
data.put(key, value);
35+
return this;
36+
}
37+
38+
@Override
39+
public String getTemplate() {
40+
return TEMPLATE;
41+
}
42+
43+
public static void main(String[] args) throws Exception {
44+
SecretWriter writer = new SecretWriter("kube-database", "kube-system");
45+
writer.withData("username", "onceas").withData("password", "onceas").stream(System.out);
46+
}
47+
}

0 commit comments

Comments
 (0)