From 392d1e9308c63015a427174433140196abe2e624 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Thu, 29 Oct 2020 17:07:51 +0100 Subject: [PATCH] Don't use globals for registering platforms configuration structs 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 --- cli/cmd/cluster/utils.go | 23 ++++++++++++++++++++++- pkg/platform/aks/aks.go | 5 ----- pkg/platform/aws/aws.go | 5 ----- pkg/platform/baremetal/baremetal.go | 5 ----- pkg/platform/packet/packet.go | 5 ----- pkg/platform/platform.go | 25 ------------------------- pkg/platform/tinkerbell/tinkerbell.go | 5 ----- 7 files changed, 22 insertions(+), 51 deletions(-) diff --git a/cli/cmd/cluster/utils.go b/cli/cmd/cluster/utils.go index 3171e1ff4..747038722 100644 --- a/cli/cmd/cluster/utils.go +++ b/cli/cmd/cluster/utils.go @@ -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 ( @@ -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 { @@ -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, diff --git a/pkg/platform/aks/aks.go b/pkg/platform/aks/aks.go index a756c2726..e36c29772 100644 --- a/pkg/platform/aks/aks.go +++ b/pkg/platform/aks/aks.go @@ -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 diff --git a/pkg/platform/aws/aws.go b/pkg/platform/aws/aws.go index aae1dfb7e..89c106829 100644 --- a/pkg/platform/aws/aws.go +++ b/pkg/platform/aws/aws.go @@ -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{} diff --git a/pkg/platform/baremetal/baremetal.go b/pkg/platform/baremetal/baremetal.go index 25db678a7..d066f45f6 100644 --- a/pkg/platform/baremetal/baremetal.go +++ b/pkg/platform/baremetal/baremetal.go @@ -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{} diff --git a/pkg/platform/packet/packet.go b/pkg/platform/packet/packet.go index 4d5e3ade2..dd88ae408 100644 --- a/pkg/platform/packet/packet.go +++ b/pkg/platform/packet/packet.go @@ -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{} diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go index 25e3a67b6..411549d8d 100644 --- a/pkg/platform/platform.go +++ b/pkg/platform/platform.go @@ -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 { diff --git a/pkg/platform/tinkerbell/tinkerbell.go b/pkg/platform/tinkerbell/tinkerbell.go index 727bcfbb8..621d82932 100644 --- a/pkg/platform/tinkerbell/tinkerbell.go +++ b/pkg/platform/tinkerbell/tinkerbell.go @@ -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 {