Skip to content

Commit

Permalink
litmsuctl create scenario command independent of install-chaos-experi…
Browse files Browse the repository at this point in the history
…ments step (#114)

* Fixing FetchWeightages. Defaulting to 10

Signed-off-by: Nico J <nicolasj@microsoft.com>
Signed-off-by: iamnicoj <nicolasj@outlook.com>
Signed-off-by: Nico J <nicolasj@microsoft.com>

* new pseudocode for calculating FetchWeightages

Signed-off-by: Nico J <nicolasj@microsoft.com>
Signed-off-by: iamnicoj <nicolasj@outlook.com>
Signed-off-by: Nico J <nicolasj@microsoft.com>

* New pseudocode for handling weightage

Signed-off-by: Nico J <nicolasj@microsoft.com>
Signed-off-by: iamnicoj <nicolasj@outlook.com>
Signed-off-by: Nico J <nicolasj@microsoft.com>

* re-implementing FetchWeightages for properly handling weightages

Signed-off-by: Nico J <nicolasj@microsoft.com>
Signed-off-by: iamnicoj <nicolasj@outlook.com>
Signed-off-by: Nico J <nicolasj@microsoft.com>

* Update pkg/utils/workflow.go

Co-authored-by: Saranya Jena <saranya.jena@harness.io>
Signed-off-by: iamnicoj <nicolasj@outlook.com>
Signed-off-by: Nico J <nicolasj@microsoft.com>

* Fixing FetchWeightages. Defaulting to 10

Signed-off-by: Nico J <nicolasj@microsoft.com>
Signed-off-by: iamnicoj <nicolasj@outlook.com>
Signed-off-by: Nico J <nicolasj@microsoft.com>

* signing all using gpg commits

Signed-off-by: Nico J <nicolasj@microsoft.com>

Signed-off-by: Nico J <nicolasj@microsoft.com>
Signed-off-by: iamnicoj <nicolasj@outlook.com>
Co-authored-by: Saranya Jena <saranya.jena@harness.io>
  • Loading branch information
iamnicoj and Saranya-jena committed Dec 12, 2022
1 parent e598721 commit 5ae06c1
Showing 1 changed file with 56 additions and 28 deletions.
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
}

0 comments on commit 5ae06c1

Please sign in to comment.