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 getKubeconfig() dependency on viper
Browse files Browse the repository at this point in the history
Pass explicity kubeconfig path via options instead.

Also fixes #1094.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian authored and knrt10 committed Oct 22, 2020
1 parent 4d9b9d0 commit 3455bd0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
13 changes: 11 additions & 2 deletions cli/cmd/component-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -81,6 +89,7 @@ func componentApply(contextLogger *log.Entry, componentsList []string) error {

kg := kubeconfigGetter{
platformRequired: false,
path: options.kubeconfigPath,
}

kubeconfig, err := kg.getKubeconfig(contextLogger, lokoConfig)
Expand Down
3 changes: 3 additions & 0 deletions cli/cmd/component-delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func runDelete(cmd *cobra.Command, args []string) {
options := componentDeleteOptions{
confirm: confirm,
deleteNamespace: deleteNamespace,
kubeconfigPath: kubeconfigFlag,
}

if err := componentDelete(contextLogger, args, options); err != nil {
Expand All @@ -76,6 +77,7 @@ func runDelete(cmd *cobra.Command, args []string) {
type componentDeleteOptions struct {
confirm bool
deleteNamespace bool
kubeconfigPath string
}

// componentDelete implements 'lokoctl component delete' separated from CLI
Expand Down Expand Up @@ -106,6 +108,7 @@ func componentDelete(contextLogger *log.Entry, componentsList []string, options

kg := kubeconfigGetter{
platformRequired: false,
path: options.kubeconfigPath,
}

kubeconfig, err := kg.getKubeconfig(contextLogger, lokoConfig)
Expand Down
9 changes: 4 additions & 5 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"+
Expand Down
12 changes: 3 additions & 9 deletions cli/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func getConfiguredPlatform(lokoConfig *config.Config, require bool) (platform.Pl

type kubeconfigGetter struct {
platformRequired bool
path string
}

// getKubeconfig finds the right kubeconfig file to use for an action and returns it's content.
Expand Down Expand Up @@ -141,15 +142,8 @@ func (kg kubeconfigGetter) getKubeconfigSource(contextLogger *log.Entry, lokoCon
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.
Expand Down
13 changes: 4 additions & 9 deletions cli/cmd/utils_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -112,14 +107,14 @@ func TestGetKubeconfigBadConfig(t *testing.T) {

func TestGetKubeconfigNoConfigButRequired(t *testing.T) {
k := &kubeconfigSources{
env: "/foo",
flag: "/bar",
env: "/foo",
}

contextLogger, lokoConfig := prepareKubeconfigSource(t, k)

kg := kubeconfigGetter{
platformRequired: true,
path: "/bar",
}

kubeconfig, err := kg.getKubeconfig(contextLogger, lokoConfig)
Expand Down Expand Up @@ -192,14 +187,14 @@ func TestGetKubeconfigSourceFlag(t *testing.T) {
count = 0
}
}`,
flag: expectedPath[0],
env: "/badpath",
env: "/badpath",
}

contextLogger, lokoConfig := prepareKubeconfigSource(t, k)

kg := kubeconfigGetter{
platformRequired: true,
path: expectedPath[0],
}

kubeconfig, err := kg.getKubeconfigSource(contextLogger, lokoConfig)
Expand Down

0 comments on commit 3455bd0

Please sign in to comment.