简体   繁体   English

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

[英]Kubernetes java api client change pods labels

I want to change (edit/add) labels on pods with specific labels.我想更改(编辑/添加)具有特定标签的 pod 上的标签。 I have the following code:我有以下代码:

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;
}

Based on the example from here and from this thread and this thread I understand that a body should be provided as parameter to coreV1Api.patchNamespacedPod method.基于herethis threadthis thread的示例,我了解应该将主体作为参数提供给coreV1Api.patchNamespacedPod方法。

Can someone provide an example on how a body should look like?有人可以提供一个关于身体应该是什么样子的例子吗? Further, in the examples linked above I see that there is the notion of operation (defined by op field).此外,在上面链接的示例中,我看到存在操作的概念(由 op 字段定义)。 What are the available operations (link to the docs would help)?有哪些可用的操作(链接到文档会有所帮助)?

Dependencies:依赖项:

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

Later edit稍后编辑

I've changed the library version to 8.0.2 and now I was able to change the code as follows:我已将库版本更改为 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);
}

The above questions are still available because now I receive errors regarding the bad format/form of the body.上述问题仍然可用,因为现在我收到有关身体格式/形式错误的错误。

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

You could use JsonObject instead of String for body argument.您可以使用JsonObject而不是 String 作为body参数。
For example:例如:

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