• 【总结】kubernates crd client-java 关于自定义资源的增删改查


    Java model 准备

    首先使用 crd.yml 和 kubernetes CRD 自动生成 Java model 类,这是一切的前提,之前在这个地方也卡了很久。如何生成在另外一个文章中已经有所记录。
    使用 crd.yml 和 kubernetes CRD 自动生成 Java model 类

    CustomObjectsApi 文档学习

    官网 kubernetes-client/java 的 CustomObjectsApi 介绍使用
    在这个里面我们能找到所有关于自定义资源的增删改查的 api 的操作,还有示范的例子,可以根据自己的需求进行相关的改造使用。

    示例

    1、查看所有的自定义资源列表

    public List<V1alpha1xxxx> getxxxList() {
        log.info("getxxxxList start to work");
        CustomObjectsApi apiInstance = kubernetesService.getCustomObjectsApi();
        Object result = null;
        try {
          result =
              apiInstance.listNamespacedCustomObject(
                  group, version, namespace, plural, null, null, null, null, null, null, null, null,
                  null, null);
    
        } catch (ApiException e) {
          log.info(
              "listNamespacedCustomObject api call failed with status code "
                  + e.getCode()
                  + ": "
                  + e.getResponseBody());
          e.printStackTrace();
        }
    
        ObjectMapper objectMapper = kubernetesApplicationService.getObjectMapper();
    
        V1alpha1xxxList v1alpha1xxxList =
            objectMapper.convertValue(result, V1alpha1xxxList.class);
    
        return v1alpha1xxxList.getItems();
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    2、得到指定的某一个自定义资源

    public V1alpha1xxx getxxx(String name) {
      log.info("getxxx start to work");
      //        "name": "admin-1akyr08e9wwt"
      Object result =
          kubernetesApplicationService.getCustomObjectsApi(group, version, namespace, plural, name);
      ObjectMapper objectMapper = kubernetesApplicationService.getObjectMapper();
      V1alpha1xxx v1alpha1xxx = objectMapper.convertValue(result, V1alpha1xxx.class);
    
      return v1alpha1xxx;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    CustomObjectsApi 去获得指定的某一个自定义资源

    public Object getCustomObjectsApi(
          String group, String version, String namespace, String plural, String name) {
        CustomObjectsApi apiInstance = kubernetesService.getCustomObjectsApi();
        Object result = null;
        try {
          result = apiInstance.getNamespacedCustomObject(group, version, namespace, plural, name);
    
        } catch (ApiException e) {
          if (e.getCode() == 404) {
            log.info("custom object {} does not exist", name);
          } else {
            log.info(
                "getNamespacedCustomObject api call failed with status code "
                    + e.getCode()
                    + ": "
                    + e.getResponseBody());
            e.printStackTrace();
          }
        }
    
        return result;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3、创建一个自定义资源

    public V1alpha1xxx createxxx(V1alpha1xxx v1alpha1xxxb) {
        log.info("createxxx start to work");
        Object customObject =
            kubernetesApplicationService.createNamespacedCustomObject(
                group,
                version,
                namespace,
                plural,
                v1alpha1xxx.getMetadata().getName(),
                v1alpha1xxx);
      
        return v1alpha1xxx;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    CustomObjectsApi去创建一个自定义的资源

    public Object createNamespacedCustomObject(
          String group,
          String version,
          String namespace,
          String plural,
          String name,
          KubernetesObject kubernetesObject) {
        CustomObjectsApi apiInstance = kubernetesService.getCustomObjectsApi();
        Object customObject = null;
        try {
          customObject =
              apiInstance.createNamespacedCustomObject(
                  group, version, namespace, plural, kubernetesObject, null, null, null);
          log.info("createNamespacedCustomObject {}", name);
        } catch (ApiException e) {
          log.info(
              "createNamespacedCustomObject api call failed with status code "
                  + e.getCode()
                  + ": "
                  + e.getResponseBody());
          e.printStackTrace();
        }
    
        return customObject;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    4、删除自定义资源

     public void deleteBayesJob(String name) {
        log.info("deletexxx {} start to work", name);
    
        V1alpha1xxx xxx = getxxx(name);
        if (xxx == null) {
          log.error("can not get xxx {}", name);
          return;
        }
    
        kubernetesApplicationService.deleteNamespacedCustomObject(
            group, version, namespace, plural, name);
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    CustomObjectsApi删除指定的自定义资源

    public void deleteNamespacedCustomObject(
          String group, String version, String namespace, String plural, String name) {
        V1DeleteOptions body = new V1DeleteOptions();
        CustomObjectsApi apiInstance = kubernetesService.getCustomObjectsApi();
    
        try {
          Object result =
              apiInstance.deleteNamespacedCustomObject(
                  group, version, namespace, plural, name, null, null, null, null, body);
    
          log.info("delete namespaced customObject {}", name);
    
        } catch (ApiException e) {
          log.info(
              "deleteNamespacedCustomObject api call failed with status code "
                  + e.getCode()
                  + ": "
                  + e.getResponseBody());
          e.printStackTrace();
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    5、修改自定义资源

    public void killxxx(String name) {
      log.info("killxxx start to work");
      CustomObjectsApi apiInstance = kubernetesService.getCustomObjectsApi();
    
      V1alpha1xxx bxxx = getBaxxx(name);
      if (bxxx== null) {
        log.error("can not get bxxx {}", name);
        throw new InvalidRequestException("invalid name " + name);
      }
    
      Map<String, String> annotations = bxxx.getMetadata().getAnnotations();
      if (annotations == null) {
        bxxx
            .getMetadata()
            .setAnnotations(
                new HashMap<>() {
                  {
                    put("xxxxx/terminate", "true");
                  }
                });
    
      } else {
        bxxx.getMetadata().getAnnotations().put("xxxx/terminate", "true");
      }
    
      ObjectMapper objectMapper = kubernetesApplicationService.getObjectMapper();
    
      String bxxxAsString = null;
      try {
        bxxxAsString = objectMapper.writeValueAsString(bxxx);
      } catch (JsonProcessingException e) {
        log.error("killxxx writeValueAsString error {}", e.getMessage());
        e.printStackTrace();
        throw new RuntimeException(e);
      }
      Gson gson = new Gson();
      JsonObject jsonObject = gson.fromJson(bayesJobAsString, JsonObject.class);
    
      kubernetesApplicationService.replaceNamespacedCustomObject(
          group, version, namespace, plural, jsonObject, name);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    CustomObjectsApi replace某一个自定义资源

    public void replaceNamespacedCustomObject(
          String group,
          String version,
          String namespace,
          String plural,
          JsonObject jsonObject,
          String name) {
        log.info("replaceNamespacedCustomObject name {} ,jsonObject {}", name, jsonObject);
        CustomObjectsApi apiInstance = kubernetesService.getCustomObjectsApi();
    
        try {
          Object result =
              apiInstance.replaceNamespacedCustomObject(
                  group, version, namespace, plural, name, jsonObject, null, null);
          log.info("replaceNamespacedCustomObject name {} result {}", name, result);
    
        } catch (ApiException e) {
          log.error("Exception when calling CustomObjectsApi#replaceNamespacedCustomObject");
          log.error("Status code: {} , Reason: {}", e.getCode(), e.getResponseBody());
          e.printStackTrace();
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    这个是先查找自定义资源A是否存在,如果存在的话,修改自定义资源A的某些属性,然后将其A进行序列化。之后 gson.fromJson 然后再进行自定义资源A的replace

  • 相关阅读:
    Ubuntu 释放nvidia显存
    Hadoop相关
    express学习3-捕获错误
    QT程序打包
    第五章 路由技术及应用
    本科生学深度学习,搭建环境,再不入坑就晚了
    Golang Array 数组使用注意事项和细节
    LSTM基础理论与实例
    Java如何使用SAX(Simple API for XML)解析XML呢?
    放大器OPA855的噪声计算实例
  • 原文地址:https://blog.csdn.net/qq_43430343/article/details/133701487