Add workspace creation feature
This commit is contained in:
83
internal/api/handlers.go
Normal file
83
internal/api/handlers.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"gitea.starryskymeow.cn/xkm/educode-controller/internal/k8s"
|
||||
"github.com/gin-gonic/gin"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
const Namespace = "default" // Or get from config
|
||||
|
||||
type Handler struct {
|
||||
clientset *kubernetes.Clientset
|
||||
}
|
||||
|
||||
func NewHandler(clientset *kubernetes.Clientset) *Handler {
|
||||
return &Handler{clientset: clientset}
|
||||
}
|
||||
|
||||
func (h *Handler) createWorkspace(c *gin.Context) {
|
||||
var req CreateWorkspaceRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
image := k8s.DefaultImage
|
||||
if req.Image != "" {
|
||||
image = req.Image
|
||||
}
|
||||
|
||||
k8sReq := &k8s.WorkspaceRequest{
|
||||
Image: image,
|
||||
Env: req.Env,
|
||||
WorkspaceID: req.WorkspaceID,
|
||||
Clientset: h.clientset,
|
||||
Namespace: Namespace,
|
||||
}
|
||||
|
||||
if req.ResourceLimits != nil {
|
||||
k8sReq.ResourceLimits = &k8s.ResourceLimits{
|
||||
CPU: req.ResourceLimits.CPU,
|
||||
Memory: req.ResourceLimits.Memory,
|
||||
}
|
||||
}
|
||||
|
||||
err := k8s.CreateWorkspace(k8sReq)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusAccepted, gin.H{"status": "creating", "workspaceId": req.WorkspaceID})
|
||||
}
|
||||
|
||||
func (h *Handler) deleteWorkspace(c *gin.Context) {
|
||||
workspaceID := c.Param("workspaceID")
|
||||
|
||||
err := k8s.DeleteWorkspace(h.clientset, Namespace, workspaceID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusAccepted, gin.H{"status": "deleting", "workspaceId": workspaceID})
|
||||
}
|
||||
|
||||
func (h *Handler) getWorkspace(c *gin.Context) {
|
||||
workspaceID := c.Param("workspaceID")
|
||||
|
||||
// TODO: Call k8s manager to get resource status
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok", "workspaceId": workspaceID})
|
||||
}
|
||||
|
||||
func (h *Handler) extendWorkspace(c *gin.Context) {
|
||||
workspaceID := c.Param("workspaceID")
|
||||
|
||||
// TODO: Call k8s manager to patch resource annotation
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "extended", "workspaceId": workspaceID})
|
||||
}
|
||||
34
internal/api/server.go
Normal file
34
internal/api/server.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
// NewServer creates and configures a new Gin server.
|
||||
func NewServer(clientset *kubernetes.Clientset) *gin.Engine {
|
||||
r := gin.Default()
|
||||
|
||||
// Create a new handler with the clientset
|
||||
h := NewHandler(clientset)
|
||||
|
||||
// Health check endpoint
|
||||
r.GET("/healthz", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"status": "ok",
|
||||
})
|
||||
})
|
||||
|
||||
v1 := r.Group("/api/v1")
|
||||
{
|
||||
workspaces := v1.Group("/workspaces")
|
||||
{
|
||||
workspaces.POST("", h.createWorkspace)
|
||||
workspaces.DELETE("/:workspaceID", h.deleteWorkspace)
|
||||
workspaces.GET("/:workspaceID", h.getWorkspace)
|
||||
workspaces.PATCH("/:workspaceID", h.extendWorkspace)
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
15
internal/api/types.go
Normal file
15
internal/api/types.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package api
|
||||
|
||||
// CreateWorkspaceRequest defines the request body for creating a new workspace.
|
||||
type CreateWorkspaceRequest struct {
|
||||
Image string `json:"image"`
|
||||
Env map[string]string `json:"env"`
|
||||
ResourceLimits *ResourceLimits `json:"resourceLimits"`
|
||||
WorkspaceID string `json:"workspaceId"`
|
||||
}
|
||||
|
||||
// ResourceLimits defines the CPU and memory resource limits.
|
||||
type ResourceLimits struct {
|
||||
CPU string `json:"cpu"`
|
||||
Memory string `json:"memory"`
|
||||
}
|
||||
Reference in New Issue
Block a user