Skip to content

Commit

Permalink
Chore: support ports and delete pod in time
Browse files Browse the repository at this point in the history
  • Loading branch information
icovej committed May 20, 2023
1 parent 274cd49 commit 4410118
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 16 deletions.
1 change: 1 addition & 0 deletions PlatformBackEnd/Ns.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"namespace":"test-ns","days":5},{"namespace":"test-ns-1","days":5}]
2 changes: 1 addition & 1 deletion PlatformBackEnd/Pod.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"PodName":"metagpu-test-2","UserName":"C"},{"PodName":"metagpu-test-3","UserName":"D"},{"PodName":"gpu-test-with-gp","UserName":"C"},{"PodName":"metagpu-test-10","UserName":"C"},{"PodName":"metagpu-test-4","UserName":"C"},{"PodName":"metagpu-test-15","UserName":"C"},{"PodName":"metagpu-test-80","UserName":"C"},{"PodName":"metagpu-test-11","UserName":"D"}]
[{"PodName":"metagpu-test-2","UserName":"C"},{"PodName":"metagpu-test-3","UserName":"D"},{"PodName":"gpu-test-with-gp","UserName":"C"},{"PodName":"metagpu-test-10","UserName":"C"},{"PodName":"metagpu-test-4","UserName":"C"},{"PodName":"metagpu-test-15","UserName":"C"},{"PodName":"metagpu-test-80","UserName":"C"},{"PodName":"metagpu-test-11","UserName":"D"},{"PodName":"test","UserName":"C"}]
Binary file added PlatformBackEnd/bin/platform
Binary file not shown.
34 changes: 34 additions & 0 deletions PlatformBackEnd/controller/k8s_resource_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,37 @@ func GetClusterNodeData(c *gin.Context) {
})
glog.Info("Succeed to get node data")
}

func CreateNamespace(c *gin.Context) {
var ns data.NsData
err := c.ShouldBindJSON(&ns)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": data.API_PARAMETER_ERROR,
"message": fmt.Sprintf("Method CreateNamespace gets invalid request payload, err is %v", err.Error()),
})
glog.Errorf("Method CreateNamespace gets invalid request payload, the error is %v", err.Error())
return
}
glog.Infof("Succeed to get request to create namespace %v", ns.Namespace)

_, err = tools.CreateNamespace(ns.Namespace)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": data.OPERATION_FAILURE,
"message": fmt.Sprintf("Failed to cerate namespace %v, the error is %v", ns.Namespace, err.Error()),
})
glog.Errorf("Failed to cerate namespace %v, the error is %v", ns.Namespace, err.Error())
return
}

r, _ := tools.CheckNs()
r = append(r, ns)
_ = tools.WriteNs(r)

c.JSON(http.StatusOK, gin.H{
"code": data.SUCCESS,
"message": fmt.Sprintf("Scceed to create namespace %v", ns.Namespace),
})
glog.Infof("Scceed to create namespace %v", ns.Namespace)
}
21 changes: 19 additions & 2 deletions PlatformBackEnd/controller/pod_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,24 @@ func CreatePod(c *gin.Context) {
Name: pod.Container,
Image: pod.Imagename,
ImagePullPolicy: corev1.PullIfNotPresent,
Command: []string{"/bin/bash", "-ce", "tail -f /dev/null"}, //["/bin/sh","-ce","sleep 3600"],
Ports: []corev1.ContainerPort{
{
Name: "http",
ContainerPort: pod.CPort[0],
HostPort: pod.HPort[0],
},
{
Name: "https",
ContainerPort: pod.CPort[1],
HostPort: pod.HPort[1],
},
{
Name: "ssh",
ContainerPort: pod.CPort[2],
HostPort: pod.HPort[2],
},
},
Command: []string{"/bin/bash", "-ce", "tail -f /dev/null"}, //["/bin/sh","-ce","sleep 3600"],
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: memReq,
Expand Down Expand Up @@ -286,7 +303,7 @@ func DeletePod(c *gin.Context) {
}
glog.Infof("Succeed to get request to delete pod %v", pod.Podname)

err = tools.DeletePod(pod)
err = tools.DeletePod(pod.Namespace, pod.Podname)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": data.OPERATION_FAILURE,
Expand Down
42 changes: 31 additions & 11 deletions PlatformBackEnd/data/data_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
"github.com/googollee/go-socket.io/engineio/transport"
"github.com/googollee/go-socket.io/engineio/transport/polling"
"github.com/googollee/go-socket.io/engineio/transport/websocket"
v1 "k8s.io/api/core/v1"
)

const (
// user data file
UserFile = "User.json"
PodFile = "Pod.json"
UserFile = "User.json"
PodFile = "Pod.json"
NamespaceFile = "Ns.json"

// the metric of nvidia/com
// the value is set by Kubernetes
Expand Down Expand Up @@ -103,16 +105,34 @@ type DirData struct {

// Pod data from the front-end
type PodData struct {
Podname string `json:"podname"`
Container string `json:"container"`
Memory string `json:"memory"`
Cpu string `json:"cpu"`
Gpu string `json:"gpu"`
Memlim string `json:"memlim"`
Cpulim string `json:"cpulim"`
Gpulim string `json:"gpulim"`
Imagename string `json:"imagename"`
Podname string `json:"podname"`
Container string `json:"container"`
Memory string `json:"memory"`
Cpu string `json:"cpu"`
Gpu string `json:"gpu"`
Memlim string `json:"memlim"`
Cpulim string `json:"cpulim"`
Gpulim string `json:"gpulim"`
Imagename string `json:"imagename"`
Namespace string `json:"namespace"`
CPort []int32 `json:"cport"`
HPort []int32 `json:"hport"`
}

type PodInfo struct {
Name string
AgeInDays int
Status v1.PodPhase
}

type NsData struct {
Namespace string `json:"namespace"`
Days int `json:"days"`
}

type PodTimeDelete struct {
NsData
Time int
}

// Used to read event file
Expand Down
12 changes: 10 additions & 2 deletions PlatformBackEnd/main.go → PlatformBackEnd/run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ func main() {
defer glog.Flush()
glog.Info("Succeed to start platform")

go func() {
for {
tools.DeletPodInTime()
}
}()

_ = tools.CreateFile(data.UserFile)
_ = tools.CreateFile(data.PodFile)
_ = tools.CreateFile(data.NamespaceFile)

// Init Gin
router := gin.Default()
Expand Down Expand Up @@ -71,10 +78,10 @@ func main() {

// Admin Opts
router.POST("/login", controller.Login)
router.GET("/getuser_notoken", controller.GetUserInfo_NoToken)
router.POST("/modify_user", controller.ModifyUser)
router.POST("/ns_create", controller.CreateNamespace)
router.GET("/getuser_notoken", controller.GetUserInfo_NoToken)
router.Static("/logs", flag.Lookup("log_dir").Value.String())
router.Static("/dockerfiles", flag.Lookup("dockerfiles_path").Value.String())

api := router.Group("/api")
api.Use(tools.JWTAuth())
Expand All @@ -91,6 +98,7 @@ func main() {

// Image Opts
router.POST("/image", controller.CreateImage)
router.Static("/dockerfiles", flag.Lookup("dockerfiles_path").Value.String())

// Pod Opts
router.POST("/create_pod", controller.CreatePod)
Expand Down

0 comments on commit 4410118

Please sign in to comment.