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

Commit

Permalink
pkg/platform: aggregate getters into Meta() function
Browse files Browse the repository at this point in the history
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 <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Apr 6, 2020
1 parent 4589317 commit 6434042
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/cluster-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 11 additions & 12 deletions pkg/platform/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,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 {
Expand Down Expand Up @@ -212,15 +220,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
Expand Down
13 changes: 6 additions & 7 deletions pkg/platform/baremetal/baremetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
24 changes: 11 additions & 13 deletions pkg/platform/packet/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions pkg/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6434042

Please sign in to comment.