Skip to content

Commit

Permalink
update(contribution-guide): updating contribution guide according to …
Browse files Browse the repository at this point in the history
…new schema changes (#35)

Signed-off-by: shubhamchaudhary <shubham.chaudhary@mayadata.io>
  • Loading branch information
ispeakc0de committed Jul 3, 2020
1 parent 2320a97 commit 416a023
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 28 deletions.
34 changes: 34 additions & 0 deletions contribute/developer-guide/generate_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ def generate_chaoslib(chaoslib_parent_path, chaoslib_name, chaoslib_config, litm
with open(chaoslib_filename, "w+") as f:
f.write(output_from_parsed_template)

# generate_environment creates the environment for the experiment
def generate_environment(environment_parent_path, environment_config, litmus_env):
environment_filename = environment_parent_path + '/environment.go'

if os.path.isdir(environment_parent_path) != True:
os.makedirs(environment_parent_path)

# Load Jinja2 template
template = litmus_env.get_template('./templates/environment.tmpl')
output_from_parsed_template = template.render(environment_config)
with open(environment_filename, "w+") as f:
f.write(output_from_parsed_template)

# generate_types creates the types.go for the experiment
def generate_types(types_parent_path, types_config, litmus_env):
types_filename = types_parent_path + '/types.go'

if os.path.isdir(types_parent_path) != True:
os.makedirs(types_parent_path)

# Load Jinja2 template
template = litmus_env.get_template('./templates/types.tmpl')
output_from_parsed_template = template.render(types_config)
with open(types_filename, "w+") as f:
f.write(output_from_parsed_template)

# generate_experiment creates the expriment.go file
def generate_experiment(experiment_parent_path, experiment_name, experiment_config, litmus_env):
ansible_logic_filename = experiment_parent_path + '/' + experiment_name + '.go'
Expand Down Expand Up @@ -147,6 +173,8 @@ def main():
chart_dir = litmus_root + '/experiments/' + entity_parent

chaoslib_dir = litmus_root + '/chaoslib/litmus/' + config['name']
environment_dir = litmus_root + '/pkg/' + config['name'] + '/environment'
types_dir = litmus_root + '/pkg/' + config['name'] + '/types'
# if a folder with specified/derived chart name is not present, create it
if os.path.isdir(chart_dir) != True:
os.makedirs(chart_dir)
Expand Down Expand Up @@ -182,6 +210,12 @@ def main():
# generate chaoslib
generate_chaoslib(chaoslib_dir, entity_name, config, env)

# generate environment.go
generate_environment(environment_dir, config, env)

# generate environment.go
generate_types(types_dir, config, env)


if __name__=="__main__":
main()
51 changes: 51 additions & 0 deletions contribute/developer-guide/templates/environment.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package environment

import (
"os"
"strconv"

clientTypes "k8s.io/apimachinery/pkg/types"

experimentTypes "github.com/litmuschaos/litmus-go/pkg/pod-delete/types"
"github.com/litmuschaos/litmus-go/pkg/types"
)

// STEPS TO GETENV OF YOUR CHOICE HERE
// ADDED FOR FEW MANDATORY FIELD

//GetENV fetches all the env variables from the runner pod
func GetENV(experimentDetails *experimentTypes.ExperimentDetails, expName string) {
experimentDetails.ExperimentName = expName
experimentDetails.ChaosNamespace = Getenv("CHAOS_NAMESPACE", "litmus")
experimentDetails.EngineName = Getenv("CHAOSENGINE", "")
experimentDetails.ChaosDuration, _ = strconv.Atoi(Getenv("TOTAL_CHAOS_DURATION", "30"))
experimentDetails.ChaosInterval, _ = strconv.Atoi(Getenv("CHAOS_INTERVAL", "10"))
experimentDetails.RampTime, _ = strconv.Atoi(Getenv("RAMP_TIME", "0"))
experimentDetails.ChaosLib = Getenv("LIB", "litmus")
experimentDetails.AppNS = Getenv("APP_NAMESPACE", "")
experimentDetails.AppLabel = Getenv("APP_LABEL", "")
experimentDetails.AppKind = Getenv("APP_KIND", "")
experimentDetails.ChaosUID = clientTypes.UID(Getenv("CHAOS_UID", ""))
experimentDetails.InstanceID = Getenv("INSTANCE_ID", "")
experimentDetails.ChaosPodName = Getenv("POD_NAME", "")
}

// Getenv fetch the env and set the default value, if any
func Getenv(key string, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
value = defaultValue
}
return value
}

//InitialiseChaosVariables initialise all the global variables
func InitialiseChaosVariables(chaosDetails *types.ChaosDetails, experimentDetails *experimentTypes.ExperimentDetails) {

chaosDetails.ChaosNamespace = experimentDetails.ChaosNamespace
chaosDetails.ChaosPodName = experimentDetails.ChaosPodName
chaosDetails.ChaosUID = experimentDetails.ChaosUID
chaosDetails.EngineName = experimentDetails.EngineName
chaosDetails.ExperimentName = experimentDetails.ExperimentName
chaosDetails.InstanceID = experimentDetails.InstanceID
}
62 changes: 36 additions & 26 deletions contribute/developer-guide/templates/experiment.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/litmuschaos/litmus-go/pkg/environment"
"github.com/litmuschaos/litmus-go/pkg/events"
"github.com/litmuschaos/litmus-go/pkg/log"
experimentEnv "github.com/litmuschaos/litmus-go/pkg/{{ name }}/environment"
experimentTypes "github.com/litmuschaos/litmus-go/pkg/{{ name }}/types"
"github.com/litmuschaos/litmus-go/pkg/result"
"github.com/litmuschaos/litmus-go/pkg/status"
"github.com/litmuschaos/litmus-go/pkg/types"
Expand All @@ -14,7 +16,6 @@ func init() {
// Log as JSON instead of the default ASCII formatter.
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
// ForceColors: true,
DisableSorting: true,
DisableLevelTruncation: true,
})
Expand All @@ -23,41 +24,47 @@ func init() {
func main() {

var err error
experimentsDetails := types.ExperimentDetails{}
experimentsDetails := experimentTypes.ExperimentDetails{}
resultDetails := types.ResultDetails{}
eventsDetails := types.EventDetails{}
clients := environment.ClientSets{}
chaosDetails := types.ChaosDetails{}

//Getting kubeConfig and Generate ClientSets
if err := clients.GenerateClientSetFromKubeConfig(); err != nil {
log.Fatalf("Unable to Get the kubeconfig due to %v", err)
}

//Fetching all the ENV passed for the runner pod
//Fetching all the ENV passed from the runner pod
log.Infof("[PreReq]: Getting the ENV for the %v experiment", experimentsDetails.ExperimentName)
environment.GetENV(&experimentsDetails, "{{ name }}")
experimentEnv.GetENV(&experimentsDetails, "{{ name }}")

// Intialise Chaos Result Parameters
environment.SetResultAttributes(&resultDetails, &experimentsDetails)
environment.SetResultAttributes(&resultDetails, experimentsDetails.EngineName, experimentsDetails.ExperimentName)

// Intialise the chaos attributes
experimentEnv.InitialiseChaosVariables(&chaosDetails, &experimentsDetails)

//Updating the chaos result in the beggining of experiment
log.Infof("[PreReq]: Updating the chaos result of %v experiment (SOT)", experimentsDetails.ExperimentName)
err = result.ChaosResult(&experimentsDetails, clients, &resultDetails, "SOT")
err = result.ChaosResult(&chaosDetails, clients, &resultDetails, "SOT")
if err != nil {
log.Errorf("Unable to Create the Chaos Result due to %v", err)
resultDetails.FailStep = "Updating the chaos result of pod-delete experiment (SOT)"
err = result.ChaosResult(&experimentsDetails, clients, &resultDetails, "EOT")
err = result.ChaosResult(&chaosDetails, clients, &resultDetails, "EOT")
return
}

// Set the chaos result uid
result.SetResultUID(&resultDetails, clients, &chaosDetails)

//DISPLAY THE APP INFORMATION
log.InfoWithValues("The application informations are as follows", logrus.Fields{
"Namespace": experimentsDetails.AppNS,
"Label": experimentsDetails.AppLabel,
"Ramp Time": experimentsDetails.RampTime,
})


// ADD A PRE-CHAOS CHECK OF YOUR CHOICE HERE
// POD STATUS CHECKS FOR THE APPLICATION UNDER TEST AND AUXILIARY APPLICATIONS ARE ADDED BY DEFAULT

Expand All @@ -67,25 +74,24 @@ func main() {
if err != nil {
log.Errorf("Application status check failed due to %v\n", err)
resultDetails.FailStep = "Verify that the AUT (Application Under Test) is running (pre-chaos)"
result.ChaosResult(&experimentsDetails, clients, &resultDetails, "EOT")
result.ChaosResult(&chaosDetails, clients, &resultDetails, "EOT")
return
}

{% if auxiliaryAppCheck is defined and auxiliaryAppCheck == true %}
//PRE-CHAOS AUXILIARY APPLICATION STATUS CHECK
if experimentsDetails.AuxiliaryAppInfo != "" {
log.Info("[Status]: Verify that the Auxiliary Applications are running (pre-chaos)")
err = status.CheckAuxiliaryApplicationStatus(&experimentsDetails, clients)
err = status.CheckAuxiliaryApplicationStatus(experimentsDetails.AuxiliaryAppInfo, clients)
if err != nil {
log.Errorf("Auxiliary Application status check failed due to %v", err)
resultDetails.FailStep = "Verify that the Auxiliary Applications are running (pre-chaos)"
result.ChaosResult(&experimentsDetails, clients,&resultDetails,"EOT"); return
result.ChaosResult(&chaosDetails, clients,&resultDetails,"EOT"); return
}
{% endif %}

if experimentsDetails.EngineName != "" {
environment.SetEventAttributes(&eventsDetails, types.PreChaosCheck, "AUT is Running successfully")
events.GenerateEvents(&experimentsDetails, clients, &eventsDetails)
environment.SetEngineEventAttributes(&eventsDetails, types.PreChaosCheck, "AUT is Running successfully", &chaosDetails)
events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosEngine")
}

// INVOKE THE CHAOSLIB OF YOUR CHOICE HERE, WHICH WILL CONTAIN
Expand All @@ -97,7 +103,7 @@ func main() {
err = {{ name }}.{{ name }}()
if err != nil {
log.Errorf("Chaos injection failed due to %v\n", err)
result.ChaosResult(&experimentsDetails, clients,&resultDetails,"EOT"); return
result.ChaosResult(&chaosDetails, clients,&resultDetails,"EOT"); return
}
resultDetails.Verdict="Pass"
}
Expand All @@ -106,40 +112,44 @@ func main() {
// POD STATUS CHECKS FOR THE APPLICATION UNDER TEST AND AUXILIARY APPLICATIONS ARE ADDED BY DEFAULT

//POST-CHAOS APPLICATION STATUS CHECK
log.Info("[Status]: Verify that the AUT (Application Under Test) is running (post-chaos)")
log.Info("[Status]: Verify that the AUT (Application Under Test) is running (post-chaos)")
err = status.CheckApplicationStatus(experimentsDetails.AppNS, experimentsDetails.AppLabel, clients)
if err != nil {
log.Errorf("Application status check failed due to %v\n", err)
resultDetails.FailStep = "Verify that the AUT (Application Under Test) is running (post-chaos)"
result.ChaosResult(&experimentsDetails, clients,&resultDetails,"EOT"); return
result.ChaosResult(&chaosDetails, clients, &resultDetails, "EOT")
return
}

{% if auxiliaryAppCheck is defined and auxiliaryAppCheck == true %}
//POST-CHAOS AUXILIARY APPLICATION STATUS CHECK
if experimentsDetails.AuxiliaryAppInfo != "" {
log.Info("[Status]: Verify that the Auxiliary Applications are running (post-chaos)")
err = status.CheckAuxiliaryApplicationStatus(&experimentsDetails, clients)
err = status.CheckAuxiliaryApplicationStatus(experimentsDetails.AuxiliaryAppInfo, clients)
if err != nil {
log.Errorf("Auxiliary Application status check failed due to %v", err)
resultDetails.FailStep = "Verify that the Auxiliary Applications are running (post-chaos)"
result.ChaosResult(&experimentsDetails, clients,&resultDetails,"EOT"); return
result.ChaosResult(&chaosDetails, clients,&resultDetails,"EOT"); return
}
{% endif %}

if experimentsDetails.EngineName != "" {
environment.SetEventAttributes(&eventsDetails, types.PostChaosCheck, "AUT is Running successfully")
events.GenerateEvents(&experimentsDetails, clients, &eventsDetails)
environment.SetEngineEventAttributes(&eventsDetails, types.PostChaosCheck, "AUT is Running successfully", &chaosDetails)
events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosEngine")
}

//Updating the chaosResult in the end of experiment
log.Infof("[The End]: Updating the chaos result of %v experiment (EOT)", experimentsDetails.ExperimentName)
err = result.ChaosResult(&experimentsDetails, clients, &resultDetails, "EOT")
err = result.ChaosResult(&chaosDetails, clients, &resultDetails, "EOT")
if err != nil {
log.Fatalf("Unable to Update the Chaos Result due to %v\n", err)
}
if experimentsDetails.EngineName != "" {
msg := experimentsDetails.ExperimentName + "experiment has been" + resultDetails.Verdict + "ed"
environment.SetEventAttributes(&eventsDetails, types.Summary, msg)
events.GenerateEvents(&experimentsDetails, clients, &eventsDetails)
msg := experimentsDetails.ExperimentName + " experiment has been " + resultDetails.Verdict + "ed"
environment.SetEngineEventAttributes(&eventsDetails, types.Summary, msg, &chaosDetails)
events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosEngine")
}

msg := experimentsDetails.ExperimentName + " experiment has been " + resultDetails.Verdict + "ed"
environment.SetResultEventAttributes(&eventsDetails, types.Summary, msg, &resultDetails)
events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosResult")
}
25 changes: 25 additions & 0 deletions contribute/developer-guide/templates/types.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package types

import (
clientTypes "k8s.io/apimachinery/pkg/types"
)

// ADD THE ATTRIBUTES OF YOUR CHOICE HERE
// FEW MENDATORY ATTRIBUTES ARE ADDED BY DEFAULT

// ExperimentDetails is for collecting all the experiment-related details
type ExperimentDetails struct {
ExperimentName string
EngineName string
ChaosDuration int
ChaosInterval int
RampTime int
ChaosLib string
AppNS string
AppLabel string
AppKind string
ChaosUID clientTypes.UID
InstanceID string
ChaosNamespace string
ChaosPodName string
}
2 changes: 1 addition & 1 deletion pkg/pod-cpu-hog/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func GetENV(experimentDetails *experimentTypes.ExperimentDetails, expName string
experimentDetails.InstanceID = Getenv("INSTANCE_ID", "")
experimentDetails.ChaosPodName = Getenv("POD_NAME", "")
experimentDetails.CPUcores, _ = strconv.Atoi(Getenv("CPU_CORES", "1"))
experimentDetails.PodsAffectedPerc, _ = strconv.Atoi(Getenv("PODS_AFFECTED_PERC", "100"))
experimentDetails.PodsAffectedPerc, _ = strconv.Atoi(Getenv("PODS_AFFECTED_PERC", "0"))
}

// Getenv fetch the env and set the default value, if any
Expand Down
2 changes: 1 addition & 1 deletion pkg/pod-memory-hog/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GetENV(experimentDetails *experimentTypes.ExperimentDetails, expName string
experimentDetails.ChaosUID = clientTypes.UID(Getenv("CHAOS_UID", ""))
experimentDetails.InstanceID = Getenv("INSTANCE_ID", "")
experimentDetails.ChaosPodName = Getenv("POD_NAME", "")
experimentDetails.PodsAffectedPerc, _ = strconv.Atoi(Getenv("PODS_AFFECTED_PERC", "100"))
experimentDetails.PodsAffectedPerc, _ = strconv.Atoi(Getenv("PODS_AFFECTED_PERC", "0"))
experimentDetails.MemoryConsumption, _ = strconv.Atoi(Getenv("MEMORY_CONSUMPTION", "500"))
}

Expand Down

0 comments on commit 416a023

Please sign in to comment.