diff --git a/chaoscenter/graphql/server/graph/resolver.go b/chaoscenter/graphql/server/graph/resolver.go index e08b658a214..d36423b5abb 100644 --- a/chaoscenter/graphql/server/graph/resolver.go +++ b/chaoscenter/graphql/server/graph/resolver.go @@ -23,6 +23,7 @@ import ( "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments" gitops2 "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/gitops" image_registry2 "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/image_registry" + dbSchemaProbe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/probe" envHandler "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/environment/handler" gitops3 "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/gitops" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/image_registry" @@ -55,9 +56,10 @@ func NewConfig(mongodbOperator mongodb.MongoOperator) generated.Config { gitopsOperator := gitops2.NewGitOpsOperator(mongodbOperator) imageRegistryOperator := image_registry2.NewImageRegistryOperator(mongodbOperator) EnvironmentOperator := environments.NewEnvironmentOperator(mongodbOperator) + probeOperator := dbSchemaProbe.NewChaosProbeOperator(mongodbOperator) //service - probeService := probe.NewProbeService() + probeService := probe.NewProbeService(probeOperator) chaosHubService := chaoshub.NewService(chaosHubOperator) chaosInfrastructureService := chaos_infrastructure.NewChaosInfrastructureService(chaosInfraOperator, EnvironmentOperator) chaosExperimentService := chaos_experiment2.NewChaosExperimentService(chaosExperimentOperator, chaosInfraOperator, chaosExperimentRunOperator, probeService) @@ -67,8 +69,8 @@ func NewConfig(mongodbOperator mongodb.MongoOperator) generated.Config { environmentService := envHandler.NewEnvironmentService(EnvironmentOperator) //handler - chaosExperimentHandler := handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, chaosInfrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbOperator) - choasExperimentRunHandler := runHandler.NewChaosExperimentRunHandler(chaosExperimentRunService, chaosInfrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbOperator) + chaosExperimentHandler := handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, chaosInfrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeService, mongodbOperator) + choasExperimentRunHandler := runHandler.NewChaosExperimentRunHandler(chaosExperimentRunService, chaosInfrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeService, mongodbOperator) config := generated.Config{ Resolvers: &Resolver{ diff --git a/chaoscenter/graphql/server/pkg/chaos_experiment/handler/fuzz_tests/handler_fuzz_test.go b/chaoscenter/graphql/server/pkg/chaos_experiment/handler/fuzz_tests/handler_fuzz_test.go index 707a85be553..4afb3160560 100644 --- a/chaoscenter/graphql/server/pkg/chaos_experiment/handler/fuzz_tests/handler_fuzz_test.go +++ b/chaoscenter/graphql/server/pkg/chaos_experiment/handler/fuzz_tests/handler_fuzz_test.go @@ -4,6 +4,8 @@ import ( "context" "testing" + dbProbeMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/model/mocks" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/handler" chaosExperimentMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/model/mocks" chaosExperimentRunMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment_run/model/mocks" @@ -45,8 +47,9 @@ func NewMockServices() *MockServices { chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator) chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator) chaosExperimentService = new(chaosExperimentMocks.ChaosExperimentService) + probeService = new(dbProbeMocks.ProbeService) ) - var chaosExperimentHandler = handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbMockOperator) + var chaosExperimentHandler = handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeService, mongodbMockOperator) return &MockServices{ ChaosExperimentService: chaosExperimentService, ChaosExperimentRunService: chaosExperimentRunService, diff --git a/chaoscenter/graphql/server/pkg/chaos_experiment/handler/handler.go b/chaoscenter/graphql/server/pkg/chaos_experiment/handler/handler.go index ee51be6d05f..419ae421dcb 100644 --- a/chaoscenter/graphql/server/pkg/chaos_experiment/handler/handler.go +++ b/chaoscenter/graphql/server/pkg/chaos_experiment/handler/handler.go @@ -9,7 +9,7 @@ import ( "strconv" "time" - probeUtils "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/utils" + probe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/handler" "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" chaosTypes "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1" @@ -48,6 +48,7 @@ type ChaosExperimentHandler struct { gitOpsService gitops.Service chaosExperimentOperator *dbChaosExperiment.Operator chaosExperimentRunOperator *dbChaosExperimentRun.Operator + probeService probe.Service mongodbOperator mongodb.MongoOperator } @@ -59,6 +60,7 @@ func NewChaosExperimentHandler( gitOpsService gitops.Service, chaosExperimentOperator *dbChaosExperiment.Operator, chaosExperimentRunOperator *dbChaosExperimentRun.Operator, + probeService probe.Service, mongodbOperator mongodb.MongoOperator, ) *ChaosExperimentHandler { return &ChaosExperimentHandler{ @@ -68,6 +70,7 @@ func NewChaosExperimentHandler( gitOpsService: gitOpsService, chaosExperimentOperator: chaosExperimentOperator, chaosExperimentRunOperator: chaosExperimentRunOperator, + probeService: probeService, mongodbOperator: mongodbOperator, } } @@ -1347,7 +1350,7 @@ func (c *ChaosExperimentHandler) GetProbesInExperimentRun(ctx context.Context, p } for _, probeName := range _probe.ProbeNames { - singleProbe, err := dbSchemaProbe.GetProbeByName(ctx, probeName, projectID) + singleProbe, err := dbSchemaProbe.NewChaosProbeOperator(c.mongodbOperator).GetProbeByName(ctx, probeName, projectID) if err != nil { return nil, err } @@ -1456,7 +1459,7 @@ func (c *ChaosExperimentHandler) UpdateCronExperimentState(ctx context.Context, return false, errors.New("failed to marshal workflow manifest") } - cronWorkflowManifest, err = probeUtils.GenerateCronExperimentManifestWithProbes(string(updatedManifest), experiment.ProjectID) + cronWorkflowManifest, err = c.probeService.GenerateCronExperimentManifestWithProbes(string(updatedManifest), experiment.ProjectID) if err != nil { return false, fmt.Errorf("failed to unmarshal experiment manifest, error: %v", err) } diff --git a/chaoscenter/graphql/server/pkg/chaos_experiment/handler/handler_test.go b/chaoscenter/graphql/server/pkg/chaos_experiment/handler/handler_test.go index bb4ea2ff3c2..e11e8d6d38d 100644 --- a/chaoscenter/graphql/server/pkg/chaos_experiment/handler/handler_test.go +++ b/chaoscenter/graphql/server/pkg/chaos_experiment/handler/handler_test.go @@ -6,6 +6,8 @@ import ( "reflect" "testing" + dbProbeMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/model/mocks" + "github.com/golang-jwt/jwt" "github.com/google/uuid" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" @@ -46,8 +48,9 @@ func NewMockServices() *MockServices { chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator) chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator) chaosExperimentService = new(chaosExperimentMocks.ChaosExperimentService) + probeService = new(dbProbeMocks.ProbeService) ) - var chaosExperimentHandler = NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbMockOperator) + var chaosExperimentHandler = NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeService, mongodbMockOperator) return &MockServices{ ChaosExperimentService: chaosExperimentService, ChaosExperimentRunService: chaosExperimentRunService, diff --git a/chaoscenter/graphql/server/pkg/chaos_experiment/ops/service_test.go b/chaoscenter/graphql/server/pkg/chaos_experiment/ops/service_test.go index 65f2a6bfce0..71bd89da8b8 100644 --- a/chaoscenter/graphql/server/pkg/chaos_experiment/ops/service_test.go +++ b/chaoscenter/graphql/server/pkg/chaos_experiment/ops/service_test.go @@ -10,6 +10,8 @@ import ( "reflect" "testing" + dbSchemaProbe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/probe" + "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" @@ -28,10 +30,11 @@ import ( var ( mongodbMockOperator = new(dbMocks.MongoOperator) + probeOperator = dbSchemaProbe.NewChaosProbeOperator(mongodbMockOperator) infraOperator = dbChaosInfra.NewInfrastructureOperator(mongodbMockOperator) chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator) chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator) - probeService = probe.NewProbeService() + probeService = probe.NewProbeService(probeOperator) ) var chaosExperimentRunTestService = NewChaosExperimentService(chaosExperimentOperator, infraOperator, chaosExperimentRunOperator, probeService) diff --git a/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler/fuzz_tests/handler_fuzz_test.go b/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler/fuzz_tests/handler_fuzz_test.go index 56af374f378..c806c538a01 100644 --- a/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler/fuzz_tests/handler_fuzz_test.go +++ b/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler/fuzz_tests/handler_fuzz_test.go @@ -6,6 +6,8 @@ import ( "testing" "time" + dbProbeMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/model/mocks" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler" chaosInfraMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_infrastructure/model/mocks" dbChaosExperiment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment" @@ -32,6 +34,7 @@ type MockServices struct { ChaosExperimentRunOperator *dbChaosExperimentRun.Operator MongodbOperator *dbMocks.MongoOperator ChaosExperimentRunHandler *handler.ChaosExperimentRunHandler + ProbeService *dbProbeMocks.ProbeService } func NewMockServices() *MockServices { @@ -42,6 +45,7 @@ func NewMockServices() *MockServices { chaosExperimentRunService = new(typesMocks.ChaosExperimentRunService) chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator) chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator) + probeService = new(dbProbeMocks.ProbeService) ) var chaosExperimentRunHandler = handler.NewChaosExperimentRunHandler( chaosExperimentRunService, @@ -49,6 +53,7 @@ func NewMockServices() *MockServices { gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, + probeService, mongodbMockOperator, ) return &MockServices{ @@ -58,6 +63,7 @@ func NewMockServices() *MockServices { ChaosExperimentOperator: chaosExperimentOperator, ChaosExperimentRunOperator: chaosExperimentRunOperator, MongodbOperator: mongodbMockOperator, + ProbeService: probeService, ChaosExperimentRunHandler: chaosExperimentRunHandler, } } diff --git a/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler/handler.go b/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler/handler.go index 166abe19973..d301ecd8c02 100644 --- a/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler/handler.go +++ b/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler/handler.go @@ -11,6 +11,8 @@ import ( "strings" "time" + probe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/handler" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization" probeUtils "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/utils" @@ -41,9 +43,8 @@ import ( store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store" dbChaosExperiment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment" - dbChaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure" - "github.com/google/uuid" + dbChaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure" ) // ChaosExperimentRunHandler is the handler for chaos experiment @@ -53,6 +54,7 @@ type ChaosExperimentRunHandler struct { gitOpsService gitops.Service chaosExperimentOperator *dbChaosExperiment.Operator chaosExperimentRunOperator *dbChaosExperimentRun.Operator + probeService probe.Service mongodbOperator mongodb.MongoOperator } @@ -63,6 +65,7 @@ func NewChaosExperimentRunHandler( gitOpsService gitops.Service, chaosExperimentOperator *dbChaosExperiment.Operator, chaosExperimentRunOperator *dbChaosExperimentRun.Operator, + probeService probe.Service, mongodbOperator mongodb.MongoOperator, ) *ChaosExperimentRunHandler { return &ChaosExperimentRunHandler{ @@ -71,6 +74,7 @@ func NewChaosExperimentRunHandler( gitOpsService: gitOpsService, chaosExperimentOperator: chaosExperimentOperator, chaosExperimentRunOperator: chaosExperimentRunOperator, + probeService: probeService, mongodbOperator: mongodbOperator, } } @@ -911,7 +915,7 @@ func (c *ChaosExperimentRunHandler) RunChaosWorkFlow(ctx context.Context, projec } // Generate Probe in the manifest - workflowManifest, err = probeUtils.GenerateExperimentManifestWithProbes(string(manifestString), projectID) + workflowManifest, err = c.probeService.GenerateExperimentManifestWithProbes(string(manifestString), projectID) if err != nil { return nil, fmt.Errorf("failed to generate probes in workflow manifest, err: %v", err) } @@ -944,7 +948,7 @@ func (c *ChaosExperimentRunHandler) RunCronExperiment(ctx context.Context, proje return workflow.Revision[i].UpdatedAt > workflow.Revision[j].UpdatedAt }) - cronExperimentManifest, err := probeUtils.GenerateCronExperimentManifestWithProbes(workflow.Revision[0].ExperimentManifest, workflow.ProjectID) + cronExperimentManifest, err := c.probeService.GenerateCronExperimentManifestWithProbes(workflow.Revision[0].ExperimentManifest, workflow.ProjectID) if err != nil { return errors.New("failed to unmarshal experiment manifest") } diff --git a/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler/handler_test.go b/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler/handler_test.go index 6f08fa1f6cf..f22e59b545a 100644 --- a/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler/handler_test.go +++ b/chaoscenter/graphql/server/pkg/chaos_experiment_run/handler/handler_test.go @@ -11,6 +11,9 @@ import ( "testing" "time" + probe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/handler" + dbProbeMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/model/mocks" + "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" @@ -38,9 +41,10 @@ var ( gitOpsService = new(dbGitOpsMocks.GitOpsService) chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator) chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator) + probeService = new(dbProbeMocks.ProbeService) ) -var chaosExperimentRunHandler = NewChaosExperimentRunHandler(chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbMockOperator) +var chaosExperimentRunHandler = NewChaosExperimentRunHandler(chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeService, mongodbMockOperator) // TestMain is the entry point for testing func TestMain(m *testing.M) { @@ -56,6 +60,7 @@ func TestNewChaosExperimentRunHandler(t *testing.T) { gitOpsService gitops.Service chaosExperimentOperator *dbChaosExperiment.Operator chaosExperimentRunOperator *dbChaosExperimentRun.Operator + probeService probe.Service mongodbOperator mongodb.MongoOperator } tests := []struct { @@ -71,6 +76,7 @@ func TestNewChaosExperimentRunHandler(t *testing.T) { gitOpsService: gitOpsService, chaosExperimentOperator: chaosExperimentOperator, chaosExperimentRunOperator: chaosExperimentRunOperator, + probeService: probeService, mongodbOperator: mongodbMockOperator, }, want: &ChaosExperimentRunHandler{ @@ -79,13 +85,14 @@ func TestNewChaosExperimentRunHandler(t *testing.T) { gitOpsService: gitOpsService, chaosExperimentOperator: chaosExperimentOperator, chaosExperimentRunOperator: chaosExperimentRunOperator, + probeService: probeService, mongodbOperator: mongodbMockOperator, }, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - if got := NewChaosExperimentRunHandler(tc.args.chaosExperimentRunService, tc.args.infrastructureService, tc.args.gitOpsService, tc.args.chaosExperimentOperator, tc.args.chaosExperimentRunOperator, tc.args.mongodbOperator); !reflect.DeepEqual(got, tc.want) { + if got := NewChaosExperimentRunHandler(tc.args.chaosExperimentRunService, tc.args.infrastructureService, tc.args.gitOpsService, tc.args.chaosExperimentOperator, tc.args.chaosExperimentRunOperator, tc.args.probeService, tc.args.mongodbOperator); !reflect.DeepEqual(got, tc.want) { t.Errorf("NewChaosExperimentRunHandler() = %v, want %v", got, tc.want) } }) diff --git a/chaoscenter/graphql/server/pkg/chaos_infrastructure/fuzz/fuzz_test.go b/chaoscenter/graphql/server/pkg/chaos_infrastructure/fuzz/fuzz_test.go index b8fa0ff67b0..4fc9881b3d4 100644 --- a/chaoscenter/graphql/server/pkg/chaos_infrastructure/fuzz/fuzz_test.go +++ b/chaoscenter/graphql/server/pkg/chaos_infrastructure/fuzz/fuzz_test.go @@ -4,6 +4,8 @@ import ( "context" "testing" + dbProbeMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/model/mocks" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/handler" chaosExperimentMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/model/mocks" @@ -41,9 +43,10 @@ func NewMockServices() *MockServices { gitOpsService = new(dbGitOpsMocks.GitOpsService) chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator) chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator) + probeService = new(dbProbeMocks.ProbeService) chaosExperimentService = new(chaosExperimentMocks.ChaosExperimentService) ) - var chaosExperimentHandler = handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbMockOperator) + var chaosExperimentHandler = handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, infrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, probeService, mongodbMockOperator) return &MockServices{ ChaosExperimentService: chaosExperimentService, ChaosExperimentRunService: chaosExperimentRunService, diff --git a/chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go b/chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go index 5f6ad864ed6..1860d3dae1f 100644 --- a/chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go +++ b/chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go @@ -10,10 +10,22 @@ import ( "go.mongodb.org/mongo-driver/mongo" ) +// Operator is the model for probe collection +type Operator struct { + operator mongodb.MongoOperator +} + +// NewChaosProbeOperator returns a new instance of Operator +func NewChaosProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator { + return &Operator{ + operator: mongodbOperator, + } +} + // CreateProbe creates a probe of a specific type (HTTP, PROM, K8s or CMD) // as a shared entity in the database -func CreateProbe(ctx context.Context, probe Probe) error { - err := mongodb.Operator.Create(ctx, mongodb.ChaosProbeCollection, probe) +func (p *Operator) CreateProbe(ctx context.Context, probe Probe) error { + err := p.operator.Create(ctx, mongodb.ChaosProbeCollection, probe) if err != nil { return err } @@ -21,8 +33,8 @@ func CreateProbe(ctx context.Context, probe Probe) error { } // GetAggregateProbes takes a mongo pipeline to retrieve the project details from the database -func GetAggregateProbes(ctx context.Context, pipeline mongo.Pipeline) (*mongo.Cursor, error) { - results, err := mongodb.Operator.Aggregate(ctx, mongodb.ChaosProbeCollection, pipeline) +func (p *Operator) GetAggregateProbes(ctx context.Context, pipeline mongo.Pipeline) (*mongo.Cursor, error) { + results, err := p.operator.Aggregate(ctx, mongodb.ChaosProbeCollection, pipeline) if err != nil { return nil, err } @@ -31,8 +43,8 @@ func GetAggregateProbes(ctx context.Context, pipeline mongo.Pipeline) (*mongo.Cu } // IsProbeUnique returns true if probe is unique -func IsProbeUnique(ctx context.Context, query bson.D) (bool, error) { - count, err := mongodb.Operator.CountDocuments(ctx, mongodb.ChaosProbeCollection, query) +func (p *Operator) IsProbeUnique(ctx context.Context, query bson.D) (bool, error) { + count, err := p.operator.CountDocuments(ctx, mongodb.ChaosProbeCollection, query) if err != nil { return false, err } @@ -44,8 +56,8 @@ func IsProbeUnique(ctx context.Context, query bson.D) (bool, error) { } // UpdateProbe updates details of a Probe -func UpdateProbe(ctx context.Context, query bson.D, updateQuery bson.D) (*mongo.UpdateResult, error) { - result, err := mongodb.Operator.Update(ctx, mongodb.ChaosProbeCollection, query, updateQuery) +func (p *Operator) UpdateProbe(ctx context.Context, query bson.D, updateQuery bson.D) (*mongo.UpdateResult, error) { + result, err := p.operator.Update(ctx, mongodb.ChaosProbeCollection, query, updateQuery) if err != nil { return nil, err } @@ -56,8 +68,8 @@ func UpdateProbe(ctx context.Context, query bson.D, updateQuery bson.D) (*mongo. } // UpdateProbes updates details of Probe -func UpdateProbes(ctx context.Context, query bson.D, updateQuery bson.D) (*mongo.UpdateResult, error) { - result, err := mongodb.Operator.UpdateMany(ctx, mongodb.ChaosProbeCollection, query, updateQuery) +func (p *Operator) UpdateProbes(ctx context.Context, query bson.D, updateQuery bson.D) (*mongo.UpdateResult, error) { + result, err := p.operator.UpdateMany(ctx, mongodb.ChaosProbeCollection, query, updateQuery) if err != nil { return nil, err } @@ -68,9 +80,9 @@ func UpdateProbes(ctx context.Context, query bson.D, updateQuery bson.D) (*mongo } // GetProbeByName fetches the details of a single Probe with its Probe Name -func GetProbeByName(ctx context.Context, probeName string, projectID string) (Probe, error) { +func (p *Operator) GetProbeByName(ctx context.Context, probeName string, projectID string) (Probe, error) { var probe Probe - result, err := mongodb.Operator.Get(ctx, mongodb.ChaosProbeCollection, bson.D{{"name", probeName}, {"project_id", projectID}, {"is_removed", false}}) + result, err := p.operator.Get(ctx, mongodb.ChaosProbeCollection, bson.D{{"name", probeName}, {"project_id", projectID}, {"is_removed", false}}) err = result.Decode(&probe) if err != nil { return Probe{}, err diff --git a/chaoscenter/graphql/server/pkg/probe/handler/handler.go b/chaoscenter/graphql/server/pkg/probe/handler/handler.go index fa9d45cf46b..193b81147d2 100644 --- a/chaoscenter/graphql/server/pkg/probe/handler/handler.go +++ b/chaoscenter/graphql/server/pkg/probe/handler/handler.go @@ -4,10 +4,15 @@ import ( "context" "encoding/json" "errors" + "fmt" "sort" "strconv" + "strings" "time" + argoTypes "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" + "github.com/ghodss/yaml" + dbChaosExperiment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/probe/utils" globalUtils "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" @@ -19,7 +24,6 @@ import ( "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb" dbChaosExperimentRun "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment_run" dbSchemaProbe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/probe" - "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" @@ -34,12 +38,18 @@ type Service interface { GetProbeReference(ctx context.Context, probeName, projectID string) (*model.GetProbeReferenceResponse, error) GetProbeYAMLData(ctx context.Context, probe model.GetProbeYAMLRequest, projectID string) (string, error) ValidateUniqueProbe(ctx context.Context, probeName, projectID string) (bool, error) + GenerateExperimentManifestWithProbes(manifest string, projectID string) (argoTypes.Workflow, error) + GenerateCronExperimentManifestWithProbes(manifest string, projectID string) (argoTypes.CronWorkflow, error) } -type probe struct{} +type probeService struct { + probeOperator *dbSchemaProbe.Operator +} -func NewProbeService() Service { - return &probe{} +func NewProbeService(probeOperator *dbSchemaProbe.Operator) Service { + return &probeService{ + probeOperator: probeOperator, + } } func Error(logFields logrus.Fields, message string) error { @@ -48,7 +58,7 @@ func Error(logFields logrus.Fields, message string) error { } // AddProbe - Create a new Probe -func (p *probe) AddProbe(ctx context.Context, probe model.ProbeRequest, projectID string) (*model.Probe, error) { +func (p *probeService) AddProbe(ctx context.Context, probe model.ProbeRequest, projectID string) (*model.Probe, error) { // TODO: Add check if probe exists var ( @@ -118,7 +128,7 @@ func (p *probe) AddProbe(ctx context.Context, probe model.ProbeRequest, projectI } // Adding the new probe into database. - err = dbSchemaProbe.CreateProbe(ctx, *newProbe) + err = p.probeOperator.CreateProbe(ctx, *newProbe) if err != nil { return nil, err } @@ -127,14 +137,14 @@ func (p *probe) AddProbe(ctx context.Context, probe model.ProbeRequest, projectI } // UpdateProbe - Update a new Probe -func (p *probe) UpdateProbe(ctx context.Context, request model.ProbeRequest, projectID string) (string, error) { +func (p *probeService) UpdateProbe(ctx context.Context, request model.ProbeRequest, projectID string) (string, error) { tkn := ctx.Value(authorization.AuthKey).(string) username, err := authorization.GetUsername(tkn) if err != nil { return "", err } - pr, err := dbSchemaProbe.GetProbeByName(ctx, request.Name, projectID) + pr, err := p.probeOperator.GetProbeByName(ctx, request.Name, projectID) if err != nil { return "", err } @@ -197,7 +207,7 @@ func (p *probe) UpdateProbe(ctx context.Context, request model.ProbeRequest, pro {"is_removed", false}, } - _, err = dbSchemaProbe.UpdateProbe(ctx, filterQuery, updateQuery) + _, err = p.probeOperator.UpdateProbe(ctx, filterQuery, updateQuery) if err != nil { return "", err } @@ -206,9 +216,9 @@ func (p *probe) UpdateProbe(ctx context.Context, request model.ProbeRequest, pro } // GetProbe - List a single Probe -func (p *probe) GetProbe(ctx context.Context, probeName, projectID string) (*model.Probe, error) { +func (p *probeService) GetProbe(ctx context.Context, probeName, projectID string) (*model.Probe, error) { - probe, err := dbSchemaProbe.GetProbeByName(ctx, probeName, projectID) + probe, err := p.probeOperator.GetProbeByName(ctx, probeName, projectID) if err != nil { return nil, err } @@ -217,14 +227,14 @@ func (p *probe) GetProbe(ctx context.Context, probeName, projectID string) (*mod } // GetProbeYAMLData - Get the probe yaml data compatible with the chaos engine manifest -func (p *probe) GetProbeYAMLData(ctx context.Context, probeRequest model.GetProbeYAMLRequest, projectID string) (string, error) { +func (p *probeService) GetProbeYAMLData(ctx context.Context, probeRequest model.GetProbeYAMLRequest, projectID string) (string, error) { - probe, err := dbSchemaProbe.GetProbeByName(ctx, probeRequest.ProbeName, projectID) + probe, err := p.probeOperator.GetProbeByName(ctx, probeRequest.ProbeName, projectID) if err != nil { return "", err } - manifest, err := utils.GenerateProbeManifest(probe.GetOutputProbe(), probeRequest.Mode) + manifest, err := p.GenerateProbeManifest(probe.GetOutputProbe(), probeRequest.Mode) if err != nil { return "", err } @@ -233,7 +243,7 @@ func (p *probe) GetProbeYAMLData(ctx context.Context, probeRequest model.GetProb } // ListProbes - List a single/all Probes -func (p *probe) ListProbes(ctx context.Context, probeNames []string, infrastructureType *model.InfrastructureType, filter *model.ProbeFilterInput, projectID string) ([]*model.Probe, error) { +func (p *probeService) ListProbes(ctx context.Context, probeNames []string, infrastructureType *model.InfrastructureType, filter *model.ProbeFilterInput, projectID string) ([]*model.Probe, error) { var pipeline mongo.Pipeline // Match the Probe Names from the input array @@ -336,7 +346,7 @@ func (p *probe) ListProbes(ctx context.Context, probeNames []string, infrastruct var allProbes []dbSchemaProbe.Probe - probeCursor, err := dbSchemaProbe.GetAggregateProbes(ctx, pipeline) + probeCursor, err := p.probeOperator.GetAggregateProbes(ctx, pipeline) if err != nil { return nil, err } @@ -474,9 +484,9 @@ func GetProbeExecutionHistoryInExperimentRuns(projectID string, probeName string } // DeleteProbe - Deletes a single Probe -func (p *probe) DeleteProbe(ctx context.Context, probeName, projectID string) (bool, error) { +func (p *probeService) DeleteProbe(ctx context.Context, probeName, projectID string) (bool, error) { - _, err := dbSchemaProbe.GetProbeByName(ctx, probeName, projectID) + _, err := p.probeOperator.GetProbeByName(ctx, probeName, projectID) if err != nil { return false, err } @@ -500,7 +510,7 @@ func (p *probe) DeleteProbe(ctx context.Context, probeName, projectID string) (b }}, } - _, err = dbSchemaProbe.UpdateProbe(ctx, query, update) + _, err = p.probeOperator.UpdateProbe(ctx, query, update) if err != nil { return false, err } @@ -509,7 +519,7 @@ func (p *probe) DeleteProbe(ctx context.Context, probeName, projectID string) (b } // GetProbeReference - Get the experiment details the probe is referencing to -func (p *probe) GetProbeReference(ctx context.Context, probeName, projectID string) (*model.GetProbeReferenceResponse, error) { +func (p *probeService) GetProbeReference(ctx context.Context, probeName, projectID string) (*model.GetProbeReferenceResponse, error) { var pipeline mongo.Pipeline @@ -564,7 +574,7 @@ func (p *probe) GetProbeReference(ctx context.Context, probeName, projectID stri pipeline = append(pipeline, experimentWithSelectedProbeName) // Call aggregation on pipeline - probeCursor, err := dbSchemaProbe.GetAggregateProbes(ctx, pipeline) + probeCursor, err := p.probeOperator.GetAggregateProbes(ctx, pipeline) if err != nil { return nil, err } @@ -694,17 +704,582 @@ func (p *probe) GetProbeReference(ctx context.Context, probeName, projectID stri } // ValidateUniqueProbe - Validates the uniqueness of the probe, returns true if unique -func (p *probe) ValidateUniqueProbe(ctx context.Context, probeName, projectID string) (bool, error) { +func (p *probeService) ValidateUniqueProbe(ctx context.Context, probeName, projectID string) (bool, error) { query := bson.D{ {"name", probeName}, {"project_id", bson.D{{"$eq", projectID}}}, } - isUnique, err := dbSchemaProbe.IsProbeUnique(ctx, query) + isUnique, err := p.probeOperator.IsProbeUnique(ctx, query) if err != nil { return false, err } return isUnique, nil } + +// GenerateExperimentManifestWithProbes - uses GenerateProbeManifest to get and store the respective probe attribute into Raw Data template for Non Cron Workflow +func (p *probeService) GenerateExperimentManifestWithProbes(manifest string, projectID string) (argoTypes.Workflow, error) { + var ( + backgroundContext = context.Background() + nonCronManifest argoTypes.Workflow + ) + + ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second) + defer cancel() + + err := json.Unmarshal([]byte(manifest), &nonCronManifest) + if err != nil { + return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal experiment manifest, error: %s", err.Error()) + } + + for i, template := range nonCronManifest.Spec.Templates { + artifact := template.Inputs.Artifacts + + if len(artifact) > 0 { + if artifact[0].Raw == nil { + continue + } + data := artifact[0].Raw.Data + if len(data) > 0 { + var ( + meta v1alpha1.ChaosEngine + annotation = make(map[string]string) + probes []v1alpha1.ProbeAttributes + httpProbe HTTPProbeAttributes + cmdProbe CMDProbeAttributes + promProbe PROMProbeAttributes + k8sProbe K8SProbeAttributes + ) + + err := yaml.Unmarshal([]byte(data), &meta) + if err != nil { + return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal chaosengine, error: %s", err.Error()) + } + if strings.ToLower(meta.Kind) == "chaosengine" { + + probes = meta.Spec.Experiments[0].Spec.Probe + + if meta.Annotations != nil { + annotation = meta.Annotations + } + + for _, key := range annotation { + var manifestAnnotation []dbChaosExperiment.ProbeAnnotations + if strings.HasPrefix(key, "[{\"name\"") { + err := json.Unmarshal([]byte(key), &manifestAnnotation) + if err != nil { + return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal experiment annotation object, error: %s", err.Error()) + } + for _, annotationKey := range manifestAnnotation { + probe, err := p.probeOperator.GetProbeByName(ctx, annotationKey.Name, projectID) + if err != nil { + return argoTypes.Workflow{}, fmt.Errorf("failed to fetch probe details, error: %s", err.Error()) + } + probeManifestString, err := p.GenerateProbeManifest(probe.GetOutputProbe(), annotationKey.Mode) + if err != nil { + return argoTypes.Workflow{}, fmt.Errorf("failed to generate probe manifest, error: %s", err.Error()) + } + + if model.ProbeType(probe.Type) == model.ProbeTypeHTTPProbe { + err := json.Unmarshal([]byte(probeManifestString), &httpProbe) + if err != nil { + return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal http probe, error: %s", err.Error()) + } + + probes = append(probes, v1alpha1.ProbeAttributes{ + Name: httpProbe.Name, + Type: httpProbe.Type, + HTTPProbeInputs: &v1alpha1.HTTPProbeInputs{ + URL: httpProbe.HTTPProbeInputs.URL, + InsecureSkipVerify: httpProbe.HTTPProbeInputs.InsecureSkipVerify, + Method: httpProbe.HTTPProbeInputs.Method, + }, + RunProperties: httpProbe.RunProperties, + Mode: httpProbe.Mode, + }) + } else if model.ProbeType(probe.Type) == model.ProbeTypeCmdProbe { + err := json.Unmarshal([]byte(probeManifestString), &cmdProbe) + if err != nil { + return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal cmd probe, error: %s", err.Error()) + } + + probes = append(probes, v1alpha1.ProbeAttributes{ + Name: cmdProbe.Name, + Type: cmdProbe.Type, + CmdProbeInputs: &v1alpha1.CmdProbeInputs{ + Command: cmdProbe.CmdProbeInputs.Command, + Comparator: cmdProbe.CmdProbeInputs.Comparator, + }, + RunProperties: cmdProbe.RunProperties, + Mode: cmdProbe.Mode, + }) + } else if model.ProbeType(probe.Type) == model.ProbeTypePromProbe { + err := json.Unmarshal([]byte(probeManifestString), &promProbe) + if err != nil { + return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal prom probe, error: %s", err.Error()) + } + + probes = append(probes, v1alpha1.ProbeAttributes{ + Name: promProbe.Name, + Type: promProbe.Type, + PromProbeInputs: &v1alpha1.PromProbeInputs{ + Endpoint: promProbe.PromProbeInputs.Endpoint, + Query: promProbe.PromProbeInputs.Query, + QueryPath: promProbe.PromProbeInputs.QueryPath, + Comparator: promProbe.PromProbeInputs.Comparator, + }, + RunProperties: promProbe.RunProperties, + Mode: promProbe.Mode, + }) + } else if model.ProbeType(probe.Type) == model.ProbeTypeK8sProbe { + err := json.Unmarshal([]byte(probeManifestString), &k8sProbe) + if err != nil { + return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal k8s probe, error: %s", err.Error()) + } + + probes = append(probes, v1alpha1.ProbeAttributes{ + Name: k8sProbe.Name, + Type: k8sProbe.Type, + K8sProbeInputs: &v1alpha1.K8sProbeInputs{ + Group: k8sProbe.K8sProbeInputs.Group, + Version: k8sProbe.K8sProbeInputs.Version, + Resource: k8sProbe.K8sProbeInputs.Resource, + ResourceNames: k8sProbe.K8sProbeInputs.ResourceNames, + Namespace: k8sProbe.K8sProbeInputs.Namespace, + FieldSelector: k8sProbe.K8sProbeInputs.FieldSelector, + LabelSelector: k8sProbe.K8sProbeInputs.LabelSelector, + Operation: k8sProbe.K8sProbeInputs.Operation, + }, + RunProperties: k8sProbe.RunProperties, + Mode: k8sProbe.Mode, + }) + } + } + } + } + + if len(meta.Spec.Experiments) > 0 { + meta.Spec.Experiments[0].Spec.Probe = probes + } + + res, err := yaml.Marshal(&meta) + if err != nil { + return argoTypes.Workflow{}, errors.New("failed to marshal chaosengine") + } + nonCronManifest.Spec.Templates[i].Inputs.Artifacts[0].Raw.Data = string(res) + } + } + } + } + + return nonCronManifest, nil +} + +// GenerateCronExperimentManifestWithProbes - uses GenerateProbeManifest to get and store the respective probe attribute into Raw Data template +func (p *probeService) GenerateCronExperimentManifestWithProbes(manifest string, projectID string) (argoTypes.CronWorkflow, error) { + var ( + backgroundContext = context.Background() + cronManifest argoTypes.CronWorkflow + ) + + ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second) + defer cancel() + + if err := json.Unmarshal([]byte(manifest), &cronManifest); err != nil { + return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal experiment manifest, error: %s", err.Error()) + } + + for i, template := range cronManifest.Spec.WorkflowSpec.Templates { + artifact := template.Inputs.Artifacts + + if len(artifact) > 0 { + if artifact[0].Raw == nil { + continue + } + data := artifact[0].Raw.Data + if len(data) > 0 { + var ( + meta v1alpha1.ChaosEngine + annotation = make(map[string]string) + probes []v1alpha1.ProbeAttributes + httpProbe HTTPProbeAttributes + cmdProbe CMDProbeAttributes + promProbe PROMProbeAttributes + k8sProbe K8SProbeAttributes + ) + + if err := yaml.Unmarshal([]byte(data), &meta); err != nil { + return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal chaosengine, error: %s", err.Error()) + } + + if strings.ToLower(meta.Kind) == "chaosengine" { + + probes = meta.Spec.Experiments[0].Spec.Probe + + if meta.Annotations != nil { + annotation = meta.Annotations + } + + for _, key := range annotation { + var manifestAnnotation []dbChaosExperiment.ProbeAnnotations + err := json.Unmarshal([]byte(key), &manifestAnnotation) + if err != nil { + return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal experiment annotation object, error: %s", err.Error()) + } + for _, annotationKey := range manifestAnnotation { + probe, err := p.probeOperator.GetProbeByName(ctx, annotationKey.Name, projectID) + if err != nil { + return argoTypes.CronWorkflow{}, fmt.Errorf("failed to fetch probe details, error: %s", err.Error()) + } + + probeManifestString, err := p.GenerateProbeManifest(probe.GetOutputProbe(), annotationKey.Mode) + + if model.ProbeType(probe.Type) == model.ProbeTypeHTTPProbe { + if err := json.Unmarshal([]byte(probeManifestString), &httpProbe); err != nil { + return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal http probe, error: %s", err.Error()) + } + + probes = append(probes, v1alpha1.ProbeAttributes{ + Name: httpProbe.Name, + Type: httpProbe.Type, + HTTPProbeInputs: &v1alpha1.HTTPProbeInputs{ + URL: httpProbe.HTTPProbeInputs.URL, + InsecureSkipVerify: httpProbe.HTTPProbeInputs.InsecureSkipVerify, + Method: httpProbe.HTTPProbeInputs.Method, + }, + RunProperties: httpProbe.RunProperties, + Mode: httpProbe.Mode, + }) + } else if model.ProbeType(probe.Type) == model.ProbeTypeCmdProbe { + if err := json.Unmarshal([]byte(probeManifestString), &cmdProbe); err != nil { + return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal cmd probe, error: %s", err.Error()) + } + + probes = append(probes, v1alpha1.ProbeAttributes{ + Name: cmdProbe.Name, + Type: cmdProbe.Type, + CmdProbeInputs: &v1alpha1.CmdProbeInputs{ + Command: cmdProbe.CmdProbeInputs.Command, + Comparator: cmdProbe.CmdProbeInputs.Comparator, + }, + RunProperties: cmdProbe.RunProperties, + Mode: cmdProbe.Mode, + }) + } else if model.ProbeType(probe.Type) == model.ProbeTypePromProbe { + if err := json.Unmarshal([]byte(probeManifestString), &promProbe); err != nil { + return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal prom probe, error: %s", err.Error()) + } + + probes = append(probes, v1alpha1.ProbeAttributes{ + Name: promProbe.Name, + Type: promProbe.Type, + PromProbeInputs: &v1alpha1.PromProbeInputs{ + Endpoint: promProbe.PromProbeInputs.Endpoint, + Query: promProbe.PromProbeInputs.Query, + QueryPath: promProbe.PromProbeInputs.QueryPath, + Comparator: promProbe.PromProbeInputs.Comparator, + }, + RunProperties: promProbe.RunProperties, + Mode: promProbe.Mode, + }) + } else if model.ProbeType(probe.Type) == model.ProbeTypeK8sProbe { + if err := json.Unmarshal([]byte(probeManifestString), &k8sProbe); err != nil { + return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal k8s probe, error: %s", err.Error()) + } + + probes = append(probes, v1alpha1.ProbeAttributes{ + Name: k8sProbe.Name, + Type: k8sProbe.Type, + K8sProbeInputs: &v1alpha1.K8sProbeInputs{ + Group: k8sProbe.K8sProbeInputs.Group, + Version: k8sProbe.K8sProbeInputs.Version, + Resource: k8sProbe.K8sProbeInputs.Resource, + ResourceNames: k8sProbe.K8sProbeInputs.ResourceNames, + Namespace: k8sProbe.K8sProbeInputs.Namespace, + FieldSelector: k8sProbe.K8sProbeInputs.FieldSelector, + LabelSelector: k8sProbe.K8sProbeInputs.LabelSelector, + Operation: k8sProbe.K8sProbeInputs.Operation, + }, + RunProperties: k8sProbe.RunProperties, + Mode: k8sProbe.Mode, + }) + } + } + } + + if len(meta.Spec.Experiments) > 0 { + meta.Spec.Experiments[0].Spec.Probe = probes + } + + res, err := yaml.Marshal(&meta) + if err != nil { + return argoTypes.CronWorkflow{}, fmt.Errorf("failed to marshal chaosengine, error: %s", err.Error()) + } + cronManifest.Spec.WorkflowSpec.Templates[i].Inputs.Artifacts[0].Raw.Data = string(res) + } + } + } + } + + return cronManifest, nil +} + +// GenerateProbeManifest - Generates the types and returns a marshalled probe attribute configuration +func (p *probeService) GenerateProbeManifest(probe *model.Probe, mode model.Mode) (string, error) { + if probe.Type == model.ProbeTypeHTTPProbe { + + httpProbeURL := probe.KubernetesHTTPProperties.URL + var _probe HTTPProbeAttributes + + _probe.Name = probe.Name + _probe.Type = string(probe.Type) + _probe.Mode = string(mode) + + if probe.KubernetesHTTPProperties.InsecureSkipVerify != nil { + _probe.HTTPProbeInputs = v1alpha1.HTTPProbeInputs{ + InsecureSkipVerify: *probe.KubernetesHTTPProperties.InsecureSkipVerify, + } + } + + if probe.KubernetesHTTPProperties.Method.Get != nil { + _probe.HTTPProbeInputs = v1alpha1.HTTPProbeInputs{ + URL: httpProbeURL, + Method: v1alpha1.HTTPMethod{ + Get: &v1alpha1.GetMethod{ + Criteria: probe.KubernetesHTTPProperties.Method.Get.Criteria, + ResponseCode: probe.KubernetesHTTPProperties.Method.Get.ResponseCode, + }, + }, + } + } else if probe.KubernetesHTTPProperties.Method.Post != nil { + _probe.HTTPProbeInputs = v1alpha1.HTTPProbeInputs{ + URL: httpProbeURL, + Method: v1alpha1.HTTPMethod{ + Post: &v1alpha1.PostMethod{ + Criteria: probe.KubernetesHTTPProperties.Method.Post.Criteria, + ResponseCode: probe.KubernetesHTTPProperties.Method.Post.ResponseCode, + }, + }, + } + + if probe.KubernetesHTTPProperties.Method.Post.ContentType != nil { + _probe.HTTPProbeInputs.Method.Post.ContentType = *probe.KubernetesHTTPProperties.Method.Post.ContentType + } + + if probe.KubernetesHTTPProperties.Method.Post.Body != nil { + _probe.HTTPProbeInputs.Method.Post.Body = *probe.KubernetesHTTPProperties.Method.Post.Body + } + + if probe.KubernetesHTTPProperties.Method.Post.BodyPath != nil { + _probe.HTTPProbeInputs.Method.Post.BodyPath = *probe.KubernetesHTTPProperties.Method.Post.BodyPath + } + } + + _probe.RunProperties = v1alpha1.RunProperty{ + ProbeTimeout: probe.KubernetesHTTPProperties.ProbeTimeout, + Interval: probe.KubernetesHTTPProperties.Interval, + } + + if probe.KubernetesHTTPProperties.Attempt != nil { + _probe.RunProperties.Attempt = *probe.KubernetesHTTPProperties.Attempt + } + + if probe.KubernetesHTTPProperties.Retry != nil { + _probe.RunProperties.Retry = *probe.KubernetesHTTPProperties.Retry + } + + if probe.KubernetesHTTPProperties.ProbePollingInterval != nil { + _probe.RunProperties.ProbePollingInterval = *probe.KubernetesHTTPProperties.ProbePollingInterval + } + + if probe.KubernetesHTTPProperties.EvaluationTimeout != nil { + _probe.RunProperties.EvaluationTimeout = *probe.KubernetesHTTPProperties.EvaluationTimeout + } + + if probe.KubernetesHTTPProperties.StopOnFailure != nil { + _probe.RunProperties.StopOnFailure = *probe.KubernetesHTTPProperties.StopOnFailure + } + + y, err := json.Marshal(_probe) + if err != nil { + return "", err + } + + return string(y), err + } else if probe.Type == model.ProbeTypeCmdProbe { + + var _probe CMDProbeAttributes + + _probe.Name = probe.Name + _probe.Type = string(probe.Type) + _probe.Mode = string(mode) + _probe.CmdProbeInputs = v1alpha1.CmdProbeInputs{ + Command: probe.KubernetesCMDProperties.Command, + Comparator: v1alpha1.ComparatorInfo{ + Type: probe.KubernetesCMDProperties.Comparator.Type, + Criteria: probe.KubernetesCMDProperties.Comparator.Criteria, + Value: probe.KubernetesCMDProperties.Comparator.Value, + }, + } + + _probe.RunProperties = v1alpha1.RunProperty{ + ProbeTimeout: probe.KubernetesCMDProperties.ProbeTimeout, + Interval: probe.KubernetesCMDProperties.Interval, + } + + if probe.KubernetesCMDProperties.Attempt != nil { + _probe.RunProperties.Attempt = *probe.KubernetesCMDProperties.Attempt + } + + if probe.KubernetesCMDProperties.Retry != nil { + _probe.RunProperties.Retry = *probe.KubernetesCMDProperties.Retry + } + + if probe.KubernetesCMDProperties.ProbePollingInterval != nil { + _probe.RunProperties.ProbePollingInterval = *probe.KubernetesCMDProperties.ProbePollingInterval + } + + if probe.KubernetesCMDProperties.EvaluationTimeout != nil { + _probe.RunProperties.EvaluationTimeout = *probe.KubernetesCMDProperties.EvaluationTimeout + } + + if probe.KubernetesCMDProperties.InitialDelay != nil { + _probe.RunProperties.InitialDelay = *probe.KubernetesCMDProperties.InitialDelay + } + + if probe.KubernetesCMDProperties.StopOnFailure != nil { + _probe.RunProperties.StopOnFailure = *probe.KubernetesCMDProperties.StopOnFailure + } + + y, err := json.Marshal(_probe) + if err != nil { + return "", err + } + + return string(y), err + } else if probe.Type == model.ProbeTypePromProbe { + + var _probe PROMProbeAttributes + + _probe.Name = probe.Name + _probe.Type = string(probe.Type) + _probe.Mode = string(mode) + _probe.PromProbeInputs = v1alpha1.PromProbeInputs{ + Endpoint: probe.PromProperties.Endpoint, + Comparator: v1alpha1.ComparatorInfo{ + Type: probe.PromProperties.Comparator.Type, + Criteria: probe.PromProperties.Comparator.Criteria, + Value: probe.PromProperties.Comparator.Value, + }, + } + + if probe.PromProperties.Query != nil { + _probe.PromProbeInputs.Query = *probe.PromProperties.Query + } + + if probe.PromProperties.QueryPath != nil { + _probe.PromProbeInputs.QueryPath = *probe.PromProperties.QueryPath + } + + _probe.RunProperties = v1alpha1.RunProperty{ + ProbeTimeout: probe.PromProperties.ProbeTimeout, + Interval: probe.PromProperties.Interval, + } + + if probe.PromProperties.Attempt != nil { + _probe.RunProperties.Attempt = *probe.PromProperties.Attempt + } + + if probe.PromProperties.Retry != nil { + _probe.RunProperties.Retry = *probe.PromProperties.Retry + } + + if probe.PromProperties.ProbePollingInterval != nil { + _probe.RunProperties.ProbePollingInterval = *probe.PromProperties.ProbePollingInterval + } + + if probe.PromProperties.EvaluationTimeout != nil { + _probe.RunProperties.EvaluationTimeout = *probe.PromProperties.EvaluationTimeout + } + + if probe.PromProperties.InitialDelay != nil { + _probe.RunProperties.InitialDelay = *probe.PromProperties.InitialDelay + } + + if probe.PromProperties.StopOnFailure != nil { + _probe.RunProperties.StopOnFailure = *probe.PromProperties.StopOnFailure + } + + y, err := json.Marshal(_probe) + if err != nil { + return "", err + } + + return string(y), err + } else if probe.Type == model.ProbeTypeK8sProbe { + + var _probe K8SProbeAttributes + + _probe.Name = probe.Name + _probe.Type = string(probe.Type) + _probe.Mode = string(mode) + _probe.K8sProbeInputs.Version = probe.K8sProperties.Version + _probe.K8sProbeInputs.Resource = probe.K8sProperties.Resource + _probe.K8sProbeInputs.Operation = probe.K8sProperties.Operation + + if probe.K8sProperties.Group != nil { + _probe.K8sProbeInputs.Group = *probe.K8sProperties.Group + } + + if probe.K8sProperties.Namespace != nil { + _probe.K8sProbeInputs.Namespace = *probe.K8sProperties.Namespace + } + + if probe.K8sProperties.FieldSelector != nil { + _probe.K8sProbeInputs.FieldSelector = *probe.K8sProperties.FieldSelector + } + + if probe.K8sProperties.LabelSelector != nil { + _probe.K8sProbeInputs.LabelSelector = *probe.K8sProperties.LabelSelector + } + + _probe.RunProperties = v1alpha1.RunProperty{ + ProbeTimeout: probe.K8sProperties.ProbeTimeout, + Interval: probe.K8sProperties.Interval, + } + + if probe.K8sProperties.Attempt != nil { + _probe.RunProperties.Attempt = *probe.K8sProperties.Attempt + } + + if probe.K8sProperties.Retry != nil { + _probe.RunProperties.Retry = *probe.K8sProperties.Retry + } + + if probe.K8sProperties.ProbePollingInterval != nil { + _probe.RunProperties.ProbePollingInterval = *probe.K8sProperties.ProbePollingInterval + } + + if probe.K8sProperties.EvaluationTimeout != nil { + _probe.RunProperties.EvaluationTimeout = *probe.K8sProperties.EvaluationTimeout + } + + if probe.K8sProperties.InitialDelay != nil { + _probe.RunProperties.InitialDelay = *probe.K8sProperties.InitialDelay + } + + if probe.K8sProperties.StopOnFailure != nil { + _probe.RunProperties.StopOnFailure = *probe.K8sProperties.StopOnFailure + } + + y, err := json.Marshal(_probe) + if err != nil { + return "", err + } + + return string(y), err + } + return "", nil +} diff --git a/chaoscenter/graphql/server/pkg/probe/utils/types.go b/chaoscenter/graphql/server/pkg/probe/handler/types.go similarity index 99% rename from chaoscenter/graphql/server/pkg/probe/utils/types.go rename to chaoscenter/graphql/server/pkg/probe/handler/types.go index d45ce7db1b6..8052733fda7 100644 --- a/chaoscenter/graphql/server/pkg/probe/utils/types.go +++ b/chaoscenter/graphql/server/pkg/probe/handler/types.go @@ -1,4 +1,4 @@ -package utils +package handler import ( "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1" diff --git a/chaoscenter/graphql/server/pkg/probe/model/mocks/service.go b/chaoscenter/graphql/server/pkg/probe/model/mocks/service.go new file mode 100644 index 00000000000..d06f611ae3d --- /dev/null +++ b/chaoscenter/graphql/server/pkg/probe/model/mocks/service.go @@ -0,0 +1,76 @@ +package mocks + +import ( + "context" + + "github.com/stretchr/testify/mock" + + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + + "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" +) + +// ProbeService is an autogenerated mock type for the Service type +type ProbeService struct { + mock.Mock +} + +// AddProbe provides a mock function with given fields: ctx, probe, projectID +func (_m *ProbeService) AddProbe(ctx context.Context, probe model.ProbeRequest, projectID string) (*model.Probe, error) { + ret := _m.Called(ctx, probe, projectID) + return ret.Get(0).(*model.Probe), ret.Error(1) +} + +// DeleteProbe provides a mock function with given fields: ctx, probeName, projectID +func (_m *ProbeService) DeleteProbe(ctx context.Context, probeName string, projectID string) (bool, error) { + ret := _m.Called(ctx, probeName, projectID) + return ret.Get(0).(bool), ret.Error(1) +} + +// GenerateCronExperimentManifestWithProbes provides a mock function with given fields: manifest, projectID +func (_m *ProbeService) GenerateCronExperimentManifestWithProbes(manifest string, projectID string) (v1alpha1.CronWorkflow, error) { + ret := _m.Called(manifest, projectID) + return ret.Get(0).(v1alpha1.CronWorkflow), ret.Error(1) +} + +// GenerateExperimentManifestWithProbes provides a mock function with given fields: manifest, projectID +func (_m *ProbeService) GenerateExperimentManifestWithProbes(manifest string, projectID string) (v1alpha1.Workflow, error) { + ret := _m.Called(manifest, projectID) + return ret.Get(0).(v1alpha1.Workflow), ret.Error(1) +} + +// GetProbe provides a mock function with given fields: ctx, probeName, projectID +func (_m *ProbeService) GetProbe(ctx context.Context, probeName string, projectID string) (*model.Probe, error) { + ret := _m.Called(ctx, probeName, projectID) + return ret.Get(0).(*model.Probe), ret.Error(1) +} + +// GetProbeReference provides a mock function with given fields: ctx, probeName, projectID +func (_m *ProbeService) GetProbeReference(ctx context.Context, probeName string, projectID string) (*model.GetProbeReferenceResponse, error) { + ret := _m.Called(ctx, probeName, projectID) + return ret.Get(0).(*model.GetProbeReferenceResponse), ret.Error(1) +} + +// GetProbeYAMLData provides a mock function with given fields: ctx, probe, projectID +func (_m *ProbeService) GetProbeYAMLData(ctx context.Context, probe model.GetProbeYAMLRequest, projectID string) (string, error) { + ret := _m.Called(ctx, probe, projectID) + return ret.Get(0).(string), ret.Error(1) +} + +// ListProbes provides a mock function with given fields: ctx, probeNames, infrastructureType, filter, projectID +func (_m *ProbeService) ListProbes(ctx context.Context, probeNames []string, infrastructureType *model.InfrastructureType, filter *model.ProbeFilterInput, projectID string) ([]*model.Probe, error) { + ret := _m.Called(ctx, probeNames, infrastructureType, filter, projectID) + return ret.Get(0).([]*model.Probe), ret.Error(1) +} + +// UpdateProbe provides a mock function with given fields: ctx, probe, projectID +func (_m *ProbeService) UpdateProbe(ctx context.Context, probe model.ProbeRequest, projectID string) (string, error) { + ret := _m.Called(ctx, probe, projectID) + return ret.Get(0).(string), ret.Error(1) +} + +// ValidateUniqueProbe provides a mock function with given fields: ctx, probeName, projectID +func (_m *ProbeService) ValidateUniqueProbe(ctx context.Context, probeName string, projectID string) (bool, error) { + ret := _m.Called(ctx, probeName, projectID) + return ret.Get(0).(bool), ret.Error(1) +} diff --git a/chaoscenter/graphql/server/pkg/probe/utils/utils.go b/chaoscenter/graphql/server/pkg/probe/utils/utils.go index bd378c33249..6fb6ee2603d 100644 --- a/chaoscenter/graphql/server/pkg/probe/utils/utils.go +++ b/chaoscenter/graphql/server/pkg/probe/utils/utils.go @@ -1,12 +1,10 @@ package utils import ( - "context" "encoding/json" "errors" "fmt" "strings" - "time" argoTypes "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" "github.com/ghodss/yaml" @@ -220,264 +218,6 @@ func AddK8SProbeProperties(newProbe *dbSchemaProbe.Probe, request model.ProbeReq return newProbe } -// GenerateProbeManifest - Generates the types and returns a marshalled probe attribute configuration -func GenerateProbeManifest(probe *model.Probe, mode model.Mode) (string, error) { - if probe.Type == model.ProbeTypeHTTPProbe { - - httpProbeURL := probe.KubernetesHTTPProperties.URL - var _probe HTTPProbeAttributes - - _probe.Name = probe.Name - _probe.Type = string(probe.Type) - _probe.Mode = string(mode) - - if probe.KubernetesHTTPProperties.InsecureSkipVerify != nil { - _probe.HTTPProbeInputs = v1alpha1.HTTPProbeInputs{ - InsecureSkipVerify: *probe.KubernetesHTTPProperties.InsecureSkipVerify, - } - } - - if probe.KubernetesHTTPProperties.Method.Get != nil { - _probe.HTTPProbeInputs = v1alpha1.HTTPProbeInputs{ - URL: httpProbeURL, - Method: v1alpha1.HTTPMethod{ - Get: &v1alpha1.GetMethod{ - Criteria: probe.KubernetesHTTPProperties.Method.Get.Criteria, - ResponseCode: probe.KubernetesHTTPProperties.Method.Get.ResponseCode, - }, - }, - } - } else if probe.KubernetesHTTPProperties.Method.Post != nil { - _probe.HTTPProbeInputs = v1alpha1.HTTPProbeInputs{ - URL: httpProbeURL, - Method: v1alpha1.HTTPMethod{ - Post: &v1alpha1.PostMethod{ - Criteria: probe.KubernetesHTTPProperties.Method.Post.Criteria, - ResponseCode: probe.KubernetesHTTPProperties.Method.Post.ResponseCode, - }, - }, - } - - if probe.KubernetesHTTPProperties.Method.Post.ContentType != nil { - _probe.HTTPProbeInputs.Method.Post.ContentType = *probe.KubernetesHTTPProperties.Method.Post.ContentType - } - - if probe.KubernetesHTTPProperties.Method.Post.Body != nil { - _probe.HTTPProbeInputs.Method.Post.Body = *probe.KubernetesHTTPProperties.Method.Post.Body - } - - if probe.KubernetesHTTPProperties.Method.Post.BodyPath != nil { - _probe.HTTPProbeInputs.Method.Post.BodyPath = *probe.KubernetesHTTPProperties.Method.Post.BodyPath - } - } - - _probe.RunProperties = v1alpha1.RunProperty{ - ProbeTimeout: probe.KubernetesHTTPProperties.ProbeTimeout, - Interval: probe.KubernetesHTTPProperties.Interval, - } - - if probe.KubernetesHTTPProperties.Attempt != nil { - _probe.RunProperties.Attempt = *probe.KubernetesHTTPProperties.Attempt - } - - if probe.KubernetesHTTPProperties.Retry != nil { - _probe.RunProperties.Retry = *probe.KubernetesHTTPProperties.Retry - } - - if probe.KubernetesHTTPProperties.ProbePollingInterval != nil { - _probe.RunProperties.ProbePollingInterval = *probe.KubernetesHTTPProperties.ProbePollingInterval - } - - if probe.KubernetesHTTPProperties.EvaluationTimeout != nil { - _probe.RunProperties.EvaluationTimeout = *probe.KubernetesHTTPProperties.EvaluationTimeout - } - - if probe.KubernetesHTTPProperties.StopOnFailure != nil { - _probe.RunProperties.StopOnFailure = *probe.KubernetesHTTPProperties.StopOnFailure - } - - y, err := json.Marshal(_probe) - if err != nil { - return "", err - } - - return string(y), err - } else if probe.Type == model.ProbeTypeCmdProbe { - - var _probe CMDProbeAttributes - - _probe.Name = probe.Name - _probe.Type = string(probe.Type) - _probe.Mode = string(mode) - _probe.CmdProbeInputs = v1alpha1.CmdProbeInputs{ - Command: probe.KubernetesCMDProperties.Command, - Comparator: v1alpha1.ComparatorInfo{ - Type: probe.KubernetesCMDProperties.Comparator.Type, - Criteria: probe.KubernetesCMDProperties.Comparator.Criteria, - Value: probe.KubernetesCMDProperties.Comparator.Value, - }, - } - - _probe.RunProperties = v1alpha1.RunProperty{ - ProbeTimeout: probe.KubernetesCMDProperties.ProbeTimeout, - Interval: probe.KubernetesCMDProperties.Interval, - } - - if probe.KubernetesCMDProperties.Attempt != nil { - _probe.RunProperties.Attempt = *probe.KubernetesCMDProperties.Attempt - } - - if probe.KubernetesCMDProperties.Retry != nil { - _probe.RunProperties.Retry = *probe.KubernetesCMDProperties.Retry - } - - if probe.KubernetesCMDProperties.ProbePollingInterval != nil { - _probe.RunProperties.ProbePollingInterval = *probe.KubernetesCMDProperties.ProbePollingInterval - } - - if probe.KubernetesCMDProperties.EvaluationTimeout != nil { - _probe.RunProperties.EvaluationTimeout = *probe.KubernetesCMDProperties.EvaluationTimeout - } - - if probe.KubernetesCMDProperties.InitialDelay != nil { - _probe.RunProperties.InitialDelay = *probe.KubernetesCMDProperties.InitialDelay - } - - if probe.KubernetesCMDProperties.StopOnFailure != nil { - _probe.RunProperties.StopOnFailure = *probe.KubernetesCMDProperties.StopOnFailure - } - - y, err := json.Marshal(_probe) - if err != nil { - return "", err - } - - return string(y), err - } else if probe.Type == model.ProbeTypePromProbe { - - var _probe PROMProbeAttributes - - _probe.Name = probe.Name - _probe.Type = string(probe.Type) - _probe.Mode = string(mode) - _probe.PromProbeInputs = v1alpha1.PromProbeInputs{ - Endpoint: probe.PromProperties.Endpoint, - Comparator: v1alpha1.ComparatorInfo{ - Type: probe.PromProperties.Comparator.Type, - Criteria: probe.PromProperties.Comparator.Criteria, - Value: probe.PromProperties.Comparator.Value, - }, - } - - if probe.PromProperties.Query != nil { - _probe.PromProbeInputs.Query = *probe.PromProperties.Query - } - - if probe.PromProperties.QueryPath != nil { - _probe.PromProbeInputs.QueryPath = *probe.PromProperties.QueryPath - } - - _probe.RunProperties = v1alpha1.RunProperty{ - ProbeTimeout: probe.PromProperties.ProbeTimeout, - Interval: probe.PromProperties.Interval, - } - - if probe.PromProperties.Attempt != nil { - _probe.RunProperties.Attempt = *probe.PromProperties.Attempt - } - - if probe.PromProperties.Retry != nil { - _probe.RunProperties.Retry = *probe.PromProperties.Retry - } - - if probe.PromProperties.ProbePollingInterval != nil { - _probe.RunProperties.ProbePollingInterval = *probe.PromProperties.ProbePollingInterval - } - - if probe.PromProperties.EvaluationTimeout != nil { - _probe.RunProperties.EvaluationTimeout = *probe.PromProperties.EvaluationTimeout - } - - if probe.PromProperties.InitialDelay != nil { - _probe.RunProperties.InitialDelay = *probe.PromProperties.InitialDelay - } - - if probe.PromProperties.StopOnFailure != nil { - _probe.RunProperties.StopOnFailure = *probe.PromProperties.StopOnFailure - } - - y, err := json.Marshal(_probe) - if err != nil { - return "", err - } - - return string(y), err - } else if probe.Type == model.ProbeTypeK8sProbe { - - var _probe K8SProbeAttributes - - _probe.Name = probe.Name - _probe.Type = string(probe.Type) - _probe.Mode = string(mode) - _probe.K8sProbeInputs.Version = probe.K8sProperties.Version - _probe.K8sProbeInputs.Resource = probe.K8sProperties.Resource - _probe.K8sProbeInputs.Operation = probe.K8sProperties.Operation - - if probe.K8sProperties.Group != nil { - _probe.K8sProbeInputs.Group = *probe.K8sProperties.Group - } - - if probe.K8sProperties.Namespace != nil { - _probe.K8sProbeInputs.Namespace = *probe.K8sProperties.Namespace - } - - if probe.K8sProperties.FieldSelector != nil { - _probe.K8sProbeInputs.FieldSelector = *probe.K8sProperties.FieldSelector - } - - if probe.K8sProperties.LabelSelector != nil { - _probe.K8sProbeInputs.LabelSelector = *probe.K8sProperties.LabelSelector - } - - _probe.RunProperties = v1alpha1.RunProperty{ - ProbeTimeout: probe.K8sProperties.ProbeTimeout, - Interval: probe.K8sProperties.Interval, - } - - if probe.K8sProperties.Attempt != nil { - _probe.RunProperties.Attempt = *probe.K8sProperties.Attempt - } - - if probe.K8sProperties.Retry != nil { - _probe.RunProperties.Retry = *probe.K8sProperties.Retry - } - - if probe.K8sProperties.ProbePollingInterval != nil { - _probe.RunProperties.ProbePollingInterval = *probe.K8sProperties.ProbePollingInterval - } - - if probe.K8sProperties.EvaluationTimeout != nil { - _probe.RunProperties.EvaluationTimeout = *probe.K8sProperties.EvaluationTimeout - } - - if probe.K8sProperties.InitialDelay != nil { - _probe.RunProperties.InitialDelay = *probe.K8sProperties.InitialDelay - } - - if probe.K8sProperties.StopOnFailure != nil { - _probe.RunProperties.StopOnFailure = *probe.K8sProperties.StopOnFailure - } - - y, err := json.Marshal(_probe) - if err != nil { - return "", err - } - - return string(y), err - } - return "", nil -} - // ParseProbesFromManifest - Parses the manifest to return probes which is stored in the DB func ParseProbesFromManifest(wfType *dbChaosExperiment.ChaosExperimentType, manifest string) ([]dbChaosExperiment.Probes, error) { var ( @@ -696,313 +436,6 @@ func ParseProbesFromManifestForRuns(wfType *dbChaosExperiment.ChaosExperimentTyp return probes, nil } -// GenerateExperimentManifestWithProbes - uses GenerateProbeManifest to get and store the respective probe attribute into Raw Data template for Non Cron Workflow -func GenerateExperimentManifestWithProbes(manifest string, projectID string) (argoTypes.Workflow, error) { - var ( - backgroundContext = context.Background() - nonCronManifest argoTypes.Workflow - ) - - ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second) - defer cancel() - - err := json.Unmarshal([]byte(manifest), &nonCronManifest) - if err != nil { - return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal experiment manifest, error: %s", err.Error()) - } - - for i, template := range nonCronManifest.Spec.Templates { - artifact := template.Inputs.Artifacts - - if len(artifact) > 0 { - if artifact[0].Raw == nil { - continue - } - data := artifact[0].Raw.Data - if len(data) > 0 { - var ( - meta v1alpha1.ChaosEngine - annotation = make(map[string]string) - probes []v1alpha1.ProbeAttributes - httpProbe HTTPProbeAttributes - cmdProbe CMDProbeAttributes - promProbe PROMProbeAttributes - k8sProbe K8SProbeAttributes - ) - - err := yaml.Unmarshal([]byte(data), &meta) - if err != nil { - return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal chaosengine, error: %s", err.Error()) - } - if strings.ToLower(meta.Kind) == "chaosengine" { - - probes = meta.Spec.Experiments[0].Spec.Probe - - if meta.Annotations != nil { - annotation = meta.Annotations - } - - for _, key := range annotation { - var manifestAnnotation []dbChaosExperiment.ProbeAnnotations - if strings.HasPrefix(key, "[{\"name\"") { - err := json.Unmarshal([]byte(key), &manifestAnnotation) - if err != nil { - return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal experiment annotation object, error: %s", err.Error()) - } - for _, annotationKey := range manifestAnnotation { - probe, err := dbSchemaProbe.GetProbeByName(ctx, annotationKey.Name, projectID) - if err != nil { - return argoTypes.Workflow{}, fmt.Errorf("failed to fetch probe details, error: %s", err.Error()) - } - probeManifestString, err := GenerateProbeManifest(probe.GetOutputProbe(), annotationKey.Mode) - if err != nil { - return argoTypes.Workflow{}, fmt.Errorf("failed to generate probe manifest, error: %s", err.Error()) - } - - if model.ProbeType(probe.Type) == model.ProbeTypeHTTPProbe { - err := json.Unmarshal([]byte(probeManifestString), &httpProbe) - if err != nil { - return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal http probe, error: %s", err.Error()) - } - - probes = append(probes, v1alpha1.ProbeAttributes{ - Name: httpProbe.Name, - Type: httpProbe.Type, - HTTPProbeInputs: &v1alpha1.HTTPProbeInputs{ - URL: httpProbe.HTTPProbeInputs.URL, - InsecureSkipVerify: httpProbe.HTTPProbeInputs.InsecureSkipVerify, - Method: httpProbe.HTTPProbeInputs.Method, - }, - RunProperties: httpProbe.RunProperties, - Mode: httpProbe.Mode, - }) - } else if model.ProbeType(probe.Type) == model.ProbeTypeCmdProbe { - err := json.Unmarshal([]byte(probeManifestString), &cmdProbe) - if err != nil { - return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal cmd probe, error: %s", err.Error()) - } - - probes = append(probes, v1alpha1.ProbeAttributes{ - Name: cmdProbe.Name, - Type: cmdProbe.Type, - CmdProbeInputs: &v1alpha1.CmdProbeInputs{ - Command: cmdProbe.CmdProbeInputs.Command, - Comparator: cmdProbe.CmdProbeInputs.Comparator, - }, - RunProperties: cmdProbe.RunProperties, - Mode: cmdProbe.Mode, - }) - } else if model.ProbeType(probe.Type) == model.ProbeTypePromProbe { - err := json.Unmarshal([]byte(probeManifestString), &promProbe) - if err != nil { - return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal prom probe, error: %s", err.Error()) - } - - probes = append(probes, v1alpha1.ProbeAttributes{ - Name: promProbe.Name, - Type: promProbe.Type, - PromProbeInputs: &v1alpha1.PromProbeInputs{ - Endpoint: promProbe.PromProbeInputs.Endpoint, - Query: promProbe.PromProbeInputs.Query, - QueryPath: promProbe.PromProbeInputs.QueryPath, - Comparator: promProbe.PromProbeInputs.Comparator, - }, - RunProperties: promProbe.RunProperties, - Mode: promProbe.Mode, - }) - } else if model.ProbeType(probe.Type) == model.ProbeTypeK8sProbe { - err := json.Unmarshal([]byte(probeManifestString), &k8sProbe) - if err != nil { - return argoTypes.Workflow{}, fmt.Errorf("failed to unmarshal k8s probe, error: %s", err.Error()) - } - - probes = append(probes, v1alpha1.ProbeAttributes{ - Name: k8sProbe.Name, - Type: k8sProbe.Type, - K8sProbeInputs: &v1alpha1.K8sProbeInputs{ - Group: k8sProbe.K8sProbeInputs.Group, - Version: k8sProbe.K8sProbeInputs.Version, - Resource: k8sProbe.K8sProbeInputs.Resource, - ResourceNames: k8sProbe.K8sProbeInputs.ResourceNames, - Namespace: k8sProbe.K8sProbeInputs.Namespace, - FieldSelector: k8sProbe.K8sProbeInputs.FieldSelector, - LabelSelector: k8sProbe.K8sProbeInputs.LabelSelector, - Operation: k8sProbe.K8sProbeInputs.Operation, - }, - RunProperties: k8sProbe.RunProperties, - Mode: k8sProbe.Mode, - }) - } - } - } - } - - if len(meta.Spec.Experiments) > 0 { - meta.Spec.Experiments[0].Spec.Probe = probes - } - - res, err := yaml.Marshal(&meta) - if err != nil { - return argoTypes.Workflow{}, errors.New("failed to marshal chaosengine") - } - nonCronManifest.Spec.Templates[i].Inputs.Artifacts[0].Raw.Data = string(res) - } - } - } - } - - return nonCronManifest, nil -} - -// GenerateCronExperimentManifestWithProbes - uses GenerateProbeManifest to get and store the respective probe attribute into Raw Data template -func GenerateCronExperimentManifestWithProbes(manifest string, projectID string) (argoTypes.CronWorkflow, error) { - var ( - backgroundContext = context.Background() - cronManifest argoTypes.CronWorkflow - ) - - ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second) - defer cancel() - - if err := json.Unmarshal([]byte(manifest), &cronManifest); err != nil { - return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal experiment manifest, error: %s", err.Error()) - } - - for i, template := range cronManifest.Spec.WorkflowSpec.Templates { - artifact := template.Inputs.Artifacts - - if len(artifact) > 0 { - if artifact[0].Raw == nil { - continue - } - data := artifact[0].Raw.Data - if len(data) > 0 { - var ( - meta v1alpha1.ChaosEngine - annotation = make(map[string]string) - probes []v1alpha1.ProbeAttributes - httpProbe HTTPProbeAttributes - cmdProbe CMDProbeAttributes - promProbe PROMProbeAttributes - k8sProbe K8SProbeAttributes - ) - - if err := yaml.Unmarshal([]byte(data), &meta); err != nil { - return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal chaosengine, error: %s", err.Error()) - } - - if strings.ToLower(meta.Kind) == "chaosengine" { - - probes = meta.Spec.Experiments[0].Spec.Probe - - if meta.Annotations != nil { - annotation = meta.Annotations - } - - for _, key := range annotation { - var manifestAnnotation []dbChaosExperiment.ProbeAnnotations - err := json.Unmarshal([]byte(key), &manifestAnnotation) - if err != nil { - return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal experiment annotation object, error: %s", err.Error()) - } - for _, annotationKey := range manifestAnnotation { - probe, err := dbSchemaProbe.GetProbeByName(ctx, annotationKey.Name, projectID) - if err != nil { - return argoTypes.CronWorkflow{}, fmt.Errorf("failed to fetch probe details, error: %s", err.Error()) - } - - probeManifestString, err := GenerateProbeManifest(probe.GetOutputProbe(), annotationKey.Mode) - - if model.ProbeType(probe.Type) == model.ProbeTypeHTTPProbe { - if err := json.Unmarshal([]byte(probeManifestString), &httpProbe); err != nil { - return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal http probe, error: %s", err.Error()) - } - - probes = append(probes, v1alpha1.ProbeAttributes{ - Name: httpProbe.Name, - Type: httpProbe.Type, - HTTPProbeInputs: &v1alpha1.HTTPProbeInputs{ - URL: httpProbe.HTTPProbeInputs.URL, - InsecureSkipVerify: httpProbe.HTTPProbeInputs.InsecureSkipVerify, - Method: httpProbe.HTTPProbeInputs.Method, - }, - RunProperties: httpProbe.RunProperties, - Mode: httpProbe.Mode, - }) - } else if model.ProbeType(probe.Type) == model.ProbeTypeCmdProbe { - if err := json.Unmarshal([]byte(probeManifestString), &cmdProbe); err != nil { - return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal cmd probe, error: %s", err.Error()) - } - - probes = append(probes, v1alpha1.ProbeAttributes{ - Name: cmdProbe.Name, - Type: cmdProbe.Type, - CmdProbeInputs: &v1alpha1.CmdProbeInputs{ - Command: cmdProbe.CmdProbeInputs.Command, - Comparator: cmdProbe.CmdProbeInputs.Comparator, - }, - RunProperties: cmdProbe.RunProperties, - Mode: cmdProbe.Mode, - }) - } else if model.ProbeType(probe.Type) == model.ProbeTypePromProbe { - if err := json.Unmarshal([]byte(probeManifestString), &promProbe); err != nil { - return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal prom probe, error: %s", err.Error()) - } - - probes = append(probes, v1alpha1.ProbeAttributes{ - Name: promProbe.Name, - Type: promProbe.Type, - PromProbeInputs: &v1alpha1.PromProbeInputs{ - Endpoint: promProbe.PromProbeInputs.Endpoint, - Query: promProbe.PromProbeInputs.Query, - QueryPath: promProbe.PromProbeInputs.QueryPath, - Comparator: promProbe.PromProbeInputs.Comparator, - }, - RunProperties: promProbe.RunProperties, - Mode: promProbe.Mode, - }) - } else if model.ProbeType(probe.Type) == model.ProbeTypeK8sProbe { - if err := json.Unmarshal([]byte(probeManifestString), &k8sProbe); err != nil { - return argoTypes.CronWorkflow{}, fmt.Errorf("failed to unmarshal k8s probe, error: %s", err.Error()) - } - - probes = append(probes, v1alpha1.ProbeAttributes{ - Name: k8sProbe.Name, - Type: k8sProbe.Type, - K8sProbeInputs: &v1alpha1.K8sProbeInputs{ - Group: k8sProbe.K8sProbeInputs.Group, - Version: k8sProbe.K8sProbeInputs.Version, - Resource: k8sProbe.K8sProbeInputs.Resource, - ResourceNames: k8sProbe.K8sProbeInputs.ResourceNames, - Namespace: k8sProbe.K8sProbeInputs.Namespace, - FieldSelector: k8sProbe.K8sProbeInputs.FieldSelector, - LabelSelector: k8sProbe.K8sProbeInputs.LabelSelector, - Operation: k8sProbe.K8sProbeInputs.Operation, - }, - RunProperties: k8sProbe.RunProperties, - Mode: k8sProbe.Mode, - }) - } - } - } - - if len(meta.Spec.Experiments) > 0 { - meta.Spec.Experiments[0].Spec.Probe = probes - } - - res, err := yaml.Marshal(&meta) - if err != nil { - return argoTypes.CronWorkflow{}, fmt.Errorf("failed to marshal chaosengine, error: %s", err.Error()) - } - cronManifest.Spec.WorkflowSpec.Templates[i].Inputs.Artifacts[0].Raw.Data = string(res) - } - } - } - } - - return cronManifest, nil -} - func InsertProbeRefAnnotation(rawYaml, value string) (string, error) { var data interface{}