Repository: kinvolk/lokomotive
Pull request #1018 (2020-09-25 14:06:12)

cli/cmd: cleanups part 3
~**Includes commits from #1015**~

Part of #603.

invidian: @ipochi I had to rebase, one more LGTM?

Patch:
diff --git a/cli/cmd/cluster-apply.go b/cli/cmd/cluster-apply.go
index 8ca1dea3..6cf166ba 100644
--- a/cli/cmd/cluster-apply.go
+++ b/cli/cmd/cluster-apply.go
@@ -57,14 +57,32 @@ func runClusterApply(cmd *cobra.Command, args []string) {
 		"args":    args,
 	})
 
-	if err := clusterApply(contextLogger); err != nil {
+	options := clusterApplyOptions{
+		confirm:         confirm,
+		upgradeKubelets: upgradeKubelets,
+		skipComponents:  skipComponents,
+		verbose:         verbose,
+	}
+
+	if err := clusterApply(contextLogger, options); err != nil {
 		contextLogger.Fatalf("Applying cluster failed: %v", err)
 	}
 }
 
+type clusterApplyOptions struct {
+	confirm         bool
+	upgradeKubelets bool
+	skipComponents  bool
+	verbose         bool
+}
+
 //nolint:funlen
-func clusterApply(contextLogger *log.Entry) error {
-	c, err := initialize(contextLogger)
+func clusterApply(contextLogger *log.Entry, options clusterApplyOptions) error {
+	cc := clusterConfig{
+		verbose: options.verbose,
+	}
+
+	c, err := cc.initialize(contextLogger)
 	if err != nil {
 		return fmt.Errorf("initializing: %w", err)
 	}
@@ -74,7 +92,7 @@ func clusterApply(contextLogger *log.Entry) error {
 		return fmt.Errorf("checking if cluster exists: %w", err)
 	}
 
-	if exists && !confirm {
+	if exists && !options.confirm {
 		// TODO: We could plan to a file and use it when installing.
 		if err := c.terraformExecutor.Plan(); err != nil {
 			return fmt.Errorf("reconciling cluster state: %v", err)
@@ -93,7 +111,11 @@ func clusterApply(contextLogger *log.Entry) error {
 
 	fmt.Printf("\nYour configurations are stored in %s\n", c.assetDir)
 
-	kubeconfig, err := getKubeconfig(contextLogger, c.lokomotiveConfig, true)
+	kg := kubeconfigGetter{
+		platformRequired: true,
+	}
+
+	kubeconfig, err := kg.getKubeconfig(contextLogger, c.lokomotiveConfig)
 	if err != nil {
 		return fmt.Errorf("getting kubeconfig: %v", err)
 	}
@@ -121,7 +143,7 @@ func clusterApply(contextLogger *log.Entry) error {
 
 		charts := platform.CommonControlPlaneCharts()
 
-		if upgradeKubelets {
+		if options.upgradeKubelets {
 			charts = append(charts, helm.LokomotiveChart{
 				Name:      "kubelet",
 				Namespace: "kube-system",
@@ -141,7 +163,7 @@ func clusterApply(contextLogger *log.Entry) error {
 		}
 	}
 
-	if skipComponents {
+	if options.skipComponents {
 		return nil
 	}
 
diff --git a/cli/cmd/cluster-destroy.go b/cli/cmd/cluster-destroy.go
index 854d7794..1e5f46d8 100644
--- a/cli/cmd/cluster-destroy.go
+++ b/cli/cmd/cluster-destroy.go
@@ -42,13 +42,27 @@ func runClusterDestroy(cmd *cobra.Command, args []string) {
 		"args":    args,
 	})
 
-	if err := clusterDestroy(contextLogger); err != nil {
+	options := clusterDestroyOptions{
+		confirm: confirm,
+		verbose: verbose,
+	}
+
+	if err := clusterDestroy(contextLogger, options); err != nil {
 		contextLogger.Fatalf("Destroying cluster: %v", err)
 	}
 }
 
-func clusterDestroy(contextLogger *log.Entry) error {
-	c, err := initialize(contextLogger)
+type clusterDestroyOptions struct {
+	confirm bool
+	verbose bool
+}
+
+func clusterDestroy(contextLogger *log.Entry, options clusterDestroyOptions) error {
+	cc := clusterConfig{
+		verbose: options.verbose,
+	}
+
+	c, err := cc.initialize(contextLogger)
 	if err != nil {
 		return fmt.Errorf("initializing: %w", err)
 	}
@@ -64,7 +78,7 @@ func clusterDestroy(contextLogger *log.Entry) error {
 		return nil
 	}
 
-	if !confirm {
+	if !options.confirm {
 		confirmation := askForConfirmation("WARNING: This action cannot be undone. Do you really want to destroy the cluster?")
 		if !confirmation {
 			contextLogger.Println("Cluster destroy canceled")
diff --git a/cli/cmd/cluster.go b/cli/cmd/cluster.go
index 2ede30af..ff97c927 100644
--- a/cli/cmd/cluster.go
+++ b/cli/cmd/cluster.go
@@ -50,9 +50,13 @@ type cluster struct {
 	assetDir          string
 }
 
+type clusterConfig struct {
+	verbose bool
+}
+
 // initialize does common initialization actions between cluster operations
 // and returns created objects to the caller for further use.
-func initialize(contextLogger *log.Entry) (*cluster, error) {
+func (cc clusterConfig) initialize(contextLogger *log.Entry) (*cluster, error) {
 	lokoConfig, diags := getLokoConfig()
 	if diags.HasErrors() {
 		return nil, diags
@@ -92,7 +96,7 @@ func initialize(contextLogger *log.Entry) (*cluster, error) {
 		return nil, fmt.Errorf("validating backend configuration: %v", err)
 	}
 
-	ex, err := initializeTerraform(p, b)
+	ex, err := cc.initializeTerraform(p, b)
 	if err != nil {
 		return nil, fmt.Errorf("initializing Terraform: %w", err)
 	}
@@ -107,7 +111,7 @@ func initialize(contextLogger *log.Entry) (*cluster, error) {
 
 // initializeTerraform initialized Terraform directory using given backend and platform
 // and returns configured executor.
-func initializeTerraform(p platform.Platform, b backend.Backend) (*terraform.Executor, error) {
+func (cc clusterConfig) initializeTerraform(p platform.Platform, b backend.Backend) (*terraform.Executor, error) {
 	assetDir, err := homedir.Expand(p.Meta().AssetDir)
 	if err != nil {
 		return nil, fmt.Errorf("expanding path %q: %w", p.Meta().AssetDir, err)
@@ -126,7 +130,7 @@ func initializeTerraform(p platform.Platform, b backend.Backend) (*terraform.Exe
 
 	conf := terraform.Config{
 		WorkingDir: terraform.GetTerraformRootDir(assetDir),
-		Verbose:    verbose,
+		Verbose:    cc.verbose,
 	}
 
 	ex, err := terraform.NewExecutor(conf)
diff --git a/cli/cmd/component-apply.go b/cli/cmd/component-apply.go
index daee0823..c34f482e 100644
--- a/cli/cmd/component-apply.go
+++ b/cli/cmd/component-apply.go
@@ -61,14 +61,22 @@ func runApply(cmd *cobra.Command, args []string) {
 		log.SetLevel(log.DebugLevel)
 	}
 
-	if err := componentApply(contextLogger, args); err != nil {
+	options := componentApplyOptions{
+		kubeconfigPath: kubeconfigFlag,
+	}
+
+	if err := componentApply(contextLogger, args, options); err != nil {
 		contextLogger.Fatalf("Applying components failed: %v", err)
 	}
 }
 
+type componentApplyOptions struct {
+	kubeconfigPath string
+}
+
 // componentApply implements 'lokoctl component apply' separated from CLI
 // dependencies.
-func componentApply(contextLogger *log.Entry, componentsList []string) error {
+func componentApply(contextLogger *log.Entry, componentsList []string, options componentApplyOptions) error {
 	lokoConfig, diags := getLokoConfig()
 	if diags.HasErrors() {
 		return diags
@@ -79,7 +87,12 @@ func componentApply(contextLogger *log.Entry, componentsList []string) error {
 		return fmt.Errorf("getting component objects: %w", err)
 	}
 
-	kubeconfig, err := getKubeconfig(contextLogger, lokoConfig, false)
+	kg := kubeconfigGetter{
+		platformRequired: false,
+		path:             options.kubeconfigPath,
+	}
+
+	kubeconfig, err := kg.getKubeconfig(contextLogger, lokoConfig)
 	if err != nil {
 		contextLogger.Debugf("Error in finding kubeconfig file: %s", err)
 
diff --git a/cli/cmd/component-delete.go b/cli/cmd/component-delete.go
index cd36ff45..fa3c9e2f 100644
--- a/cli/cmd/component-delete.go
+++ b/cli/cmd/component-delete.go
@@ -63,16 +63,36 @@ func runDelete(cmd *cobra.Command, args []string) {
 		log.SetLevel(log.DebugLevel)
 	}
 
+	options := componentDeleteOptions{
+		confirm:         confirm,
+		deleteNamespace: deleteNamespace,
+		kubeconfigPath:  kubeconfigFlag,
+	}
+
+	if err := componentDelete(contextLogger, args, options); err != nil {
+		contextLogger.Fatalf("Deleting components failed: %v", err)
+	}
+}
+
+type componentDeleteOptions struct {
+	confirm         bool
+	deleteNamespace bool
+	kubeconfigPath  string
+}
+
+// componentDelete implements 'lokoctl component delete' separated from CLI
+// dependencies.
+func componentDelete(contextLogger *log.Entry, componentsList []string, options componentDeleteOptions) error {
 	lokoConfig, diags := getLokoConfig()
 	if diags.HasErrors() {
-		contextLogger.Fatal(diags)
+		return diags
 	}
 
-	componentsToDelete := selectComponentNames(args, *lokoConfig.RootConfig)
+	componentsToDelete := selectComponentNames(componentsList, *lokoConfig.RootConfig)
 
 	componentObjects, err := componentNamesToObjects(componentsToDelete)
 	if err != nil {
-		contextLogger.Fatalf("getting component objects: %v", err)
+		return fmt.Errorf("getting component objects: %v", err)
 	}
 
 	confirmationMessage := fmt.Sprintf(
@@ -80,21 +100,29 @@ func runDelete(cmd *cobra.Command, args []string) {
 		strings.Join(componentsToDelete, "\n\t"),
 	)
 
-	if !confirm && !askForConfirmation(confirmationMessage) {
+	if !options.confirm && !askForConfirmation(confirmationMessage) {
 		contextLogger.Info("Components deletion cancelled.")
 
-		return
+		return nil
+	}
+
+	kg := kubeconfigGetter{
+		platformRequired: false,
+		path:             options.kubeconfigPath,
 	}
 
-	kubeconfig, err := getKubeconfig(contextLogger, lokoConfig, false)
+	kubeconfig, err := kg.getKubeconfig(contextLogger, lokoConfig)
 	if err != nil {
 		contextLogger.Debugf("Error in finding kubeconfig file: %s", err)
-		contextLogger.Fatal("Suitable kubeconfig file not found. Did you run 'lokoctl cluster apply' ?")
+
+		return fmt.Errorf("suitable kubeconfig file not found. Did you run 'lokoctl cluster apply' ?")
 	}
 
-	if err := deleteComponents(kubeconfig, componentObjects); err != nil {
-		contextLogger.Fatal(err)
+	if err := deleteComponents(kubeconfig, componentObjects, options.deleteNamespace); err != nil {
+		return fmt.Errorf("deleting components: %w", err)
 	}
+
+	return nil
 }
 
 // selectComponentNames returns list of components to operate on. If explicit list is empty,
@@ -128,7 +156,7 @@ func componentNamesToObjects(componentNames []string) ([]components.Component, e
 	return c, nil
 }
 
-func deleteComponents(kubeconfig []byte, componentObjects []components.Component) error {
+func deleteComponents(kubeconfig []byte, componentObjects []components.Component, deleteNamespace bool) error {
 	for _, compObj := range componentObjects {
 		fmt.Printf("Deleting component '%s'...\n", compObj.Metadata().Name)
 
diff --git a/cli/cmd/component-render-manifest.go b/cli/cmd/component-render-manifest.go
index 2b8a9e67..7db49689 100644
--- a/cli/cmd/component-render-manifest.go
+++ b/cli/cmd/component-render-manifest.go
@@ -40,29 +40,31 @@ func runComponentRender(cmd *cobra.Command, args []string) {
 		"args":    args,
 	})
 
+	if err := componentRenderManifest(contextLogger, args); err != nil {
+		contextLogger.Fatalf("Rendering component manifests failed: %v", err)
+	}
+}
+
+func componentRenderManifest(contextLogger *log.Entry, componentsList []string) error {
 	lokoConfig, diags := getLokoConfig()
 	if diags.HasErrors() {
 		for _, diagnostic := range diags {
 			contextLogger.Error(diagnostic.Error())
 		}
-		contextLogger.Fatal("Errors found while loading configuration")
-	}
 
-	var componentsToRender []string
-	if len(args) > 0 {
-		componentsToRender = append(componentsToRender, args...)
-	} else {
-		for _, component := range lokoConfig.RootConfig.Components {
-			componentsToRender = append(componentsToRender, component.Name)
-		}
+		return diags
 	}
 
-	if err := renderComponentManifests(lokoConfig, componentsToRender...); err != nil {
-		contextLogger.Fatal(err)
+	componentsToRender := selectComponentNames(componentsList, *lokoConfig.RootConfig)
+
+	if err := renderComponentManifests(lokoConfig, componentsToRender); err != nil {
+		return fmt.Errorf("rendering component manifests: %w", err)
 	}
+
+	return nil
 }
 
-func renderComponentManifests(lokoConfig *config.Config, componentNames ...string) error {
+func renderComponentManifests(lokoConfig *config.Config, componentNames []string) error {
 	for _, componentName := range componentNames {
 		contextLogger := log.WithFields(log.Fields{
 			"component": componentName,
@@ -70,7 +72,7 @@ func renderComponentManifests(lokoConfig *config.Config, componentNames ...strin
 
 		component, err := components.Get(componentName)
 		if err != nil {
-			return err
+			return fmt.Errorf("getting component %q: %w", componentName, err)
 		}
 
 		componentConfigBody := lokoConfig.LoadComponentConfigBody(componentName)
@@ -84,7 +86,7 @@ func renderComponentManifests(lokoConfig *config.Config, componentNames ...strin
 
 		manifests, err := component.RenderManifests()
 		if err != nil {
-			return err
+			return fmt.Errorf("rendering manifest of component %q: %w", componentName, err)
 		}
 
 		fmt.Printf("# manifests for component %s\n", componentName)
diff --git a/cli/cmd/health.go b/cli/cmd/health.go
index 999a13bd..eed76ce1 100644
--- a/cli/cmd/health.go
+++ b/cli/cmd/health.go
@@ -49,24 +49,36 @@ func runHealth(cmd *cobra.Command, args []string) {
 		log.SetLevel(log.DebugLevel)
 	}
 
+	if err := health(contextLogger); err != nil {
+		contextLogger.Fatalf("Checking cluster health failed: %v", err)
+	}
+}
+
+//nolint:funlen
+func health(contextLogger *log.Entry) error {
 	lokoConfig, diags := getLokoConfig()
 	if diags.HasErrors() {
 		for _, diagnostic := range diags {
 			contextLogger.Error(diagnostic.Error())
 		}
 
-		contextLogger.Fatal("Errors found while loading cluster configuration")
+		return diags
 	}
 
-	kubeconfig, err := getKubeconfig(contextLogger, lokoConfig, true)
+	kg := kubeconfigGetter{
+		platformRequired: true,
+	}
+
+	kubeconfig, err := kg.getKubeconfig(contextLogger, lokoConfig)
 	if err != nil {
 		contextLogger.Debugf("Error in finding kubeconfig file: %s", err)
-		contextLogger.Fatal("Suitable kubeconfig file not found. Did you run 'lokoctl cluster apply' ?")
+
+		return fmt.Errorf("suitable kubeconfig file not found. Did you run 'lokoctl cluster apply' ?")
 	}
 
 	cs, err := k8sutil.NewClientset(kubeconfig)
 	if err != nil {
-		contextLogger.Fatalf("Error in creating Kubernetes client: %q", err)
+		return fmt.Errorf("creating Kubernetes client: %w", err)
 	}
 
 	// We can skip error checking here, as getKubeconfig() already checks it.
@@ -76,18 +88,18 @@ func runHealth(cmd *cobra.Command, args []string) {
 
 	ns, err := cluster.GetNodeStatus()
 	if err != nil {
-		contextLogger.Fatalf("Error getting node status: %q", err)
+		return fmt.Errorf("getting node status: %w", err)
 	}
 
 	ns.PrettyPrint()
 
 	if !ns.Ready() {
-		contextLogger.Fatalf("The cluster is not completely ready.")
+		return fmt.Errorf("cluster is not completely ready")
 	}
 
 	components, err := cluster.Health()
 	if err != nil {
-		contextLogger.Fatalf("Error in getting Lokomotive cluster health: %q", err)
+		return fmt.Errorf("getting Lokomotive cluster health: %w", err)
 	}
 
 	w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
@@ -114,4 +126,6 @@ func runHealth(cmd *cobra.Command, args []string) {
 
 		w.Flush()
 	}
+
+	return nil
 }
diff --git a/cli/cmd/root.go b/cli/cmd/root.go
index fde09d92..08e1dd2b 100644
--- a/cli/cmd/root.go
+++ b/cli/cmd/root.go
@@ -44,13 +44,12 @@ func Execute() {
 	}
 }
 
-const (
-	kubeconfigFlag = "kubeconfig-file"
-)
+var kubeconfigFlag string
 
 func addKubeconfigFileFlag(pf *flag.FlagSet) {
-	pf.String(
-		kubeconfigFlag,
+	pf.StringVar(
+		&kubeconfigFlag,
+		"kubeconfig-file",
 		"", // Special empty default, use getKubeconfig()
 		"Path to a kubeconfig file. If empty, the following precedence order is used:\n"+
 			"  1. Cluster asset dir when a lokocfg file is present in the current directory.\n"+
diff --git a/cli/cmd/utils.go b/cli/cmd/utils.go
index 60bc634b..6104e5df 100644
--- a/cli/cmd/utils.go
+++ b/cli/cmd/utils.go
@@ -83,11 +83,16 @@ func getConfiguredPlatform(lokoConfig *config.Config, require bool) (platform.Pl
 	return platform, platform.LoadConfig(&lokoConfig.RootConfig.Cluster.Config, lokoConfig.EvalContext)
 }
 
+type kubeconfigGetter struct {
+	platformRequired bool
+	path             string
+}
+
 // getKubeconfig finds the right kubeconfig file to use for an action and returns it's content.
 //
 // If platform is required and user do not have it configured, an error is returned.
-func getKubeconfig(contextLogger *log.Entry, lokoConfig *config.Config, platformRequired bool) ([]byte, error) {
-	sources, err := getKubeconfigSource(contextLogger, lokoConfig, platformRequired)
+func (kg kubeconfigGetter) getKubeconfig(contextLogger *log.Entry, lokoConfig *config.Config) ([]byte, error) {
+	sources, err := kg.getKubeconfigSource(contextLogger, lokoConfig)
 	if err != nil {
 		return nil, fmt.Errorf("selecting kubeconfig source: %w", err)
 	}
@@ -126,9 +131,9 @@ func getKubeconfig(contextLogger *log.Entry, lokoConfig *config.Config, platform
 //
 // - kubeconfig from ~/.kube/config file.
 //
-func getKubeconfigSource(contextLogger *log.Entry, lokoConfig *config.Config, platformRequired bool) ([]string, error) { //nolint:lll
+func (kg kubeconfigGetter) getKubeconfigSource(contextLogger *log.Entry, lokoConfig *config.Config) ([]string, error) { //nolint:lll
 	// Always try reading platform configuration.
-	p, diags := getConfiguredPlatform(lokoConfig, platformRequired)
+	p, diags := getConfiguredPlatform(lokoConfig, kg.platformRequired)
 	if diags.HasErrors() {
 		for _, diagnostic := range diags {
 			contextLogger.Error(diagnostic.Error())
@@ -137,15 +142,8 @@ func getKubeconfigSource(contextLogger *log.Entry, lokoConfig *config.Config, pl
 		return nil, fmt.Errorf("loading cluster configuration")
 	}
 
-	for _, k := range viper.AllKeys() {
-		if k != kubeconfigFlag {
-			continue
-		}
-
-		// Viper takes precedence over all other options.
-		if path := viper.GetString(kubeconfigFlag); path != "" {
-			return []string{path}, nil
-		}
+	if kg.path != "" {
+		return []string{kg.path}, nil
 	}
 
 	// If platform is not configured and not required, fallback to global kubeconfig files.
@@ -187,7 +185,10 @@ 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.")
 
-	c, err := initialize(contextLogger)
+	// TODO: Add Terraform verbose support back.
+	cc := clusterConfig{}
+
+	c, err := cc.initialize(contextLogger)
 	if err != nil {
 		return nil, fmt.Errorf("initializing: %w", err)
 	}


Test patch:
diff --git a/cli/cmd/utils_internal_test.go b/cli/cmd/utils_internal_test.go
index 47fcbe30..91d3071c 100644
--- a/cli/cmd/utils_internal_test.go
+++ b/cli/cmd/utils_internal_test.go
@@ -23,13 +23,11 @@ import (
 	"testing"
 
 	log "github.com/sirupsen/logrus"
-	"github.com/spf13/viper"
 
 	"github.com/kinvolk/lokomotive/pkg/config"
 )
 
 type kubeconfigSources struct {
-	flag       string
 	env        string
 	configFile string
 }
@@ -39,9 +37,6 @@ const (
 )
 
 func prepareKubeconfigSource(t *testing.T, k *kubeconfigSources) (*log.Entry, *config.Config) {
-	// Ensure viper flag is NOT empty.
-	viper.Set(kubeconfigFlag, k.flag)
-
 	if k.env == "" {
 		// Ensure KUBECONFIG is not set.
 		if err := os.Unsetenv(kubeconfigEnvVariable); err != nil {
@@ -96,7 +91,11 @@ func TestGetKubeconfigBadConfig(t *testing.T) {
 
 	contextLogger, lokoConfig := prepareKubeconfigSource(t, k)
 
-	kubeconfig, err := getKubeconfig(contextLogger, lokoConfig, false)
+	kg := kubeconfigGetter{
+		platformRequired: false,
+	}
+
+	kubeconfig, err := kg.getKubeconfig(contextLogger, lokoConfig)
 	if err == nil {
 		t.Errorf("getting kubeconfig with bad configuration should fail")
 	}
@@ -108,13 +107,17 @@ func TestGetKubeconfigBadConfig(t *testing.T) {
 
 func TestGetKubeconfigNoConfigButRequired(t *testing.T) {
 	k := &kubeconfigSources{
-		env:  "/foo",
-		flag: "/bar",
+		env: "/foo",
 	}
 
 	contextLogger, lokoConfig := prepareKubeconfigSource(t, k)
 
-	kubeconfig, err := getKubeconfig(contextLogger, lokoConfig, true)
+	kg := kubeconfigGetter{
+		platformRequired: true,
+		path:             "/bar",
+	}
+
+	kubeconfig, err := kg.getKubeconfig(contextLogger, lokoConfig)
 	if err == nil {
 		t.Errorf("getting kubeconfig with no configuration and platform required should fail")
 	}
@@ -148,7 +151,11 @@ func TestGetKubeconfig(t *testing.T) {
 
 	contextLogger, lokoConfig := prepareKubeconfigSource(t, k)
 
-	kubeconfig, err := getKubeconfig(contextLogger, lokoConfig, false)
+	kg := kubeconfigGetter{
+		platformRequired: false,
+	}
+
+	kubeconfig, err := kg.getKubeconfig(contextLogger, lokoConfig)
 	if err != nil {
 		t.Fatalf("getting kubeconfig: %v", err)
 	}
@@ -180,13 +187,17 @@ func TestGetKubeconfigSourceFlag(t *testing.T) {
     count = 0
   }
 }`,
-		flag: expectedPath[0],
-		env:  "/badpath",
+		env: "/badpath",
 	}
 
 	contextLogger, lokoConfig := prepareKubeconfigSource(t, k)
 
-	kubeconfig, err := getKubeconfigSource(contextLogger, lokoConfig, true)
+	kg := kubeconfigGetter{
+		platformRequired: true,
+		path:             expectedPath[0],
+	}
+
+	kubeconfig, err := kg.getKubeconfigSource(contextLogger, lokoConfig)
 	if err != nil {
 		t.Fatalf("getting kubeconfig: %v", err)
 	}
@@ -223,7 +234,11 @@ func TestGetKubeconfigSourceConfigFile(t *testing.T) {
 
 	contextLogger, lokoConfig := prepareKubeconfigSource(t, k)
 
-	kubeconfig, err := getKubeconfigSource(contextLogger, lokoConfig, true)
+	kg := kubeconfigGetter{
+		platformRequired: true,
+	}
+
+	kubeconfig, err := kg.getKubeconfigSource(contextLogger, lokoConfig)
 	if err != nil {
 		t.Fatalf("getting kubeconfig: %v", err)
 	}
@@ -282,7 +297,11 @@ func TestGetKubeconfigFromAssetsDir(t *testing.T) {
 
 	contextLogger, lokoConfig := prepareKubeconfigSource(t, k)
 
-	kubeconfig, err := getKubeconfig(contextLogger, lokoConfig, true)
+	kg := kubeconfigGetter{
+		platformRequired: true,
+	}
+
+	kubeconfig, err := kg.getKubeconfig(contextLogger, lokoConfig)
 	if err != nil {
 		t.Fatalf("getting kubeconfig: %v", err)
 	}
@@ -301,7 +320,11 @@ func TestGetKubeconfigSourceBadConfigFile(t *testing.T) {
 
 	contextLogger, lokoConfig := prepareKubeconfigSource(t, k)
 
-	kubeconfig, err := getKubeconfigSource(contextLogger, lokoConfig, true)
+	kg := kubeconfigGetter{
+		platformRequired: true,
+	}
+
+	kubeconfig, err := kg.getKubeconfigSource(contextLogger, lokoConfig)
 	if err == nil {
 		t.Errorf("getting kubeconfig with bad configuration should fail")
 	}
@@ -323,7 +346,11 @@ func TestGetKubeconfigSourceEnvVariable(t *testing.T) {
 
 	contextLogger, lokoConfig := prepareKubeconfigSource(t, k)
 
-	kubeconfig, err := getKubeconfigSource(contextLogger, lokoConfig, false)
+	kg := kubeconfigGetter{
+		platformRequired: false,
+	}
+
+	kubeconfig, err := kg.getKubeconfigSource(contextLogger, lokoConfig)
 	if err != nil {
 		t.Fatalf("getting kubeconfig: %v", err)
 	}
@@ -340,7 +367,11 @@ func TestGetKubeconfigSourceDefault(t *testing.T) {
 
 	contextLogger, lokoConfig := prepareKubeconfigSource(t, k)
 
-	kubeconfig, err := getKubeconfigSource(contextLogger, lokoConfig, false)
+	kg := kubeconfigGetter{
+		platformRequired: false,
+	}
+
+	kubeconfig, err := kg.getKubeconfigSource(contextLogger, lokoConfig)
 	if err != nil {
 		t.Fatalf("getting kubeconfig: %v", err)
 	}