Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bind address option for cmd tunnel #14245

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/minikube/cmd/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
)

var cleanup bool
var bindAddress string

// tunnelCmd represents the tunnel command
var tunnelCmd = &cobra.Command{
Expand Down Expand Up @@ -93,7 +94,7 @@ var tunnelCmd = &cobra.Command{
sshKey := filepath.Join(localpath.MiniPath(), "machines", cname, "id_rsa")

outputTunnelStarted()
kicSSHTunnel := kic.NewSSHTunnel(ctx, sshPort, sshKey, clientset.CoreV1(), clientset.NetworkingV1())
kicSSHTunnel := kic.NewSSHTunnel(ctx, sshPort, sshKey, bindAddress, clientset.CoreV1(), clientset.NetworkingV1())
err = kicSSHTunnel.Start()
if err != nil {
exit.Error(reason.SvcTunnelStart, "error starting tunnel", err)
Expand All @@ -119,4 +120,5 @@ func outputTunnelStarted() {

func init() {
tunnelCmd.Flags().BoolVarP(&cleanup, "cleanup", "c", true, "call with cleanup=true to remove old tunnels")
tunnelCmd.Flags().StringVar(&bindAddress, "bind-address", "", "set tunnel bind address, empty or `*' indicates that tunnel should be available for all interfaces")
te-simonren marked this conversation as resolved.
Show resolved Hide resolved
}
27 changes: 20 additions & 7 deletions pkg/minikube/tunnel/kic/ssh_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type sshConn struct {
suppressStdOut bool
}

func createSSHConn(name, sshPort, sshKey string, resourcePorts []int32, resourceIP string, resourceName string) *sshConn {
func createSSHConn(name, sshPort, sshKey, bindAddress string, resourcePorts []int32, resourceIP string, resourceName string) *sshConn {
// extract sshArgs
sshArgs := []string{
// TODO: document the options here
Expand All @@ -53,12 +53,25 @@ func createSSHConn(name, sshPort, sshKey string, resourcePorts []int32, resource
askForSudo := false
var privilegedPorts []int32
for _, port := range resourcePorts {
arg := fmt.Sprintf(
"-L %d:%s:%d",
port,
resourceIP,
port,
)
var arg string
if bindAddress == "" || bindAddress == "*" {
// bind on all interfaces
arg = fmt.Sprintf(
"-L %d:%s:%d",
port,
resourceIP,
port,
)
} else {
// bind on specify address only
arg = fmt.Sprintf(
"-L %s:%d:%s:%d",
bindAddress,
port,
resourceIP,
port,
)
}

// check if any port is privileged
if port < 1024 {
Expand Down
8 changes: 5 additions & 3 deletions pkg/minikube/tunnel/kic/ssh_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type SSHTunnel struct {
ctx context.Context
sshPort string
sshKey string
bindAddress string
v1Core typed_core.CoreV1Interface
v1Networking typed_networking.NetworkingV1Interface
LoadBalancerEmulator tunnel.LoadBalancerEmulator
Expand All @@ -45,11 +46,12 @@ type SSHTunnel struct {
}

// NewSSHTunnel ...
func NewSSHTunnel(ctx context.Context, sshPort, sshKey string, v1Core typed_core.CoreV1Interface, v1Networking typed_networking.NetworkingV1Interface) *SSHTunnel {
func NewSSHTunnel(ctx context.Context, sshPort, sshKey, bindAddress string, v1Core typed_core.CoreV1Interface, v1Networking typed_networking.NetworkingV1Interface) *SSHTunnel {
return &SSHTunnel{
ctx: ctx,
sshPort: sshPort,
sshKey: sshKey,
bindAddress: bindAddress,
v1Core: v1Core,
LoadBalancerEmulator: tunnel.NewLoadBalancerEmulator(v1Core),
v1Networking: v1Networking,
Expand Down Expand Up @@ -124,7 +126,7 @@ func (t *SSHTunnel) startConnection(svc v1.Service) {
}

// create new ssh conn
newSSHConn := createSSHConn(uniqName, t.sshPort, t.sshKey, resourcePorts, svc.Spec.ClusterIP, svc.Name)
newSSHConn := createSSHConn(uniqName, t.sshPort, t.sshKey, t.bindAddress, resourcePorts, svc.Spec.ClusterIP, svc.Name)
t.conns[newSSHConn.name] = newSSHConn

go func() {
Expand Down Expand Up @@ -154,7 +156,7 @@ func (t *SSHTunnel) startConnectionIngress(ingress v1_networking.Ingress) {
resourceIP := "127.0.0.1"

// create new ssh conn
newSSHConn := createSSHConn(uniqName, t.sshPort, t.sshKey, resourcePorts, resourceIP, ingress.Name)
newSSHConn := createSSHConn(uniqName, t.sshPort, t.sshKey, t.bindAddress, resourcePorts, resourceIP, ingress.Name)
t.conns[newSSHConn.name] = newSSHConn

go func() {
Expand Down