-
Notifications
You must be signed in to change notification settings - Fork 654
/
service.go
644 lines (559 loc) · 21.2 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
package gitops
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/ghodss/yaml"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
chaosExperimentOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/ops"
chaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_infrastructure"
dataStore "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store"
store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/gitops"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/grpc"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"go.mongodb.org/mongo-driver/bson"
grpc2 "google.golang.org/grpc"
)
const (
timeout = time.Second * 5
tempPath = "/tmp/gitops_test/"
)
var (
gitLock = NewGitLock()
backgroundContext = context.Background()
)
type Service interface {
GitOpsNotificationHandler(ctx context.Context, infra chaos_infrastructure.ChaosInfra, experimentID string) (string, error)
EnableGitOpsHandler(ctx context.Context, projectID string, config model.GitConfig) (bool, error)
DisableGitOpsHandler(ctx context.Context, projectID string) (bool, error)
UpdateGitOpsDetailsHandler(ctx context.Context, projectID string, config model.GitConfig) (bool, error)
GetGitOpsDetails(ctx context.Context, projectID string) (*model.GitConfigResponse, error)
UpsertExperimentToGit(ctx context.Context, projectID string, experiment *model.ChaosExperimentRequest) error
DeleteExperimentFromGit(ctx context.Context, projectID string, experiment *model.ChaosExperimentRequest) error
GitOpsSyncHandler(singleRun bool)
SyncDBToGit(ctx context.Context, config GitConfig) error
}
type gitOpsService struct {
gitOpsOperator *gitops.Operator
chaosExperimentOps chaos_experiment.Operator
chaosExperimentService chaosExperimentOps.Service
}
// NewGitOpsService returns a new instance of a gitOpsService
func NewGitOpsService(gitOpsOperator *gitops.Operator, chaosExperimentService chaosExperimentOps.Service, chaosExperimentOps chaos_experiment.Operator) Service {
return &gitOpsService{
gitOpsOperator: gitOpsOperator,
chaosExperimentService: chaosExperimentService,
chaosExperimentOps: chaosExperimentOps,
}
}
// GitOpsNotificationHandler sends experiment run request(single run experiment only) to agent on GitOps notification
func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra chaos_infrastructure.ChaosInfra, experimentID string) (string, error) {
gitLock.Lock(infra.ProjectID, nil)
defer gitLock.Unlock(infra.ProjectID, nil)
config, err := g.gitOpsOperator.GetGitConfig(ctx, infra.ProjectID)
if err != nil {
return "", errors.New("Cannot get Git Config from DB : " + err.Error())
}
if config == nil {
return "GitOps Disabled", nil
}
query := bson.D{{"infra_id", infra.InfraID}, {"experiment_id", experimentID}, {"is_removed", false}}
experiments, err := g.chaosExperimentOps.GetExperiments(query)
if err != nil {
log.Error("Could not get experiment :", err)
return "could not get experiment", err
}
if len(experiments) == 0 {
return "", errors.New("no such experiment found")
}
resKind := gjson.Get(experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest, "kind").String()
if strings.ToLower(resKind) == "cronexperiment" { // no op
return "Request Acknowledged for experimentID: " + experimentID, nil
}
experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest, err = sjson.Set(experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest, "metadata.name", experiments[0].Name+"-"+strconv.FormatInt(time.Now().UnixMilli(), 10))
if err != nil {
log.Error("Failed to updated experiment name :", err)
return "", errors.New("Failed to updated experiment name " + err.Error())
}
username := "git-ops"
chaosInfra.SendExperimentToSubscriber(experiments[0].ProjectID, &model.ChaosExperimentRequest{
ExperimentManifest: experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest,
InfraID: experiments[0].InfraID,
}, &username, nil, "create", store.Store)
return "Request Acknowledged for experimentID: " + experimentID, nil
}
// EnableGitOpsHandler enables GitOps for a particular project
func (g *gitOpsService) EnableGitOpsHandler(ctx context.Context, projectID string, config model.GitConfig) (bool, error) {
gitLock.Lock(projectID, nil)
defer gitLock.Unlock(projectID, nil)
gitLock.Lock(config.RepoURL, &config.Branch)
defer gitLock.Unlock(config.RepoURL, &config.Branch)
var conn *grpc2.ClientConn
client, conn := grpc.GetAuthGRPCSvcClient(conn)
defer func(conn *grpc2.ClientConn) {
err := conn.Close()
if err != nil {
log.Error("Failed to close connection : ", err)
}
}(conn)
_, err := grpc.GetProjectById(client, projectID)
if err != nil {
return false, errors.New("Failed to setup GitOps : " + err.Error())
}
log.Info("Enabling GitOps")
gitDB := gitops.GetGitConfigDB(projectID, config)
commit, err := SetupGitOps(GitUserFromContext(ctx), GetGitOpsConfig(gitDB))
if err != nil {
return false, errors.New("Failed to setup GitOps : " + err.Error())
}
gitDB.LatestCommit = commit
err = g.gitOpsOperator.AddGitConfig(ctx, &gitDB)
if err != nil {
return false, errors.New("Failed to enable GitOps in DB : " + err.Error())
}
return true, nil
}
// DisableGitOpsHandler disables GitOps for a specific project
func (g *gitOpsService) DisableGitOpsHandler(ctx context.Context, projectID string) (bool, error) {
gitLock.Lock(projectID, nil)
defer gitLock.Unlock(projectID, nil)
log.Info("Disabling GitOps")
err := g.gitOpsOperator.DeleteGitConfig(ctx, projectID)
if err != nil {
return false, errors.New("Failed to delete git config from DB : " + err.Error())
}
err = os.RemoveAll(DefaultPath + projectID)
if err != nil {
return false, errors.New("Failed to delete git repo from disk : " + err.Error())
}
return true, nil
}
// UpdateGitOpsDetailsHandler updates an exiting GitOps config for a project
func (g *gitOpsService) UpdateGitOpsDetailsHandler(ctx context.Context, projectID string, config model.GitConfig) (bool, error) {
gitLock.Lock(projectID, nil)
defer gitLock.Unlock(projectID, nil)
gitLock.Lock(config.RepoURL, &config.Branch)
defer gitLock.Unlock(config.RepoURL, &config.Branch)
existingConfig, err := g.gitOpsOperator.GetGitConfig(ctx, projectID)
if err != nil {
return false, errors.New("Cannot get Git Config from DB : " + err.Error())
}
if existingConfig == nil {
return false, errors.New("GitOps Disabled ")
}
log.Info("Enabling GitOps")
gitDB := gitops.GetGitConfigDB(projectID, config)
gitConfig := GetGitOpsConfig(gitDB)
originalPath := gitConfig.LocalPath
gitConfig.LocalPath = tempPath + gitConfig.ProjectID
commit, err := SetupGitOps(GitUserFromContext(ctx), gitConfig)
if err != nil {
return false, errors.New("Failed to setup GitOps : " + err.Error())
}
gitDB.LatestCommit = commit
err = g.gitOpsOperator.ReplaceGitConfig(ctx, bson.D{{"project_id", projectID}}, &gitDB)
if err != nil {
return false, errors.New("Failed to enable GitOps in DB : " + err.Error())
}
err = os.RemoveAll(originalPath)
if err != nil {
return false, errors.New("Cannot remove existing repo : " + err.Error())
}
err = os.Rename(gitConfig.LocalPath, originalPath)
if err != nil {
return false, errors.New("Cannot copy new repo : " + err.Error())
}
return true, nil
}
// GetGitOpsDetails returns the current GitOps config for the requested project
func (g *gitOpsService) GetGitOpsDetails(ctx context.Context, projectID string) (*model.GitConfigResponse, error) {
gitLock.Lock(projectID, nil)
defer gitLock.Unlock(projectID, nil)
config, err := g.gitOpsOperator.GetGitConfig(ctx, projectID)
if err != nil {
return nil, errors.New("Cannot get Git Config from DB : " + err.Error())
}
if config == nil {
return &model.GitConfigResponse{
ProjectID: projectID,
Enabled: false,
}, nil
}
resp := model.GitConfigResponse{
Enabled: true,
ProjectID: config.ProjectID,
Branch: &config.Branch,
RepoURL: &config.RepositoryURL,
AuthType: &config.AuthType,
}
switch config.AuthType {
case model.AuthTypeToken:
resp.Token = config.Token
case model.AuthTypeBasic:
resp.UserName = config.UserName
resp.Password = config.Password
case model.AuthTypeSSH:
resp.SSHPrivateKey = config.SSHPrivateKey
}
return &resp, nil
}
// UpsertExperimentToGit adds/updates experiment to git
func (g *gitOpsService) UpsertExperimentToGit(ctx context.Context, projectID string, experiment *model.ChaosExperimentRequest) error {
gitLock.Lock(projectID, nil)
defer gitLock.Unlock(projectID, nil)
config, err := g.gitOpsOperator.GetGitConfig(ctx, projectID)
if err != nil {
return errors.New("Cannot get Git Config from DB : " + err.Error())
}
if config == nil {
return nil
}
gitLock.Lock(config.RepositoryURL, &config.Branch)
defer gitLock.Unlock(config.RepositoryURL, &config.Branch)
gitConfig := GetGitOpsConfig(*config)
err = g.SyncDBToGit(ctx, gitConfig)
if err != nil {
return errors.New("Sync Error | " + err.Error())
}
experimentPath := gitConfig.LocalPath + "/" + ProjectDataPath + "/" + gitConfig.ProjectID + "/" + experiment.ExperimentName + ".yaml"
data, err := yaml.JSONToYAML([]byte(experiment.ExperimentManifest))
if err != nil {
return errors.New("Cannot convert manifest to yaml : " + err.Error())
}
err = os.WriteFile(experimentPath, data, 0644)
if err != nil {
return errors.New("Cannot write experiment to git : " + err.Error())
}
commit, err := gitConfig.GitCommit(GitUserFromContext(ctx), "Updated Experiment : "+experiment.ExperimentName, nil)
if err != nil {
return errors.New("Cannot commit experiment to git : " + err.Error())
}
err = gitConfig.GitPush()
if err != nil {
return errors.New("Cannot push experiment to git : " + err.Error())
}
query := bson.D{{"project_id", gitConfig.ProjectID}}
update := bson.D{{"$set", bson.D{{"latest_commit", commit}}}}
err = g.gitOpsOperator.UpdateGitConfig(ctx, query, update)
if err != nil {
return errors.New("Failed to update git config : " + err.Error())
}
return nil
}
// DeleteExperimentFromGit deletes experiment from git
func (g *gitOpsService) DeleteExperimentFromGit(ctx context.Context, projectID string, experiment *model.ChaosExperimentRequest) error {
log.Info("Deleting Experiment...")
gitLock.Lock(projectID, nil)
defer gitLock.Unlock(projectID, nil)
config, err := g.gitOpsOperator.GetGitConfig(ctx, projectID)
if err != nil {
return errors.New("Cannot get Git Config from DB : " + err.Error())
}
if config == nil {
return nil
}
gitLock.Lock(config.RepositoryURL, &config.Branch)
defer gitLock.Unlock(config.RepositoryURL, &config.Branch)
gitConfig := GetGitOpsConfig(*config)
err = g.SyncDBToGit(ctx, gitConfig)
if err != nil {
return errors.New("Sync Error | " + err.Error())
}
experimentPath := ProjectDataPath + "/" + gitConfig.ProjectID + "/" + experiment.ExperimentName + ".yaml"
exists, err := PathExists(gitConfig.LocalPath + "/" + experimentPath)
if err != nil {
return errors.New("Cannot delete experiment from git : " + err.Error())
}
if !exists {
log.Error("File not found in git : ", gitConfig.LocalPath+"/"+experimentPath)
return nil
}
err = os.RemoveAll(gitConfig.LocalPath + "/" + experimentPath)
if err != nil {
return errors.New("Cannot delete experiment from git : " + err.Error())
}
commit, err := gitConfig.GitCommit(GitUserFromContext(ctx), "Deleted Experiment : "+experiment.ExperimentName, &experimentPath)
if err != nil {
log.Error("Error", err)
return errors.New("Cannot commit experiment[delete] to git : " + err.Error())
}
err = gitConfig.GitPush()
if err != nil {
log.Error("Error", err)
return errors.New("Cannot push experiment[delete] to git : " + err.Error())
}
query := bson.D{{"project_id", gitConfig.ProjectID}}
update := bson.D{{"$set", bson.D{{"latest_commit", commit}}}}
err = g.gitOpsOperator.UpdateGitConfig(ctx, query, update)
if err != nil {
return errors.New("Failed to update git config : " + err.Error())
}
return nil
}
// GitSyncHelper sync a particular repo with DB
func (g *gitOpsService) gitSyncHelper(config gitops.GitConfigDB, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
gitLock.Lock(config.ProjectID, nil)
defer gitLock.Unlock(config.ProjectID, nil)
gitLock.Lock(config.RepositoryURL, &config.Branch)
defer gitLock.Unlock(config.RepositoryURL, &config.Branch)
ctx, cancel := context.WithTimeout(backgroundContext, timeout)
defer cancel()
// get most recent data from db after acquiring lock
conf, err := g.gitOpsOperator.GetGitConfig(ctx, config.ProjectID)
if err != nil {
log.Error("Repo Sync ERROR: ", config.ProjectID, err.Error())
}
if conf == nil {
return
}
gitConfig := GetGitOpsConfig(*conf)
err = g.SyncDBToGit(nil, gitConfig)
if err != nil {
log.Error("Repo Sync ERROR: ", conf.ProjectID, err.Error())
}
}
// GitOpsSyncHandler syncs all repos in the DB
func (g *gitOpsService) GitOpsSyncHandler(singleRun bool) {
const syncGroupSize = 10
const syncInterval = 2 * time.Minute
for {
ctx, cancel := context.WithTimeout(backgroundContext, timeout)
log.Info("Running GitOps DB Sync...")
configs, err := g.gitOpsOperator.GetAllGitConfig(ctx)
cancel()
if err != nil {
log.Error("Failed to get git configs from db : ", err) //condition
}
count := len(configs)
if count > 0 {
log.Info("Updating : ", configs) // condition
count = count - 1
for count >= 0 {
min := count - (syncGroupSize - 1)
if min < 0 {
min = 0
}
wg := sync.WaitGroup{}
wg.Add(count + 1 - min)
for count >= min {
go g.gitSyncHelper(configs[count], &wg)
count -= 1
}
wg.Wait()
}
log.Info("GitOps DB Sync Complete") //condition
}
if singleRun {
break
}
time.Sleep(syncInterval)
}
}
// SyncDBToGit syncs the DB with the GitRepo for the project
func (g *gitOpsService) SyncDBToGit(ctx context.Context, config GitConfig) error {
repositoryExists, err := PathExists(config.LocalPath)
if err != nil {
return fmt.Errorf("Error while checking repo exists, err: %s", err)
}
if !repositoryExists {
err = config.setupGitRepo(GitUserFromContext(ctx))
} else {
err = config.GitPull()
if err != nil {
return errors.New("Error syncing DB : " + err.Error())
}
}
latestCommit, files, err := config.GetChanges()
if err != nil {
return errors.New("Error Getting File Changes : " + err.Error())
}
if latestCommit == config.LatestCommit {
return nil
}
log.Info(latestCommit, " ", config.LatestCommit, "File Changes: ", files)
newExperiments := false
for file := range files {
if !strings.HasSuffix(file, ".yaml") {
continue
}
// check if file was deleted or not
exists, err := PathExists(config.LocalPath + "/" + file)
if err != nil {
return errors.New("Error checking file in local repo : " + file + " | " + err.Error())
}
if !exists {
err = g.deleteExperiment(file, config)
if err != nil {
log.Error("Error while deleting experiment db entry : " + file + " | " + err.Error())
continue
}
continue
}
// read changes [new additions/updates]
data, err := os.ReadFile(config.LocalPath + "/" + file)
if err != nil {
log.Error("Error reading data from git file : " + file + " | " + err.Error())
continue
}
data, err = yaml.YAMLToJSON(data)
if err != nil {
log.Error("Error unmarshalling data from git file : " + file + " | " + err.Error())
continue
}
wfID := gjson.Get(string(data), "metadata.labels.experiment_id").String()
kind := strings.ToLower(gjson.Get(string(data), "kind").String())
if kind != "cronexperiment" && kind != "experiment" && kind != "chaosengine" {
continue
}
log.Info("WFID in changed File :", wfID)
if wfID == "" {
log.Info("New Experiment pushed to git : " + file)
flag, err := g.createExperiment(ctx, string(data), file, config)
if err != nil {
log.Error("Error while creating new experiment db entry : " + file + " | " + err.Error())
continue
}
if flag {
newExperiments = true
}
} else {
err = g.updateExperiment(ctx, string(data), wfID, file, config)
if err != nil {
log.Error("Error while updating experiment db entry : " + file + " | " + err.Error())
continue
}
}
}
// push experiments with experiment_id added
if newExperiments {
latestCommit, err = config.GitCommit(GitUserFromContext(ctx), "Updated New Experiments", nil)
if err != nil {
return errors.New("Cannot commit experiments to git : " + err.Error())
}
err = config.GitPush()
if err != nil {
return errors.New("Cannot push experiments to git : " + err.Error())
}
}
query := bson.D{{"project_id", config.ProjectID}}
update := bson.D{{"$set", bson.D{{"latest_commit", latestCommit}}}}
if ctx == nil {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
err = g.gitOpsOperator.UpdateGitConfig(ctx, query, update)
} else {
err = g.gitOpsOperator.UpdateGitConfig(ctx, query, update)
}
if err != nil {
return errors.New("Failed to update git config : " + err.Error())
}
return nil
}
// createExperiment helps in creating a new experiment during the SyncDBToGit operation
func (g *gitOpsService) createExperiment(ctx context.Context, data, file string, config GitConfig) (bool, error) {
_, fileName := filepath.Split(file)
fileName = strings.Replace(fileName, ".yaml", "", -1)
wfName := gjson.Get(data, "metadata.name").String()
infraID := gjson.Get(data, "metadata.labels.infra_id").String()
log.Info("Experiment Details | wf_name: ", wfName, " infra_id: ", infraID)
if wfName == "" || infraID == "" {
return false, nil
}
if fileName != wfName {
return false, errors.New("file name doesn't match experiment name")
}
experiment := model.ChaosExperimentRequest{
ExperimentID: nil,
ExperimentManifest: data,
CronSyntax: "",
ExperimentName: wfName,
ExperimentDescription: "",
Weightages: nil,
IsCustomExperiment: true,
InfraID: infraID,
}
revID := ""
input, wfType, err := g.chaosExperimentService.ProcessExperiment(ctx, &experiment, config.ProjectID, revID)
if err != nil {
return false, err
}
err = g.chaosExperimentService.ProcessExperimentCreation(context.Background(), input, "git-ops", config.ProjectID, wfType, revID, store.Store)
if err != nil {
return false, err
}
experimentPath := config.LocalPath + "/" + file
yamlData, err := yaml.JSONToYAML([]byte(input.ExperimentManifest))
if err != nil {
return false, errors.New("Cannot convert manifest to yaml : " + err.Error())
}
err = os.WriteFile(experimentPath, yamlData, 0644)
if err != nil {
return false, errors.New("Cannot write experiment to git : " + err.Error())
}
return true, nil
}
// updateExperiment helps in updating an existing experiment during the SyncDBToGit operation
func (g *gitOpsService) updateExperiment(ctx context.Context, data, wfID, file string, config GitConfig) error {
_, fileName := filepath.Split(file)
fileName = strings.Replace(fileName, ".yaml", "", -1)
wfName := gjson.Get(data, "metadata.name").String()
infraID := gjson.Get(data, "metadata.labels.infra_id").String()
log.Info("Experiment Details | wf_name: ", wfName, " infra_id: ", infraID)
if wfName == "" || infraID == "" {
log.Error("Cannot Update experiment missing experiment name or infra id")
return nil
}
if fileName != wfName {
return errors.New("file name doesn't match experiment name")
}
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)
}
if infraID != experiment[0].InfraID {
log.Error("Cannot change infra id for existing experiment")
return nil
}
experimentData := model.ChaosExperimentRequest{
ExperimentID: &experiment[0].ExperimentID,
ExperimentManifest: data,
CronSyntax: experiment[0].CronSyntax,
ExperimentName: wfName,
ExperimentDescription: experiment[0].Description,
Weightages: nil,
IsCustomExperiment: experiment[0].IsCustomExperiment,
InfraID: experiment[0].InfraID,
}
revID := ""
updateRevision := false
input, wfType, err := g.chaosExperimentService.ProcessExperiment(ctx, &experimentData, config.ProjectID, revID)
if err != nil {
return err
}
return g.chaosExperimentService.ProcessExperimentUpdate(input, "git-ops", wfType, revID, updateRevision, config.ProjectID, dataStore.Store)
}
// deleteExperiment helps in deleting experiment from DB during the SyncDBToGit operation
func (g *gitOpsService) deleteExperiment(file string, config GitConfig) error {
_, fileName := filepath.Split(file)
fileName = strings.Replace(fileName, ".yaml", "", -1)
query := bson.D{{"experiment_name", fileName}, {"project_id", config.ProjectID}}
experiment, err := g.chaosExperimentOps.GetExperiment(context.Background(), query)
if err != nil {
return err
}
return g.chaosExperimentService.ProcessExperimentDelete(query, experiment, "git-ops", dataStore.Store)
}