Skip to content

Commit

Permalink
added debug mode flag and wiring
Browse files Browse the repository at this point in the history
Signed-off-by: Tullio Sebastiani <[email protected]>
  • Loading branch information
tsebastiani committed Nov 26, 2024
1 parent 6437dc8 commit 9d437c7
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 26 deletions.
8 changes: 7 additions & 1 deletion cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewGraphRunCommand(factory *providerfactory.ProviderFactory, scenarioOrches
spinner := NewSpinnerWithSuffix("running graph based chaos plan...")
volumes := make(map[string]string)
environment := make(map[string]string)
debug := false
kubeconfig, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
return err
Expand All @@ -62,6 +63,11 @@ func NewGraphRunCommand(factory *providerfactory.ProviderFactory, scenarioOrches
return fmt.Errorf("file %s does not exist", metricsProfile)
}

debug, err = cmd.Flags().GetBool("debug")
if err != nil {
return err
}

kubeconfigPath, err := utils.PrepareKubeconfig(&kubeconfig, config)
if err != nil {
return err
Expand Down Expand Up @@ -144,7 +150,7 @@ func NewGraphRunCommand(factory *providerfactory.ProviderFactory, scenarioOrches
}

go func() {
(*scenarioOrchestrator).RunGraph(nodes, executionPlan, environment, volumes, false, commChannel, ctx)
(*scenarioOrchestrator).RunGraph(nodes, executionPlan, environment, volumes, false, commChannel, ctx, debug)
}()

for {
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func Execute(providerFactory *factory.ProviderFactory, scenarioOrchestrator *sce
runCmd.LocalFlags().String("alerts-profile", "", "custom alerts profile file path")
runCmd.LocalFlags().String("metrics-profile", "", "custom metrics profile file path")
runCmd.LocalFlags().Bool("detached", false, "if set this flag will run in detached mode")
runCmd.LocalFlags().Bool("debug", false, "if set this flag will enable debug output in krkn")
runCmd.DisableFlagParsing = true
rootCmd.AddCommand(runCmd)

Expand All @@ -65,6 +66,7 @@ func Execute(providerFactory *factory.ProviderFactory, scenarioOrchestrator *sce
graphRunCmd.Flags().String("kubeconfig", "", "kubeconfig path (if not set will default to ~/.kube/config)")
graphRunCmd.Flags().String("alerts-profile", "", "custom alerts profile file path")
graphRunCmd.Flags().String("metrics-profile", "", "custom metrics profile file path")
graphRunCmd.Flags().Bool("debug", false, "if set this flag will enable debug output in krkn")

graphScaffoldCmd := NewGraphScaffoldCommand(providerFactory, config)
graphCmd.AddCommand(graphRunCmd)
Expand Down
9 changes: 7 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena
spinner.Start()

runDetached := false
debug := false

provider := GetProvider(false, factory)
scenarioDetail, err := provider.GetScenarioDetail(args[0], dataSource)
Expand Down Expand Up @@ -131,6 +132,10 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena
if a == "--detached" {
runDetached = true
}

if a == "--debug" {
debug = true
}
}
}

Expand Down Expand Up @@ -211,7 +216,7 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena
spinner.Stop()
}()

_, err = (*scenarioOrchestrator).RunAttached(quayImageUri+":"+scenarioDetail.Name, containerName, environment, false, volumes, os.Stdout, os.Stderr, &commChan, conn)
_, err = (*scenarioOrchestrator).RunAttached(quayImageUri+":"+scenarioDetail.Name, containerName, environment, false, volumes, os.Stdout, os.Stderr, &commChan, conn, debug)
if err != nil {
if exit := utils.ErrorToStatusCode(err, config); exit != nil {
os.Exit(int(*exit))
Expand All @@ -220,7 +225,7 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena
}
}
} else {
containerId, err := (*scenarioOrchestrator).Run(quayImageUri+":"+scenarioDetail.Name, containerName, environment, false, volumes, nil, conn)
containerId, err := (*scenarioOrchestrator).Run(quayImageUri+":"+scenarioDetail.Name, containerName, environment, false, volumes, nil, conn, debug)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
LabelTitleRegex string `json:"label_title_regex"`
LabelDescriptionRegex string `json:"label_description_regex"`
LabelInputFieldsRegex string `json:"label_input_fields_regex"`
DebugEnvironmentVariable string `json:"debug_environment_variable"`
}

//go:embed config.json
Expand Down
3 changes: 2 additions & 1 deletion internal/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"label_input_fields": "krknctl.input_fields",
"label_title_regex": "LABEL krknctl\\.title=\\\"?(.*)\\\"?",
"label_description_regex": "LABEL krknctl\\.description=\\\"?(.*)\\\"?",
"label_input_fields_regex": "LABEL krknctl\\.input_fields=\\'?(\\[.*\\])\\'?"
"label_input_fields_regex": "LABEL krknctl\\.input_fields=\\'?(\\[.*\\])\\'?",
"debug_environment_variable": "DEBUG"
}
8 changes: 4 additions & 4 deletions pkg/scenario_orchestrator/common_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"syscall"
)

func CommonRunGraph(scenarios models.ScenarioSet, resolvedGraph models.ResolvedGraph, extraEnv map[string]string, extraVolumeMounts map[string]string, cache bool, commChannel chan *models.GraphCommChannel, orchestrator ScenarioOrchestrator, config config.Config, ctx context.Context) {
func CommonRunGraph(scenarios models.ScenarioSet, resolvedGraph models.ResolvedGraph, extraEnv map[string]string, extraVolumeMounts map[string]string, cache bool, commChannel chan *models.GraphCommChannel, orchestrator ScenarioOrchestrator, config config.Config, ctx context.Context, debug bool) {
env := make(map[string]string)
volumes := make(map[string]string)

Expand Down Expand Up @@ -51,7 +51,7 @@ func CommonRunGraph(scenarios models.ScenarioSet, resolvedGraph models.ResolvedG

go func() {
defer wg.Done()
_, _ = orchestrator.RunAttached(scenario.Image, containerName, env, cache, volumes, file, file, nil, ctx)
_, _ = orchestrator.RunAttached(scenario.Image, containerName, env, cache, volumes, file, file, nil, ctx, debug)
}()

}
Expand All @@ -60,9 +60,9 @@ func CommonRunGraph(scenarios models.ScenarioSet, resolvedGraph models.ResolvedG
commChannel <- nil
}

func CommonRunAttached(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, stdout io.Writer, stderr io.Writer, c ScenarioOrchestrator, commChan *chan *string, ctx context.Context) (*string, error) {
func CommonRunAttached(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, stdout io.Writer, stderr io.Writer, c ScenarioOrchestrator, commChan *chan *string, ctx context.Context, debug bool) (*string, error) {

containerId, err := c.Run(image, containerName, env, cache, volumeMounts, commChan, ctx)
containerId, err := c.Run(image, containerName, env, cache, volumeMounts, commChan, ctx, debug)
if err != nil {
return nil, err
}
Expand Down
14 changes: 9 additions & 5 deletions pkg/scenario_orchestrator/docker/scenario_orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type ScenarioOrchestrator struct {
ContainerRuntime orchestratormodels.ContainerRuntime
}

func (c *ScenarioOrchestrator) Run(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, commChan *chan *string, ctx context.Context) (*string, error) {
func (c *ScenarioOrchestrator) Run(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, commChan *chan *string, ctx context.Context, debug bool) (*string, error) {

cli, err := dockerClientFromContext(ctx)
if err != nil {
Expand All @@ -49,6 +49,10 @@ func (c *ScenarioOrchestrator) Run(image string, containerName string, env map[s
}

var envVars []string
if debug == true {
envVars = append(envVars, fmt.Sprintf("%s='True'", c.GetConfig().DebugEnvironmentVariable))
}

for k, v := range env {
envVars = append(envVars, fmt.Sprintf("%s=%s", k, v))
}
Expand Down Expand Up @@ -426,13 +430,13 @@ func (c *ScenarioOrchestrator) AttachWait(containerId *string, stdout io.Writer,
return &interrupted, nil
}

func (c *ScenarioOrchestrator) RunAttached(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, stdout io.Writer, stderr io.Writer, commChan *chan *string, ctx context.Context) (*string, error) {
containerId, err := scenario_orchestrator.CommonRunAttached(image, containerName, env, cache, volumeMounts, stdout, stderr, c, commChan, ctx)
func (c *ScenarioOrchestrator) RunAttached(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, stdout io.Writer, stderr io.Writer, commChan *chan *string, ctx context.Context, debug bool) (*string, error) {
containerId, err := scenario_orchestrator.CommonRunAttached(image, containerName, env, cache, volumeMounts, stdout, stderr, c, commChan, ctx, debug)
return containerId, err
}

func (c *ScenarioOrchestrator) RunGraph(scenarios orchestratormodels.ScenarioSet, resolvedGraph orchestratormodels.ResolvedGraph, extraEnv map[string]string, extraVolumeMounts map[string]string, cache bool, commChannel chan *orchestratormodels.GraphCommChannel, ctx context.Context) {
scenario_orchestrator.CommonRunGraph(scenarios, resolvedGraph, extraEnv, extraVolumeMounts, cache, commChannel, c, c.Config, ctx)
func (c *ScenarioOrchestrator) RunGraph(scenarios orchestratormodels.ScenarioSet, resolvedGraph orchestratormodels.ResolvedGraph, extraEnv map[string]string, extraVolumeMounts map[string]string, cache bool, commChannel chan *orchestratormodels.GraphCommChannel, ctx context.Context, debug bool) {
scenario_orchestrator.CommonRunGraph(scenarios, resolvedGraph, extraEnv, extraVolumeMounts, cache, commChannel, c, c.Config, ctx, false)
}

func (c *ScenarioOrchestrator) PrintContainerRuntime() {
Expand Down
13 changes: 8 additions & 5 deletions pkg/scenario_orchestrator/podman/scenario_orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (w *progressWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

func (c *ScenarioOrchestrator) Run(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, commChan *chan *string, ctx context.Context) (*string, error) {
func (c *ScenarioOrchestrator) Run(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, commChan *chan *string, ctx context.Context, debug bool) (*string, error) {
imageExists, err := images.Exists(ctx, image, nil)
if cache == false || imageExists == false {

Expand Down Expand Up @@ -93,6 +93,9 @@ func (c *ScenarioOrchestrator) Run(image string, containerName string, env map[s

s.Name = containerName
s.Env = env
if debug == true {
s.Env[c.GetConfig().DebugEnvironmentVariable] = "True"
}
for k, v := range volumeMounts {
containerMount := specs.Mount{
Destination: v,
Expand Down Expand Up @@ -309,9 +312,9 @@ func (c *ScenarioOrchestrator) ResolveContainerName(containerName string, ctx co

// common functions

func (c *ScenarioOrchestrator) RunAttached(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, stdout io.Writer, stderr io.Writer, commChan *chan *string, ctx context.Context) (*string, error) {
func (c *ScenarioOrchestrator) RunAttached(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, stdout io.Writer, stderr io.Writer, commChan *chan *string, ctx context.Context, debug bool) (*string, error) {
time.Sleep(2)
return scenario_orchestrator.CommonRunAttached(image, containerName, env, cache, volumeMounts, stdout, stderr, c, commChan, ctx)
return scenario_orchestrator.CommonRunAttached(image, containerName, env, cache, volumeMounts, stdout, stderr, c, commChan, ctx, debug)
}

func (c *ScenarioOrchestrator) AttachWait(containerId *string, stdout io.Writer, stderr io.Writer, ctx context.Context) (*bool, error) {
Expand All @@ -323,9 +326,9 @@ func (c *ScenarioOrchestrator) AttachWait(containerId *string, stdout io.Writer,
return &interrupted, nil
}

func (c *ScenarioOrchestrator) RunGraph(scenarios orchestratormodels.ScenarioSet, resolvedGraph orchestratormodels.ResolvedGraph, extraEnv map[string]string, extraVolumeMounts map[string]string, cache bool, commChannel chan *orchestratormodels.GraphCommChannel, ctx context.Context) {
func (c *ScenarioOrchestrator) RunGraph(scenarios orchestratormodels.ScenarioSet, resolvedGraph orchestratormodels.ResolvedGraph, extraEnv map[string]string, extraVolumeMounts map[string]string, cache bool, commChannel chan *orchestratormodels.GraphCommChannel, ctx context.Context, debug bool) {
//TODO: add a getconfig method in scenarioOrchestrator
scenario_orchestrator.CommonRunGraph(scenarios, resolvedGraph, extraEnv, extraVolumeMounts, cache, commChannel, c, c.Config, ctx)
scenario_orchestrator.CommonRunGraph(scenarios, resolvedGraph, extraEnv, extraVolumeMounts, cache, commChannel, c, c.Config, ctx, debug)
}

func (c *ScenarioOrchestrator) PrintContainerRuntime() {
Expand Down
6 changes: 3 additions & 3 deletions pkg/scenario_orchestrator/scenario_orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
type ScenarioOrchestrator interface {
Connect(containerRuntimeUri string) (context.Context, error)

Run(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, commChan *chan *string, ctx context.Context) (*string, error)
Run(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, commChan *chan *string, ctx context.Context, debug bool) (*string, error)

RunAttached(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, stdout io.Writer, stderr io.Writer, commChan *chan *string, ctx context.Context) (*string, error)
RunAttached(image string, containerName string, env map[string]string, cache bool, volumeMounts map[string]string, stdout io.Writer, stderr io.Writer, commChan *chan *string, ctx context.Context, debug bool) (*string, error)

RunGraph(scenarios orchestrator_models.ScenarioSet, resolvedGraph orchestrator_models.ResolvedGraph, extraEnv map[string]string, extraVolumeMounts map[string]string, cache bool, commChannel chan *orchestrator_models.GraphCommChannel, ctx context.Context)
RunGraph(scenarios orchestrator_models.ScenarioSet, resolvedGraph orchestrator_models.ResolvedGraph, extraEnv map[string]string, extraVolumeMounts map[string]string, cache bool, commChannel chan *orchestrator_models.GraphCommChannel, ctx context.Context, debug bool)

CleanContainers(ctx context.Context) (*int, error)

Expand Down
10 changes: 5 additions & 5 deletions pkg/scenario_orchestrator/test/common_test_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func CommonTestScenarioOrchestratorRun(t *testing.T, so scenario_orchestrator.Sc
fmt.Println("CONTAINER SOCKET -> " + *socket)
timestamp := time.Now().Unix()
containerName := fmt.Sprintf("%s-%s-%d", conf.ContainerPrefix, scenario.Name, timestamp)
containerId, err := so.Run(registryUri+":"+scenario.Name, containerName, env, false, map[string]string{}, nil, ctx)
containerId, err := so.Run(registryUri+":"+scenario.Name, containerName, env, false, map[string]string{}, nil, ctx, false)
assert.Nil(t, err)
assert.NotNil(t, containerId)
return *containerId
Expand Down Expand Up @@ -116,7 +116,7 @@ func CommonTestScenarioOrchestratorRunAttached(t *testing.T, so scenario_orchest

fmt.Println("CONTAINER SOCKET -> " + *socket)
containerName1 := utils.GenerateContainerName(conf, scenario.Name, nil)
containerId, err := so.RunAttached(registryUri+":"+scenario.Name, containerName1, env, false, map[string]string{}, os.Stdout, os.Stderr, nil, ctx)
containerId, err := so.RunAttached(registryUri+":"+scenario.Name, containerName1, env, false, map[string]string{}, os.Stdout, os.Stderr, nil, ctx, false)
if err != nil {
fmt.Println("ERROR -> " + err.Error())
}
Expand All @@ -128,7 +128,7 @@ func CommonTestScenarioOrchestratorRunAttached(t *testing.T, so scenario_orchest
env["END"] = fmt.Sprintf("%d", duration)
env["EXIT_STATUS"] = exitStatus
containerName2 := utils.GenerateContainerName(conf, scenario.Name, nil)
containerId, err = so.RunAttached(registryUri+":"+scenario.Name, containerName2, env, false, map[string]string{}, os.Stdout, os.Stderr, nil, ctx)
containerId, err = so.RunAttached(registryUri+":"+scenario.Name, containerName2, env, false, map[string]string{}, os.Stdout, os.Stderr, nil, ctx, false)
if err != nil {
fmt.Println("ERROR -> " + err.Error())
}
Expand Down Expand Up @@ -273,7 +273,7 @@ func CommonTestScenarioOrchestratorRunGraph(t *testing.T, so scenario_orchestrat

commChannel := make(chan *models.GraphCommChannel)
go func() {
so.RunGraph(nodes, executionPlan, map[string]string{}, map[string]string{}, false, commChannel, ctx)
so.RunGraph(nodes, executionPlan, map[string]string{}, map[string]string{}, false, commChannel, ctx, false)
}()

for {
Expand Down Expand Up @@ -408,7 +408,7 @@ func CommonTestScenarioOrchestratorResolveContainerName(t *testing.T, so scenari

fmt.Println("CONTAINER SOCKET -> " + *socket)
containerName := utils.GenerateContainerName(conf, scenario.Name, nil)
containerId, err := so.RunAttached(registryUri+":"+scenario.Name, containerName, env, false, map[string]string{}, os.Stdout, os.Stderr, nil, ctx)
containerId, err := so.RunAttached(registryUri+":"+scenario.Name, containerName, env, false, map[string]string{}, os.Stdout, os.Stderr, nil, ctx, false)
assert.Nil(t, err)
assert.NotNil(t, containerId)

Expand Down

0 comments on commit 9d437c7

Please sign in to comment.