Fixed issue with mongo watch events in graphql server (#4160)

* Fixed issue with mongo watch events to add image registry on project creation

Signed-off-by: Saranya-jena <saranya.jena@harness.io>

* Fixed imports

Signed-off-by: Saranya-jena <saranya.jena@harness.io>

* resolved comments

Signed-off-by: Saranya-jena <saranya.jena@harness.io>

* fixed imports

Signed-off-by: Saranya-jena <saranya.jena@harness.io>

---------

Signed-off-by: Saranya-jena <saranya.jena@harness.io>
This commit is contained in:
Saranya Jena 2023-09-13 13:10:47 +05:30 committed by GitHub
parent 2cc7d4c67b
commit a64d321e12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 138 additions and 83 deletions

View File

@ -391,10 +391,12 @@ func (c *ChaosExperimentHandler) GetExperiment(ctx context.Context, projectID st
Phase: v.Phase,
ResiliencyScore: v.ResiliencyScore,
UpdatedBy: &model.UserDetails{
UserID: v.UpdatedBy,
Username: v.UpdatedBy.Username,
UserID: v.UpdatedBy.UserID,
},
CreatedBy: &model.UserDetails{
UserID: v.CreatedBy,
Username: v.CreatedBy.Username,
UserID: v.CreatedBy.UserID,
},
UpdatedAt: strconv.FormatInt(v.UpdatedAt, 10),
CreatedAt: strconv.FormatInt(v.CreatedAt, 10),
@ -424,10 +426,10 @@ func (c *ChaosExperimentHandler) GetExperiment(ctx context.Context, projectID st
IsRemoved: exp.IsRemoved,
Infra: chaosInfrastructure,
UpdatedBy: &model.UserDetails{
Username: exp.UpdatedBy,
Username: exp.UpdatedBy.Username,
},
CreatedBy: &model.UserDetails{
Username: exp.UpdatedBy,
Username: exp.UpdatedBy.Username,
},
RecentExperimentRunDetails: recentExpRuns,
},
@ -754,10 +756,10 @@ func (c *ChaosExperimentHandler) ListExperiment(projectID string, request model.
Phase: v.Phase,
ResiliencyScore: v.ResiliencyScore,
UpdatedBy: &model.UserDetails{
Username: v.UpdatedBy,
Username: v.UpdatedBy.Username,
},
CreatedBy: &model.UserDetails{
Username: v.UpdatedBy,
Username: v.UpdatedBy.Username,
},
UpdatedAt: strconv.FormatInt(v.UpdatedAt, 10),
CreatedAt: strconv.FormatInt(v.CreatedAt, 10),
@ -781,10 +783,10 @@ func (c *ChaosExperimentHandler) ListExperiment(projectID string, request model.
IsRemoved: workflow.IsRemoved,
Infra: chaosInfrastructure,
UpdatedBy: &model.UserDetails{
Username: workflow.UpdatedBy,
Username: workflow.UpdatedBy.Username,
},
CreatedBy: &model.UserDetails{
Username: workflow.UpdatedBy,
Username: workflow.UpdatedBy.Username,
},
RecentExperimentRunDetails: recentExpRuns,
}

View File

@ -191,8 +191,12 @@ func (c *chaosExperimentService) ProcessExperimentCreation(ctx context.Context,
CreatedAt: timeNow,
UpdatedAt: timeNow,
IsRemoved: false,
CreatedBy: username,
UpdatedBy: username,
CreatedBy: mongodb.UserDetailResponse{
Username: username,
},
UpdatedBy: mongodb.UserDetailResponse{
Username: username,
},
},
Revision: revision,
RecentExperimentRunDetails: []dbChaosExperiment.ExperimentRunDetail{},

View File

@ -251,7 +251,7 @@ func (c *ChaosExperimentRunHandler) GetExperimentRun(ctx context.Context, projec
RunSequence: int(wfRun.RunSequence),
UpdatedBy: &model.UserDetails{
Username: wfRun.UpdatedBy,
Username: wfRun.UpdatedBy.Username,
},
UpdatedAt: strconv.FormatInt(wfRun.UpdatedAt, 10),
CreatedAt: strconv.FormatInt(wfRun.CreatedAt, 10),
@ -621,7 +621,7 @@ func (c *ChaosExperimentRunHandler) ListExperimentRun(projectID string, request
ExecutionData: workflow.ExecutionData,
IsRemoved: &workflow.IsRemoved,
UpdatedBy: &model.UserDetails{
Username: workflow.UpdatedBy,
Username: workflow.UpdatedBy.Username,
},
UpdatedAt: strconv.FormatInt(workflow.UpdatedAt, 10),
CreatedAt: strconv.FormatInt(workflow.CreatedAt, 10),
@ -811,9 +811,13 @@ func (c *ChaosExperimentRunHandler) RunChaosWorkFlow(ctx context.Context, projec
Audit: mongodb.Audit{
IsRemoved: false,
CreatedAt: currentTime,
CreatedBy: username,
CreatedBy: mongodb.UserDetailResponse{
Username: username,
},
UpdatedAt: currentTime,
UpdatedBy: username,
UpdatedBy: mongodb.UserDetailResponse{
Username: username,
},
},
},
}
@ -853,9 +857,13 @@ func (c *ChaosExperimentRunHandler) RunChaosWorkFlow(ctx context.Context, projec
Audit: mongodb.Audit{
IsRemoved: false,
CreatedAt: currentTime,
CreatedBy: username,
CreatedBy: mongodb.UserDetailResponse{
Username: username,
},
UpdatedAt: currentTime,
UpdatedBy: username,
UpdatedBy: mongodb.UserDetailResponse{
Username: username,
},
},
NotifyID: &notifyID,
Completed: false,
@ -1171,7 +1179,9 @@ func (c *ChaosExperimentRunHandler) ChaosExperimentRunEvent(event model.Experime
IsRemoved: false,
CreatedAt: time.Now().UnixMilli(),
UpdatedAt: time.Now().UnixMilli(),
UpdatedBy: string(updatedBy),
UpdatedBy: mongodb.UserDetailResponse{
Username: string(updatedBy),
},
},
},
}
@ -1259,8 +1269,12 @@ func (c *ChaosExperimentRunHandler) ChaosExperimentRunEvent(event model.Experime
Audit: mongodb.Audit{
IsRemoved: isRemoved,
UpdatedAt: currentTime.UnixMilli(),
UpdatedBy: string(updatedBy),
CreatedBy: string(updatedBy),
UpdatedBy: mongodb.UserDetailResponse{
Username: string(updatedBy),
},
CreatedBy: mongodb.UserDetailResponse{
Username: string(updatedBy),
},
},
})
if err != nil {

View File

@ -140,8 +140,12 @@ func (in *infraService) RegisterInfra(c context.Context, projectID string, input
CreatedAt: currentTime.UnixMilli(),
UpdatedAt: currentTime.UnixMilli(),
IsRemoved: false,
CreatedBy: username,
UpdatedBy: username,
CreatedBy: mongodb.UserDetailResponse{
Username: username,
},
UpdatedBy: mongodb.UserDetailResponse{
Username: username,
},
},
EnvironmentID: input.EnvironmentID,
AccessKey: utils.RandomString(32),
@ -685,9 +689,9 @@ func (in *infraService) ListInfras(projectID string, request *model.ListInfraReq
newInfra.NoOfExperiments = &infra.ExperimentDetails[0].TotalSchedules
}
newInfra.CreatedBy = &model.UserDetails{Username: infra.CreatedBy}
newInfra.CreatedBy = &model.UserDetails{Username: infra.CreatedBy.Username}
newInfra.UpdatedBy = &model.UserDetails{
Username: infra.UpdatedBy,
Username: infra.UpdatedBy.Username,
}
newInfras = append(newInfras, &newInfra)

View File

@ -106,8 +106,12 @@ func (c *chaosHubService) AddChaosHub(ctx context.Context, chaosHub model.Create
CreatedAt: currentTime.UnixMilli(),
UpdatedAt: currentTime.UnixMilli(),
IsRemoved: false,
CreatedBy: username,
UpdatedBy: username,
CreatedBy: mongodb.UserDetailResponse{
Username: username,
},
UpdatedBy: mongodb.UserDetailResponse{
Username: username,
},
},
LastSyncedAt: time.Now().UnixMilli(),
IsDefault: false,
@ -166,8 +170,12 @@ func (c *chaosHubService) AddRemoteChaosHub(ctx context.Context, chaosHub model.
CreatedAt: currentTime.UnixMilli(),
UpdatedAt: currentTime.UnixMilli(),
IsRemoved: false,
CreatedBy: username,
UpdatedBy: username,
CreatedBy: mongodb.UserDetailResponse{
Username: username,
},
UpdatedBy: mongodb.UserDetailResponse{
Username: username,
},
},
LastSyncedAt: time.Now().UnixMilli(),
IsDefault: false,
@ -237,8 +245,12 @@ func (c *chaosHubService) SaveChaosHub(ctx context.Context, chaosHub model.Creat
CreatedAt: currentTime.UnixMilli(),
UpdatedAt: currentTime.UnixMilli(),
IsRemoved: false,
CreatedBy: username,
UpdatedBy: username,
CreatedBy: mongodb.UserDetailResponse{
Username: username,
},
UpdatedBy: mongodb.UserDetailResponse{
Username: username,
},
},
LastSyncedAt: time.Now().UnixMilli(),
}
@ -634,8 +646,8 @@ func (c *chaosHubService) ListChaosHubs(ctx context.Context, projectID string, r
TotalExperiments: strconv.Itoa(experimentCount),
CreatedAt: strconv.Itoa(int(hub.CreatedAt)),
UpdatedAt: strconv.Itoa(int(hub.UpdatedAt)),
CreatedBy: &model.UserDetails{Username: hub.CreatedBy},
UpdatedBy: &model.UserDetails{Username: hub.UpdatedBy},
CreatedBy: &model.UserDetails{Username: hub.CreatedBy.Username},
UpdatedBy: &model.UserDetails{Username: hub.UpdatedBy.Username},
}
hubDetails = append(hubDetails, hubDetail)
}
@ -694,8 +706,8 @@ func (c *chaosHubService) GetChaosHub(ctx context.Context, chaosHubID string, pr
TotalExperiments: strconv.Itoa(experimentCount),
CreatedAt: strconv.Itoa(int(hub.CreatedAt)),
UpdatedAt: strconv.Itoa(int(hub.UpdatedAt)),
CreatedBy: &model.UserDetails{Username: hub.CreatedBy},
UpdatedBy: &model.UserDetails{Username: hub.UpdatedBy},
CreatedBy: &model.UserDetails{Username: hub.CreatedBy.Username},
UpdatedBy: &model.UserDetails{Username: hub.UpdatedBy.Username},
}
return hubDetail, nil
@ -755,8 +767,8 @@ func (c *chaosHubService) getChaosHubDetails(ctx context.Context, hubID string,
Tags: hub.Tags,
Description: &hub.Description,
//TODO util functions for this
CreatedBy: &model.UserDetails{Username: hub.CreatedBy},
UpdatedBy: &model.UserDetails{Username: hub.UpdatedBy},
CreatedBy: &model.UserDetails{Username: hub.CreatedBy.Username},
UpdatedBy: &model.UserDetails{Username: hub.UpdatedBy.Username},
}, nil
}

View File

@ -7,9 +7,15 @@ type ResourceDetails struct {
}
type Audit struct {
UpdatedAt int64 `bson:"updated_at"`
CreatedAt int64 `bson:"created_at"`
CreatedBy string `bson:"created_by"`
UpdatedBy string `bson:"updated_by"`
IsRemoved bool `bson:"is_removed"`
UpdatedAt int64 `bson:"updated_at"`
CreatedAt int64 `bson:"created_at"`
CreatedBy UserDetailResponse `bson:"created_by" json:"createdBy"`
UpdatedBy UserDetailResponse `bson:"updated_by" json:"updatedBy"`
IsRemoved bool `bson:"is_removed"`
}
type UserDetailResponse struct {
UserID string `bson:"user_id" json:"userID"`
Username string `bson:"username" json:"username"`
Email string `bson:"email" json:"email"`
}

View File

@ -161,10 +161,10 @@ func (probe *Probe) GetOutputProbe() *model.Probe {
Type: model.ProbeType(probe.Type),
InfrastructureType: probe.InfrastructureType,
CreatedBy: &model.UserDetails{
Username: probe.CreatedBy,
Username: probe.CreatedBy.Username,
},
UpdatedBy: &model.UserDetails{
Username: probe.UpdatedBy,
Username: probe.UpdatedBy.Username,
},
}
if probe.InfrastructureType == model.InfrastructureTypeKubernetes {

View File

@ -16,7 +16,7 @@ type Member struct {
UserID string `bson:"user_id"`
Role MemberRole `bson:"role"`
Invitation Invitation `bson:"invitation"`
JoinedAt string `bson:"joined_at"`
JoinedAt int64 `bson:"joined_at"`
}
// MemberRole defines the project role a member has in the project

View File

@ -66,8 +66,12 @@ func (e *EnvironmentService) CreateEnvironment(ctx context.Context, projectID st
CreatedAt: currentTime.UnixMilli(),
UpdatedAt: currentTime.UnixMilli(),
IsRemoved: false,
CreatedBy: username,
UpdatedBy: username,
CreatedBy: mongodb.UserDetailResponse{
Username: username,
},
UpdatedBy: mongodb.UserDetailResponse{
Username: username,
},
},
}
@ -203,8 +207,8 @@ func (e *EnvironmentService) GetEnvironment(projectID string, environmentID stri
Type: model.EnvironmentType(env.Type),
CreatedAt: strconv.FormatInt(env.CreatedAt, 10),
UpdatedAt: strconv.FormatInt(env.UpdatedAt, 10),
CreatedBy: &model.UserDetails{Username: env.CreatedBy},
UpdatedBy: &model.UserDetails{Username: env.UpdatedBy},
CreatedBy: &model.UserDetails{Username: env.CreatedBy.Username},
UpdatedBy: &model.UserDetails{Username: env.UpdatedBy.Username},
InfraIDs: env.InfraIDs,
IsRemoved: &env.IsRemoved,
}, nil
@ -394,8 +398,8 @@ func (e *EnvironmentService) ListEnvironments(projectID string, request *model.L
Type: model.EnvironmentType(env.Type),
CreatedAt: strconv.FormatInt(env.CreatedAt, 10),
UpdatedAt: strconv.FormatInt(env.UpdatedAt, 10),
CreatedBy: &model.UserDetails{Username: env.CreatedBy},
UpdatedBy: &model.UserDetails{Username: env.UpdatedBy},
CreatedBy: &model.UserDetails{Username: env.CreatedBy.Username},
UpdatedBy: &model.UserDetails{Username: env.UpdatedBy.Username},
InfraIDs: env.InfraIDs,
IsRemoved: &env.IsRemoved,
})

View File

@ -84,8 +84,12 @@ func (p *probe) AddProbe(ctx context.Context, probe model.ProbeRequest) (*model.
CreatedAt: currTime,
UpdatedAt: currTime,
IsRemoved: false,
CreatedBy: username,
UpdatedBy: username,
CreatedBy: mongodb.UserDetailResponse{
Username: username,
},
UpdatedBy: mongodb.UserDetailResponse{
Username: username,
},
},
Type: dbSchemaProbe.ProbeType(probe.Type),
InfrastructureType: probe.InfrastructureType,
@ -141,7 +145,9 @@ func (p *probe) UpdateProbe(ctx context.Context, request model.ProbeRequest) (st
UpdatedAt: time.Now().UnixMilli(),
IsRemoved: false,
CreatedBy: pr.CreatedBy,
UpdatedBy: username,
UpdatedBy: mongodb.UserDetailResponse{
Username: username,
},
},
Type: pr.Type,
InfrastructureType: pr.InfrastructureType,
@ -702,7 +708,7 @@ func GetProbeExecutionHistoryInExperimentRuns(projectID string, probeName string
ExperimentName: execution.ExperimentName,
UpdatedAt: int(execution.UpdatedAt),
UpdatedBy: &model.UserDetails{
Username: execution.UpdatedBy,
Username: execution.UpdatedBy.Username,
},
},
}

View File

@ -2,37 +2,47 @@ package projects
import (
"context"
"time"
"github.com/google/uuid"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/image_registry"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/project"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/grpc"
image_registry2 "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/image_registry"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
grpc2 "google.golang.org/grpc"
)
// ProjectInitializer creates a default hub and default image registry for a new project
func ProjectInitializer(ctx context.Context, projectID string, role string, operator mongodb.MongoOperator) error {
func ProjectInitializer(ctx context.Context, project project.Project, role string, operator mongodb.MongoOperator) error {
var bl_true = true
currentTime := time.Now().UnixMilli()
//self_deployer.StartDeployer(projectID, operator)
irOp := image_registry.NewImageRegistryOperator(operator)
irService := image_registry2.NewImageRegistryService(irOp)
_, err := irService.CreateImageRegistry(ctx, projectID, model.ImageRegistryInput{
IsDefault: bl_true,
imageRegistry := image_registry.ImageRegistry{
ImageRegistryID: uuid.New().String(),
ProjectID: project.ID,
ImageRegistryName: "docker.io",
ImageRepoName: "litmuschaos",
ImageRegistryType: "public",
SecretName: nil,
SecretNamespace: nil,
EnableRegistry: &bl_true,
})
IsDefault: true,
Audit: mongodb.Audit{
CreatedAt: currentTime,
UpdatedAt: currentTime,
CreatedBy: project.CreatedBy,
UpdatedBy: project.UpdatedBy,
IsRemoved: false,
},
}
irOp := image_registry.NewImageRegistryOperator(operator)
err := irOp.InsertImageRegistry(ctx, imageRegistry)
if err != nil {
return err
}
@ -40,37 +50,35 @@ func ProjectInitializer(ctx context.Context, projectID string, role string, oper
return nil
}
func ProjectEvents(projectEventChannel chan string, mongoClient *mongo.Client, mongoOp mongodb.MongoOperator) error {
routineCtx, cancelFn := context.WithCancel(context.Background())
_ = cancelFn
func ProjectEvents(projectEventChannel chan string, mongoClient *mongo.Client, mongoOp mongodb.MongoOperator) {
pipeline := mongo.Pipeline{
bson.D{{"$match", bson.D{{"operationType", "insert"}}}},
}
projectDetails, err := project.NewProjectOperator(mongoOp).WatchProjectEvents(routineCtx, pipeline, mongoClient)
projectDetails, err := project.NewProjectOperator(mongoOp).WatchProjectEvents(context.Background(), pipeline, mongoClient)
if err != nil {
return err
log.Error(err.Error())
}
var conn *grpc2.ClientConn
client, conn := grpc.GetAuthGRPCSvcClient(conn)
defer conn.Close()
for projectDetails.Next(routineCtx) {
for projectDetails.Next(context.Background()) {
var DbEvent project.ProjectCreationEvent
if err := projectDetails.Decode(&DbEvent); err != nil {
return err
log.Error(err.Error())
}
if DbEvent.OperationType == "insert" {
user, err := grpc.GetUserById(client, DbEvent.FullDocument.CreatedBy)
user, err := grpc.GetUserById(client, DbEvent.FullDocument.CreatedBy.UserID)
if err != nil {
logrus.Error(err)
log.Error(err)
}
err = ProjectInitializer(routineCtx, DbEvent.FullDocument.ID, user.Role, mongoOp)
err = ProjectInitializer(context.Background(), DbEvent.FullDocument, user.Role, mongoOp)
if err != nil {
logrus.Error(err)
log.Error(err)
}
//projectEventChannel <- DbEvent.OperationType
}
}
return nil
}

View File

@ -110,12 +110,7 @@ func main() {
router.GET("/readiness", handlers.ReadinessHandler())
projectEventChannel := make(chan string)
go func() {
err := projects.ProjectEvents(projectEventChannel, mongodb.MgoClient, mongodbOperator)
if err != nil {
log.Error(err.Error())
}
}()
go projects.ProjectEvents(projectEventChannel, mongodb.MgoClient, mongodbOperator)
log.Infof("chaos manager running at http://localhost:%s", utils.Config.HttpPort)
log.Fatal(http.ListenAndServe(":"+utils.Config.HttpPort, router))