105 lines
2.7 KiB
Go
105 lines
2.7 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"regexp"
|
|
|
|
"gitea.starryskymeow.cn/xkm/educode-controller/internal/k8s"
|
|
"github.com/gin-gonic/gin"
|
|
"k8s.io/client-go/kubernetes"
|
|
)
|
|
|
|
const Namespace = "educode"
|
|
|
|
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
|
|
}
|
|
if !isValidDNS1035Label(req.WorkspaceID) {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123')"})
|
|
return
|
|
}
|
|
image := k8s.DefaultImage
|
|
if req.Image != "" {
|
|
image = req.Image
|
|
}
|
|
|
|
resourceLimits := k8s.DefaultResourceLimits
|
|
if req.ResourceLimits != nil {
|
|
if req.ResourceLimits.CPU != "" {
|
|
resourceLimits.CPU = req.ResourceLimits.CPU
|
|
}
|
|
if req.ResourceLimits.Memory != "" {
|
|
resourceLimits.Memory = req.ResourceLimits.Memory
|
|
}
|
|
if req.ResourceLimits.Storage != "" {
|
|
resourceLimits.Storage = req.ResourceLimits.Storage
|
|
}
|
|
}
|
|
|
|
k8sReq := &k8s.WorkspaceRequest{
|
|
Image: image,
|
|
Env: req.Env,
|
|
ResourceLimits: &resourceLimits,
|
|
WorkspaceID: req.WorkspaceID,
|
|
Clientset: h.clientset,
|
|
Namespace: Namespace,
|
|
}
|
|
|
|
keyword, 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, "keyword": keyword})
|
|
}
|
|
|
|
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})
|
|
}
|
|
|
|
func isValidDNS1035Label(s string) bool {
|
|
if len(s) > 63 {
|
|
return false
|
|
}
|
|
if !regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`).MatchString(s) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|