【总结】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();
  }

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

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

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;

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

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

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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值