|
@@ -58,26 +58,41 @@ public class KubernetesUtil {
|
|
|
* 创建 namespace 命名空间
|
|
|
*
|
|
|
* @param apiClient 客户端
|
|
|
- * @param namespaceName 命名空间名称
|
|
|
+ * @param nsName 命名空间名称
|
|
|
* @return 命名空间对象
|
|
|
- * @throws ApiException 异常
|
|
|
*/
|
|
|
- public static V1Namespace createNamespace(ApiClient apiClient, String namespaceName) throws ApiException {
|
|
|
- CoreV1Api coreV1Api = new CoreV1Api(apiClient);
|
|
|
- V1Namespace yaml = new V1Namespace();
|
|
|
- //1 apiVersion
|
|
|
- yaml.setApiVersion("v1");
|
|
|
-
|
|
|
- //2 kind
|
|
|
- yaml.setKind("Namespace");
|
|
|
+ @SneakyThrows
|
|
|
+ public static V1Namespace createNs(ApiClient apiClient, String nsName) {
|
|
|
+ if (!getNs(apiClient).contains(nsName)) {
|
|
|
+ CoreV1Api coreV1Api = new CoreV1Api(apiClient);
|
|
|
+ V1Namespace yaml = new V1Namespace();
|
|
|
+ //1 apiVersion
|
|
|
+ yaml.setApiVersion("v1");
|
|
|
+ //2 kind
|
|
|
+ yaml.setKind("Namespace");
|
|
|
+ //3 metadata
|
|
|
+ V1ObjectMeta metadata = new V1ObjectMeta();
|
|
|
+ metadata.setName(nsName);
|
|
|
+ metadata.setLabels(null);
|
|
|
+ yaml.setMetadata(metadata);
|
|
|
+ return coreV1Api.createNamespace(yaml, null, null, null, null);
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- //3 metadata
|
|
|
- V1ObjectMeta metadata = new V1ObjectMeta();
|
|
|
- metadata.setName(namespaceName);
|
|
|
- metadata.setLabels(null);
|
|
|
- yaml.setMetadata(metadata);
|
|
|
|
|
|
- return coreV1Api.createNamespace(yaml, null, null, null, null);
|
|
|
+ /**
|
|
|
+ * 获取 namespace 列表
|
|
|
+ *
|
|
|
+ * @param apiClient api 客户端
|
|
|
+ * @return pod 名称列表
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ public static List<String> getNs(ApiClient apiClient) {
|
|
|
+ CoreV1Api coreV1Api = new CoreV1Api(apiClient);
|
|
|
+ V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(null, null, null, null, null, null, null, null, null, null);
|
|
|
+ return v1NamespaceList.getItems().stream().map(ns -> Objects.requireNonNull(ns.getMetadata()).getName()).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
public static List<String> getPod(ApiClient apiClient, String namespaceName) throws ApiException {
|