Commit 483c3ef7 authored by 黎博's avatar 黎博

新增创建namespace接口

parent ef25ec83
......@@ -5,12 +5,9 @@ import cn.qg.holmes.entity.k8s.DockerProject;
import cn.qg.holmes.entity.k8s.ServiceCreateVo;
import cn.qg.holmes.service.k8s.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.fabric8.kubernetes.api.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -252,4 +249,14 @@ public class K8sController {
return JsonResult.buildErrorStateResult("redis清除失败!", false);
}
}
/**
* 创建新的namespace
*/
@PostMapping("/namespace/create")
public JsonResult createNewNamespace(@RequestParam String name,
@RequestParam String desc,
@RequestParam String owner) {
return JsonResult.buildSuccessResult(k8sService.createNewNamespace(name, desc, owner));
}
}
......@@ -1897,8 +1897,40 @@ public class K8sService {
return map;
}
/**
* 创建新的namespace
* @param name namespace名称
* @param desc dev/test
* @param owner
* @return
*/
public Namespace createNewNamespace(String name, String desc, String owner) {
Namespace namespace = new Namespace();
ObjectMeta objectMeta = new ObjectMeta();
Map<String, String> annotations = new HashMap<>();
annotations.put("description", desc);
annotations.put("owner", owner);
objectMeta.setAnnotations(annotations);
objectMeta.setName(name);
NamespaceSpec namespaceSpec = new NamespaceSpec();
List<String> finalizers = new ArrayList<>();
finalizers.add("kubernetes");
namespaceSpec.setFinalizers(finalizers);
NamespaceStatus namespaceStatus = new NamespaceStatus();
namespaceStatus.setPhase("Active");
namespace.setApiVersion("v1");
namespace.setKind("Namespace");
namespace.setMetadata(objectMeta);
namespace.setSpec(namespaceSpec);
namespace.setStatus(namespaceStatus);
return kubernetesClient.namespaces().create(namespace);
}
public static void main(String[] args) {
K8sService k8sService = new K8sService();
System.out.println(k8sService.getPodDetail("k8s", "base", "redis"));
k8sService.createNewNamespace("test6", "dev", "黎博");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment