Skip to content

Commit 1b6cc78

Browse files
committed
add registerResource
1 parent 095dde7 commit 1b6cc78

3 files changed

Lines changed: 128 additions & 3 deletions

File tree

src/main/java/io/github/kubesys/client/KubernetesClient.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,115 @@ public JsonNode bindingResource(String pod, String namespace, String host) throw
10451045
return createResource(KubeUtil.toBinding(pod, namespace, host));
10461046
}
10471047

1048+
1049+
/**
1050+
* {
1051+
"kind": "CustomResourceDefinition",
1052+
"apiVersion": "apiextensions.k8s.io/v1",
1053+
"metadata": {
1054+
"name": "virtualmachines.doslab.io",
1055+
"uid": "eac76c1c-254d-4895-b815-381755805da7",
1056+
"resourceVersion": "10577917",
1057+
"generation": 1,
1058+
"creationTimestamp": "2023-10-21T08:23:57Z",
1059+
"labels": {
1060+
"kubevirt.io": ""
1061+
},
1062+
"annotations": {
1063+
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"apiextensions.k8s.io/v1\",\"kind\":\"CustomResourceDefinition\",\"metadata\":{\"annotations\":{},\"labels\":{\"kubevirt.io\":\"\"},\"name\":\"virtualmachines.doslab.io\"},\"spec\":{\"group\":\"doslab.io\",\"names\":{\"kind\":\"VirtualMachine\",\"plural\":\"virtualmachines\",\"shortNames\":[\"vm\",\"vms\"],\"singular\":\"virtualmachine\"},\"scope\":\"Namespaced\",\"versions\":[{\"additionalPrinterColumns\":[{\"jsonPath\":\".spec.domain.uuid.text\",\"name\":\"UUID\",\"type\":\"string\"},{\"jsonPath\":\".spec.domain._type\",\"name\":\"TYPE\",\"type\":\"string\"},{\"jsonPath\":\".spec.domain.vcpu._current\",\"name\":\"CPU\",\"type\":\"string\"},{\"jsonPath\":\".spec.domain.currentMemory.text\",\"name\":\"RAM(KB)\",\"type\":\"string\"},{\"jsonPath\":\".spec.powerstate\",\"name\":\"STATUS\",\"type\":\"string\"},{\"jsonPath\":\".metadata.creationTimestamp\",\"name\":\"AGE\",\"type\":\"date\"},{\"jsonPath\":\".spec.nodeName\",\"name\":\"NODE\",\"type\":\"string\"},{\"jsonPath\":\".spec.status.conditions.state.waiting.reason\",\"name\":\"MESSAGE\",\"type\":\"string\"},{\"jsonPath\":\".spec.image\",\"name\":\"IMAGE\",\"type\":\"string\"}],\"name\":\"v1\",\"schema\":{\"openAPIV3Schema\":{\"properties\":{\"spec\":{\"type\":\"object\",\"x-kubernetes-preserve-unknown-fields\":true}},\"type\":\"object\"}},\"served\":true,\"storage\":true}]}}\n"
1064+
},
1065+
"spec": {
1066+
"group": "doslab.io",
1067+
"names": {
1068+
"plural": "virtualmachines",
1069+
"singular": "virtualmachine",
1070+
"shortNames": ["vm", "vms"],
1071+
"kind": "VirtualMachine",
1072+
"listKind": "VirtualMachineList"
1073+
},
1074+
"scope": "Namespaced",
1075+
"versions": [{
1076+
"name": "v1",
1077+
"served": true,
1078+
"storage": true,
1079+
"schema": {
1080+
"openAPIV3Schema": {
1081+
"type": "object",
1082+
"properties": {
1083+
"spec": {
1084+
"type": "object",
1085+
"x-kubernetes-preserve-unknown-fields": true
1086+
}
1087+
}
1088+
}
1089+
},
1090+
"additionalPrinterColumns": [{
1091+
"name": "UUID",
1092+
"type": "string",
1093+
"jsonPath": ".spec.domain.uuid.text"
1094+
}, {
1095+
"name": "TYPE",
1096+
"type": "string",
1097+
"jsonPath": ".spec.domain._type"
1098+
}, {
1099+
"name": "CPU",
1100+
"type": "string",
1101+
"jsonPath": ".spec.domain.vcpu._current"
1102+
}, {
1103+
"name": "RAM(KB)",
1104+
"type": "string",
1105+
"jsonPath": ".spec.domain.currentMemory.text"
1106+
}, {
1107+
"name": "STATUS",
1108+
"type": "string",
1109+
"jsonPath": ".spec.powerstate"
1110+
}, {
1111+
"name": "AGE",
1112+
"type": "date",
1113+
"jsonPath": ".metadata.creationTimestamp"
1114+
}, {
1115+
"name": "NODE",
1116+
"type": "string",
1117+
"jsonPath": ".spec.nodeName"
1118+
}, {
1119+
"name": "MESSAGE",
1120+
"type": "string",
1121+
"jsonPath": ".spec.status.conditions.state.waiting.reason"
1122+
}, {
1123+
"name": "IMAGE",
1124+
"type": "string",
1125+
"jsonPath": ".spec.image"
1126+
}]
1127+
}],
1128+
"conversion": {
1129+
"strategy": "None"
1130+
}
1131+
},
1132+
"status": {
1133+
"conditions": null,
1134+
"acceptedNames": {
1135+
"plural": "",
1136+
"kind": ""
1137+
},
1138+
"storedVersions": ["v1"]
1139+
}
1140+
}
1141+
*
1142+
* @param crd 上面就是一个CRD例子
1143+
* @return /apis/doslab.io/v1
1144+
* @throws Exception
1145+
*/
1146+
public boolean registerResource(JsonNode crd) throws Exception {
1147+
if (crd.has("kind") && crd.get("kind").asText().equals("CustomResourceDefinition")) {
1148+
StringBuilder sb = new StringBuilder();
1149+
sb.append("/apis/").append(crd.get("spec").get("group").asText()).append("/")
1150+
.append(crd.get("spec").get("versions").get(0).get("name").asText());
1151+
getAnalyzer().getRegistry().registerKinds(this, sb.toString());
1152+
return true;
1153+
}
1154+
1155+
throw new KubernetesInternalServerErrorException("it is not a valid crd.");
1156+
}
10481157
/**********************************************************
10491158
*
10501159
*

src/main/java/io/github/kubesys/client/cores/KubernetesRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void registerKinds(KubernetesClient caller, String path) throws Exception
7878

7979
// we only support a version for each resources
8080
if (ruleBase.getNameMapping().containsKey(fullKind) ||
81-
(fullKind == shortKind && fullKind.endsWith("Options"))) {
81+
(fullKind.equals(shortKind) && fullKind.endsWith("Options"))) {
8282
continue;
8383
}
8484

src/test/java/io/github/kubesys/client/testcases/WatchKindTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public void doDeleted(JsonNode node) {
3636
@Override
3737
public void doAdded(JsonNode node) {
3838
System.out.println(node);
39+
try {
40+
client.registerResource(node);
41+
} catch (Exception e) {
42+
// TODO Auto-generated catch block
43+
e.printStackTrace();
44+
}
3945
}
4046

4147
@Override
@@ -44,9 +50,19 @@ public void doClose() {
4450
}
4551

4652
};
47-
client.watchResources("Pod", KubernetesConstants.VALUE_ALL_NAMESPACES, watcher);
53+
// client.watchResources("Pod", KubernetesConstants.VALUE_ALL_NAMESPACES, watcher);
4854

49-
// client.watchResources("apiextensions.k8s.io.CustomResourceDefinition", KubernetesConstants.VALUE_ALL_NAMESPACES, watcher);
55+
client.watchResources("apiextensions.k8s.io.CustomResourceDefinition", KubernetesConstants.VALUE_ALL_NAMESPACES, watcher);
56+
57+
// try {
58+
// client.listResources("doslab.io.VirtualMachine");
59+
// } catch (Exception ex) {
60+
// System.out.println("暂时不支持doslab.io.VirtualMachine");
61+
// }
62+
////
63+
// Thread.sleep(30000);
64+
////
65+
// System.out.println(client.listResources("doslab.io.VirtualMachine"));
5066
// or
5167
// client.watchResources("apps.Deployment", KubernetesConstants.VALUE_ALL_NAMESPACES, watcher);
5268

0 commit comments

Comments
 (0)