From ebe1ed104a6bf7cbe2717ba3477ee27d95efa750 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Thu, 26 Mar 2020 10:31:16 +0100 Subject: [PATCH] pkg/platform: aggregate getters into Meta() function So adding new properties does not require adding new interface method and implementing it for all platforms. This is required, as for adding support for managed Kubernetes platforms, we will need to add more fields here. Refs #215 #216 Signed-off-by: Mateusz Gozdek --- cli/cmd/cluster-apply.go | 2 +- cli/cmd/cluster.go | 4 ++-- cli/cmd/health.go | 2 +- cli/cmd/utils.go | 2 +- pkg/platform/aws/aws.go | 23 +++++++++++------------ pkg/platform/baremetal/baremetal.go | 13 ++++++------- pkg/platform/packet/packet.go | 24 +++++++++++------------- pkg/platform/platform.go | 9 +++++++-- 8 files changed, 40 insertions(+), 39 deletions(-) diff --git a/cli/cmd/cluster-apply.go b/cli/cmd/cluster-apply.go index 8b6be9509..765b871c0 100644 --- a/cli/cmd/cluster-apply.go +++ b/cli/cmd/cluster-apply.go @@ -77,7 +77,7 @@ func runClusterApply(cmd *cobra.Command, args []string) { fmt.Printf("\nYour configurations are stored in %s\n", assetDir) kubeconfigPath := assetsKubeconfig(assetDir) - if err := verifyCluster(kubeconfigPath, p.GetExpectedNodes()); err != nil { + if err := verifyCluster(kubeconfigPath, p.Meta().ExpectedNodes); err != nil { ctxLogger.Fatalf("Verify cluster: %v", err) } diff --git a/cli/cmd/cluster.go b/cli/cmd/cluster.go index fd4f488f1..eee2c53a3 100644 --- a/cli/cmd/cluster.go +++ b/cli/cmd/cluster.go @@ -79,7 +79,7 @@ func initialize(ctxLogger *logrus.Entry) (*terraform.Executor, platform.Platform b = local.NewLocalBackend() } - assetDir, err := homedir.Expand(p.GetAssetDir()) + assetDir, err := homedir.Expand(p.Meta().AssetDir) if err != nil { ctxLogger.Fatalf("Error expanding path: %v", err) } @@ -97,7 +97,7 @@ func initialize(ctxLogger *logrus.Entry) (*terraform.Executor, platform.Platform // initializeTerraform initialized Terraform directory using given backend and platform // and returns configured executor. func initializeTerraform(ctxLogger *logrus.Entry, p platform.Platform, b backend.Backend) *terraform.Executor { - assetDir, err := homedir.Expand(p.GetAssetDir()) + assetDir, err := homedir.Expand(p.Meta().AssetDir) if err != nil { ctxLogger.Fatalf("Error expanding path: %v", err) } diff --git a/cli/cmd/health.go b/cli/cmd/health.go index aa42e9fed..a9746430c 100644 --- a/cli/cmd/health.go +++ b/cli/cmd/health.go @@ -64,7 +64,7 @@ func runHealth(cmd *cobra.Command, args []string) { contextLogger.Fatal("No cluster configured") } - cluster, err := lokomotive.NewCluster(client, p.GetExpectedNodes()) + cluster, err := lokomotive.NewCluster(client, p.Meta().ExpectedNodes) if err != nil { contextLogger.Fatalf("Error in creating new Lokomotive cluster: %q", err) } diff --git a/cli/cmd/utils.go b/cli/cmd/utils.go index 09a550925..1e21a3b69 100644 --- a/cli/cmd/utils.go +++ b/cli/cmd/utils.go @@ -83,7 +83,7 @@ func getAssetDir() (string, error) { return "", nil } - return cfg.GetAssetDir(), nil + return cfg.Meta().AssetDir, nil } // expandKubeconfigPath tries to expand ~ in the given kubeconfig path. diff --git a/pkg/platform/aws/aws.go b/pkg/platform/aws/aws.go index 0474af93a..b7675fd36 100644 --- a/pkg/platform/aws/aws.go +++ b/pkg/platform/aws/aws.go @@ -100,9 +100,17 @@ func NewConfig() *config { } } -// GetAssetDir returns asset directory path -func (c *config) GetAssetDir() string { - return c.AssetDir +// Meta is part of Platform interface and returns common information about the platform configuration. +func (c *config) Meta() platform.Meta { + nodes := c.ControllerCount + for _, workerpool := range c.WorkerPools { + nodes += workerpool.Count + } + + return platform.Meta{ + AssetDir: c.AssetDir, + ExpectedNodes: nodes, + } } func (c *config) Apply(ex *terraform.Executor) error { @@ -211,15 +219,6 @@ func createTerraformConfigFile(cfg *config, terraformRootDir string) error { return nil } -func (c *config) GetExpectedNodes() int { - nodes := c.ControllerCount - for _, workerpool := range c.WorkerPools { - nodes += workerpool.Count - } - - return nodes -} - // checkValidConfig validates cluster configuration. func (c *config) checkValidConfig() hcl.Diagnostics { var diagnostics hcl.Diagnostics diff --git a/pkg/platform/baremetal/baremetal.go b/pkg/platform/baremetal/baremetal.go index a605aadf9..fbd0276ab 100644 --- a/pkg/platform/baremetal/baremetal.go +++ b/pkg/platform/baremetal/baremetal.go @@ -62,9 +62,12 @@ func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) return gohcl.DecodeBody(*configBody, evalContext, c) } -// GetAssetDir returns asset directory path -func (c *config) GetAssetDir() string { - return c.AssetDir +// Meta is part of Platform interface and returns common information about the platform configuration. +func (c *config) Meta() platform.Meta { + return platform.Meta{ + AssetDir: c.AssetDir, + ExpectedNodes: len(c.ControllerMacs) + len(c.WorkerMacs), + } } func NewConfig() *config { @@ -195,7 +198,3 @@ func createTerraformConfigFile(cfg *config, terraformPath string) error { } return nil } - -func (c *config) GetExpectedNodes() int { - return len(c.ControllerMacs) + len(c.WorkerMacs) -} diff --git a/pkg/platform/packet/packet.go b/pkg/platform/packet/packet.go index a5ff0907c..ff6262f37 100644 --- a/pkg/platform/packet/packet.go +++ b/pkg/platform/packet/packet.go @@ -102,9 +102,17 @@ func NewConfig() *config { } } -// GetAssetDir returns asset directory path -func (c *config) GetAssetDir() string { - return c.AssetDir +// Meta is part of Platform interface and returns common information about the platform configuration. +func (c *config) Meta() platform.Meta { + nodes := c.ControllerCount + for _, workerpool := range c.WorkerPools { + nodes += workerpool.Count + } + + return platform.Meta{ + AssetDir: c.AssetDir, + ExpectedNodes: nodes, + } } func (c *config) Initialize(ex *terraform.Executor) error { @@ -237,16 +245,6 @@ func (c *config) terraformSmartApply(ex *terraform.Executor, dnsProvider dns.DNS return ex.Apply() } -func (c *config) GetExpectedNodes() int { - workers := 0 - - for _, wp := range c.WorkerPools { - workers += wp.Count - } - - return c.ControllerCount + workers -} - // checkValidConfig validates cluster configuration. func (c *config) checkValidConfig() hcl.Diagnostics { var diagnostics hcl.Diagnostics diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go index c9d98a5f3..0f2d237cf 100644 --- a/pkg/platform/platform.go +++ b/pkg/platform/platform.go @@ -27,8 +27,13 @@ type Platform interface { Apply(*terraform.Executor) error Destroy(*terraform.Executor) error Initialize(*terraform.Executor) error - GetAssetDir() string - GetExpectedNodes() int + Meta() Meta +} + +// Meta is a generic information format about the platform. +type Meta struct { + AssetDir string + ExpectedNodes int } // platforms is a collection where all platforms gets automatically registered