listNamespace
    @GetMapping("/listNamespace")
    public ResultUtil listNamespace() throws ApiException {
        CoreV1Api apiInstance = new CoreV1Api(k8sInit.getConnection());
        String pretty = "true";
        V1NamespaceList namespaceList;
        try {
            namespaceList = apiInstance.listNamespace(pretty, true, null,
                    null, null, null, null, null, null);
            for (V1Namespace namespace : namespaceList.getItems()) {
            	System.out.println(namespace.getMetadata().getName());
        }
        } catch (ApiException e) {
            System.err.println("Exception when calling CoreV1Api#listNamespace");
            e.printStackTrace();
            return ResultUtil.error(String.valueOf(e.getCode()), e.getMessage());
        }
        return ResultUtil.success( namespaceList);
    }
createNamespace

链接

    /**
     * 根据名称删除namespace  TODO: 能删除成功,但会提示错误 Expected a string but was BEGIN_OBJECT 官方错误
     * @return
     */
    @GetMapping("/deleteNamespace/{name}")
    public ResultUtil deleteNamespace(@PathVariable String name) {
        System.out.println("删除");
        ApiClient client = k8sInit.getConnection();
        CoreV1Api apiInstance = new CoreV1Api(client);
        String pretty = "true";
        try {
            apiInstance.deleteNamespace(name, pretty, null, null, true,
                    null, null);
        } catch (Exception ignored) {

        }
        return ResultUtil.success();
    }

    /**
     * 根据name查询namespace的信息
     * @param name namespace的名称
     * @return
     */
    @GetMapping("/readNamespace/{name}")
    public ResultUtil readNamespace(@PathVariable String name) {
        CoreV1Api apiInstance = new CoreV1Api(k8sInit.getConnection());
        V1Namespace result;
        try {
            result = apiInstance.readNamespace(name, "true", null, null);
        } catch (ApiException e) {
            return ResultUtil.error(String.valueOf(e.getCode()), e.getMessage());
        }
        return ResultUtil.success(result);
    }







    /**
     * 替换namespace的内容
     * 可以使用,但没有搞懂具体作用
     * @param body
     * @return
     */
    @PostMapping("/replaceNamespace")
    public ResultUtil replaceNamespace(@RequestBody V1Namespace body) {
        CoreV1Api apiInstance = new CoreV1Api(k8sInit.getConnection());
        V1Namespace result;
        // V1NamespaceStatus status = new V1NamespaceStatus();
        // status.setPhase("Terminating");
        // body.setStatus(status);
        try {
            result = apiInstance.replaceNamespace("test-namespace", body, "true", null, null);
            System.out.println(result);
        } catch (ApiException e) {
            return ResultUtil.error(String.valueOf(e.getCode()), e.getMessage());
        }
        return ResultUtil.success(result);
    }


    // 更新namespace TODO: 422 : Unprocessable Entity
    @PatchMapping("/patchNamespace")
    public ResultUtil patchNamespace(@RequestBody V1Patch body) {
        CoreV1Api apiInstance = new CoreV1Api(k8sInit.getConnection());
        String pretty = "true";
        V1Namespace result;
        System.out.println(body);
        try {
            result = apiInstance.patchNamespace("test-namespace", body, pretty, null, null, true);
        } catch (ApiException e) {
            System.err.println("Exception when calling CoreV1Api#patchNamespace");
            System.err.println("Status code: " + e.getCode());
            System.err.println("Reason: " + e.getResponseBody());
            System.err.println("Response headers: " + e.getResponseHeaders());
            e.printStackTrace();
            return ResultUtil.error(String.valueOf(e.getCode()), e.getMessage());
        }
        return ResultUtil.success(result);
    }
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐