k8s java-client创建namespace
方式一:直接传参数V1Namespace,参数有点麻烦@PostMapping("/createNamespace")public ResultUtil createNamespaces(@RequestBody V1Namespace body) {CoreV1Api apiInstance = new CoreV1Api(k8sInit.getConnection());String pret
·
方式一:
直接传参数V1Namespace,参数有点麻烦
@PostMapping("/createNamespace")
public ResultUtil createNamespaces(@RequestBody V1Namespace body) {
CoreV1Api apiInstance = new CoreV1Api(k8sInit.getConnection());
String pretty = "true";
V1Namespace result = new V1Namespace();
try {
result = apiInstance.createNamespace(body, pretty, null, null);
} catch (ApiException e) {
if(e.getCode() == 409) {
return ResultUtil.error("409", "Error: Namespace already exists!");
}
if(e.getCode() == 201) {
return ResultUtil.error("201", "Error: Namespace already exists!");
}
if(e.getCode() == 401) {
return ResultUtil.error("401", "Error: No permission!");
}
}
return ResultUtil.success(result);
}
postman
POST http://localhost:8080/api/v1/namespace/createNamespace
{
"apiVersion":"v1",
"kind":"Namespace",
"metadata":{
"name":"test-namespace",
"labels":{
"name":"test-namespace"
}
}
}
kuboard上查看结果
方式二
发现了new V1NamespaceBuilder()创建的方式,发现了新大陆一样,只需要传一个namespace
/**
* 创建namespace
* https://github.com/zq2599/blog_demos/blob/master/kubernetesclient/openapi/src/main/java/com/bolingcavalry/openapi/OpenAPIDemoApplication.java
* @param namespace
* @return
*/
@GetMapping("/createNamespace2/{namespace}")
public ResultUtil createNamespaces2(@PathVariable String namespace) {
CoreV1Api apiInstance = new CoreV1Api(k8sInit.getConnection());
V1Namespace v1Namespace = new V1NamespaceBuilder()
.withNewMetadata()
.withName(namespace)
.endMetadata()
.build();
V1Namespace ns;
try {
ns = apiInstance.createNamespace(v1Namespace, null, null, null);
} catch (ApiException e) {
e.printStackTrace();
return ResultUtil.error(String.valueOf(e.getCode()), e.getMessage());
}
return ResultUtil.success(ns);
}
postman
GET http://localhost:8080/api/v1/namespace/createNamespace2/test-namespace2
参考
- Kubernetes官方java客户端之六:OpenAPI基本操作
- 这是个大佬的博客,github上有开源的k8s api的源码,但内容不多,还有一些其他的k8s教程等等
- 程序员欣宸文章汇总(kubernetes篇)
- github上的k8s相关源码
- 一些接口的参数解释
更多推荐
已为社区贡献6条内容
所有评论(0)