本文主要是介绍【总结】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
这篇关于【总结】kubernates crd client-java 关于自定义资源的增删改查的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!