Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

litmsuctl create scenario command independent of install-chaos-experiments step #114

Merged
merged 7 commits into from
Dec 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 56 additions & 28 deletions pkg/utils/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/url"
"regexp"
"strconv"
"strings"

"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
chaosTypes "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1"
Expand Down Expand Up @@ -120,41 +121,68 @@ func sliceContains(s []string, e string) bool {
// both artifacts and remote experiment specs.
func FetchWeightages(chaosWorkFlowRequest *model.ChaosWorkFlowRequest, templates []v1alpha1.Template) error {

var chaosEngines []string

// Fetch all present experiments and append them to the experiments array
for _, t := range templates {
// Only the template named "install-chaos-experiments" contains ChaosExperiment(s)
if t.Name == "install-chaos-experiments" {
for _, a := range t.Inputs.Artifacts {
chaosEngines = append(chaosEngines, a.Name)
}
}

// Template that contains ChaosEngine manifest
if sliceContains(chaosEngines, t.Name) {
var weightageInput model.WeightagesInput
var err error
var err error

var chaosEngine chaosTypes.ChaosEngine
err = yaml.Unmarshal([]byte(t.Inputs.Artifacts[0].Raw.Data), &chaosEngine)
if err != nil {
return errors.New("Error parsing ChaosEngine: " + err.Error())
if t.Inputs.Artifacts != nil && len(t.Inputs.Artifacts) > 0 {
if t.Inputs.Artifacts[0].Raw == nil {
continue
}
weightageInput.ExperimentName = chaosEngine.ObjectMeta.GenerateName
w, ok := t.Metadata.Labels["weight"]

if !ok {
White.Println("Weightage for ChaosExperiment/" + weightageInput.ExperimentName + " not provided, defaulting to 10.")
w = "10"
}
weightageInput.Weightage, err = strconv.Atoi(w)
if err != nil {
return errors.New("Invalid weightage for ChaosExperiment/" + weightageInput.ExperimentName + ".")
}
var data = t.Inputs.Artifacts[0].Raw.Data
if len(data) > 0 {
// This replacement is required because chaos engine yaml have a syntax template. example:{{ workflow.parameters.adminModeNamespace }}
// And it is not able the unmarshal the yamlstring to chaos engine struct
data = strings.ReplaceAll(data, "{{", "")
data = strings.ReplaceAll(data, "}}", "")

var chaosEngine chaosTypes.ChaosEngine

err = yaml.Unmarshal([]byte(data), &chaosEngine)

if err != nil {
return errors.New("failed to unmarshal chaosengine")
}

if strings.ToLower(chaosEngine.Kind) == "chaosengine" {
var weightageInput model.WeightagesInput

chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput)
weightageInput.ExperimentName = chaosEngine.ObjectMeta.GenerateName

if len(weightageInput.ExperimentName) == 0 {
return errors.New("empty chaos experiment name")
}

if len(chaosEngine.Spec.Experiments) == 0 {
return errors.New("no experiments specified in chaosengine - " + weightageInput.ExperimentName)
}

w, ok := t.Metadata.Labels["weight"]

if !ok {
White.Println("Weightage for ChaosExperiment/" + weightageInput.ExperimentName + " not provided, defaulting to 10.")
w = "10"
}
weightageInput.Weightage, err = strconv.Atoi(w)

if err != nil {
return errors.New("Invalid weightage for ChaosExperiment/" + weightageInput.ExperimentName + ".")
}

chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput)
}
}
}
}

// If no experiments are present in the workflow, adds a 0 to the Weightages array so it doesn't fail (same behaviour as the UI)
if len(chaosWorkFlowRequest.Weightages) == 0 {
White.Println("No experiments found in the chaos scenario, defaulting experiments weightage to 0.")
var weightageInput model.WeightagesInput
weightageInput.ExperimentName = ""
weightageInput.Weightage = 0
chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput)
}
return nil
}