Skip to content

Commit

Permalink
Global scenario environment variables (#19)
Browse files Browse the repository at this point in the history
Signed-off-by: Tullio Sebastiani <[email protected]>
  • Loading branch information
tsebastiani authored Jan 10, 2025
1 parent aa0de25 commit 4997f9f
Show file tree
Hide file tree
Showing 30 changed files with 428 additions and 281 deletions.
2 changes: 1 addition & 1 deletion cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"fmt"
"github.com/krkn-chaos/krknctl/internal/config"
"github.com/krkn-chaos/krknctl/pkg/config"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/utils"
"github.com/spf13/cobra"
Expand Down
14 changes: 3 additions & 11 deletions cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"github.com/fatih/color"
"github.com/krkn-chaos/krknctl/internal/config"
"github.com/krkn-chaos/krknctl/pkg/config"
"github.com/krkn-chaos/krknctl/pkg/provider/factory"
"github.com/krkn-chaos/krknctl/pkg/provider/models"
"github.com/krkn-chaos/krknctl/pkg/text"
Expand All @@ -18,13 +18,9 @@ func NewDescribeCommand(factory *factory.ProviderFactory, config config.Config)
Long: `describes a scenario`,
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
dataSource, err := BuildDataSource(config, false, nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
provider := GetProvider(false, factory)

scenarios, err := FetchScenarios(provider, dataSource)
scenarios, err := FetchScenarios(provider)
if err != nil {
log.Fatalf("Error fetching scenarios: %v", err)
return []string{}, cobra.ShellCompDirectiveError
Expand All @@ -34,14 +30,10 @@ func NewDescribeCommand(factory *factory.ProviderFactory, config config.Config)

},
RunE: func(cmd *cobra.Command, args []string) error {
dataSource, err := BuildDataSource(config, false, nil)
if err != nil {
return err
}
spinner := NewSpinnerWithSuffix("fetching scenario details...")
spinner.Start()
provider := GetProvider(false, factory)
scenarioDetail, err := provider.GetScenarioDetail(args[0], dataSource)
scenarioDetail, err := provider.GetScenarioDetail(args[0])
if err != nil {
return err
}
Expand Down
48 changes: 28 additions & 20 deletions cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/krkn-chaos/krknctl/internal/config"
"github.com/krkn-chaos/krknctl/pkg/config"
"github.com/krkn-chaos/krknctl/pkg/dependencygraph"
"github.com/krkn-chaos/krknctl/pkg/provider"
providerfactory "github.com/krkn-chaos/krknctl/pkg/provider/factory"
Expand Down Expand Up @@ -41,7 +41,6 @@ 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 @@ -64,7 +63,6 @@ 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
}
Expand Down Expand Up @@ -94,18 +92,14 @@ func NewGraphRunCommand(factory *providerfactory.ProviderFactory, scenarioOrches
nodes := make(map[string]models.ScenarioNode)
err = json.Unmarshal(file, &nodes)

dataSource, err := BuildDataSource(config, false, nil)
if err != nil {
return err
}
dataProvider := GetProvider(false, factory)
nameChannel := make(chan *struct {
name *string
err error
})
spinner.Start()
go func() {
validateScenariosInput(dataProvider, dataSource, nodes, nameChannel)
validateScenariosInput(dataProvider, nodes, nameChannel)
}()

for {
Expand Down Expand Up @@ -162,7 +156,7 @@ func NewGraphRunCommand(factory *providerfactory.ProviderFactory, scenarioOrches
}

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

for {
Expand All @@ -187,7 +181,7 @@ func NewGraphRunCommand(factory *providerfactory.ProviderFactory, scenarioOrches
return command
}

func validateScenariosInput(provider provider.ScenarioDataProvider, dataSource string, nodes map[string]models.ScenarioNode, scenarioNameChannel chan *struct {
func validateScenariosInput(provider provider.ScenarioDataProvider, nodes map[string]models.ScenarioNode, scenarioNameChannel chan *struct {
name *string
err error
}) {
Expand All @@ -200,21 +194,38 @@ func validateScenariosInput(provider provider.ScenarioDataProvider, dataSource s
name *string
err error
}{name: &n.Name, err: nil}
scenarioDetail, err := provider.GetScenarioDetail(n.Name, dataSource)
scenarioDetail, err := provider.GetScenarioDetail(n.Name)

if err != nil {
scenarioNameChannel <- &struct {
name *string
err error
}{name: &n.Name, err: err}
return
}

if scenarioDetail == nil {
scenarioNameChannel <- &struct {
name *string
err error
}{name: &n.Name, err: fmt.Errorf("scenario %s not found in %s", n.Name, dataSource)}
}{name: &n.Name, err: fmt.Errorf("scenario %s not found", n.Name)}
return
}

globalDetail, err := provider.GetGlobalEnvironment()
if err != nil {
scenarioNameChannel <- &struct {
name *string
err error
}{name: &n.Name, err: err}
return
}

// adding the global env fields to the scenario fields so, if global env is
// added to the scenario the validation is available

scenarioDetail.Fields = append(scenarioDetail.Fields, globalDetail.Fields...)

for k, v := range n.Env {
field := scenarioDetail.GetFieldByEnvVar(k)
if field == nil {
Expand Down Expand Up @@ -264,13 +275,9 @@ func NewGraphScaffoldCommand(factory *providerfactory.ProviderFactory, config co
Long: `Scaffolds a dependency graph based run`,
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
dataSource, err := BuildDataSource(config, false, nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
dataProvider := GetProvider(false, factory)

scenarios, err := FetchScenarios(dataProvider, dataSource)
scenarios, err := FetchScenarios(dataProvider)
if err != nil {
log.Fatalf("Error fetching scenarios: %v", err)
return []string{}, cobra.ShellCompDirectiveError
Expand All @@ -279,12 +286,13 @@ func NewGraphScaffoldCommand(factory *providerfactory.ProviderFactory, config co
return *scenarios, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
dataSource, err := BuildDataSource(config, false, nil)
dataProvider := GetProvider(false, factory)
includeGlobalEnv, err := cmd.Flags().GetBool("global-env")
if err != nil {
return err
}
dataProvider := GetProvider(false, factory)
output, err := dataProvider.ScaffoldScenarios(args, dataSource)

output, err := dataProvider.ScaffoldScenarios(args, includeGlobalEnv)
if err != nil {
return err
}
Expand Down
8 changes: 2 additions & 6 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"github.com/fatih/color"
"github.com/krkn-chaos/krknctl/internal/config"
"github.com/krkn-chaos/krknctl/pkg/config"
providerfactory "github.com/krkn-chaos/krknctl/pkg/provider/factory"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator"
"github.com/spf13/cobra"
Expand All @@ -30,14 +30,10 @@ func NewListScenariosCommand(factory *providerfactory.ProviderFactory, config co
Long: `list available krkn-hub scenarios`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
dataSource, err := BuildDataSource(config, false, nil)
if err != nil {
return err
}
provider := GetProvider(false, factory)
s := NewSpinnerWithSuffix("fetching scenarios...")
s.Start()
scenarios, err := provider.GetRegistryImages(dataSource)
scenarios, err := provider.GetRegistryImages()
if err != nil {
s.Stop()
log.Fatalf("failed to fetch scenarios: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/query_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/krkn-chaos/krknctl/internal/config"
"github.com/krkn-chaos/krknctl/pkg/config"
provider_models "github.com/krkn-chaos/krknctl/pkg/provider/models"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models"
Expand Down
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"fmt"
"github.com/krkn-chaos/krknctl/internal/config"
"github.com/krkn-chaos/krknctl/pkg/config"
"github.com/krkn-chaos/krknctl/pkg/provider/factory"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -53,7 +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 @@ -66,9 +66,9 @@ 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)
graphScaffoldCmd.Flags().Bool("global-env", false, "if set this flag will add global environment variables to each scenario in the graph")
graphCmd.AddCommand(graphRunCmd)
graphCmd.AddCommand(graphScaffoldCmd)
rootCmd.AddCommand(graphCmd)
Expand Down
Loading

0 comments on commit 4997f9f

Please sign in to comment.