I want to add extended resources for kubernetes nodes, and I can do this by curl command indicated in: https://kubernetes.io/docs/tasks/administer-cluster/extended-resource-node/, that is:
curl --header "Content-Type: application/json-patch+json" \
--request PATCH \
--data '[{"op": "add", "path": "/status/capacity/example.com~1dongle",
"value": "4"}]' \
http://localhost:8001/api/v1/nodes/<your-node-name>/status
and then, I can create a pod requiring example.com/dongle resource.
but, how to do this using fabric8 java API?
I tried it in my java demo application with all nodes related API, but it doesn't work. The snippet code is as following:
String ns = "thisisatest";
String master = "http://192.168.1.45:8080/";
Config config = new ConfigBuilder().withMasterUrl(master).build();
try (KubernetesClient client = new DefaultKubernetesClient(config)) {
try {
if(client.namespaces().withName(ns).get() == null) {
log("Create namespace:", client.namespaces().create(new NamespaceBuilder().withNewMetadata().withName(ns).endMetadata().build()));
}
String podNameWithExtRes = "k8s-n1";
/*step 1: patch extended resource*/
NodeStatus ndStatus = client.nodes().withName(podNameWithExtRes).get().getStatus();
Map<String, Quantity> ndCap = ndStatus.getCapacity();
ndCap.put("example.com/dongle", new Quantity("2"));
ndStatus.setCapacity(ndCap);
log("status info: \n", ndStatus.toString());
// ndStatus.setAllocatable(mapSrc);
Node n1 = client.nodes().withName(podNameWithExtRes).get();
n1.setStatus(ndStatus);
// client.nodes().withName(podNameWithExtRes).delete(); // it can be deleted successfully
// client.nodes().create(n1); // error
client.nodes().createOrReplace(n1);
log("n1 status: \n", n1.getStatus().toString());
log("get node status: \n", client.nodes().withName(podNameWithExtRes).get().getStatus().toString());
// ...
}
}
At the beginning, I didn't add client.nodes().create* clause, but it didn't take any effect. I realize it may need to write back the settings. However, even I add it, it doesn't take effect either.
- createOrReplace() runs without error, but it doesn't save the effect to the node.
logs of "n1 status":
capacity={cpu=Quantity(amount=4, format=null,additionalProperties={}), ..., pods=Quantity(amount=110, format=null, additionalProperties={}), example.com/dongle=Quantity(amount=2, format=null, additionalProperties={})},
logs of "get node status":
capacity={cpu=Quantity(amount=4, format=null, additionalProperties={}), ..., pods=Quantity(amount=110, format=null, additionalProperties={})},
And, it responses nothing when I run the command in the terminal:
kubectl describe node k8s-n1 | grep dongle
create(n1) prompts the following error:
io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: POST at: http://192.168.1.45:8080/api/v1/nodes. Message: resourceVersion should not be set on objects to be created. Received status: Status(apiVersion=v1, code=500, details=null, kind=Status, message=resourceVersion should not be set on objects to be created, metadata=ListMeta(_continue=null, resourceVersion=null, selfLink=null, additionalProperties={}), reason=null, status=Failure, additionalProperties={}).
How to make it work?