Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): handle two modules owning the same resource #262

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions cyclops-ctrl/internal/controller/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()))
Expand All @@ -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", "*")

Expand Down
Loading