Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Don't use globals for registering platforms configuration structs
Browse files Browse the repository at this point in the history
This commit moves platforms registration logic from using globals to
single function in cli/cmd/cluster package, as this is the only user of
old GetPlatforms() function.

For platform unit tests, NewConfig() function exported by each platform
package should be used instead.

Refs #992

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Dec 3, 2020
1 parent 79ad7c7 commit 392d1e9
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 51 deletions.
23 changes: 22 additions & 1 deletion cli/cmd/cluster/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
"github.com/kinvolk/lokomotive/pkg/backend"
"github.com/kinvolk/lokomotive/pkg/config"
"github.com/kinvolk/lokomotive/pkg/platform"
"github.com/kinvolk/lokomotive/pkg/platform/aks"
"github.com/kinvolk/lokomotive/pkg/platform/aws"
"github.com/kinvolk/lokomotive/pkg/platform/baremetal"
"github.com/kinvolk/lokomotive/pkg/platform/packet"
"github.com/kinvolk/lokomotive/pkg/platform/tinkerbell"
)

const (
Expand Down Expand Up @@ -54,6 +59,22 @@ func getConfiguredBackend(lokoConfig *config.Config) (backend.Backend, hcl.Diagn
return backend, backend.LoadConfig(&lokoConfig.RootConfig.Backend.Config, lokoConfig.EvalContext)
}

func getPlatform(name string) (platform.Platform, error) {
platforms := map[string]platform.Platform{
aks.Name: aks.NewConfig(),
aws.Name: aws.NewConfig(),
packet.Name: packet.NewConfig(),
baremetal.Name: baremetal.NewConfig(),
tinkerbell.Name: tinkerbell.NewConfig(),
}

if p, ok := platforms[name]; ok {
return p, nil
}

return nil, fmt.Errorf("platform %q not found", name)
}

// getConfiguredPlatform loads a platform from the given configuration file.
func getConfiguredPlatform(lokoConfig *config.Config, require bool) (platform.Platform, hcl.Diagnostics) {
if lokoConfig.RootConfig.Cluster == nil && !require {
Expand All @@ -70,7 +91,7 @@ func getConfiguredPlatform(lokoConfig *config.Config, require bool) (platform.Pl
}
}

platform, err := platform.GetPlatform(lokoConfig.RootConfig.Cluster.Name)
platform, err := getPlatform(lokoConfig.RootConfig.Cluster.Name)
if err != nil {
diag := &hcl.Diagnostic{
Severity: hcl.DiagError,
Expand Down
5 changes: 0 additions & 5 deletions pkg/platform/aks/aks.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ const (
kubernetesVersion = "1.18.10"
)

// init registers AKS as a platform.
func init() { //nolint:gochecknoinits
platform.Register(Name, NewConfig())
}

// NewConfig returns new AKS platform configuration with default values set.
//
//nolint:golint
Expand Down
5 changes: 0 additions & 5 deletions pkg/platform/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ const (
Name = "aws"
)

// init registers aws as a platform
func init() {
platform.Register(Name, NewConfig())
}

func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics {
if configBody == nil {
return hcl.Diagnostics{}
Expand Down
5 changes: 0 additions & 5 deletions pkg/platform/baremetal/baremetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ const (
Name = "bare-metal"
)

// init registers bare-metal as a platform
func init() {
platform.Register(Name, NewConfig())
}

func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics {
if configBody == nil {
return hcl.Diagnostics{}
Expand Down
5 changes: 0 additions & 5 deletions pkg/platform/packet/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ const (
Name = "packet"
)

// init registers packet as a platform
func init() {
platform.Register(Name, NewConfig())
}

func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics {
if configBody == nil {
return hcl.Diagnostics{}
Expand Down
25 changes: 0 additions & 25 deletions pkg/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,6 @@ type Meta struct {
ControlplaneCharts []helm.LokomotiveChart
}

// platforms is a collection where all platforms gets automatically registered
var platforms map[string]Platform

// initialize package's global variable when package is imported
func init() {
platforms = make(map[string]Platform)
}

// Register adds platform into internal map
func Register(name string, p Platform) {
if _, exists := platforms[name]; exists {
panic(fmt.Sprintf("platform with name %q registered already", name))
}
platforms[name] = p
}

// GetPlatform returns platform based on the name
func GetPlatform(name string) (Platform, error) {
platform, exists := platforms[name]
if !exists {
return nil, fmt.Errorf("no platform with name %q found", name)
}
return platform, nil
}

// AppendVersionTag appends the lokoctl-version tag to a given tags map.
func AppendVersionTag(tags *map[string]string) {
if tags == nil {
Expand Down
5 changes: 0 additions & 5 deletions pkg/platform/tinkerbell/tinkerbell.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ func (w *WorkerPool) Name() string {
return w.PoolName
}

// init registers tinkerbell as a platform.
func init() { //nolint:gochecknoinits
platform.Register(Name, NewConfig())
}

// LoadConfig loads platform configuration using given HCL structs.
func (c *Config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics {
if configBody == nil {
Expand Down

0 comments on commit 392d1e9

Please sign in to comment.