Skip to content

Commit

Permalink
fix: KUBECONFIG gets set when using --kubeconfig via var DEVSPACE_FLAGS
Browse files Browse the repository at this point in the history
Signed-off-by: Armin Schlegel <armin.schlegel@gmx.de>
  • Loading branch information
siredmar committed May 29, 2024
1 parent 3212b31 commit b254376
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/util/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flags

import (
"fmt"
"os"
"strings"

"github.com/loft-sh/devspace/pkg/devspace/env"
Expand All @@ -18,6 +19,18 @@ func ApplyExtraFlags(cobraCmd *cobra.Command, osArgs []string, forceParsing bool
return nil, err
}

for i := 0; i < len(flags); i++ {
if flags[i] == "--kubeconfig" {
if i+1 < len(flags) {
err = os.Setenv("KUBECONFIG", flags[i+1])
if err != nil {
return nil, err
}
break
}
}
}

commandFlags, err := ParseCommandLine(env.GlobalGetEnv(envName))
if err != nil {
return nil, err
Expand Down
37 changes: 37 additions & 0 deletions pkg/util/flags/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package flags

import (
"os"
"testing"

"github.com/loft-sh/devspace/cmd/flags"
"github.com/loft-sh/devspace/pkg/devspace/env"
"github.com/spf13/cobra"
"gotest.tools/assert"
)

func Test_ApplyExtraFlagsWithEmpty(t *testing.T) {
mycmd := cobra.Command{}
flags, err := ApplyExtraFlags(&mycmd, []string{}, false)
assert.NilError(t, err, "Error applying extra flags")
assert.Equal(t, 0, len(flags), "Flags should be empty")
}

func Test_ApplyExtraFlagsWithDevspaceFlags(t *testing.T) {
devspaceFlags := env.GlobalGetEnv("DEVSPACE_FLAGS")
assert.Equal(t, "", devspaceFlags, "DEVSPACE_FLAGS should be empty")

devspaceFlags = "-s --kubeconfig /path/to/kubeconfig --debug"
err := os.Setenv("DEVSPACE_FLAGS", devspaceFlags)
assert.NilError(t, err, "Error setting DEVSPACE_FLAGS")
mycmd := &cobra.Command{}
persistentFlags := mycmd.PersistentFlags()
_ = flags.SetGlobalFlags(persistentFlags)

flags, err := ApplyExtraFlags(mycmd, []string{}, false)
assert.NilError(t, err, "Error applying extra flags")
assert.Equal(t, 4, len(flags), "Flags should have 4 elements")

kubeconfig := env.GlobalGetEnv("KUBECONFIG")
assert.Equal(t, "/path/to/kubeconfig", kubeconfig, "Path should be /path/to/kubeconfig")
}

0 comments on commit b254376

Please sign in to comment.