繁体   English   中英

Kubernetes java api 客户端更改吊舱标签

[英]Kubernetes java api client change pods labels

我想更改(编辑/添加)具有特定标签的 pod 上的标签。 我有以下代码:

public void changeLabelsForPodFilterByLabels(String namespace, Map<String, String> filterByLabels, Map<String, String> newLabels) {
    try {
        V1PodList podList = coreV1Api.listNamespacedPod(namespace, null, null, null, null, null, null, null, false);
        List<V1Pod> podsFilteredByLabel = filterPodsByLabels(podList.getItems(), filterByLabels);
        List<V1Pod> labelModifiedPods = addLabels(podsFilteredByLabel, newLabels);
        List<V1Pod> updatedPods = updatePods(namespace, labelModifiedPods);
    } catch (ApiException e) {
        logger.error("API Exception", e);
        logger.error(e.getMessage());
        logger.error(e.getResponseBody());
    }
}


private List<V1Pod> filterPodsByLabels(List<V1Pod> pods, Map<String, String> labels) {
    List<V1Pod> filteredPods = new ArrayList<>();
    logger.info("Input labels: " + labels.toString());
    for(V1Pod pod: pods) {
        logger.info("Pod: " + pod.getMetadata().getName());
        Map<String, String> podLabels = pod.getMetadata().getLabels();
        logger.info("Labels:" + podLabels.toString());
        if (podLabels.entrySet().containsAll(labels.entrySet())) {
            filteredPods.add(pod);
        }
    }
    return filteredPods;
}


private List<V1Pod> addLabels(List<V1Pod> pods, Map<String, String> newLabels) {
    List<V1Pod> updatedPods = new ArrayList<>();
    for (V1Pod pod: pods) {
        pod.getMetadata().setLabels(newLabels);
        updatedPods.add(pod);
    }
    return updatedPods;
}


private List<V1Pod> updatePods(String namespace, List<V1Pod> updatedPods) throws ApiException {

    for (V1Pod pod: updatedPods){
        V1Patch patch = new V1Patch();
        coreV1Api.patchNamespacedPod(pod.getMetadata().getName(), namespace, null, null, null, null, false);
    }
    return null;
}

基于herethis threadthis thread的示例,我了解应该将主体作为参数提供给coreV1Api.patchNamespacedPod方法。

有人可以提供一个关于身体应该是什么样子的例子吗? 此外,在上面链接的示例中,我看到存在操作的概念(由 op 字段定义)。 有哪些可用的操作(链接到文档会有所帮助)?

依赖项:

<dependency>
    <groupId>io.kubernetes</groupId>
    <artifactId>client-java</artifactId>
    <version>6.0.1</version>
</dependency>

稍后编辑

我已将库版本更改为 8.0.2,现在我可以将代码更改如下:

private List<V1Pod> updatePods(String namespace, List<V1Pod> updatedPods) throws ApiException {

    for (V1Pod pod: updatedPods){
        String body = generateBody("demo", "myvalue");
        V1Patch patch = new V1Patch(body);
        coreV1Api.patchNamespacedPod(pod.getMetadata().getName(), namespace, patch, null, null, null, false);
    }
    return null;
}

public String generateBody(String labelKey, String newLabelValue){
    return String.format("[{\"op\": \"add\", \"path\": \"/metadata/labels/%s\", \"value\": \"%s\"}]", labelKey, newLabelValue);
}

上述问题仍然可用,因为现在我收到有关身体格式/形式错误的错误。

io.kubernetes.client.openapi.ApiException:无法处理的实体

您可以使用JsonObject而不是 String 作为body参数。
例如:

JsonObject item = new JsonObject();
item.add("op", new JsonPrimitive("add"));
item.add("path", new JsonPrimitive("/metadata/labels"));

JsonObject label = new JsonObject();
label.addProperty("demo", "myvalue");
item.add("value", label);
JsonArray body = new JsonArray();
body.add(item);

coreV1Api.patchNamespacedPod(pod.getMetadata().getName(), namespace, body, null, null, null, false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM