Skip to content

Commit 149d5a0

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> # Conflicts: # pkg/hostagent/api/api.go # pkg/hostagent/hostagent.go # pkg/limatype/lima_instance.go
1 parent da86387 commit 149d5a0

File tree

7 files changed

+32
-8
lines changed

7 files changed

+32
-8
lines changed

cmd/limactl/shell.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,11 @@ func shellAction(cmd *cobra.Command, args []string) error {
273273
if olderSSH {
274274
logLevel = "QUIET"
275275
}
276+
sshAddress, sshPort := inst.SSHAddressPort()
276277
sshArgs = append(sshArgs, []string{
277278
"-o", fmt.Sprintf("LogLevel=%s", logLevel),
278-
"-p", strconv.Itoa(inst.SSHLocalPort),
279-
inst.SSHAddress,
279+
"-p", strconv.Itoa(sshPort),
280+
sshAddress,
280281
"--",
281282
script,
282283
}...)

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33

44
package api
55

6+
import "net"
7+
68
type Info struct {
79
// indicate instance is started by launchd or systemd if not empty
810
AutoStartedIdentifier string `json:"autoStartedIdentifier,omitempty"`
9-
// SSHLocalPort is the local port on the host for SSH access to the VM.
11+
// Guest IP address directly accessible from the host.
12+
GuestIP net.IP `json:"guestIP,omitempty"`
13+
// SSH local port on the host forwarded to the guest's port 22.
1014
SSHLocalPort int `json:"sshLocalPort,omitempty"`
1115
}

pkg/hostagent/hostagent.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,10 @@ func (a *HostAgent) GuestIP() net.IP {
521521
}
522522

523523
func (a *HostAgent) Info(_ context.Context) (*hostagentapi.Info, error) {
524+
guestIP := a.GuestIP()
524525
info := &hostagentapi.Info{
525526
AutoStartedIdentifier: autostart.AutoStartedIdentifier(),
527+
GuestIP: guestIP,
526528
SSHLocalPort: a.sshLocalPort,
527529
}
528530
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

@@ -48,6 +49,8 @@ type Instance struct {
4849
LimaVersion string `json:"limaVersion"`
4950
Param map[string]string `json:"param,omitempty"`
5051
AutoStartedIdentifier string `json:"autoStartedIdentifier,omitempty"`
52+
// Guest IP address directly accessible from the host.
53+
GuestIP net.IP `json:"guestIP,omitempty"`
5154
}
5255

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

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
inst.AutoStartedIdentifier = info.AutoStartedIdentifier
8788
}
@@ -347,10 +348,11 @@ func PrintInstances(w io.Writer, instances []*limatype.Instance, format string,
347348
if strings.HasPrefix(dir, homeDir) {
348349
dir = strings.Replace(dir, homeDir, "~", 1)
349350
}
351+
sshAddress, sshPort := instance.SSHAddressPort()
350352
fmt.Fprintf(w, "%s\t%s\t%s",
351353
instance.Name,
352354
instance.Status,
353-
fmt.Sprintf("%s:%d", instance.SSHAddress, instance.SSHLocalPort),
355+
fmt.Sprintf("%s:%d", sshAddress, sshPort),
354356
)
355357
if !hideType {
356358
fmt.Fprintf(w, "\t%s",

0 commit comments

Comments
 (0)