diff --git a/chaoscenter/graphql/server/graph/model/models_gen.go b/chaoscenter/graphql/server/graph/model/models_gen.go index 9734945dfb1..f677ebfc8e7 100644 --- a/chaoscenter/graphql/server/graph/model/models_gen.go +++ b/chaoscenter/graphql/server/graph/model/models_gen.go @@ -1871,6 +1871,8 @@ type RegisterInfraRequest struct { Tolerations []*Toleration `json:"tolerations,omitempty"` // Tags of the infra Tags []string `json:"tags,omitempty"` + // Installation type indicating if the infra uses helm or kubernetes manifest + InstallationType *string `json:"installationType,omitempty"` } // Response received for registering a new infra @@ -1883,6 +1885,8 @@ type RegisterInfraResponse struct { Name string `json:"name"` // Infra Manifest Manifest string `json:"manifest"` + // Infra Helm Command + HelmCommand string `json:"helmCommand"` } type ResilienceScoreCategory struct { diff --git a/chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go b/chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go index d489046d0d1..7deb486d9a6 100644 --- a/chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go +++ b/chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go @@ -28,6 +28,36 @@ func GetEndpoint(host string) (string, error) { return host + "/api/query", nil } +func GetHelmCommand(infra dbChaosInfra.ChaosInfra, infraURL string) string { + var ( + infraNamespace string + DefaultInfraNamespace = "litmus" + ) + if infra.InfraNamespace != nil && *infra.InfraNamespace != "" { + infraNamespace = *infra.InfraNamespace + } else { + infraNamespace = DefaultInfraNamespace + } + commands := fmt.Sprintf(`helm install litmus-agent litmuschaos/litmus-agent \ +--namespace %s --create-namespace \ +--set "LITMUS_URL=%s" \ +--set "INFRA_ID=%s" \ +--set "ACCESS_KEY=%s" \ +--set "global.INFRA_MODE=%s"`, + infraNamespace, + infraURL, + infra.InfraID, + infra.AccessKey, + infra.InfraScope, + ) + if infra.InfraScope == NamespaceScope { + commands += ` \ +--set "crds.create=false" \ +--set "workflow-controller.crds.create=false"` + } + return commands +} + func GetK8sInfraYaml(host string, infra dbChaosInfra.ChaosInfra) ([]byte, error) { var config SubscriberConfigurations diff --git a/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go b/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go index 8c9663d241f..ac4db768931 100644 --- a/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go +++ b/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go @@ -36,6 +36,13 @@ const ( NamespaceScope string = "namespace" ) +type InstallationMode string + +const ( + Manifest InstallationMode = "kubernetes" + Helm InstallationMode = "helm" +) + type Service interface { RegisterInfra(c context.Context, projectID string, input model.RegisterInfraRequest) (*model.RegisterInfraResponse, error) ConfirmInfraRegistration(request model.InfraIdentity, r store.StateData) (*model.ConfirmInfraRegistrationResponse, error) @@ -205,17 +212,34 @@ func (in *infraService) RegisterInfra(c context.Context, projectID string, input return nil, err } - manifestYaml, err := GetK8sInfraYaml(fmt.Sprintf("%s://%s", referrerURL.Scheme, referrerURL.Host), newInfra) - if err != nil { - return nil, err + if input.InstallationType == nil { + return nil, fmt.Errorf("installation type is required") } - return &model.RegisterInfraResponse{ - InfraID: newInfra.InfraID, - Token: token, - Name: newInfra.Name, - Manifest: string(manifestYaml), - }, nil + switch *input.InstallationType { + case string(Manifest): + manifestYaml, err := GetK8sInfraYaml(fmt.Sprintf("%s://%s", referrerURL.Scheme, referrerURL.Host), newInfra) + if err != nil { + return nil, err + } + + return &model.RegisterInfraResponse{ + InfraID: newInfra.InfraID, + Token: token, + Name: newInfra.Name, + Manifest: string(manifestYaml), + }, nil + case string(Helm): + helmCommand := GetHelmCommand(newInfra, fmt.Sprintf("%s://%s", referrerURL.Scheme, referrerURL.Host)) + return &model.RegisterInfraResponse{ + InfraID: newInfra.InfraID, + Token: token, + Name: newInfra.Name, + HelmCommand: helmCommand, + }, nil + default: + return nil, errors.New("invalid installation type") + } } // DeleteInfra takes infraIDs and r parameters, deletes the infras from the database and sends a request to the subscriber for clean-up