我想为kubernetes节点添加扩展资源,我可以通过以下所示的curl命令来做到这一点: https://kubernetes.io/docs/tasks/administer-cluster/extended-resource-node/,即:
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
然后,我可以创建一个需要example.com/dongle资源的pod。
但是,如何使用fabric8 Java API做到这一点?
我在与所有节点相关的API的Java演示应用程序中尝试了此操作,但是它不起作用。片段代码如下:
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());
// ...
}
}
在一开始,我没有添加client.nodes()。create *子句,但是它没有任何效果。我意识到可能需要回写设置。但是,即使我添加了它,也不会生效。
“ n1状态”的日志:
capacity={cpu=Quantity(amount=4, format=null,additionalProperties={}), ..., pods=Quantity(amount=110, format=null, additionalProperties={}), example.com/dongle=Quantity(amount=2, format=null, additionalProperties={})},
“获取节点状态”的日志:
capacity={cpu=Quantity(amount=4, format=null, additionalProperties={}), ..., pods=Quantity(amount=110, format=null, additionalProperties={})},
而且,当我在终端中运行命令时,它什么也没有响应:
kubectl describe node k8s-n1 | grep dongle
create(n1)提示以下错误:
io.fabric8.kubernetes.client.KubernetesClientException:执行失败:POST位于http://192.168.1.45:8080/api/v1/nodes。消息:不应在要创建的对象上设置resourceVersion。收到的状态:不应在要创建的对象上设置Status(apiVersion = v1,code = 500,Details = null,kind = Status,message = resourceVersion,meta = ListMeta(_continue = null,resourceVersion = null,selfLink = null,另外,PropertyProperties = {}),原因= null,状态= Failure,additionalProperties = {})。
如何使其工作?
答案 0 :(得分:0)
我尝试了以下更多的fabric8 API,但它们都不起作用:
// client.nodes().withName(podNameWithExtRes).get().setStatus(ndStatus); // not working
// client.nodes().withName(podNameWithExtRes).patch(n1); // not working
// client.nodes().withName(podNameWithExtRes).patch(n1).setStatus(ndStatus); // not working
// client.nodes().withName(podNameWithExtRes).edit().withStatus(ndStatus).done(); // not working
// client.nodes().withName(podNameWithExtRes).edit().withStatus(ndStatus).buildStatus(); // not working
// client.nodes().withName(podNameWithExtRes).replace(n1); // not working
最后,我通过一种变通办法使其工作:
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
private static void patchRequest(String request) throws IOException {
try {
String urlStr = "http://192.168.1.45:8080/api/v1/nodes/k8s-n1/status";
String requestContents = "[{\"op\": \"" + request + "\", \"path\": \"/status/capacity/example.com~1dongle\", \"value\": \"2\"}]";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPatch newPatch = new HttpPatch(urlStr);
newPatch.setEntity(new StringEntity(requestContents, ContentType.parse("application/json-patch+json")));
HttpResponse response = httpclient.execute(newPatch);
logger.info(response.toString());
String resultBody = EntityUtils.toString(response.getEntity());
EntityUtils.consume(response.getEntity());
logger.info("Response Code : " + response.getStatusLine().getStatusCode());
logger.info(resultBody);
httpclient.close();
} catch (IOException e) {
logger.error("patchRequest exception:", e);
throw e;
}
}
public static void main(String[] args) {
// ...
try {
logger.info("add the extended resource");
patchRequest("add");
// ...
log("get node status: \n", client.nodes().withName(podNameWithExtRes).get().getStatus().toString());
// ...
logger.info("remove the extended resource");
patchRequest("remove");
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
// ...
}
现在,“获取节点状态”的日志:
capacity={cpu=Quantity(amount=4, format=null, additionalProperties={}), example.com/dongle=Quantity(amount=1, format=null, additionalProperties={}), ... pods=Quantity(amount=110, format=null, additionalProperties={})},