Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Pod list、describe、create、delete、update功能实现 #26

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions controllers/kubernetes/pods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package kubernetes

import (
"fmt"
"genbu/common/global"
service "genbu/service/kubernetes"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
corev1 "k8s.io/api/core/v1"
"log"
"strconv"
)

/*
@Auth: Menah3m
@CreateTime: 2024/1/19
@Desc:
*/

type MyPod = corev1.Pod

func GetPodList(c *gin.Context) {
clusterID := c.Param("cid")
namespace := c.Query("namespace")
pageStr := c.Query("page")
page, err := strconv.Atoi(pageStr)
if err != nil {
global.ReturnContext(c).Failed("获取page参数失败", err.Error())
return
}
pageSizeStr := c.Query("pagesize")
pageSize, err := strconv.Atoi(pageSizeStr)
if err != nil {
global.ReturnContext(c).Failed("获取pageSize参数失败", err.Error())
return
}

ret, err := service.NewK8sInterface().GetPodList(clusterID, namespace, page, pageSize)
if err != nil {
global.ReturnContext(c).Failed("获取Pod List失败", err.Error())
return
}
global.ReturnContext(c).Successful("获取Pod List成功", ret)
}

func GetPod(c *gin.Context) {
clusterID := c.Param("cid")
namespace := c.Query("namespace")
name := c.Query("name")

ret, err := service.NewK8sInterface().GetPod(clusterID, namespace, name)
if err != nil {
global.ReturnContext(c).Failed("获取Pod失败", err.Error())
return
}
global.ReturnContext(c).Successful("获取Pod成功", ret)
}

func CreatePod(c *gin.Context) {
clusterID := c.Param("cid")
var myPod MyPod
err := c.ShouldBindJSON(&myPod)
ret, err := service.NewK8sInterface().CreatePod(clusterID, &myPod)
if err != nil {
global.ReturnContext(c).Failed("创建Pod失败", err.Error())
return
}
global.ReturnContext(c).Successful("创建Pod成功", ret)
}

func UpdatePod(c *gin.Context) {
clusterID := c.Param("cid")
var myPatch MyPod
err := c.ShouldBindJSON(&myPatch)
ret, err := service.NewK8sInterface().UpdatePod(clusterID, &myPatch)
if err != nil {
global.ReturnContext(c).Failed("更新Pod失败", err.Error())
return
}
global.ReturnContext(c).Successful("更新Pod成功", ret)
}

func DeletePod(c *gin.Context) {
clusterID := c.Param("cid")
namespace := c.Query("namespace")
pods := c.QueryArray("pod")
// fmt.Println(pods)
ret, err := service.NewK8sInterface().DeletePod(clusterID, namespace, pods)
if err != nil {
global.ReturnContext(c).Failed("删除Pod失败", err.Error())
return
}
global.ReturnContext(c).Successful("删除Pod成功", ret)
}

func GetPodLogs(c *gin.Context) {
clusterID := c.Param("cid")
namespace := c.Query("namespace")
name := c.Query("name")
follow := c.Query("follow")

followB, err := strconv.ParseBool(follow)
if err != nil {
fmt.Println("转换失败:", err)
return
}

ret, err := service.NewK8sInterface().GetPodLogs(clusterID, namespace, name, followB)
if err != nil {
global.ReturnContext(c).Failed("获取Log失败", err.Error())
return
}
global.ReturnContext(c).Successful("获取Pod成功", ret)
}

func WatchPod(c *gin.Context) {
clusterID := c.Param("cid")
namespace := c.Query("namespace")

ws, err := websocket.Upgrade(c.Writer, c.Request, nil, 1024, 1024)
if err != nil {
global.ReturnContext(c).Failed("创建websocket失败", err.Error())
return
}
pw, _ := service.NewK8sInterface().WatchPod(clusterID, namespace)

fmt.Println(pw)
// for event := range pw.ResultChan() {

// // c.Writer.Write([]byte(message))
// // err = ws.WriteMessage(websocket.TextMessage, []byte(message))
// // if err != nil {
// // log.Println("Error writing to WebSocket:", err.Error())
// // return
// // }
// }
ws.Close()

}

func ExecPod(c *gin.Context) {
clusterID := c.Param("cid")
namespace := c.Query("namespace")

ws, err := websocket.Upgrade(c.Writer, c.Request, nil, 1024, 1024)
if err != nil {
global.ReturnContext(c).Failed("创建websocket失败", err.Error())
return
}
fmt.Println(ws)
pw, err := service.NewK8sInterface().WatchPod(clusterID, namespace)
fmt.Println(pw)
for event := range pw.ResultChan() {
pod, ok := event.Object.(*corev1.Pod)
if !ok {
continue
}

message := fmt.Sprintf("Pod %s [%s]: %s", pod.Name, event.Type, pod.Status.Phase)
err = ws.WriteMessage(websocket.TextMessage, []byte(message))
if err != nil {
log.Println("Error writing to WebSocket:", err.Error())
return
}
}

}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module genbu

go 1.17
go 1.21

require (
github.com/appleboy/gin-jwt/v2 v2.9.1
Expand All @@ -14,6 +14,7 @@ require (
)

require (
github.com/gorilla/websocket v1.5.1
github.com/patrickmn/go-cache v2.1.0+incompatible
k8s.io/api v0.29.0
k8s.io/apimachinery v0.29.0
Expand Down Expand Up @@ -69,9 +70,11 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/spf13/afero v1.9.5 // indirect
Expand Down
Loading