Skip to content

Commit

Permalink
Added check to prevent duplicate experiments with same name (#4187)
Browse files Browse the repository at this point in the history
* Added check to prevent duplicate experiments with same name

* Added duplicate experiment name check in chaos center

* minor fix

Signed-off-by: Sarthak Jain <[email protected]>

---------

Signed-off-by: Sarthak Jain <[email protected]>
  • Loading branch information
SarthakJain26 authored Sep 28, 2023
1 parent e3f6868 commit 25a436d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
43 changes: 35 additions & 8 deletions chaoscenter/graphql/server/pkg/chaos_experiment/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ func (c *ChaosExperimentHandler) SaveChaosExperiment(ctx context.Context, reques
// Updating the existing experiment
if wfDetails.ExperimentID == request.ID {
logrus.WithFields(logFields).Info("request received to update k8s chaos experiment")
if wfDetails.Name != request.Name {
filterQuery := bson.D{
{"project_id", request.ID},
{"name", request.Name},
{"is_removed", false},
}
experimentCount, err := c.chaosExperimentOperator.CountChaosExperiments(ctx, filterQuery)
if err != nil {
return "", err
}
if experimentCount > 0 {
return "", errors.New("experiment name should be unique, duplicate experiment found with name: " + request.Name)
}
}

err = c.chaosExperimentService.ProcessExperimentUpdate(newRequest, username, wfType, revID, false, projectID, nil)
if err != nil {
Expand All @@ -129,19 +143,18 @@ func (c *ChaosExperimentHandler) CreateChaosExperiment(ctx context.Context, requ

var revID = uuid.New().String()

// Check if the workflow_name exists under same project
wfDetails, err := c.chaosExperimentOperator.GetExperiments(bson.D{
{"name", request.ExperimentName},
// Check if the experiment_name exists under same project
filterQuery := bson.D{
{"project_id", projectID},
{"tags", request.Tags},
{"name", request.ExperimentName},
{"is_removed", false},
})
}
experimentCount, err := c.chaosExperimentOperator.CountChaosExperiments(ctx, filterQuery)
if err != nil {
return nil, err
}

if wfDetails != nil || len(wfDetails) > 0 {
return nil, errors.New("experiment name already exists in this project")
if experimentCount > 0 {
return nil, errors.New("experiment name should be unique, duplicate experiment found with name: " + request.ExperimentName)
}

newRequest, wfType, err := c.chaosExperimentService.ProcessExperiment(request, projectID, revID)
Expand Down Expand Up @@ -226,6 +239,20 @@ func (c *ChaosExperimentHandler) UpdateChaosExperiment(ctx context.Context, requ
revID = uuid.New().String()
)

// Check if the experiment_name exists under same project
filterQuery := bson.D{
{"project_id", projectID},
{"name", request.ExperimentName},
{"is_removed", false},
}
experimentCount, err := c.chaosExperimentOperator.CountChaosExperiments(ctx, filterQuery)
if err != nil {
return nil, err
}
if experimentCount > 0 {
return nil, errors.New("experiment name should be unique, duplicate experiment found with name: " + request.ExperimentName)
}

newRequest, wfType, err := c.chaosExperimentService.ProcessExperiment(request, projectID, revID)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,12 @@ func (c *Operator) UpdateChaosExperiments(ctx context.Context, query bson.D, upd

return nil
}

// CountChaosExperiments returns total number of matched documents
func (c *Operator) CountChaosExperiments(ctx context.Context, query bson.D) (int64, error) {
res, err := mongodb.Operator.CountDocuments(ctx, mongodb.ChaosExperimentCollection, query)
if err != nil {
return 0, err
}
return res, nil
}
2 changes: 1 addition & 1 deletion chaoscenter/graphql/server/pkg/gitops/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ func (g *gitOpsService) updateExperiment(data, wfID, file string, config GitConf
return errors.New("file name doesn't match experiment name")
}

experiment, err := g.chaosExperimentOps.GetExperiments(bson.D{{"experiment_id", wfID}, {"project_id", config.ProjectID}, {"isRemoved", false}})
experiment, err := g.chaosExperimentOps.GetExperiments(bson.D{{"experiment_id", wfID}, {"project_id", config.ProjectID}, {"is_removed", false}})
if len(experiment) == 0 {
return errors.New("No such experiment found : " + wfID)
}
Expand Down
30 changes: 30 additions & 0 deletions litmus-portal/graphql-server/pkg/chaos-workflow/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -64,6 +65,21 @@ func NewChaosWorkflowHandler(

// CreateChaosWorkflow creates a new chaos workflow
func (c *ChaosWorkflowHandler) CreateChaosWorkflow(ctx context.Context, request *model.ChaosWorkFlowRequest, r *store.StateData) (*model.ChaosWorkFlowResponse, error) {

// Check if the experiment_name exists under same project
expDetails, err := c.chaosWorkflowOperator.GetWorkflow(bson.D{
{"name", request.WorkflowName},
{"project_id", request.ProjectID},
{"is_removed", false},
})
if err != nil && err != mongo.ErrNoDocuments {
return nil, err
}
if expDetails.WorkflowID == "" {
log.Errorf("experiment with name %s already exists", request.WorkflowName)
return nil, fmt.Errorf("experiment with name %s already exists", request.WorkflowName)
}

request, wfType, err := c.chaosWorkflowService.ProcessWorkflow(request)
if err != nil {
log.Error("error processing workflow: ", err)
Expand Down Expand Up @@ -198,6 +214,20 @@ func (c *ChaosWorkflowHandler) TerminateChaosWorkflow(ctx context.Context, proje
}

func (c *ChaosWorkflowHandler) UpdateChaosWorkflow(ctx context.Context, request *model.ChaosWorkFlowRequest, r *store.StateData) (*model.ChaosWorkFlowResponse, error) {
// Check if the experiment_name exists under same project
expDetails, err := c.chaosWorkflowOperator.GetWorkflow(bson.D{
{"name", request.WorkflowName},
{"project_id", request.ProjectID},
{"is_removed", false},
})
if err != nil && err != mongo.ErrNoDocuments {
return nil, err
}
if expDetails.WorkflowID == "" {
log.Errorf("experiment with name %s already exists", request.WorkflowName)
return nil, fmt.Errorf("experiment with name %s already exists", request.WorkflowName)
}

request, wfType, err := c.chaosWorkflowService.ProcessWorkflow(request)
if err != nil {
log.Error("error processing workflow update: ", err)
Expand Down

0 comments on commit 25a436d

Please sign in to comment.