当前位置: 首页 > 知识库问答 >
问题:

如何使用 fabric8 API 为节点通告扩展资源

孙泳
2023-03-14

我想为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()。创建*子句,但它没有生效。我意识到它可能需要写回设置。然而,即使我添加了它,它也没有生效。

  1. createOrReplace()运行没有错误,但它不会将效果保存到节点。

“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不应在要创建的对象上设置,metadata=ListMeta(_continue=null、resourceVersion=null、selfLink=null和additionalProperties={}),reason=null,status=Failure,additionalProperties={})。

如何让它发挥作用?

共有1个答案

公西俊才
2023-03-14

我已经尝试了更多的 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={})},
 类似资料:
  • 说明 本部分说明如何在现有的机器中添加一个新的计算节点。添加节点之前,OpenShift 共有四个节点,1 个 master,1 个 infra,2 个 nodes,如下命令所示: # oc get nodes NAME STATUS ROLES AGE VERSION infra.example.com Ready infr

  • 我正在尝试使用WSL在Windows中调试vscode扩展。似乎prelaunchtask正在使用cmd。exe参数,这会导致预启动任务在bash中失败。 执行任务:npm run watch /bin/bash: /d:没有这样的文件或目录终端进程终止与退出代码: 127 终端将被任务重复使用,按任意键关闭它。 有没有想过如何强制调试终端正确发出bash参数?

  • 有没有一种简单的方法可以让用javascript编写的XSLT 1.0中的扩展函数返回一个节点集? 我可以为此创建一个新的java类,但我宁愿在脚本本身中放置一些代码。 当这可以在所有或大多数XSLT处理器(VB脚本?Groovy?C#?)支持的另一种脚本语言中完成时,当然也可以。 我有以下简单脚本: 它返回一个字符串,因此在Xpath表达式中调用函数没有问题<我想要的是一个节点集结果。但是当我把

  • 注意:TPR已经停止维护,kubernetes 1.7及以上版本请使用CRD。 自定义资源是对Kubernetes API的扩展,kubernetes中的每个资源都是一个API对象的集合,例如我们在YAML文件里定义的那些spec都是对kubernetes中的资源对象的定义,所有的自定义资源可以跟kubernetes中内建的资源一样使用kubectl操作。 自定义资源 Kubernetes1.6版

  • 一、背景 Hadoop的MapReduce中多文件输出默认是TextOutFormat,输出为part-r- 00000和part-r-00001依次递增的文件名。hadoop提供了 MultipleOutputFormat类,重写该类可实现定制自定义的文件名。 二、技术细节 1.环境:hadoop 0.19(目前hadoop 0.20.2对MultipleOutputFormat支持不好),li

  • 我使用的是bootstrap treeview,它大部分工作正常,但是当我从菜单链接加载一个新页面时,我希望所选子节点的所有父节点都展开,有人知道怎么做吗? 谢谢 抢劫 这里有一些代码... 这是来自我的数据库的数组,可以很好地显示所有treeview项。。。 如果您查看数组中的ID127,您将注意到子级的“扩展”为true “状态:{“选中”:false,“禁用”:false,“扩展”:true