Skip to content

Commit 2e47b79

Browse files
committed
pkg/hostagent: Add GuestIP field to GET /v1/info endpoint`
Update to use guestIPAddress: - `limactl shell` - `limactl show-ssh` - `limactl tunnel` pkg/limatype: - Add `GuestIP net.IP` to `Instance` - Add `Instance.SSHAddressPort()` helper to provide ipAddress and port for SSH Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
1 parent 6a652ed commit 2e47b79

File tree

7 files changed

+32
-7
lines changed

7 files changed

+32
-7
lines changed

cmd/limactl/shell.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,11 @@ func shellAction(cmd *cobra.Command, args []string) error {
267267
if olderSSH {
268268
logLevel = "QUIET"
269269
}
270+
sshAddress, sshPort := inst.SSHAddressPort()
270271
sshArgs = append(sshArgs, []string{
271272
"-o", fmt.Sprintf("LogLevel=%s", logLevel),
272-
"-p", strconv.Itoa(inst.SSHLocalPort),
273-
inst.SSHAddress,
273+
"-p", strconv.Itoa(sshPort),
274+
sshAddress,
274275
"--",
275276
script,
276277
}...)

cmd/limactl/show-ssh.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
109109
if err != nil {
110110
return err
111111
}
112-
opts = append(opts, "Hostname=127.0.0.1")
113-
opts = append(opts, fmt.Sprintf("Port=%d", inst.SSHLocalPort))
112+
sshAddress, sshPort := inst.SSHAddressPort()
113+
opts = append(opts, fmt.Sprintf("Hostname=%s", sshAddress))
114+
opts = append(opts, fmt.Sprintf("Port=%d", sshPort))
114115
return sshutil.Format(w, "ssh", instName, format, opts)
115116
}
116117

cmd/limactl/tunnel.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,14 @@ func tunnelAction(cmd *cobra.Command, args []string) error {
103103
}
104104
sshArgs := append([]string{}, sshExe.Args...)
105105
sshArgs = append(sshArgs, sshutil.SSHArgsFromOpts(sshOpts)...)
106+
sshAddress, sshPort := inst.SSHAddressPort()
106107
sshArgs = append(sshArgs, []string{
107108
"-q", // quiet
108109
"-f", // background
109110
"-N", // no command
110111
"-D", fmt.Sprintf("127.0.0.1:%d", port),
111-
"-p", strconv.Itoa(inst.SSHLocalPort),
112-
inst.SSHAddress,
112+
"-p", strconv.Itoa(sshPort),
113+
sshAddress,
113114
}...)
114115
sshCmd := exec.CommandContext(ctx, sshExe.Exe, sshArgs...)
115116
sshCmd.Stdout = stderr

pkg/hostagent/api/api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
package api
55

6+
import "net"
7+
68
type Info struct {
9+
// Guest IP address directly accessible from the host.
10+
GuestIP net.IP `json:"guestIP,omitempty"`
11+
// SSH local port on the host forwarded to the guest's port 22.
712
SSHLocalPort int `json:"sshLocalPort,omitempty"`
813
}

pkg/hostagent/hostagent.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,9 @@ func (a *HostAgent) GuestIP() net.IP {
518518
}
519519

520520
func (a *HostAgent) Info(_ context.Context) (*hostagentapi.Info, error) {
521+
guestIP := a.GuestIP()
521522
info := &hostagentapi.Info{
523+
GuestIP: guestIP,
522524
SSHLocalPort: a.sshLocalPort,
523525
}
524526
return info, nil

pkg/limatype/lima_instance.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package limatype
66
import (
77
"encoding/json"
88
"errors"
9+
"net"
910
"os"
1011
"path/filepath"
1112

@@ -47,6 +48,8 @@ type Instance struct {
4748
Protected bool `json:"protected"`
4849
LimaVersion string `json:"limaVersion"`
4950
Param map[string]string `json:"param,omitempty"`
51+
// Guest IP address directly accessible from the host.
52+
GuestIP net.IP `json:"guestIP,omitempty"`
5053
}
5154

5255
// Protect protects the instance to prohibit accidental removal.
@@ -107,3 +110,13 @@ func (inst *Instance) UnmarshalJSON(data []byte) error {
107110
}
108111
return nil
109112
}
113+
114+
func (inst *Instance) SSHAddressPort() (sshAddress string, sshPort int) {
115+
sshAddress = inst.SSHAddress
116+
sshPort = inst.SSHLocalPort
117+
if inst.GuestIP != nil {
118+
sshAddress = inst.GuestIP.String()
119+
sshPort = 22
120+
}
121+
return sshAddress, sshPort
122+
}

pkg/store/instance.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func Inspect(ctx context.Context, instName string) (*limatype.Instance, error) {
8282
inst.Status = limatype.StatusBroken
8383
inst.Errors = append(inst.Errors, fmt.Errorf("failed to get Info from %q: %w", haSock, err))
8484
} else {
85+
inst.GuestIP = info.GuestIP
8586
inst.SSHLocalPort = info.SSHLocalPort
8687
}
8788
}
@@ -346,10 +347,11 @@ func PrintInstances(w io.Writer, instances []*limatype.Instance, format string,
346347
if strings.HasPrefix(dir, homeDir) {
347348
dir = strings.Replace(dir, homeDir, "~", 1)
348349
}
350+
sshAddress, sshPort := instance.SSHAddressPort()
349351
fmt.Fprintf(w, "%s\t%s\t%s",
350352
instance.Name,
351353
instance.Status,
352-
fmt.Sprintf("%s:%d", instance.SSHAddress, instance.SSHLocalPort),
354+
fmt.Sprintf("%s:%d", sshAddress, sshPort),
353355
)
354356
if !hideType {
355357
fmt.Fprintf(w, "\t%s",

0 commit comments

Comments
 (0)