Skip to content

Commit

Permalink
Refactored operator registry
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco committed Oct 14, 2024
1 parent a7dd26b commit cfd66d6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ go 1.22.5
require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.18.0
golang.org/x/mod v0.21.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
golang.org/x/sys v0.25.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
4 changes: 2 additions & 2 deletions internal/support/cmdexecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ type CmdExecutor interface {
Exec(name string, arg ...string) ([]byte, error)
}

type Executor struct{}
type CliExecutor struct{}

func (e Executor) Exec(name string, arg ...string) ([]byte, error) {
func (e CliExecutor) Exec(name string, arg ...string) ([]byte, error) {
cmd := exec.Command(name, arg...)
return cmd.Output()
}
6 changes: 2 additions & 4 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type OPERATION_PHASES string

type OperationsArguments map[string]any
type OperatorArguments map[string]any
type Option[T any] func(*T)

const (
Expand All @@ -16,9 +16,7 @@ const (
ROLLBACK OPERATION_PHASES = "ROLLBACK"
)

type OperatorArguments OperationsArguments

type Runner interface {
type Operator interface {
Run(ctx context.Context) *ExecutionReport
}

Expand Down
44 changes: 29 additions & 15 deletions pkg/operator/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ func (e *OperatorNotFoundError) Error() string {
return fmt.Sprintf("operator %s not found", e.Name)
}

type RunnerBuilderFunction func(arguments OperatorArguments, operationID string) *Runner
type OperatorBuilder func(operationID string, arguments OperatorArguments) Operator

// map[operatorName]map[operatorVersion]RunnerBuilderFunction
type OperatorsTree map[string]map[string]RunnerBuilderFunction
// map[operatorName]map[operatorVersion]OperatorBuilder
type operatorBuildersTree map[string]map[string]OperatorBuilder

func extractVersionAndRunnerName(gathererName string) (string, string, error) {
parts := strings.Split(gathererName, "@")
func extractOperatorNameAndVersion(operatorName string) (string, string, error) {
parts := strings.Split(operatorName, "@")
if len(parts) == 1 {
// no version found, just gatherer name
// no version found, just operator name
return parts[0], "", nil
}
if len(parts) != 2 {
return "", "", fmt.Errorf(
"could not extract the runner version from %s, version should follow <operatorName>@<version> syntax",
gathererName,
"could not extract the operator version from %s, version should follow <operatorName>@<version> syntax",
operatorName,
)
}
return parts[0], parts[1], nil
}

type Registry struct {
operators OperatorsTree
operators operatorBuildersTree
}

func (m *Registry) GetOperatorRunnerBuilder(name string) (RunnerBuilderFunction, error) {
operatorName, version, err := extractVersionAndRunnerName(name)
func (m *Registry) GetOperatorBuilder(name string) (OperatorBuilder, error) {
operatorName, version, err := extractOperatorNameAndVersion(name)
if err != nil {
return nil, err
}
Expand All @@ -58,21 +58,21 @@ func (m *Registry) GetOperatorRunnerBuilder(name string) (RunnerBuilderFunction,
}

func (m *Registry) AvailableOperators() []string {
gatherersList := []string{}
operatorList := []string{}

for operatorName, versions := range m.operators {
operatorVersions := []string{}
for v := range versions {
operatorVersions = append(operatorVersions, v)
}
sort.Strings(operatorVersions)
gatherersList = append(
gatherersList,
operatorList = append(
operatorList,
fmt.Sprintf("%s - %s", operatorName, strings.Join(operatorVersions, "/")),
)
}

return gatherersList
return operatorList
}

func (m *Registry) getLatestVersionForOperator(name string) (string, error) {
Expand All @@ -89,3 +89,17 @@ func (m *Registry) getLatestVersionForOperator(name string) (string, error) {

return versions[len(versions)-1], nil
}

func StandardRegistry(options ...BaseOperationOption) *Registry {
return &Registry{
operators: operatorBuildersTree{
SaptuneApplySolutionOperatorName: map[string]OperatorBuilder{
"v1": func(operationID string, arguments OperatorArguments) Operator {
return NewSaptuneApplySolution(arguments, operationID, OperatorOptions[saptuneApplySolution]{
BaseOperatorOptions: options,
})
},
},
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewSaptuneApplySolution(
) *Executor {
saptuneApply := &saptuneApplySolution{
baseOperation: newBaseOperator(operationID, arguments, options.BaseOperatorOptions...),
executor: support.Executor{},
executor: support.CliExecutor{},
}

for _, opt := range options.OperatorOptions {
Expand Down

0 comments on commit cfd66d6

Please sign in to comment.