Skip to content

Commit bbecdff

Browse files
authored
Merge pull request #2649 from lizardruss/fix-command-k8s-unauthorized
Devspace command fails when cluster is not required
2 parents 7ba5705 + 249247e commit bbecdff

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

cmd/run.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ func (cmd *RunCmd) LoadCommandsConfig(f factory.Factory, configLoader loader.Con
249249
client = nil
250250
}
251251

252+
// verify client connectivity / authn / authz
253+
_, err = client.KubeClient().Discovery().ServerVersion()
254+
if err != nil {
255+
log.Debugf("Unable to discover server version: %v", err)
256+
client = nil
257+
}
258+
252259
// Parse commands
253260
commandsInterface, err := configLoader.LoadWithParser(context.Background(), localCache, client, loader.NewCommandsParser(), configOptions, log)
254261
if err != nil {

e2e/tests/command/command.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package command
22

33
import (
44
"bytes"
5-
"github.com/onsi/ginkgo/v2"
65
"os"
6+
"path/filepath"
77

88
"github.com/loft-sh/devspace/cmd"
99
"github.com/loft-sh/devspace/cmd/flags"
1010
"github.com/loft-sh/devspace/e2e/framework"
11+
"github.com/onsi/ginkgo/v2"
1112
)
1213

1314
var _ = DevSpaceDescribe("command", func() {
@@ -66,4 +67,24 @@ var _ = DevSpaceDescribe("command", func() {
6667
framework.ExpectNoError(err)
6768
framework.ExpectEqual(stdout.String(), "test123 test456")
6869
})
70+
71+
ginkgo.It("should run command without kubernetes cluster", func() {
72+
tempDir, err := framework.CopyToTempDir("tests/command/testdata/command-without-cluster")
73+
framework.ExpectNoError(err)
74+
defer framework.CleanupTempDir(initialDir, tempDir)
75+
76+
defer os.Unsetenv("KUBECONFIG")
77+
err = os.Setenv("KUBECONFIG", filepath.Join(tempDir, "config"))
78+
framework.ExpectNoError(err)
79+
80+
stdout := &bytes.Buffer{}
81+
runCmd := &cmd.RunCmd{
82+
GlobalFlags: &flags.GlobalFlags{},
83+
Stdout: stdout,
84+
Stderr: stdout,
85+
}
86+
err = runCmd.RunRun(f, []string{"test"})
87+
framework.ExpectNoError(err)
88+
framework.ExpectEqual(stdout.String(), "Hello World")
89+
})
6990
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
clusters:
3+
- cluster:
4+
server: https://127.0.0.1:52617
5+
name: kind-kind
6+
contexts:
7+
- context:
8+
cluster: kind-kind
9+
user: kind-kind
10+
name: kind-kind
11+
current-context: kind-kind
12+
kind: Config
13+
preferences: {}
14+
users:
15+
- name: kind-kind
16+
user: {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: v2beta1
2+
name: command-without-cluster
3+
4+
commands:
5+
test: |-
6+
echo -n "Hello World"

0 commit comments

Comments
 (0)