From 632c529bcf48d224137a1ddb014665f07f4cf7e1 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 19 Oct 2020 12:13:10 +0200 Subject: [PATCH] cli/cmd: remove getLokoConfig() function 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 --- cli/cmd/cluster-apply.go | 9 ++++++++- cli/cmd/cluster-destroy.go | 17 ++++++++++++----- cli/cmd/cluster.go | 6 ++++-- cli/cmd/component-apply.go | 7 ++++++- cli/cmd/component-delete.go | 7 ++++++- cli/cmd/component-render-manifest.go | 17 ++++++++++++++--- cli/cmd/health.go | 18 +++++++++++++++--- cli/cmd/utils.go | 5 ----- cli/cmd/utils_internal_test.go | 2 +- 9 files changed, 66 insertions(+), 22 deletions(-) diff --git a/cli/cmd/cluster-apply.go b/cli/cmd/cluster-apply.go index 6cf166bae..8a8046e26 100644 --- a/cli/cmd/cluster-apply.go +++ b/cli/cmd/cluster-apply.go @@ -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" @@ -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 { @@ -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) diff --git a/cli/cmd/cluster-destroy.go b/cli/cmd/cluster-destroy.go index 1e5f46d84..524405ad9 100644 --- a/cli/cmd/cluster-destroy.go +++ b/cli/cmd/cluster-destroy.go @@ -19,6 +19,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/spf13/viper" ) var confirm bool @@ -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 { @@ -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) diff --git a/cli/cmd/cluster.go b/cli/cmd/cluster.go index ff97c927f..66ec1ec5b 100644 --- a/cli/cmd/cluster.go +++ b/cli/cmd/cluster.go @@ -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 } diff --git a/cli/cmd/component-apply.go b/cli/cmd/component-apply.go index c34f482e5..1336c384d 100644 --- a/cli/cmd/component-apply.go +++ b/cli/cmd/component-apply.go @@ -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" @@ -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 { @@ -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 } diff --git a/cli/cmd/component-delete.go b/cli/cmd/component-delete.go index 9990c18ae..f1b521432 100644 --- a/cli/cmd/component-delete.go +++ b/cli/cmd/component-delete.go @@ -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" @@ -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 { @@ -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 } diff --git a/cli/cmd/component-render-manifest.go b/cli/cmd/component-render-manifest.go index 7db49689a..321b9d18d 100644 --- a/cli/cmd/component-render-manifest.go +++ b/cli/cmd/component-render-manifest.go @@ -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" @@ -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()) diff --git a/cli/cmd/health.go b/cli/cmd/health.go index eed76ce1c..9d0e53586 100644 --- a/cli/cmd/health.go +++ b/cli/cmd/health.go @@ -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" ) @@ -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()) diff --git a/cli/cmd/utils.go b/cli/cmd/utils.go index 6104e5dff..2a3d83912 100644 --- a/cli/cmd/utils.go +++ b/cli/cmd/utils.go @@ -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" @@ -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) { diff --git a/cli/cmd/utils_internal_test.go b/cli/cmd/utils_internal_test.go index 91d3071c2..694e52959 100644 --- a/cli/cmd/utils_internal_test.go +++ b/cli/cmd/utils_internal_test.go @@ -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) }