Skip to content

Commit

Permalink
fix: allow SSH to be enabled and disabled via variable
Browse files Browse the repository at this point in the history
Fixes #2693
Fixes ENG-1886

Signed-off-by: Russell Centanni <[email protected]>
  • Loading branch information
lizardruss committed Sep 8, 2023
1 parent c477eaf commit 32a1958
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 8 deletions.
3 changes: 2 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"

"github.com/loft-sh/devspace/pkg/util/ptr"
"mvdan.cc/sh/v3/expand"

"github.com/loft-sh/devspace/pkg/devspace/compose"
Expand Down Expand Up @@ -759,7 +760,7 @@ func (cmd *InitCmd) addDevConfig(config *latest.Config, imageName, image string,
devConfig.DevImage = devImage

devConfig.SSH = &latest.SSH{
Enabled: true,
Enabled: ptr.Bool(true),
}

if devConfig.ProxyCommands == nil {
Expand Down
114 changes: 111 additions & 3 deletions e2e/tests/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package ssh

import (
"context"
"github.com/onsi/ginkgo/v2"
"os"
"os/exec"
"time"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"

"github.com/loft-sh/devspace/cmd"
"github.com/loft-sh/devspace/cmd/flags"
"github.com/loft-sh/devspace/e2e/framework"
Expand Down Expand Up @@ -34,7 +36,7 @@ var _ = DevSpaceDescribe("ssh", func() {
framework.ExpectNoError(err)
})

ginkgo.It("devspace dev should start an SSH service", func() {
ginkgo.It("devspace dev should start an SSH service", func(ctx context.Context) {
tempDir, err := framework.CopyToTempDir("tests/ssh/testdata/ssh-simple")
framework.ExpectNoError(err)
defer framework.CleanupTempDir(initialDir, tempDir)
Expand All @@ -45,7 +47,7 @@ var _ = DevSpaceDescribe("ssh", func() {

// create a new dev command and start it
done := make(chan error)
cancelCtx, cancel := context.WithCancel(context.Background())
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()

go func() {
Expand Down Expand Up @@ -80,4 +82,110 @@ var _ = DevSpaceDescribe("ssh", func() {
err = <-done
framework.ExpectNoError(err)
})

ginkgo.It("devspace dev should NOT start an SSH service when disabled with a variable", func(ctx context.Context) {
tempDir, err := framework.CopyToTempDir("tests/ssh/testdata/ssh-variable")
framework.ExpectNoError(err)
defer framework.CleanupTempDir(initialDir, tempDir)

ns, err := kubeClient.CreateNamespace("ssh")
framework.ExpectNoError(err)
defer framework.ExpectDeleteNamespace(kubeClient, ns)

// create a new dev command and start it
done := make(chan error)
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()

go func() {
defer ginkgo.GinkgoRecover()

devCmd := &cmd.RunPipelineCmd{
GlobalFlags: &flags.GlobalFlags{
NoWarn: true,
Namespace: ns,
Vars: []string{"SSH=false"},
},
Pipeline: "dev",
Ctx: cancelCtx,
}

done <- devCmd.RunDefault(f)
}()

// connect to the SSH server
gomega.Eventually(func(g gomega.Gomega) {
cmd := exec.Command("ssh", "test.ssh-variable.devspace", "ls")
out, err := cmd.CombinedOutput()
output := string(out)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(output).To(
gomega.Or(
gomega.ContainSubstring("Could not resolve hostname test.ssh-variable.devspace"),
gomega.ContainSubstring("ssh: connect to host localhost port 10023"),
),
)
}).
WithPolling(time.Second).
WithTimeout(time.Second * 10).
Should(gomega.Succeed())

cancel()

err = <-done
framework.ExpectNoError(err)
})

ginkgo.It("devspace dev should start an SSH service when enabled with a variable", func(ctx context.Context) {
tempDir, err := framework.CopyToTempDir("tests/ssh/testdata/ssh-variable")
framework.ExpectNoError(err)
defer framework.CleanupTempDir(initialDir, tempDir)

ns, err := kubeClient.CreateNamespace("ssh")
framework.ExpectNoError(err)
defer framework.ExpectDeleteNamespace(kubeClient, ns)

// create a new dev command and start it
done := make(chan error)
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()

go func() {
defer ginkgo.GinkgoRecover()

devCmd := &cmd.RunPipelineCmd{
GlobalFlags: &flags.GlobalFlags{
NoWarn: true,
Namespace: ns,
Vars: []string{"SSH=true"},
},
Pipeline: "dev",
Ctx: cancelCtx,
}

done <- devCmd.RunDefault(f)
}()

// connect to the SSH server
gomega.Eventually(func(g gomega.Gomega) {
cmd := exec.Command("ssh", "test.ssh-variable.devspace", "ls")
out, err := cmd.CombinedOutput()
output := string(out)
g.Expect(err).To(gomega.HaveOccurred())
g.Expect(output).To(
gomega.Or(
gomega.ContainSubstring("Could not resolve hostname test.ssh-variable.devspace"),
gomega.ContainSubstring("ssh: connect to host localhost port 10023"),
),
)
}).
WithPolling(time.Second).
WithTimeout(time.Second * 10).
Should(gomega.Succeed())

cancel()

err = <-done
framework.ExpectNoError(err)
})
})
23 changes: 23 additions & 0 deletions e2e/tests/ssh/testdata/ssh-variable/devspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: v2beta1
vars:
IMAGE: alpine
SSH:
value: true
deployments:
test:
helm:
chart:
name: component-chart
repo: https://charts.devspace.sh
values:
containers:
- image: ${IMAGE}
command: ["sleep"]
args: ["999999999999"]
dev:
test:
imageSelector: ${IMAGE}
ssh:
enabled: ${SSH}
localHostname: test.ssh-variable.devspace
localPort: 10023
2 changes: 1 addition & 1 deletion pkg/devspace/config/versions/latest/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ type SSH struct {
// Enabled can be used to enable the ssh server within the container. By default,
// DevSpace will generate the required keys and create an entry in your ~/.ssh/config
// for this container that can be used via `ssh dev-config-name.dev-project-name.devspace`
Enabled bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`

// LocalHostname is the local ssh host to write to the ~/.ssh/config
LocalHostname string `yaml:"localHostname,omitempty" json:"localHostname,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions pkg/devspace/services/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package ssh
import (
"bytes"
"fmt"
"github.com/mgutz/ansi"
"github.com/mitchellh/go-homedir"
"io"
"path/filepath"
"strconv"
Expand All @@ -21,6 +19,8 @@ import (
"github.com/loft-sh/devspace/pkg/devspace/services/targetselector"
"github.com/loft-sh/devspace/pkg/devspace/services/terminal"
"github.com/loft-sh/devspace/pkg/util/tomb"
"github.com/mgutz/ansi"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
kubectlExec "k8s.io/client-go/util/exec"
Expand All @@ -35,7 +35,7 @@ func StartSSH(ctx devspacecontext.Context, devPod *latest.DevPod, selector targe
// init done array is used to track when sync was initialized
initDoneArray := []chan struct{}{}
loader.EachDevContainer(devPod, func(devContainer *latest.DevContainer) bool {
if devContainer.SSH == nil {
if devContainer.SSH == nil || (devContainer.SSH.Enabled != nil && !*devContainer.SSH.Enabled) {
return true
}

Expand Down

0 comments on commit 32a1958

Please sign in to comment.