Skip to content

Commit

Permalink
Added backend changes for stop experiment (#4227)
Browse files Browse the repository at this point in the history
* Added backend changes for stop experiment

Signed-off-by: Saranya-jena <[email protected]>

* fixed issues with subscriber for stop experiemnt

Signed-off-by: Saranya-jena <[email protected]>

* updated logic

Signed-off-by: Saranya-jena <[email protected]>

* fixed imports

Signed-off-by: Saranya-jena <[email protected]>

* fixed imports

Signed-off-by: Saranya-jena <[email protected]>

---------

Signed-off-by: Saranya-jena <[email protected]>
  • Loading branch information
Saranya-jena authored Oct 26, 2023
1 parent 291cbbf commit a0393ef
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ extend type Mutation {
experimentID: String!
projectID: ID!
): RunChaosExperimentResponse!

"""
stopExperiment will halt all the ongoing runs of a particular experiment
"""
stopExperimentRuns(projectID: ID!, experimentID:String!, experimentRunID: String, notifyID: String): Boolean! @authorized
}
24 changes: 24 additions & 0 deletions chaoscenter/graphql/server/graph/chaos_experiment_run.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ func (r *mutationResolver) RunChaosExperiment(ctx context.Context, experimentID
return &model.RunChaosExperimentResponse{NotifyID: uiResponse.NotifyID}, err
}

func (r *mutationResolver) StopExperimentRuns(ctx context.Context, projectID string, experimentID string, experimentRunID *string, notifyID *string) (bool, error) {
logFields := logrus.Fields{
"projectId": projectID,
"chaosExperimentId": experimentID,
"chaosExperimentRunId": experimentRunID,
"notifyID": notifyID,
}

logrus.WithFields(logFields).Info("request received to stop chaos experiment")
err := authorization.ValidateRole(ctx, projectID,
authorization.MutationRbacRules[authorization.StopChaosExperiment],
model.InvitationAccepted.String())
if err != nil {
return false, err
}

uiResponse, err := r.chaosExperimentHandler.StopExperimentRuns(ctx, projectID, experimentID, experimentRunID, data_store.Store)
if err != nil {
logrus.WithFields(logFields).Error(err)
return false, err
}
return uiResponse, nil
}

func (r *queryResolver) GetExperimentRun(ctx context.Context, projectID string, experimentRunID *string, notifyID *string) (*model.ExperimentRun, error) {
logFields := logrus.Fields{
"projectId": projectID,
Expand Down
123 changes: 123 additions & 0 deletions chaoscenter/graphql/server/graph/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 30 additions & 29 deletions chaoscenter/graphql/server/pkg/authorization/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
CreateChaosWorkFlow RoleQuery = "CreateChaosWorkFlow"
ReRunChaosWorkFlow RoleQuery = "ReRunChaosWorkFlow"
DeleteChaosWorkflow RoleQuery = "DeleteChaosWorkflow"
StopChaosExperiment RoleQuery = "StopChaosExperiment"
TerminateChaosWorkflow RoleQuery = "TerminateChaosWorkflow"
SyncWorkflow RoleQuery = "SyncWorkflow"
SendInvitation RoleQuery = "SendInvitation"
Expand Down Expand Up @@ -84,35 +85,35 @@ const (
)

var MutationRbacRules = map[RoleQuery][]string{
UserInfrastructureReg: {MemberRoleOwnerString, MemberRoleEditorString},
CreateChaosWorkFlow: {MemberRoleOwnerString, MemberRoleEditorString},
ReRunChaosWorkFlow: {MemberRoleOwnerString, MemberRoleEditorString},
DeleteChaosWorkflow: {MemberRoleOwnerString, MemberRoleEditorString},
TerminateChaosWorkflow: {MemberRoleOwnerString, MemberRoleEditorString},
SyncWorkflow: {MemberRoleOwnerString, MemberRoleEditorString},
SendInvitation: {MemberRoleOwnerString},
AcceptInvitation: {MemberRoleViewerString, MemberRoleEditorString},
DeclineInvitation: {MemberRoleViewerString, MemberRoleEditorString},
RemoveInvitation: {MemberRoleOwnerString},
LeaveProject: {MemberRoleViewerString, MemberRoleEditorString},
UpdateProjectName: {MemberRoleOwnerString},
AddChaosHub: {MemberRoleOwnerString, MemberRoleEditorString},
SyncHub: {MemberRoleOwnerString, MemberRoleEditorString},
UpdateChaosWorkflow: {MemberRoleOwnerString, MemberRoleEditorString},
DeleteInfrastructures: {MemberRoleOwnerString, MemberRoleEditorString},
UpdateChaosHub: {MemberRoleOwnerString, MemberRoleEditorString},
DeleteChaosHub: {MemberRoleOwnerString, MemberRoleEditorString},
EnableGitOps: {MemberRoleOwnerString},
DisableGitOps: {MemberRoleOwnerString},
UpdateGitOps: {MemberRoleOwnerString},
CreateDataSource: {MemberRoleOwnerString, MemberRoleEditorString},
CreateDashBoard: {MemberRoleOwnerString, MemberRoleEditorString},
UpdateDataSource: {MemberRoleOwnerString, MemberRoleEditorString},
UpdateDashboard: {MemberRoleOwnerString, MemberRoleEditorString},
DeleteDashboard: {MemberRoleOwnerString, MemberRoleEditorString},
DeleteDataSource: {MemberRoleOwnerString, MemberRoleEditorString},
ListWorkflowRuns: {MemberRoleOwnerString, MemberRoleEditorString, MemberRoleViewerString},
GetWorkflowRun: {MemberRoleOwnerString, MemberRoleEditorString, MemberRoleViewerString},
UserInfrastructureReg: {MemberRoleOwnerString, MemberRoleEditorString},
CreateChaosWorkFlow: {MemberRoleOwnerString, MemberRoleEditorString},
ReRunChaosWorkFlow: {MemberRoleOwnerString, MemberRoleEditorString},
DeleteChaosWorkflow: {MemberRoleOwnerString, MemberRoleEditorString},
StopChaosExperiment: {MemberRoleOwnerString, MemberRoleEditorString},
SyncWorkflow: {MemberRoleOwnerString, MemberRoleEditorString},
SendInvitation: {MemberRoleOwnerString},
AcceptInvitation: {MemberRoleViewerString, MemberRoleEditorString},
DeclineInvitation: {MemberRoleViewerString, MemberRoleEditorString},
RemoveInvitation: {MemberRoleOwnerString},
LeaveProject: {MemberRoleViewerString, MemberRoleEditorString},
UpdateProjectName: {MemberRoleOwnerString},
AddChaosHub: {MemberRoleOwnerString, MemberRoleEditorString},
SyncHub: {MemberRoleOwnerString, MemberRoleEditorString},
UpdateChaosWorkflow: {MemberRoleOwnerString, MemberRoleEditorString},
DeleteInfrastructures: {MemberRoleOwnerString, MemberRoleEditorString},
UpdateChaosHub: {MemberRoleOwnerString, MemberRoleEditorString},
DeleteChaosHub: {MemberRoleOwnerString, MemberRoleEditorString},
EnableGitOps: {MemberRoleOwnerString},
DisableGitOps: {MemberRoleOwnerString},
UpdateGitOps: {MemberRoleOwnerString},
CreateDataSource: {MemberRoleOwnerString, MemberRoleEditorString},
CreateDashBoard: {MemberRoleOwnerString, MemberRoleEditorString},
UpdateDataSource: {MemberRoleOwnerString, MemberRoleEditorString},
UpdateDashboard: {MemberRoleOwnerString, MemberRoleEditorString},
DeleteDashboard: {MemberRoleOwnerString, MemberRoleEditorString},
DeleteDataSource: {MemberRoleOwnerString, MemberRoleEditorString},
ListWorkflowRuns: {MemberRoleOwnerString, MemberRoleEditorString, MemberRoleViewerString},
GetWorkflowRun: {MemberRoleOwnerString, MemberRoleEditorString, MemberRoleViewerString},
ListInfrastructures: {MemberRoleOwnerString, MemberRoleEditorString,
MemberRoleViewerString},
GetInfrastructure: {MemberRoleOwnerString, MemberRoleEditorString,
Expand Down
63 changes: 63 additions & 0 deletions chaoscenter/graphql/server/pkg/chaos_experiment/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -1316,3 +1317,65 @@ func (c *ChaosExperimentHandler) validateDuplicateExperimentName(ctx context.Con

return nil
}

func (c *ChaosExperimentHandler) StopExperimentRuns(ctx context.Context, projectID string, experimentID string, experimentRunID *string, r *store.StateData) (bool, error) {

var experimentRunsID []string

tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)

query := bson.D{
{"experiment_id", experimentID},
{"project_id", projectID},
{"is_removed", false},
}
experiment, err := c.chaosExperimentOperator.GetExperiment(context.TODO(), query)
if err != nil {
return false, err
}

// if experimentID is provided & no expRunID is present (stop all the corresponding experiment runs)
if experimentRunID == nil {

// if experiment is of cron type, disable it
if experiment.CronSyntax != "" {

err = c.DisableCronExperiment(username, experiment, projectID, r)
if err != nil {
return false, err
}
}

// Fetching all the experiment runs in the experiment
expRuns, err := dbChaosExperimentRun.NewChaosExperimentRunOperator(c.mongodbOperator).GetExperimentRuns(bson.D{
{"experiment_id", experimentID},
{"is_removed", false},
})
if err != nil {
return false, err
}

for _, runs := range expRuns {
if (runs.Phase == string(model.ExperimentRunStatusRunning) || runs.Phase == string(model.ExperimentRunStatusTimeout)) && !runs.Completed {
experimentRunsID = append(experimentRunsID, runs.ExperimentRunID)
}
}

// Check if experiment run count is 0 and if it's not a cron experiment
if len(experimentRunsID) == 0 && experiment.CronSyntax == "" {
return false, fmt.Errorf("no running or timeout experiments found")
}
} else if experimentRunID != nil && *experimentRunID != "" {
experimentRunsID = []string{*experimentRunID}
}

for _, runID := range experimentRunsID {
err = c.chaosExperimentRunService.ProcessExperimentRunStop(ctx, query, &runID, experiment, username, projectID, r)
if err != nil {
return false, err
}
}

return true, nil
}
25 changes: 25 additions & 0 deletions chaoscenter/graphql/server/pkg/chaos_experiment_run/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
type Service interface {
ProcessExperimentRunDelete(ctx context.Context, query bson.D, workflowRunID *string, experimentRun dbChaosExperimentRun.ChaosExperimentRun, workflow dbChaosExperiment.ChaosExperimentRequest, username string, r *store.StateData) error
ProcessCompletedExperimentRun(execData ExecutionData, wfID string, runID string) (ExperimentRunMetrics, error)
ProcessExperimentRunStop(ctx context.Context, query bson.D, experimentRunID *string, experiment dbChaosExperiment.ChaosExperimentRequest, username string, projectID string, r *store.StateData) error
}

// chaosWorkflowService is the implementation of the chaos workflow service
Expand Down Expand Up @@ -70,6 +71,30 @@ func (c *chaosExperimentRunService) ProcessExperimentRunDelete(ctx context.Conte
return nil
}

// ProcessExperimentRunStop deletes a workflow entry and updates the database
func (c *chaosExperimentRunService) ProcessExperimentRunStop(ctx context.Context, query bson.D, experimentRunID *string, experiment dbChaosExperiment.ChaosExperimentRequest, username string, projectID string, r *store.StateData) error {
update := bson.D{
{"$set", bson.D{
{"updated_at", time.Now().UnixMilli()},
{"updated_by", mongodb.UserDetailResponse{
Username: username,
}},
}},
}

err := c.chaosExperimentRunOperator.UpdateExperimentRunWithQuery(ctx, query, update)
if err != nil {
return err
}
if r != nil {
chaos_infrastructure.SendExperimentToSubscriber(projectID, &model.ChaosExperimentRequest{
InfraID: experiment.InfraID,
}, &username, experimentRunID, "workflow_run_stop", r)
}

return nil
}

// ProcessCompletedExperimentRun calculates the Resiliency Score and returns the updated ExecutionData
func (c *chaosExperimentRunService) ProcessCompletedExperimentRun(execData ExecutionData, wfID string, runID string) (ExperimentRunMetrics, error) {
weightSum, totalTestResult := 0, 0
Expand Down
Loading

0 comments on commit a0393ef

Please sign in to comment.