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

Commit

Permalink
cli/cmd: make initialize return a struct
Browse files Browse the repository at this point in the history
Which will make it easier to return error if some part of initialization
fails.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Sep 25, 2020
1 parent 9a25a8e commit 3ab2eca
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
26 changes: 13 additions & 13 deletions cli/cmd/cluster-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func runClusterApply(cmd *cobra.Command, args []string) {

//nolint:funlen
func clusterApply(contextLogger *log.Entry) error {
ex, p, lokoConfig, assetDir := initialize(contextLogger)
c := initialize(contextLogger)

exists := clusterExists(contextLogger, ex)
exists := clusterExists(contextLogger, &c.terraformExecutor)
if exists && !confirm {
// TODO: We could plan to a file and use it when installing.
if err := ex.Plan(); err != nil {
if err := c.terraformExecutor.Plan(); err != nil {
return fmt.Errorf("reconciling cluster state: %v", err)
}

Expand All @@ -80,18 +80,18 @@ func clusterApply(contextLogger *log.Entry) error {
}
}

if err := p.Apply(ex); err != nil {
if err := c.platform.Apply(&c.terraformExecutor); err != nil {
return fmt.Errorf("applying platform: %v", err)
}

fmt.Printf("\nYour configurations are stored in %s\n", assetDir)
fmt.Printf("\nYour configurations are stored in %s\n", c.assetDir)

kubeconfig, err := getKubeconfig(contextLogger, lokoConfig, true)
kubeconfig, err := getKubeconfig(contextLogger, c.lokomotiveConfig, true)
if err != nil {
return fmt.Errorf("getting kubeconfig: %v", err)
}

if err := verifyCluster(kubeconfig, p.Meta().ExpectedNodes); err != nil {
if err := verifyCluster(kubeconfig, c.platform.Meta().ExpectedNodes); err != nil {
return fmt.Errorf("verifying cluster: %v", err)
}

Expand All @@ -102,14 +102,14 @@ func clusterApply(contextLogger *log.Entry) error {
}

// Do controlplane upgrades only if cluster already exists and it is not a managed platform.
if exists && !p.Meta().Managed {
if exists && !c.platform.Meta().Managed {
fmt.Printf("\nEnsuring that cluster controlplane is up to date.\n")

cu := controlplaneUpdater{
kubeconfig: kubeconfig,
assetDir: assetDir,
assetDir: c.assetDir,
contextLogger: *contextLogger,
ex: *ex,
ex: c.terraformExecutor,
}

charts := platform.CommonControlPlaneCharts()
Expand All @@ -126,7 +126,7 @@ func clusterApply(contextLogger *log.Entry) error {
}
}

if ph, ok := p.(platform.PlatformWithPostApplyHook); ok {
if ph, ok := c.platform.(platform.PlatformWithPostApplyHook); ok {
if err := ph.PostApplyHook(kubeconfig); err != nil {
return fmt.Errorf("running platform post install hook: %v", err)
}
Expand All @@ -137,14 +137,14 @@ func clusterApply(contextLogger *log.Entry) error {
}

componentsToApply := []string{}
for _, component := range lokoConfig.RootConfig.Components {
for _, component := range c.lokomotiveConfig.RootConfig.Components {
componentsToApply = append(componentsToApply, component.Name)
}

contextLogger.Println("Applying component configuration")

if len(componentsToApply) > 0 {
if err := applyComponents(lokoConfig, kubeconfig, componentsToApply...); err != nil {
if err := applyComponents(c.lokomotiveConfig, kubeconfig, componentsToApply...); err != nil {
return fmt.Errorf("applying component configuration: %v", err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions cli/cmd/cluster-destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func runClusterDestroy(cmd *cobra.Command, args []string) {
}

func clusterDestroy(contextLogger *log.Entry) error {
ex, p, _, _ := initialize(contextLogger)
c := initialize(contextLogger)

if !clusterExists(contextLogger, ex) {
if !clusterExists(contextLogger, &c.terraformExecutor) {
contextLogger.Println("Cluster already destroyed, nothing to do")

return nil
Expand All @@ -65,7 +65,7 @@ func clusterDestroy(contextLogger *log.Entry) error {
}
}

if err := p.Destroy(ex); err != nil {
if err := c.platform.Destroy(&c.terraformExecutor); err != nil {
return fmt.Errorf("destroying cluster: %v", err)
}

Expand Down
18 changes: 16 additions & 2 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ func init() {
RootCmd.AddCommand(clusterCmd)
}

// cluster is a temporary helper struct to aggregate objects which are used
// for managing the cluster and components.
type cluster struct {
terraformExecutor terraform.Executor
platform platform.Platform
lokomotiveConfig *config.Config
assetDir string
}

// initialize does common initialization actions between cluster operations
// and returns created objects to the caller for further use.
func initialize(contextLogger *log.Entry) (*terraform.Executor, platform.Platform, *config.Config, string) {
func initialize(contextLogger *log.Entry) *cluster {
lokoConfig, diags := getLokoConfig()
if diags.HasErrors() {
contextLogger.Fatal(diags)
Expand Down Expand Up @@ -85,7 +94,12 @@ func initialize(contextLogger *log.Entry) (*terraform.Executor, platform.Platfor

ex := initializeTerraform(contextLogger, p, b)

return ex, p, lokoConfig, assetDir
return &cluster{
terraformExecutor: *ex,
platform: p,
lokomotiveConfig: lokoConfig,
assetDir: assetDir,
}
}

// initializeTerraform initialized Terraform directory using given backend and platform
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ func readKubeconfigFromTerraformState(contextLogger *log.Entry) ([]byte, error)
contextLogger.Warn("Kubeconfig file not found in assets directory, pulling kubeconfig from " +
"Terraform state, this might be slow. Run 'lokoctl cluster apply' to fix it.")

ex, _, _, _ := initialize(contextLogger) //nolint:dogsled
c := initialize(contextLogger)

kubeconfig := ""

if err := ex.Output(kubeconfigTerraformOutputKey, &kubeconfig); err != nil {
if err := c.terraformExecutor.Output(kubeconfigTerraformOutputKey, &kubeconfig); err != nil {
return nil, fmt.Errorf("reading kubeconfig file content from Terraform state: %w", err)
}

Expand Down

0 comments on commit 3ab2eca

Please sign in to comment.