From b8c813cb6c6b3d413ede560466062e5d67fd8798 Mon Sep 17 00:00:00 2001 From: Hanshal Mehta <122217807+hanshal101@users.noreply.github.com> Date: Thu, 9 May 2024 00:17:42 +0530 Subject: [PATCH] feat(backend): handle two modules owning the same resource --- cyclops-ctrl/internal/controller/modules.go | 30 +++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/cyclops-ctrl/internal/controller/modules.go b/cyclops-ctrl/internal/controller/modules.go index 979ef7e4..88f1650e 100644 --- a/cyclops-ctrl/internal/controller/modules.go +++ b/cyclops-ctrl/internal/controller/modules.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/gin-gonic/gin" + v1 "k8s.io/api/apps/v1" "github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1" "github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/cluster/k8sclient" @@ -216,9 +217,14 @@ func (m *Modules) CreateModule(ctx *gin.Context) { return } + if !m.CheckModule(&module) { + ctx.JSON(http.StatusBadRequest, dto.NewError("Error same module exists", "Same module found")) + return + } m.telemetryClient.ModuleCreation() err = m.kubernetesClient.CreateModule(module) + if err != nil { fmt.Println(err) ctx.JSON(http.StatusInternalServerError, dto.NewError("Error creating module", err.Error())) @@ -229,6 +235,30 @@ func (m *Modules) CreateModule(ctx *gin.Context) { ctx.Status(http.StatusOK) } +func (m *Modules) CheckModule(module *v1alpha1.Module) bool { + deployments, err := m.kubernetesClient.GetDeployments(module.Namespace) + if err != nil { + fmt.Printf("error : %v\n", err) + return false + } + + for _, deployment := range deployments { + if isSharedDeployment(&deployment) { + fmt.Printf("Deployment %s is shared\n", deployment.Name) + return false + } + } + return true +} + +func isSharedDeployment(deployment *v1.Deployment) bool { + labels := deployment.GetLabels() + deploymentName := deployment.Name + cyclopsName := labels["cyclops.name"] + + return deploymentName != cyclopsName +} + func (m *Modules) UpdateModule(ctx *gin.Context) { ctx.Header("Access-Control-Allow-Origin", "*")