Skip to content

Commit

Permalink
Merge pull request #8290 from sharifelgamal/native-ssh
Browse files Browse the repository at this point in the history
respect native-ssh param properly
  • Loading branch information
sharifelgamal committed May 28, 2020
2 parents c77959a + 12c1d7e commit a2c8823
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
9 changes: 1 addition & 8 deletions cmd/minikube/cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"os"

"github.com/docker/machine/libmachine/ssh"
"github.com/spf13/cobra"

"k8s.io/minikube/pkg/minikube/config"
Expand Down Expand Up @@ -58,13 +57,7 @@ var sshCmd = &cobra.Command{
}
}

if nativeSSHClient {
ssh.SetDefaultClient(ssh.Native)
} else {
ssh.SetDefaultClient(ssh.External)
}

err = machine.CreateSSHShell(co.API, *co.Config, *n, args)
err = machine.CreateSSHShell(co.API, *co.Config, *n, args, nativeSSHClient)
if err != nil {
// This is typically due to a non-zero exit code, so no need for flourish.
out.ErrLn("ssh: %v", err)
Expand Down
12 changes: 6 additions & 6 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,6 @@ func provisionWithDriver(cmd *cobra.Command, ds registry.DriverState, existing *
cc.MinikubeISO = url
}

if viper.GetBool(nativeSSH) {
ssh.SetDefaultClient(ssh.Native)
} else {
ssh.SetDefaultClient(ssh.External)
}

var existingAddons map[string]bool
if viper.GetBool(installAddons) {
existingAddons = map[string]bool{}
Expand All @@ -265,6 +259,12 @@ func provisionWithDriver(cmd *cobra.Command, ds registry.DriverState, existing *
return node.Starter{}, err
}

if viper.GetBool(nativeSSH) {
ssh.SetDefaultClient(ssh.Native)
} else {
ssh.SetDefaultClient(ssh.External)
}

return node.Starter{
Runner: mRunner,
PreExists: preExists,
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/machine/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func TestCreateSSHShell(t *testing.T) {
cc.Name = viper.GetString("profile")

cliArgs := []string{"exit"}
if err := CreateSSHShell(api, cc, config.Node{Name: "minikube"}, cliArgs); err != nil {
if err := CreateSSHShell(api, cc, config.Node{Name: "minikube"}, cliArgs, true); err != nil {
t.Fatalf("Error running ssh command: %v", err)
}

Expand Down
10 changes: 9 additions & 1 deletion pkg/minikube/machine/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ package machine

import (
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/driver"
)

// CreateSSHShell creates a new SSH shell / client
func CreateSSHShell(api libmachine.API, cc config.ClusterConfig, n config.Node, args []string) error {
func CreateSSHShell(api libmachine.API, cc config.ClusterConfig, n config.Node, args []string, native bool) error {
machineName := driver.MachineName(cc, n)
host, err := LoadHost(api, machineName)
if err != nil {
Expand All @@ -42,6 +43,13 @@ func CreateSSHShell(api libmachine.API, cc config.ClusterConfig, n config.Node,
}

client, err := host.CreateSSHClient()

if native {
ssh.SetDefaultClient(ssh.Native)
} else {
ssh.SetDefaultClient(ssh.External)
}

if err != nil {
return errors.Wrap(err, "Creating ssh client")
}
Expand Down
10 changes: 6 additions & 4 deletions test/integration/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ func validateSSHCmd(ctx context.Context, t *testing.T, profile string) {
mctx, cancel := context.WithTimeout(ctx, Minutes(1))
defer cancel()

want := "hello\n"
want := "hello"

rr, err := Run(t, exec.CommandContext(mctx, Target(), "-p", profile, "ssh", "echo hello"))
if mctx.Err() == context.DeadlineExceeded {
Expand All @@ -846,15 +846,16 @@ func validateSSHCmd(ctx context.Context, t *testing.T, profile string) {
if err != nil {
t.Errorf("failed to run an ssh command. args %q : %v", rr.Command(), err)
}
if rr.Stdout.String() != want {
// trailing whitespace differs between native and external SSH clients, so let's trim it and call it a day
if strings.TrimSpace(rr.Stdout.String()) != want {
t.Errorf("expected minikube ssh command output to be -%q- but got *%q*. args %q", want, rr.Stdout.String(), rr.Command())
}

// testing hostname as well because testing something like "minikube ssh echo" could be confusing
// because it is not clear if echo was run inside minikube on the powershell
// so better to test something inside minikube, that is meaningful per profile
// in this case /etc/hostname is same as the profile name
want = profile + "\n"
want = profile
rr, err = Run(t, exec.CommandContext(mctx, Target(), "-p", profile, "ssh", "cat /etc/hostname"))
if mctx.Err() == context.DeadlineExceeded {
t.Errorf("failed to run command by deadline. exceeded timeout : %s", rr.Command())
Expand All @@ -863,7 +864,8 @@ func validateSSHCmd(ctx context.Context, t *testing.T, profile string) {
if err != nil {
t.Errorf("failed to run an ssh command. args %q : %v", rr.Command(), err)
}
if rr.Stdout.String() != want {
// trailing whitespace differs between native and external SSH clients, so let's trim it and call it a day
if strings.TrimSpace(rr.Stdout.String()) != want {
t.Errorf("expected minikube ssh command output to be -%q- but got *%q*. args %q", want, rr.Stdout.String(), rr.Command())
}
}
Expand Down

0 comments on commit a2c8823

Please sign in to comment.