From d16f9e2d382a0a8cbe1a55f79b67030751383ff3 Mon Sep 17 00:00:00 2001 From: Tullio Sebastiani Date: Wed, 11 Dec 2024 11:45:00 +0100 Subject: [PATCH] refactoring + GetGlobalEnvironment Signed-off-by: Tullio Sebastiani test fixes Signed-off-by: Tullio Sebastiani --- cmd/clean.go | 2 +- cmd/describe.go | 14 +-- cmd/graph.go | 26 ++--- cmd/list.go | 8 +- cmd/query_status.go | 2 +- cmd/root.go | 2 +- cmd/run.go | 21 +--- cmd/utils.go | 24 +---- main.go | 2 +- {internal => pkg}/config/config.go | 21 +++- {internal => pkg}/config/config.json | 6 +- pkg/provider/factory/provider_factory.go | 2 +- pkg/provider/factory/provider_factory_test.go | 2 +- pkg/provider/offline/scenario_provider.go | 11 ++- pkg/provider/provider.go | 7 +- pkg/provider/quay/scenario_provider.go | 97 +++++++++++++++---- pkg/provider/quay/scenario_provider_test.go | 46 ++++----- pkg/scenario_orchestrator/common_functions.go | 2 +- .../docker/scenario_orchestrator.go | 2 +- .../docker/scenario_orchestrator_test.go | 2 +- pkg/scenario_orchestrator/factory/factory.go | 2 +- .../factory/factory_test.go | 2 +- .../podman/scenario_orchestrator.go | 2 +- .../podman/scenario_orchestrator_test.go | 2 +- .../scenario_orchestrator.go | 2 +- .../test/common_test_functions.go | 36 ++----- pkg/scenario_orchestrator/utils/utils.go | 2 +- pkg/scenario_orchestrator/utils/utils_test.go | 2 +- pkg/typing/field_test.go | 16 +++ 29 files changed, 192 insertions(+), 173 deletions(-) rename {internal => pkg}/config/config.go (73%) rename {internal => pkg}/config/config.json (87%) diff --git a/cmd/clean.go b/cmd/clean.go index 49a918fd..0783c7b5 100644 --- a/cmd/clean.go +++ b/cmd/clean.go @@ -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" diff --git a/cmd/describe.go b/cmd/describe.go index df5dfe81..9820bba3 100644 --- a/cmd/describe.go +++ b/cmd/describe.go @@ -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" @@ -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 @@ -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 } diff --git a/cmd/graph.go b/cmd/graph.go index 53984cd8..177aab23 100644 --- a/cmd/graph.go +++ b/cmd/graph.go @@ -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" @@ -94,10 +94,6 @@ 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 @@ -105,7 +101,7 @@ func NewGraphRunCommand(factory *providerfactory.ProviderFactory, scenarioOrches }) spinner.Start() go func() { - validateScenariosInput(dataProvider, dataSource, nodes, nameChannel) + validateScenariosInput(dataProvider, nodes, nameChannel) }() for { @@ -187,7 +183,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 }) { @@ -200,7 +196,7 @@ 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 @@ -212,7 +208,7 @@ func validateScenariosInput(provider provider.ScenarioDataProvider, dataSource s 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 } for k, v := range n.Env { @@ -264,13 +260,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 @@ -279,12 +271,8 @@ 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) - if err != nil { - return err - } dataProvider := GetProvider(false, factory) - output, err := dataProvider.ScaffoldScenarios(args, dataSource) + output, err := dataProvider.ScaffoldScenarios(args) if err != nil { return err } diff --git a/cmd/list.go b/cmd/list.go index 7f9bd25b..023a9d51 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -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" @@ -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) diff --git a/cmd/query_status.go b/cmd/query_status.go index 2a6580ae..a880359c 100644 --- a/cmd/query_status.go +++ b/cmd/query_status.go @@ -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" diff --git a/cmd/root.go b/cmd/root.go index e96c2d3e..550754b0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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" diff --git a/cmd/run.go b/cmd/run.go index 839894cd..411bba5d 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -4,7 +4,7 @@ import ( "errors" "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/scenario_orchestrator" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/utils" @@ -25,13 +25,8 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena DisableFlagParsing: false, 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 - } 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 @@ -41,12 +36,8 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena }, PreRunE: func(cmd *cobra.Command, args []string) error { - dataSource, err := BuildDataSource(config, false, nil) - if err != nil { - return err - } provider := GetProvider(false, factory) - scenarioDetail, err := provider.GetScenarioDetail(args[0], dataSource) + scenarioDetail, err := provider.GetScenarioDetail(args[0]) if err != nil { return err } @@ -71,10 +62,6 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena RunE: func(cmd *cobra.Command, args []string) error { (*scenarioOrchestrator).PrintContainerRuntime() spinner := NewSpinnerWithSuffix("validating input...") - dataSource, err := BuildDataSource(config, false, nil) - if err != nil { - return err - } // Starts validating input message spinner.Start() @@ -83,7 +70,7 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena debug := false provider := GetProvider(false, factory) - scenarioDetail, err := provider.GetScenarioDetail(args[0], dataSource) + scenarioDetail, err := provider.GetScenarioDetail(args[0]) if err != nil { return err } diff --git a/cmd/utils.go b/cmd/utils.go index 5e7828af..968130d7 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -2,7 +2,7 @@ package cmd import ( "github.com/briandowns/spinner" - "github.com/krkn-chaos/krknctl/internal/config" + "github.com/krkn-chaos/krknctl/pkg/config" "github.com/krkn-chaos/krknctl/pkg/provider" "github.com/krkn-chaos/krknctl/pkg/provider/factory" "github.com/spf13/cobra" @@ -39,8 +39,8 @@ func GetProvider(offline bool, providerFactory *factory.ProviderFactory) provide return dataProvider } -func FetchScenarios(provider provider.ScenarioDataProvider, dataSource string) (*[]string, error) { - scenarios, err := provider.GetRegistryImages(dataSource) +func FetchScenarios(provider provider.ScenarioDataProvider) (*[]string, error) { + scenarios, err := provider.GetRegistryImages() if err != nil { return nil, err } @@ -51,24 +51,6 @@ func FetchScenarios(provider provider.ScenarioDataProvider, dataSource string) ( return &foundScenarios, nil } -func BuildDataSource(config config.Config, offline bool, offlineSource *string) (string, error) { - var dataSource = "" - if offline == true { - if offlineSource != nil { - dataSource = *offlineSource - } else { - dataSource = "" - } - } else { - var err error - dataSource, err = config.GetQuayRepositoryApiUri() - if err != nil { - return "", err - } - } - return dataSource, nil -} - func CheckFileExists(filePath string) bool { if _, err := os.Stat(filePath); os.IsNotExist(err) { return false diff --git a/main.go b/main.go index fdaad08f..442c6a30 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/fatih/color" "github.com/krkn-chaos/krknctl/cmd" - krknctlconfig "github.com/krkn-chaos/krknctl/internal/config" + krknctlconfig "github.com/krkn-chaos/krknctl/pkg/config" providerfactory "github.com/krkn-chaos/krknctl/pkg/provider/factory" scenarioorchestratorfactory "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/factory" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/utils" diff --git a/internal/config/config.go b/pkg/config/config.go similarity index 73% rename from internal/config/config.go rename to pkg/config/config.go index 9c4e5f37..765d4e99 100644 --- a/internal/config/config.go +++ b/pkg/config/config.go @@ -10,8 +10,10 @@ type Config struct { Version string `json:"version"` QuayHost string `json:"quay_host"` QuayOrg string `json:"quay_org"` - QuayRegistry string `json:"quay_registry"` - QuayRepositoryApi string `json:"quay_repositoryApi"` + QuayScenarioRegistry string `json:"quay_scenario_registry"` + QuayBaseImageRegistry string `json:"quay_base_image_registry"` + QuayBaseImageTag string `json:"quay_base_image_tag"` + QuayRepositoryApi string `json:"quay_repository_api"` ContainerPrefix string `json:"container_prefix"` KubeconfigPrefix string `json:"kubeconfig_prefix"` PodmanDarwinSocketTemplate string `json:"podman_darwin_socket_template"` @@ -46,16 +48,25 @@ func LoadConfig() (Config, error) { } func (c *Config) GetQuayImageUri() (string, error) { - imageUri, err := url.JoinPath(c.QuayHost, c.QuayOrg, c.QuayRegistry) + imageUri, err := url.JoinPath(c.QuayHost, c.QuayOrg, c.QuayScenarioRegistry) if err != nil { return "", err } return imageUri, nil } -func (c *Config) GetQuayRepositoryApiUri() (string, error) { +func (c *Config) GetQuayScenarioRepositoryApiUri() (string, error) { baseHost := "https://" + c.QuayHost - repositoryUri, err := url.JoinPath(baseHost, c.QuayRepositoryApi, c.QuayOrg, c.QuayRegistry) + repositoryUri, err := url.JoinPath(baseHost, c.QuayRepositoryApi, c.QuayOrg, c.QuayScenarioRegistry) + if err != nil { + return "", err + } + return repositoryUri, nil +} + +func (c *Config) GetQuayEnvironmentApiUri() (string, error) { + baseHost := "https://" + c.QuayHost + repositoryUri, err := url.JoinPath(baseHost, c.QuayRepositoryApi, c.QuayOrg, c.QuayBaseImageRegistry) if err != nil { return "", err } diff --git a/internal/config/config.json b/pkg/config/config.json similarity index 87% rename from internal/config/config.json rename to pkg/config/config.json index 25fefa96..5ba4464f 100644 --- a/internal/config/config.json +++ b/pkg/config/config.json @@ -2,8 +2,10 @@ "version": "0.0.1", "quay_host": "quay.io", "quay_org": "krkn-chaos", - "quay_registry": "krkn-hub", - "quay_repositoryApi": "api/v1/repository", + "quay_scenario_registry": "krkn-hub", + "quay_base_image_registry" : "krkn", + "quay_base_image_tag" : "latest", + "quay_repository_api": "api/v1/repository", "container_prefix": "krknctl", "kubeconfig_prefix": "krknctl-kubeconfig", "krknctl_logs": "krknct-log", diff --git a/pkg/provider/factory/provider_factory.go b/pkg/provider/factory/provider_factory.go index a749be95..590efabf 100644 --- a/pkg/provider/factory/provider_factory.go +++ b/pkg/provider/factory/provider_factory.go @@ -1,7 +1,7 @@ package factory import ( - "github.com/krkn-chaos/krknctl/internal/config" + "github.com/krkn-chaos/krknctl/pkg/config" "github.com/krkn-chaos/krknctl/pkg/provider" "github.com/krkn-chaos/krknctl/pkg/provider/offline" "github.com/krkn-chaos/krknctl/pkg/provider/quay" diff --git a/pkg/provider/factory/provider_factory_test.go b/pkg/provider/factory/provider_factory_test.go index 469b083c..b3855193 100644 --- a/pkg/provider/factory/provider_factory_test.go +++ b/pkg/provider/factory/provider_factory_test.go @@ -1,7 +1,7 @@ package factory import ( - "github.com/krkn-chaos/krknctl/internal/config" + "github.com/krkn-chaos/krknctl/pkg/config" "github.com/krkn-chaos/krknctl/pkg/provider" "github.com/krkn-chaos/krknctl/pkg/provider/offline" "github.com/krkn-chaos/krknctl/pkg/provider/quay" diff --git a/pkg/provider/offline/scenario_provider.go b/pkg/provider/offline/scenario_provider.go index 746af2e8..950b22be 100644 --- a/pkg/provider/offline/scenario_provider.go +++ b/pkg/provider/offline/scenario_provider.go @@ -8,15 +8,20 @@ import ( type ScenarioProvider struct { } -func (p *ScenarioProvider) GetRegistryImages(dataSource string) (*[]models.ScenarioTag, error) { +func (p *ScenarioProvider) GetRegistryImages() (*[]models.ScenarioTag, error) { return nil, errors.New("not yet implemented") } -func (p *ScenarioProvider) GetScenarioDetail(scenario string, dataSource string) (*models.ScenarioDetail, error) { +func (p *ScenarioProvider) GetScenarioDetail(scenario string) (*models.ScenarioDetail, error) { return nil, errors.New("not yet implemented") } -func (p *ScenarioProvider) ScaffoldScenarios(scenarios []string, dataSource string) (*string, error) { +func (p *ScenarioProvider) ScaffoldScenarios(scenarios []string) (*string, error) { + //TODO implement me + panic("implement me") +} + +func (p *ScenarioProvider) GetGlobalEnvironment() (*models.ScenarioDetail, error) { //TODO implement me panic("implement me") } diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go index e5bf2554..4b0789b1 100644 --- a/pkg/provider/provider.go +++ b/pkg/provider/provider.go @@ -10,7 +10,8 @@ const ( ) type ScenarioDataProvider interface { - GetRegistryImages(dataSource string) (*[]models.ScenarioTag, error) - GetScenarioDetail(scenario string, dataSource string) (*models.ScenarioDetail, error) - ScaffoldScenarios(scenarios []string, dataSource string) (*string, error) + GetRegistryImages() (*[]models.ScenarioTag, error) + GetGlobalEnvironment() (*models.ScenarioDetail, error) + GetScenarioDetail(scenario string) (*models.ScenarioDetail, error) + ScaffoldScenarios(scenarios []string) (*string, error) } diff --git a/pkg/provider/quay/scenario_provider.go b/pkg/provider/quay/scenario_provider.go index 15932875..882bb706 100644 --- a/pkg/provider/quay/scenario_provider.go +++ b/pkg/provider/quay/scenario_provider.go @@ -6,7 +6,7 @@ import ( "errors" "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/models" models2 "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models" "github.com/krkn-chaos/krknctl/pkg/typing" @@ -23,7 +23,7 @@ type ScenarioProvider struct { Config *config.Config } -func (p *ScenarioProvider) GetRegistryImages(dataSource string) (*[]models.ScenarioTag, error) { +func (p *ScenarioProvider) getRegistryImages(dataSource string) (*[]models.ScenarioTag, error) { tagBaseUrl, err := url.Parse(dataSource + "/tag") if err != nil { return nil, err @@ -65,6 +65,19 @@ func (p *ScenarioProvider) GetRegistryImages(dataSource string) (*[]models.Scena return &scenarioTags, deferErr } +func (p *ScenarioProvider) GetRegistryImages() (*[]models.ScenarioTag, error) { + dataSource, err := p.Config.GetQuayScenarioRepositoryApiUri() + if err != nil { + return nil, err + } + + scenarioTags, err := p.getRegistryImages(dataSource) + if err != nil { + return nil, err + } + return scenarioTags, nil +} + func (p *ScenarioProvider) getInstructionScenario(rootNodeName string) models2.ScenarioNode { node := models2.ScenarioNode{} node.Comment = fmt.Sprintf("**READ CAREFULLY** To create your scenario run plan, assign an ID to each scenario definition (or keep the existing randomly assigned ones if preferred). "+ @@ -74,7 +87,7 @@ func (p *ScenarioProvider) getInstructionScenario(rootNodeName string) models2.S return node } -func (p *ScenarioProvider) ScaffoldScenarios(scenarios []string, dataSource string) (*string, error) { +func (p *ScenarioProvider) ScaffoldScenarios(scenarios []string) (*string, error) { var scenarioDetails []models.ScenarioDetail // handles babble panic when american word dictionary is not installed @@ -87,7 +100,7 @@ func (p *ScenarioProvider) ScaffoldScenarios(scenarios []string, dataSource stri babbler := babble.NewBabbler() babbler.Count = 1 for _, scenarioName := range scenarios { - scenarioDetail, err := p.GetScenarioDetail(scenarioName, dataSource) + scenarioDetail, err := p.GetScenarioDetail(scenarioName) if err != nil { return nil, err @@ -99,7 +112,7 @@ func (p *ScenarioProvider) ScaffoldScenarios(scenarios []string, dataSource stri scenarioDetails = append(scenarioDetails, *scenarioDetail) } - // builds all the indexes for the json upfront so I can suggest the root node in the _comment + // builds all the indexes for the json upfront, so I can suggest the root node in the _comment var indexes []string for _, scenario := range scenarios { indexes = append(indexes, fmt.Sprintf("%s-%s", scenario, strings.ToLower(babbler.Babble()))) @@ -159,22 +172,8 @@ func (p *ScenarioProvider) ScaffoldScenarios(scenarios []string, dataSource stri return &jsonBuf, nil } -func (p *ScenarioProvider) GetScenarioDetail(scenario string, dataSource string) (*models.ScenarioDetail, error) { - scenarios, err := p.GetRegistryImages(dataSource) - if err != nil { - return nil, err - } - var foundScenario *models.ScenarioTag = nil - for _, scenarioTag := range *scenarios { - if scenarioTag.Name == scenario { - foundScenario = &scenarioTag - } - } - if foundScenario == nil { - return nil, nil - } - - baseURL, err := url.Parse(dataSource + "/manifest/" + foundScenario.Digest) +func (p *ScenarioProvider) getScenarioDetail(dataSource string, foundScenario *models.ScenarioTag) (*models.ScenarioDetail, error) { + baseURL, err := url.Parse(dataSource + "/manifest/" + (*foundScenario).Digest) if err != nil { return nil, err } @@ -239,6 +238,62 @@ func (p *ScenarioProvider) GetScenarioDetail(scenario string, dataSource string) return &scenarioDetail, deferErr } +func (p *ScenarioProvider) GetScenarioDetail(scenario string) (*models.ScenarioDetail, error) { + dataSource, err := p.Config.GetQuayScenarioRepositoryApiUri() + if err != nil { + return nil, err + } + scenarios, err := p.GetRegistryImages() + if err != nil { + return nil, err + } + var foundScenario *models.ScenarioTag = nil + for _, scenarioTag := range *scenarios { + if scenarioTag.Name == scenario { + foundScenario = &scenarioTag + } + } + if foundScenario == nil { + return nil, nil + } + + scenarioDetail, err := p.getScenarioDetail(dataSource, foundScenario) + if err != nil { + return nil, err + } + return scenarioDetail, nil +} + +func (p *ScenarioProvider) GetGlobalEnvironment() (*models.ScenarioDetail, error) { + dataSource, err := p.Config.GetQuayEnvironmentApiUri() + if err != nil { + return nil, err + } + var foundScenario *models.ScenarioTag = nil + scenarios, err := p.getRegistryImages(dataSource) + if err != nil { + return nil, err + } + if scenarios == nil { + return nil, fmt.Errorf("no tags found in registry %s", dataSource) + } + for _, scenarioTag := range *scenarios { + if scenarioTag.Name == p.Config.QuayBaseImageTag { + foundScenario = &scenarioTag + } + } + if foundScenario == nil { + return nil, nil + } + + globalEnvDetail, err := p.getScenarioDetail(dataSource, foundScenario) + if err != nil { + return nil, err + } + return globalEnvDetail, nil + +} + func (p *ScenarioProvider) parseTitle(s string) (*string, error) { reDoubleQuotes, err := regexp.Compile(p.Config.LabelTitleRegex) if err != nil { diff --git a/pkg/provider/quay/scenario_provider_test.go b/pkg/provider/quay/scenario_provider_test.go index a7b0769a..90ba1984 100644 --- a/pkg/provider/quay/scenario_provider_test.go +++ b/pkg/provider/quay/scenario_provider_test.go @@ -1,7 +1,7 @@ package quay import ( - krknctlconfig "github.com/krkn-chaos/krknctl/internal/config" + krknctlconfig "github.com/krkn-chaos/krknctl/pkg/config" "github.com/stretchr/testify/assert" "strings" "testing" @@ -16,23 +16,20 @@ func getConfig(t *testing.T) krknctlconfig.Config { func getTestConfig(t *testing.T) krknctlconfig.Config { conf := getConfig(t) - conf.QuayRegistry = "krknctl-test" + conf.QuayScenarioRegistry = "krknctl-test" return conf } func getWrongConfig(t *testing.T) krknctlconfig.Config { conf := getConfig(t) - conf.QuayRegistry = "do_not_exist" + conf.QuayScenarioRegistry = "do_not_exist" return conf } func TestScenarioProvider_GetRegistryImages(t *testing.T) { config := getTestConfig(t) provider := ScenarioProvider{Config: &config} - - uri, err := config.GetQuayRepositoryApiUri() - assert.NoError(t, err) - scenarios, err := provider.GetRegistryImages(uri) + scenarios, err := provider.GetRegistryImages() assert.Nil(t, err) assert.NotNil(t, scenarios) assert.Greater(t, len(*scenarios), 0) @@ -45,9 +42,7 @@ func TestScenarioProvider_GetRegistryImages(t *testing.T) { wrongConfig := getWrongConfig(t) wrongProvider := ScenarioProvider{Config: &wrongConfig} - uri, err = wrongConfig.GetQuayRepositoryApiUri() - assert.NoError(t, err) - _, err = wrongProvider.GetRegistryImages(uri) + _, err = wrongProvider.GetRegistryImages() assert.Error(t, err) } @@ -55,30 +50,28 @@ func TestScenarioProvider_GetRegistryImages(t *testing.T) { func TestQuayScenarioProvider_GetScenarioDetail(t *testing.T) { config := getTestConfig(t) provider := ScenarioProvider{Config: &config} - uri, err := config.GetQuayRepositoryApiUri() - assert.NoError(t, err) - scenario, err := provider.GetScenarioDetail("cpu-hog", uri) + scenario, err := provider.GetScenarioDetail("cpu-hog") assert.Nil(t, err) assert.NotNil(t, scenario) assert.Equal(t, len(scenario.Fields), 5) - scenario, err = provider.GetScenarioDetail("cpu-memory-notitle", uri) + scenario, err = provider.GetScenarioDetail("cpu-memory-notitle") assert.NotNil(t, err) assert.True(t, strings.Contains(err.Error(), "krknctl.title LABEL not found in tag: cpu-memory-notitle")) assert.Nil(t, scenario) - scenario, err = provider.GetScenarioDetail("cpu-memory-nodescription", uri) + scenario, err = provider.GetScenarioDetail("cpu-memory-nodescription") assert.NotNil(t, err) assert.True(t, strings.Contains(err.Error(), "krknctl.description LABEL not found in tag: cpu-memory-nodescription")) assert.Nil(t, scenario) - scenario, err = provider.GetScenarioDetail("cpu-memory-noinput", uri) + scenario, err = provider.GetScenarioDetail("cpu-memory-noinput") assert.NotNil(t, err) assert.True(t, strings.Contains(err.Error(), "krknctl.input_fields LABEL not found in tag: cpu-memory-noinput")) assert.Nil(t, scenario) - scenario, err = provider.GetScenarioDetail("not-found", uri) + scenario, err = provider.GetScenarioDetail("not-found") assert.Nil(t, err) assert.Nil(t, scenario) @@ -86,21 +79,30 @@ func TestQuayScenarioProvider_GetScenarioDetail(t *testing.T) { func TestQuayScenarioProvider_ScaffoldScenarios(t *testing.T) { config := getConfig(t) - uri, err := config.GetQuayRepositoryApiUri() - assert.NoError(t, err) provider := ScenarioProvider{Config: &config} - scenarios, err := provider.GetRegistryImages(uri) + scenarios, err := provider.GetRegistryImages() assert.Nil(t, err) assert.NotNil(t, scenarios) scenarioNames := []string{"node-cpu-hog", "node-memory-hog", "dummy-scenario"} - json, err := provider.ScaffoldScenarios(scenarioNames, uri) + json, err := provider.ScaffoldScenarios(scenarioNames) assert.Nil(t, err) assert.NotNil(t, json) - json, err = provider.ScaffoldScenarios([]string{"node-cpu-hog", "does-not-exist"}, uri) + json, err = provider.ScaffoldScenarios([]string{"node-cpu-hog", "does-not-exist"}) assert.Nil(t, json) assert.NotNil(t, err) } + +func TestQuayScenarioProvider_GetGlobalEnvironment(t *testing.T) { + config := getConfig(t) + config.QuayOrg = "rh_ee_tsebasti" + provider := ScenarioProvider{Config: &config} + + baseImageScenario, err := provider.GetGlobalEnvironment() + assert.Nil(t, err) + assert.NotNil(t, baseImageScenario) + assert.Greater(t, len(baseImageScenario.Fields), 0) +} diff --git a/pkg/scenario_orchestrator/common_functions.go b/pkg/scenario_orchestrator/common_functions.go index 921da60a..f7961790 100644 --- a/pkg/scenario_orchestrator/common_functions.go +++ b/pkg/scenario_orchestrator/common_functions.go @@ -4,7 +4,7 @@ import ( "context" "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/scenario_orchestrator/models" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/utils" "io" diff --git a/pkg/scenario_orchestrator/docker/scenario_orchestrator.go b/pkg/scenario_orchestrator/docker/scenario_orchestrator.go index 541e9f58..2ef00234 100644 --- a/pkg/scenario_orchestrator/docker/scenario_orchestrator.go +++ b/pkg/scenario_orchestrator/docker/scenario_orchestrator.go @@ -12,7 +12,7 @@ import ( "github.com/docker/docker/client" "github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/stdcopy" - "github.com/krkn-chaos/krknctl/internal/config" + "github.com/krkn-chaos/krknctl/pkg/config" providermodels "github.com/krkn-chaos/krknctl/pkg/provider/models" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator" orchestratormodels "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models" diff --git a/pkg/scenario_orchestrator/docker/scenario_orchestrator_test.go b/pkg/scenario_orchestrator/docker/scenario_orchestrator_test.go index a7bb76ce..46e630da 100644 --- a/pkg/scenario_orchestrator/docker/scenario_orchestrator_test.go +++ b/pkg/scenario_orchestrator/docker/scenario_orchestrator_test.go @@ -4,7 +4,7 @@ import ( "context" "fmt" dockercontainer "github.com/docker/docker/api/types/container" - "github.com/krkn-chaos/krknctl/internal/config" + "github.com/krkn-chaos/krknctl/pkg/config" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/test" "github.com/stretchr/testify/assert" diff --git a/pkg/scenario_orchestrator/factory/factory.go b/pkg/scenario_orchestrator/factory/factory.go index 81754132..b9a12465 100644 --- a/pkg/scenario_orchestrator/factory/factory.go +++ b/pkg/scenario_orchestrator/factory/factory.go @@ -1,7 +1,7 @@ package factory import ( - "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/docker" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models" diff --git a/pkg/scenario_orchestrator/factory/factory_test.go b/pkg/scenario_orchestrator/factory/factory_test.go index 07b89f0d..51e48213 100644 --- a/pkg/scenario_orchestrator/factory/factory_test.go +++ b/pkg/scenario_orchestrator/factory/factory_test.go @@ -1,7 +1,7 @@ package factory import ( - "github.com/krkn-chaos/krknctl/internal/config" + "github.com/krkn-chaos/krknctl/pkg/config" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/docker" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/podman" diff --git a/pkg/scenario_orchestrator/podman/scenario_orchestrator.go b/pkg/scenario_orchestrator/podman/scenario_orchestrator.go index da1165c2..d4bc382f 100644 --- a/pkg/scenario_orchestrator/podman/scenario_orchestrator.go +++ b/pkg/scenario_orchestrator/podman/scenario_orchestrator.go @@ -11,7 +11,7 @@ import ( "github.com/containers/podman/v5/pkg/errorhandling" "github.com/containers/podman/v5/pkg/specgen" "github.com/docker/docker/api/types/mount" - "github.com/krkn-chaos/krknctl/internal/config" + "github.com/krkn-chaos/krknctl/pkg/config" providermodels "github.com/krkn-chaos/krknctl/pkg/provider/models" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator" orchestratormodels "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models" diff --git a/pkg/scenario_orchestrator/podman/scenario_orchestrator_test.go b/pkg/scenario_orchestrator/podman/scenario_orchestrator_test.go index efb26611..a9cd7b54 100644 --- a/pkg/scenario_orchestrator/podman/scenario_orchestrator_test.go +++ b/pkg/scenario_orchestrator/podman/scenario_orchestrator_test.go @@ -4,7 +4,7 @@ import ( "context" "fmt" "github.com/containers/podman/v5/pkg/bindings/containers" - "github.com/krkn-chaos/krknctl/internal/config" + "github.com/krkn-chaos/krknctl/pkg/config" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/test" "github.com/stretchr/testify/assert" diff --git a/pkg/scenario_orchestrator/scenario_orchestrator.go b/pkg/scenario_orchestrator/scenario_orchestrator.go index d5670040..e17fb4b8 100644 --- a/pkg/scenario_orchestrator/scenario_orchestrator.go +++ b/pkg/scenario_orchestrator/scenario_orchestrator.go @@ -2,7 +2,7 @@ package scenario_orchestrator import ( "context" - "github.com/krkn-chaos/krknctl/internal/config" + "github.com/krkn-chaos/krknctl/pkg/config" orchestrator_models "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models" "io" "os" diff --git a/pkg/scenario_orchestrator/test/common_test_functions.go b/pkg/scenario_orchestrator/test/common_test_functions.go index f2eafa5e..5cbd181f 100644 --- a/pkg/scenario_orchestrator/test/common_test_functions.go +++ b/pkg/scenario_orchestrator/test/common_test_functions.go @@ -4,7 +4,7 @@ import ( "encoding/json" "errors" "fmt" - krknctlconfig "github.com/krkn-chaos/krknctl/internal/config" + krknctlconfig "github.com/krkn-chaos/krknctl/pkg/config" "github.com/krkn-chaos/krknctl/pkg/dependencygraph" "github.com/krkn-chaos/krknctl/pkg/provider/quay" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator" @@ -28,7 +28,7 @@ func CommonGetConfig(t *testing.T) krknctlconfig.Config { func CommonGetTestConfig(t *testing.T) krknctlconfig.Config { conf := CommonGetConfig(t) - conf.QuayRegistry = "krknctl-test" + conf.QuayScenarioRegistry = "krknctl-test" return conf } @@ -44,10 +44,7 @@ func CommonTestScenarioOrchestratorRun(t *testing.T, so scenario_orchestrator.Sc registryUri, err := conf.GetQuayImageUri() assert.Nil(t, err) - apiUri, err := conf.GetQuayRepositoryApiUri() - assert.Nil(t, err) - - scenario, err := quayProvider.GetScenarioDetail("dummy-scenario", apiUri) + scenario, err := quayProvider.GetScenarioDetail("dummy-scenario") assert.Nil(t, err) assert.NotNil(t, scenario) kubeconfig, err := utils.PrepareKubeconfig(nil, conf) @@ -91,9 +88,7 @@ func CommonTestScenarioOrchestratorRunAttached(t *testing.T, so scenario_orchest quayProvider := quay.ScenarioProvider{Config: &conf} registryUri, err := conf.GetQuayImageUri() assert.Nil(t, err) - apiUri, err := conf.GetQuayRepositoryApiUri() - assert.Nil(t, err) - scenario, err := quayProvider.GetScenarioDetail("failing-scenario", apiUri) + scenario, err := quayProvider.GetScenarioDetail("failing-scenario") assert.Nil(t, err) assert.NotNil(t, scenario) kubeconfig, err := utils.PrepareKubeconfig(nil, conf) @@ -230,9 +225,7 @@ func CommonTestScenarioOrchestratorRunGraph(t *testing.T, so scenario_orchestrat fmt.Println("Current user: " + (*currentUser).Name) fmt.Println("current user id" + (*currentUser).Uid) quayProvider := quay.ScenarioProvider{Config: &config} - repositoryApi, err := config.GetQuayRepositoryApiUri() - assert.Nil(t, err) - scenario, err := quayProvider.GetScenarioDetail("dummy-scenario", repositoryApi) + scenario, err := quayProvider.GetScenarioDetail("dummy-scenario") assert.Nil(t, err) assert.NotNil(t, scenario) kubeconfig, err := utils.PrepareKubeconfig(nil, config) @@ -386,9 +379,7 @@ func CommonTestScenarioOrchestratorResolveContainerName(t *testing.T, so scenari quayProvider := quay.ScenarioProvider{Config: &conf} registryUri, err := conf.GetQuayImageUri() assert.Nil(t, err) - apiUri, err := conf.GetQuayRepositoryApiUri() - assert.Nil(t, err) - scenario, err := quayProvider.GetScenarioDetail("failing-scenario", apiUri) + scenario, err := quayProvider.GetScenarioDetail("failing-scenario") assert.Nil(t, err) assert.NotNil(t, scenario) kubeconfig, err := utils.PrepareKubeconfig(nil, conf) @@ -439,10 +430,7 @@ func CommonTestScenarioOrchestratorKillContainers(t *testing.T, so scenario_orch registryUri, err := conf.GetQuayImageUri() assert.Nil(t, err) - apiUri, err := conf.GetQuayRepositoryApiUri() - assert.Nil(t, err) - - scenario, err := quayProvider.GetScenarioDetail("dummy-scenario", apiUri) + scenario, err := quayProvider.GetScenarioDetail("dummy-scenario") assert.Nil(t, err) assert.NotNil(t, scenario) kubeconfig, err := utils.PrepareKubeconfig(nil, conf) @@ -508,10 +496,7 @@ func CommonTestScenarioOrchestratorListRunningScenarios(t *testing.T, so scenari registryUri, err := conf.GetQuayImageUri() assert.Nil(t, err) - apiUri, err := conf.GetQuayRepositoryApiUri() - assert.Nil(t, err) - - scenario, err := quayProvider.GetScenarioDetail("dummy-scenario", apiUri) + scenario, err := quayProvider.GetScenarioDetail("dummy-scenario") assert.Nil(t, err) assert.NotNil(t, scenario) kubeconfig, err := utils.PrepareKubeconfig(nil, conf) @@ -575,10 +560,7 @@ func CommonTestScenarioOrchestratorInspectRunningScenario(t *testing.T, so scena registryUri, err := conf.GetQuayImageUri() assert.Nil(t, err) - apiUri, err := conf.GetQuayRepositoryApiUri() - assert.Nil(t, err) - - scenario, err := quayProvider.GetScenarioDetail("dummy-scenario", apiUri) + scenario, err := quayProvider.GetScenarioDetail("dummy-scenario") assert.Nil(t, err) assert.NotNil(t, scenario) kubeconfig, err := utils.PrepareKubeconfig(nil, conf) diff --git a/pkg/scenario_orchestrator/utils/utils.go b/pkg/scenario_orchestrator/utils/utils.go index 2637b544..3455a431 100644 --- a/pkg/scenario_orchestrator/utils/utils.go +++ b/pkg/scenario_orchestrator/utils/utils.go @@ -7,7 +7,7 @@ import ( "github.com/containers/podman/v5/pkg/bindings" images "github.com/docker/docker/api/types/image" "github.com/docker/docker/client" - "github.com/krkn-chaos/krknctl/internal/config" + "github.com/krkn-chaos/krknctl/pkg/config" orchestatormodels "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models" "github.com/krkn-chaos/krknctl/pkg/text" "io/fs" diff --git a/pkg/scenario_orchestrator/utils/utils_test.go b/pkg/scenario_orchestrator/utils/utils_test.go index 6d1ffe57..1fd5c274 100644 --- a/pkg/scenario_orchestrator/utils/utils_test.go +++ b/pkg/scenario_orchestrator/utils/utils_test.go @@ -2,7 +2,7 @@ package utils import ( "fmt" - krknctlconfig "github.com/krkn-chaos/krknctl/internal/config" + krknctlconfig "github.com/krkn-chaos/krknctl/pkg/config" "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models" "github.com/stretchr/testify/assert" "io/fs" diff --git a/pkg/typing/field_test.go b/pkg/typing/field_test.go index 92b9b844..c8588456 100644 --- a/pkg/typing/field_test.go +++ b/pkg/typing/field_test.go @@ -187,6 +187,22 @@ func TestStringField(t *testing.T) { _, err = field.Validate(nil) assert.NotNil(t, err) + // tests the correctness of a complex string with lots of escapes as a default value + + stringComplexRegex := ` + { + "name": "telemetry-filter-pattern", + "short_description": "Telemetry filter pattern", + "description": "Filter pattern for telemetry logs", + "variable": "TELEMETRY_FILTER_PATTERN", + "type": "string", + "default": "[\"(\\\\w{3}\\\\s\\\\d{1,2}\\\\s\\\\d{2}:\\\\d{2}:\\\\d{2}\\\\.\\\\d+).+\",\"kinit (\\\\d+/\\\\d+/\\\\d+\\\\s\\\\d{2}:\\\\d{2}:\\\\d{2})\\\\s+\",\"(\\\\d{4}-\\\\d{2}-\\\\d{2}T\\\\d{2}:\\\\d{2}:\\\\d{2}\\\\.\\\\d+Z).+\"]", + "required": "false" + } +` + err = json.Unmarshal([]byte(stringComplexRegex), &field) + assert.Nil(t, err) + } func TestBooleanField(t *testing.T) {