Skip to content

Commit

Permalink
refactoring + GetGlobalEnvironment
Browse files Browse the repository at this point in the history
Signed-off-by: Tullio Sebastiani <[email protected]>

test fixes

Signed-off-by: Tullio Sebastiani <[email protected]>
  • Loading branch information
tsebastiani committed Dec 11, 2024
1 parent aa0de25 commit d16f9e2
Show file tree
Hide file tree
Showing 29 changed files with 192 additions and 173 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
26 changes: 7 additions & 19 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 @@ -94,18 +94,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 @@ -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
}) {
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
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
2 changes: 1 addition & 1 deletion 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
21 changes: 4 additions & 17 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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()
Expand All @@ -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
}
Expand Down
24 changes: 3 additions & 21 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 16 additions & 5 deletions internal/config/config.go → pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions internal/config/config.json → pkg/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/factory/provider_factory.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/factory/provider_factory_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
11 changes: 8 additions & 3 deletions pkg/provider/offline/scenario_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
7 changes: 4 additions & 3 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading

0 comments on commit d16f9e2

Please sign in to comment.