diff --git a/internal/api/handlers.go b/internal/api/handlers.go index 08cf1c9..dfeb696 100644 --- a/internal/api/handlers.go +++ b/internal/api/handlers.go @@ -2,6 +2,7 @@ package api import ( "net/http" + "regexp" "gitea.starryskymeow.cn/xkm/educode-controller/internal/k8s" "github.com/gin-gonic/gin" @@ -24,6 +25,10 @@ func (h *Handler) createWorkspace(c *gin.Context) { 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 @@ -87,3 +92,13 @@ func (h *Handler) extendWorkspace(c *gin.Context) { 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 +} diff --git a/internal/k8s/workspace.go b/internal/k8s/workspace.go index 83cc28c..7fa8f78 100644 --- a/internal/k8s/workspace.go +++ b/internal/k8s/workspace.go @@ -137,10 +137,8 @@ func CreateWorkspace(req *WorkspaceRequest) error { } func DeleteWorkspace(clientset *kubernetes.Clientset, namespace, workspaceID string) error { - // Delete StatefulSet + // Delete StatefulSet with svc but not pvc. deletePolicy := metav1.DeletePropagationBackground - // TODO - // Maybe not delete pvc? err := clientset.AppsV1().StatefulSets(namespace).Delete(context.TODO(), workspaceID, metav1.DeleteOptions{ PropagationPolicy: &deletePolicy, })