add workspaceID check

This commit is contained in:
starryskymeow
2025-07-05 16:47:28 +08:00
parent 4a638c675c
commit 49f61b6a5f
2 changed files with 16 additions and 3 deletions

View File

@@ -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
}

View File

@@ -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,
})