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

Commit

Permalink
cli/cmd: remove getLokoConfig() function
Browse files Browse the repository at this point in the history
To remove implicit dependency on CLI code, which uses global variables,
so code can be moved to separate package, to fully isolate it from CLI
handling.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Oct 21, 2020
1 parent 2b4b98a commit 632c529
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 22 deletions.
9 changes: 8 additions & 1 deletion cli/cmd/cluster-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/kinvolk/lokomotive/internal"
"github.com/kinvolk/lokomotive/pkg/helm"
Expand Down Expand Up @@ -62,6 +63,8 @@ func runClusterApply(cmd *cobra.Command, args []string) {
upgradeKubelets: upgradeKubelets,
skipComponents: skipComponents,
verbose: verbose,
configPath: viper.GetString("lokocfg"),
valuesPath: viper.GetString("lokocfg-vars"),
}

if err := clusterApply(contextLogger, options); err != nil {
Expand All @@ -74,12 +77,16 @@ type clusterApplyOptions struct {
upgradeKubelets bool
skipComponents bool
verbose bool
configPath string
valuesPath string
}

//nolint:funlen
func clusterApply(contextLogger *log.Entry, options clusterApplyOptions) error {
cc := clusterConfig{
verbose: options.verbose,
verbose: options.verbose,
configPath: options.configPath,
valuesPath: options.valuesPath,
}

c, err := cc.initialize(contextLogger)
Expand Down
17 changes: 12 additions & 5 deletions cli/cmd/cluster-destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var confirm bool
Expand All @@ -43,8 +44,10 @@ func runClusterDestroy(cmd *cobra.Command, args []string) {
})

options := clusterDestroyOptions{
confirm: confirm,
verbose: verbose,
confirm: confirm,
verbose: verbose,
configPath: viper.GetString("lokocfg"),
valuesPath: viper.GetString("lokocfg-vars"),
}

if err := clusterDestroy(contextLogger, options); err != nil {
Expand All @@ -53,13 +56,17 @@ func runClusterDestroy(cmd *cobra.Command, args []string) {
}

type clusterDestroyOptions struct {
confirm bool
verbose bool
confirm bool
verbose bool
configPath string
valuesPath string
}

func clusterDestroy(contextLogger *log.Entry, options clusterDestroyOptions) error {
cc := clusterConfig{
verbose: options.verbose,
verbose: options.verbose,
configPath: options.configPath,
valuesPath: options.valuesPath,
}

c, err := cc.initialize(contextLogger)
Expand Down
6 changes: 4 additions & 2 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ type cluster struct {
}

type clusterConfig struct {
verbose bool
verbose bool
configPath string
valuesPath string
}

// initialize does common initialization actions between cluster operations
// and returns created objects to the caller for further use.
func (cc clusterConfig) initialize(contextLogger *log.Entry) (*cluster, error) {
lokoConfig, diags := getLokoConfig()
lokoConfig, diags := config.LoadConfig(cc.configPath, cc.valuesPath)
if diags.HasErrors() {
return nil, diags
}
Expand Down
7 changes: 6 additions & 1 deletion cli/cmd/component-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
Expand Down Expand Up @@ -63,6 +64,8 @@ func runApply(cmd *cobra.Command, args []string) {

options := componentApplyOptions{
kubeconfigPath: kubeconfigFlag,
configPath: viper.GetString("lokocfg"),
valuesPath: viper.GetString("lokocfg-vars"),
}

if err := componentApply(contextLogger, args, options); err != nil {
Expand All @@ -72,12 +75,14 @@ func runApply(cmd *cobra.Command, args []string) {

type componentApplyOptions struct {
kubeconfigPath string
configPath string
valuesPath string
}

// componentApply implements 'lokoctl component apply' separated from CLI
// dependencies.
func componentApply(contextLogger *log.Entry, componentsList []string, options componentApplyOptions) error {
lokoConfig, diags := getLokoConfig()
lokoConfig, diags := config.LoadConfig(options.configPath, options.valuesPath)
if diags.HasErrors() {
return diags
}
Expand Down
7 changes: 6 additions & 1 deletion cli/cmd/component-delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
Expand Down Expand Up @@ -67,6 +68,8 @@ func runDelete(cmd *cobra.Command, args []string) {
confirm: confirm,
deleteNamespace: deleteNamespace,
kubeconfigPath: kubeconfigFlag,
configPath: viper.GetString("lokocfg"),
valuesPath: viper.GetString("lokocfg-vars"),
}

if err := componentDelete(contextLogger, args, options); err != nil {
Expand All @@ -78,12 +81,14 @@ type componentDeleteOptions struct {
confirm bool
deleteNamespace bool
kubeconfigPath string
configPath string
valuesPath string
}

// componentApply implements 'lokoctl component delete' separated from CLI
// dependencies.
func componentDelete(contextLogger *log.Entry, componentsList []string, options componentDeleteOptions) error {
lokoConfig, diags := getLokoConfig()
lokoConfig, diags := config.LoadConfig(options.configPath, options.valuesPath)
if diags.HasErrors() {
return diags
}
Expand Down
17 changes: 14 additions & 3 deletions cli/cmd/component-render-manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/config"
Expand All @@ -40,13 +41,23 @@ func runComponentRender(cmd *cobra.Command, args []string) {
"args": args,
})

if err := componentRenderManifest(contextLogger, args); err != nil {
options := componentRenderManifestOptions{
configPath: viper.GetString("lokocfg"),
valuesPath: viper.GetString("lokocfg-vars"),
}

if err := componentRenderManifest(contextLogger, args, options); err != nil {
contextLogger.Fatalf("Rendering component manifests failed: %v", err)
}
}

func componentRenderManifest(contextLogger *log.Entry, componentsList []string) error {
lokoConfig, diags := getLokoConfig()
type componentRenderManifestOptions struct {
configPath string
valuesPath string
}

func componentRenderManifest(contextLogger *log.Entry, componentsList []string, options componentRenderManifestOptions) error {
lokoConfig, diags := config.LoadConfig(options.configPath, options.valuesPath)
if diags.HasErrors() {
for _, diagnostic := range diags {
contextLogger.Error(diagnostic.Error())
Expand Down
18 changes: 15 additions & 3 deletions cli/cmd/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/kinvolk/lokomotive/pkg/config"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
"github.com/kinvolk/lokomotive/pkg/lokomotive"
)
Expand Down Expand Up @@ -49,14 +51,24 @@ func runHealth(cmd *cobra.Command, args []string) {
log.SetLevel(log.DebugLevel)
}

if err := health(contextLogger); err != nil {
options := healthOptions{
configPath: viper.GetString("lokocfg"),
valuesPath: viper.GetString("lokocfg-vars"),
}

if err := health(contextLogger, options); err != nil {
contextLogger.Fatalf("Checking cluster health failed: %v", err)
}
}

type healthOptions struct {
configPath string
valuesPath string
}

//nolint:funlen
func health(contextLogger *log.Entry) error {
lokoConfig, diags := getLokoConfig()
func health(contextLogger *log.Entry, options healthOptions) error {
lokoConfig, diags := config.LoadConfig(options.configPath, options.valuesPath)
if diags.HasErrors() {
for _, diagnostic := range diags {
contextLogger.Error(diagnostic.Error())
Expand Down
5 changes: 0 additions & 5 deletions cli/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"

"github.com/kinvolk/lokomotive/pkg/backend"
"github.com/kinvolk/lokomotive/pkg/config"
Expand Down Expand Up @@ -175,10 +174,6 @@ func assetsKubeconfig(assetDir string) string {
return filepath.Join(assetDir, "cluster-assets", "auth", "kubeconfig")
}

func getLokoConfig() (*config.Config, hcl.Diagnostics) {
return config.LoadConfig(viper.GetString("lokocfg"), viper.GetString("lokocfg-vars"))
}

// readKubeconfigFromTerraformState initializes Terraform and
// reads content of cluster kubeconfig file from the Terraform.
func readKubeconfigFromTerraformState(contextLogger *log.Entry) ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/utils_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func prepareKubeconfigSource(t *testing.T, k *kubeconfigSources) (*log.Entry, *c
}
}

lokoConfig, diags := getLokoConfig()
lokoConfig, diags := config.LoadConfig(tmpDir, "")
if diags.HasErrors() {
t.Fatalf("getting lokomotive configuration: %v", err)
}
Expand Down

0 comments on commit 632c529

Please sign in to comment.