mirror of
https://github.com/1Panel-dev/KubePi.git
synced 2025-10-27 17:21:17 +08:00
feat(cluster):导入集群操作
This commit is contained in:
@@ -1,18 +1,16 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
kContext "context"
|
||||
"fmt"
|
||||
v1 "github.com/KubeOperator/ekko/internal/model/v1"
|
||||
"github.com/KubeOperator/ekko/internal/api/v1/session"
|
||||
v1Cluster "github.com/KubeOperator/ekko/internal/model/v1/cluster"
|
||||
"github.com/KubeOperator/ekko/internal/service/v1/cluster"
|
||||
"github.com/KubeOperator/ekko/internal/service/v1/common"
|
||||
pkgV1 "github.com/KubeOperator/ekko/pkg/api/v1"
|
||||
"github.com/KubeOperator/ekko/pkg/util/kubernetes"
|
||||
"github.com/KubeOperator/ekko/pkg/kubernetes"
|
||||
"github.com/asdine/storm/v3"
|
||||
"github.com/google/uuid"
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/context"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
@@ -25,62 +23,64 @@ func NewHandler() *Handler {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Create() iris.Handler {
|
||||
func (h *Handler) CreateCluster() iris.Handler {
|
||||
return func(ctx *context.Context) {
|
||||
var clusterImport Import
|
||||
if err := ctx.ReadJSON(&clusterImport); err != nil {
|
||||
var req Cluster
|
||||
if err := ctx.ReadJSON(&req); err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.Values().Set("message", err.Error())
|
||||
return
|
||||
}
|
||||
c, err := h.clusterService.Get(clusterImport.Name)
|
||||
if err != nil && err != storm.ErrNotFound {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.Values().Set("message", fmt.Sprintf("error: %s", err.Error()))
|
||||
if req.CaDataStr != "" {
|
||||
req.CaCertificate.CertData = []byte(req.CaDataStr)
|
||||
}
|
||||
if req.Spec.Authentication.Mode == "certificate" {
|
||||
req.Spec.Authentication.Certificate.CertData = []byte(req.CertDataStr)
|
||||
req.Spec.Authentication.Certificate.KeyData = []byte(req.KeyDataStr)
|
||||
}
|
||||
|
||||
client := kubernetes.NewKubernetes(req.Cluster)
|
||||
if err := client.Ping(); err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.Values().Set("message", err.Error())
|
||||
return
|
||||
}
|
||||
if c != nil && c.Name != "" {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.Values().Set("message", fmt.Sprintf("cluster name %s is alerady exist", clusterImport.Name))
|
||||
v, _ := client.Version()
|
||||
req.Status.Version = v.GitVersion
|
||||
u := ctx.Values().Get("profile")
|
||||
profile := u.(session.UserProfile)
|
||||
req.CreatedBy = profile.Name
|
||||
if err := h.clusterService.Create(&req.Cluster, common.DBOptions{}); err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.Values().Set("message", err.Error())
|
||||
return
|
||||
}
|
||||
client, err := kubernetes.NewKubernetesClient(&kubernetes.Config{
|
||||
Host: clusterImport.ApiServer,
|
||||
Token: clusterImport.Token,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.Values().Set("message", fmt.Sprintf("import cluster filed: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
_, err = client.CoreV1().Namespaces().List(kContext.TODO(), metav1.ListOptions{})
|
||||
if err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.Values().Set("message", fmt.Sprintf("import cluster failed: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
cs := v1Cluster.Cluster{
|
||||
Metadata: v1.Metadata{
|
||||
Name: clusterImport.Name,
|
||||
UUID: uuid.New().String(),
|
||||
},
|
||||
Token: clusterImport.Token,
|
||||
Router: clusterImport.Router,
|
||||
ApiServer: clusterImport.ApiServer,
|
||||
Status: "Running",
|
||||
}
|
||||
err = h.clusterService.Create(&cs)
|
||||
if err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.Values().Set("message", fmt.Sprintf("create cluster failed: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
ctx.StatusCode(iris.StatusOK)
|
||||
ctx.Values().Set("data", cs)
|
||||
ctx.Values().Set("data", &req)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ListAll() iris.Handler {
|
||||
func (h *Handler) SearchClusters() iris.Handler {
|
||||
return func(ctx *context.Context) {
|
||||
pageNum, _ := ctx.Values().GetInt(pkgV1.PageNum)
|
||||
pageSize, _ := ctx.Values().GetInt(pkgV1.PageSize)
|
||||
var conditions pkgV1.Conditions
|
||||
if ctx.GetContentLength() > 0 {
|
||||
if err := ctx.ReadJSON(&conditions); err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.Values().Set("message", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
clusters, total, err := h.clusterService.Search(pageNum, pageSize, conditions, common.DBOptions{})
|
||||
if err != nil && err != storm.ErrNotFound {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.Values().Set("message", err.Error())
|
||||
}
|
||||
ctx.Values().Set("data", pkgV1.Page{Items: clusters, Total: total})
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ListClusters() iris.Handler {
|
||||
return func(ctx *context.Context) {
|
||||
var clusters []v1Cluster.Cluster
|
||||
clusters, err := h.clusterService.All()
|
||||
@@ -94,20 +94,7 @@ func (h *Handler) ListAll() iris.Handler {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Search() iris.Handler {
|
||||
return func(ctx *context.Context) {
|
||||
pageNum, _ := ctx.Values().GetInt(pkgV1.PageNum)
|
||||
pageSize, _ := ctx.Values().GetInt(pkgV1.PageSize)
|
||||
clusters, total, err := h.clusterService.Search(pageNum, pageSize)
|
||||
if err != nil && err != storm.ErrNotFound {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.Values().Set("message", err.Error())
|
||||
}
|
||||
ctx.Values().Set("data", pkgV1.Page{Items: clusters, Total: total})
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Delete() iris.Handler {
|
||||
func (h *Handler) DeleteCluster() iris.Handler {
|
||||
return func(ctx *context.Context) {
|
||||
name := ctx.Params().GetString("name")
|
||||
err := h.clusterService.Delete(name)
|
||||
@@ -123,8 +110,8 @@ func (h *Handler) Delete() iris.Handler {
|
||||
func Install(parent iris.Party) {
|
||||
handler := NewHandler()
|
||||
sp := parent.Party("/clusters")
|
||||
sp.Post("", handler.Create())
|
||||
sp.Get("", handler.ListAll())
|
||||
sp.Delete("/{name:string}", handler.Delete())
|
||||
sp.Post("/search", handler.Search())
|
||||
sp.Post("", handler.CreateCluster())
|
||||
sp.Get("", handler.ListClusters())
|
||||
sp.Delete("/:name", handler.DeleteCluster())
|
||||
sp.Post("/search", handler.SearchClusters())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user