Skip to content

Commit

Permalink
Fixed issue with invite user modal for users which were once deactiva…
Browse files Browse the repository at this point in the history
…ted (#4185)

* Fixed issue with invite user

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

* fixed imports

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

* fixed issue with namespaced scope experiments,removed ununsed envs

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

* Removed envs and print statements

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

* Added 3.0.0 manifests

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

* updated readme

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

---------

Signed-off-by: Saranya-jena <saranya.jena@harness.io>
  • Loading branch information
Saranya-jena authored Sep 26, 2023
1 parent bd59209 commit 6141ddd
Show file tree
Hide file tree
Showing 17 changed files with 5,911 additions and 37 deletions.
4 changes: 2 additions & 2 deletions chaoscenter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ metrics:
enabled: false
prometheusRule:
enabled: false

# bitnami/mongodb is not yet supported on ARM.
# Using unofficial tools to build bitnami/mongodb (arm64 support)
# more info: https://github.com/ZCube/bitnami-compat
Expand All @@ -43,5 +43,5 @@ helm install my-release bitnami/mongodb --values mongo-values.yml -n <NAMESPACE>
Applying the manifest file will install all the required service account configuration and ChaosCenter.

```shell
kubectl apply -f https://litmuschaos.github.io/litmus/3.0.0-beta10/litmus-3.0.0-beta10.yaml
kubectl apply -f https://litmuschaos.github.io/litmus/3.0.0-beta13/litmus-3.0.0.yaml
```
8 changes: 4 additions & 4 deletions chaoscenter/authentication/pkg/project/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Repository interface {
UpdateInvite(projectID string, userID string, invitation entities.Invitation, role *entities.MemberRole) error
UpdateProjectName(projectID string, projectName string) error
GetAggregateProjects(pipeline mongo.Pipeline, opts *options.AggregateOptions) (*mongo.Cursor, error)
UpdateProjectState(userID string, deactivateTime int64, isDeactivate bool) error
UpdateProjectState(ctx context.Context, userID string, deactivateTime int64, isDeactivate bool) error
GetOwnerProjects(ctx context.Context, userID string) ([]*entities.Project, error)
GetProjectRole(projectID string, userID string) (*entities.MemberRole, error)
GetProjectMembers(projectID string, state string) ([]*entities.Member, error)
Expand Down Expand Up @@ -288,7 +288,7 @@ func (r repository) GetAggregateProjects(pipeline mongo.Pipeline, opts *options.
}

// UpdateProjectState updates the deactivated_at state of the member and removed_at field of the project
func (r repository) UpdateProjectState(userID string, deactivateTime int64, isDeactivate bool) error {
func (r repository) UpdateProjectState(ctx context.Context, userID string, deactivateTime int64, isDeactivate bool) error {
opts := options.Update().SetArrayFilters(options.ArrayFilters{
Filters: []interface{}{
bson.D{{"elem.user_id", userID}},
Expand All @@ -302,7 +302,7 @@ func (r repository) UpdateProjectState(userID string, deactivateTime int64, isDe
}},
}

_, err := r.Collection.UpdateMany(context.Background(), filter, update, opts)
_, err := r.Collection.UpdateMany(ctx, filter, update, opts)
if err != nil {
//log.Print("Error updating user's state in projects : ", err)
return err
Expand All @@ -324,7 +324,7 @@ func (r repository) UpdateProjectState(userID string, deactivateTime int64, isDe
}},
}

_, err = r.Collection.UpdateMany(context.Background(), filter, update)
_, err = r.Collection.UpdateMany(ctx, filter, update)
if err != nil {
//log.Print("Error updating user's state in projects : ", err)
return err
Expand Down
6 changes: 3 additions & 3 deletions chaoscenter/authentication/pkg/services/project_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type projectService interface {
UpdateInvite(projectID string, userID string, invitation entities.Invitation, role *entities.MemberRole) error
UpdateProjectName(projectID string, projectName string) error
GetAggregateProjects(pipeline mongo.Pipeline, opts *options.AggregateOptions) (*mongo.Cursor, error)
UpdateProjectState(userID string, deactivateTime int64, isDeactivate bool) error
UpdateProjectState(ctx context.Context, userID string, deactivateTime int64, isDeactivate bool) error
GetOwnerProjectIDs(ctx context.Context, userID string) ([]*entities.Project, error)
GetProjectRole(projectID string, userID string) (*entities.MemberRole, error)
GetProjectMembers(projectID string, state string) ([]*entities.Member, error)
Expand Down Expand Up @@ -68,8 +68,8 @@ func (a applicationService) GetAggregateProjects(pipeline mongo.Pipeline, opts *
return a.projectRepository.GetAggregateProjects(pipeline, opts)
}

func (a applicationService) UpdateProjectState(userID string, deactivateTime int64, isDeactivate bool) error {
return a.projectRepository.UpdateProjectState(userID, deactivateTime, isDeactivate)
func (a applicationService) UpdateProjectState(ctx context.Context, userID string, deactivateTime int64, isDeactivate bool) error {
return a.projectRepository.UpdateProjectState(ctx, userID, deactivateTime, isDeactivate)
}
func (a applicationService) GetOwnerProjectIDs(ctx context.Context, userID string) ([]*entities.Project, error) {
return a.projectRepository.GetOwnerProjects(ctx, userID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func (a applicationService) UpdateStateTransaction(userRequest entities.UpdateUs
}

// Updating details in user collection
err = a.UpdateUserState(userRequest.Username, *userRequest.IsDeactivate, deactivateTime)
err = a.UpdateUserState(sc, userRequest.Username, *userRequest.IsDeactivate, deactivateTime)
if err != nil {
log.Info(err)
return utils.ErrServerError
}
// Updating details in project collection
err = a.UpdateProjectState(user.ID, deactivateTime, *userRequest.IsDeactivate)
err = a.UpdateProjectState(sc, user.ID, deactivateTime, *userRequest.IsDeactivate)
if err != nil {
log.Info(err)
return utils.ErrServerError
Expand Down
8 changes: 5 additions & 3 deletions chaoscenter/authentication/pkg/services/user_service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package services

import (
"context"

"github.com/litmuschaos/litmus/chaoscenter/authentication/pkg/entities"
)

Expand All @@ -16,7 +18,7 @@ type userService interface {
CreateUser(user *entities.User) (*entities.User, error)
UpdateUser(user *entities.UserDetails) error
IsAdministrator(user *entities.User) error
UpdateUserState(username string, isDeactivate bool, deactivateTime int64) error
UpdateUserState(ctx context.Context, username string, isDeactivate bool, deactivateTime int64) error
InviteUsers(invitedUsers []string) (*[]entities.User, error)
}

Expand Down Expand Up @@ -71,8 +73,8 @@ func (a applicationService) IsAdministrator(user *entities.User) error {
}

// UpdateUserState updates deactivated_at state of the user
func (a applicationService) UpdateUserState(username string, isDeactivate bool, deactivateTime int64) error {
return a.userRepository.UpdateUserState(username, isDeactivate, deactivateTime)
func (a applicationService) UpdateUserState(ctx context.Context, username string, isDeactivate bool, deactivateTime int64) error {
return a.userRepository.UpdateUserState(ctx, username, isDeactivate, deactivateTime)
}

func (a applicationService) InviteUsers(invitedUsers []string) (*[]entities.User, error) {
Expand Down
15 changes: 9 additions & 6 deletions chaoscenter/authentication/pkg/user/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Repository interface {
CreateUser(user *entities.User) (*entities.User, error)
UpdateUser(user *entities.UserDetails) error
IsAdministrator(user *entities.User) error
UpdateUserState(username string, isDeactivate bool, deactivateTime int64) error
UpdateUserState(ctx context.Context, username string, isDeactivate bool, deactivateTime int64) error
InviteUsers(invitedUsers []string) (*[]entities.User, error)
}

Expand Down Expand Up @@ -94,8 +94,11 @@ func (r repository) InviteUsers(invitedUsers []string) (*[]entities.User, error)
{"_id", bson.D{
{"$nin", invitedUsers},
}},
{"deactivated_at", bson.D{
{"$exists", false},
{"$or", bson.A{
bson.D{{"deactivated_at", bson.D{
{"$exists", false},
}}},
bson.D{{"deactivated_at", nil}},
}},
})

Expand Down Expand Up @@ -231,15 +234,15 @@ func (r repository) IsAdministrator(user *entities.User) error {
}

// UpdateUserState updates the deactivated_at state of the user
func (r repository) UpdateUserState(username string, isDeactivate bool, deactivateTime int64) error {
func (r repository) UpdateUserState(ctx context.Context, username string, isDeactivate bool, deactivateTime int64) error {
var err error
if isDeactivate {
_, err = r.Collection.UpdateOne(context.Background(), bson.M{"username": username}, bson.M{"$set": bson.M{
_, err = r.Collection.UpdateOne(ctx, bson.M{"username": username}, bson.M{"$set": bson.M{
"deactivated_at": deactivateTime,
"is_removed": true,
}})
} else {
_, err = r.Collection.UpdateOne(context.Background(), bson.M{"username": username}, bson.M{"$set": bson.M{
_, err = r.Collection.UpdateOne(ctx, bson.M{"username": username}, bson.M{"$set": bson.M{
"deactivated_at": nil,
"is_removed": false,
}})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/k8s"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"
"github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"io/ioutil"
"os"
Expand Down Expand Up @@ -223,7 +223,7 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs

// SendRequestToSubscriber sends events from the graphQL server to the subscribers listening for the requests
func SendRequestToSubscriber(subscriberRequest SubscriberRequests, r store.StateData) {
if utils.Config.InfraScope == string(model.InfraScopeCluster) {
if utils.Config.ChaosCenterScope == string(model.InfraScopeCluster) {
/*
namespace = Obtain from WorkflowManifest or
from frontend as a separate workflowNamespace field under ChaosWorkFlowRequest model
Expand All @@ -250,17 +250,19 @@ func SendRequestToSubscriber(subscriberRequest SubscriberRequests, r store.State

// SendExperimentToSubscriber sends the workflow to the subscriber to be handled
func SendExperimentToSubscriber(projectID string, workflow *model.ChaosExperimentRequest, username *string, externalData *string, reqType string, r *store.StateData) {
workflowNamespace := gjson.Get(workflow.ExperimentManifest, "metadata.namespace").String()

if workflowNamespace == "" {
workflowNamespace = utils.Config.InfraNamespace
var workflowObj unstructured.Unstructured
err := yaml.Unmarshal([]byte(workflow.ExperimentManifest), &workflowObj)
if err != nil {
fmt.Errorf("error while parsing experiment manifest %v", err)
}

SendRequestToSubscriber(SubscriberRequests{
K8sManifest: workflow.ExperimentManifest,
RequestType: reqType,
ProjectID: projectID,
InfraID: workflow.InfraID,
Namespace: workflowNamespace,
Namespace: workflowObj.GetNamespace(),
ExternalData: externalData,
Username: username,
}, *r)
Expand Down
1 change: 0 additions & 1 deletion chaoscenter/graphql/server/pkg/k8s/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
var (
decUnstructured = yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
dr dynamic.ResourceInterface
AgentNamespace = utils.Config.InfraNamespace
)

// InfraResource This function handles cluster operations
Expand Down
2 changes: 0 additions & 2 deletions chaoscenter/graphql/server/pkg/probe/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func addKubernetesCMDProbeProperties(newProbe *dbSchemaProbe.Probe, request mode
// CMD Probe -> Source
if request.KubernetesCMDProperties.Source != nil {
var source *v1alpha1.SourceDetails
fmt.Println("source", []byte(*request.KubernetesCMDProperties.Source), *request.KubernetesCMDProperties.Source)

err := json.Unmarshal([]byte(*request.KubernetesCMDProperties.Source), &source)
if err != nil {
Expand Down Expand Up @@ -757,7 +756,6 @@ func GenerateExperimentManifestWithProbes(manifest string, projectID string) (ar
if err != nil {
return argoTypes.Workflow{}, fmt.Errorf("failed to fetch probe details, error: %s", err.Error())
}
fmt.Println("probes", probes)
probeManifestString, err := GenerateProbeManifest(probe.GetOutputProbe(), annotationKey.Mode)
if err != nil {
return argoTypes.Workflow{}, fmt.Errorf("failed to generate probe manifest, error: %s", err.Error())
Expand Down
4 changes: 0 additions & 4 deletions chaoscenter/graphql/server/utils/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ type Configuration struct {
InfraDeployments string `required:"true" split_words:"true"`
DbServer string `required:"true" split_words:"true"`
JwtSecret string `required:"true" split_words:"true"`
SelfAgent string `required:"true" split_words:"true"`
InfraScope string `required:"true" split_words:"true"`
InfraNamespace string `required:"true" split_words:"true"`
LitmusPortalNamespace string `required:"true" split_words:"true"`
DbUser string `required:"true" split_words:"true"`
DbPassword string `required:"true" split_words:"true"`
Expand All @@ -24,7 +21,6 @@ type Configuration struct {
LitmusChaosRunnerImage string `required:"true" split_words:"true"`
LitmusChaosExporterImage string `required:"true" split_words:"true"`
ContainerRuntimeExecutor string `required:"true" split_words:"true"`
HubBranchName string `required:"true" split_words:"true"`
WorkflowHelperImageVersion string `required:"true" split_words:"true"`
ServerServiceName string `split_words:"true"`
NodeName string `split_words:"true"`
Expand Down
5 changes: 2 additions & 3 deletions chaoscenter/subscriber/pkg/k8s/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,12 @@ func AgentRegister(infraData map[string]string) (bool, error) {

func applyRequest(requestType string, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
ctx := context.TODO()

logrus.Info("Applying request for kind: ", obj.GetKind(), ", resource name: ", obj.GetName(), ", and namespace: ", obj.GetNamespace())
if requestType == "create" {
response, err := dr.Create(ctx, obj, metav1.CreateOptions{})
if k8s_errors.IsAlreadyExists(err) {
// This doesnt ever happen even if it does already exist
// This doesn't ever happen even if it does already exist
logrus.Info("Already exists")
return nil, nil
}
Expand Down Expand Up @@ -254,7 +255,6 @@ func applyRequest(requestType string, obj *unstructured.Unstructured) (*unstruct
}
logrus.Info("successfully deleted for kind: ", obj.GetKind(), ", resource name: ", obj.GetName(), ", and namespace: ", obj.GetNamespace())
} else if obj.GetLabels() != nil {
fmt.Println(obj)
objLabels := obj.GetLabels()
delete(objLabels, "updated_by")
err = dr.DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: labels.FormatLabels(objLabels)})
Expand Down Expand Up @@ -338,7 +338,6 @@ func AgentOperations(infraAction types.Action) (*unstructured.Unstructured, erro
if err != nil {
return nil, err
}

// Obtain REST interface for the GVR
if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
// namespaced resources should specify the namespace
Expand Down
2 changes: 1 addition & 1 deletion chaoscenter/subscriber/pkg/requests/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func RequestProcessor(infraData map[string]string, r types.RawData) error {
} else if strings.Index("create update delete get", strings.ToLower(r.Payload.Data.InfraConnect.Action.RequestType)) >= 0 {
_, err := k8s.AgentOperations(r.Payload.Data.InfraConnect.Action)
if err != nil {
return errors.New("error performing infra operationn: " + err.Error())
return errors.New("error performing infra operation: " + err.Error())
}
} else if strings.Index("workflow_delete workflow_run_delete ", strings.ToLower(r.Payload.Data.InfraConnect.Action.RequestType)) >= 0 {

Expand Down
Loading

0 comments on commit 6141ddd

Please sign in to comment.