DKube/controller/pod.go
2022-09-25 21:07:16 +08:00

208 lines
4.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"dkube/service"
"github.com/gin-gonic/gin"
"github.com/wonderivan/logger"
"net/http"
)
var Pod pod
type pod struct{}
// GetPods 获取Pod列表支持分页、过滤、排序
func (p *pod) GetPods(ctx *gin.Context) {
params := new(struct {
FilterName string `form:"filter_name"`
Namespace string `form:"namespace"`
Limit int `form:"limit"`
Page int `form:"page"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind绑定参数失败" + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": "Bind参数绑定失败" + err.Error(),
"data": nil,
})
return
}
data, err := service.Pod.GetPods(params.FilterName, params.Namespace, params.Limit, params.Page)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Pod列表成功",
"data": data,
})
}
//获取Pod详情
func (p *pod) GetPodDetail(ctx *gin.Context) {
params := new(struct {
PodName string `form:"pod_name"`
Namespace string `form:"namespace"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind绑定参数失败" + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": "Bind参数绑定失败" + err.Error(),
"data": nil,
})
return
}
data, err := service.Pod.GetPodDetail(params.PodName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Pod列表成功",
"data": data,
})
}
//删除Pod
func (p *pod) DeletePod(ctx *gin.Context) {
params := new(struct {
PodName string `json:"pod_name"`
Namespace string `json:"namespace"`
})
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind绑定参数失败" + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": "Bind参数绑定失败" + err.Error(),
"data": nil,
})
return
}
err := service.Pod.DeletePod(params.PodName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "删除Pod成功",
"data": nil,
})
}
//更新Pod
func (p *pod) UpdatePod(ctx *gin.Context) {
params := new(struct {
Namespace string `json:"namespace"`
Content string `json:"content"`
})
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind绑定参数失败" + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": "Bind参数绑定失败" + err.Error(),
"data": nil,
})
return
}
err := service.Pod.UpdatePod(params.Namespace, params.Content)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "更新Pod成功",
"data": nil,
})
}
//获取Pod容器
func (p *pod) GetPodContainer(ctx *gin.Context) {
params := new(struct {
PodName string `form:"pod_name"`
Namespace string `form:"namespace"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败" + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Pod.GetPodContainer(params.PodName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Pod容器成功",
"data": data,
})
}
//获取Pod容器日志
func (p *pod) GetPodLog(ctx *gin.Context) {
params := new(struct {
ContainerName string `form:"container_name"`
PodName string `form:"pod_name"`
Namespace string `form:"namespace"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败" + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Pod.GetPodLog(params.ContainerName, params.PodName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Pod容器日志成功",
"data": data,
})
}
//获取每个Namespace的Pod数量
func (p *pod) GetPodNumPerNp(ctx *gin.Context) {
data, err := service.Pod.GetPodNumPerNp()
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取每个namespace的Pod数量成功",
"data": data,
})
}