Skip to content

Commit b63b072

Browse files
committed
pkg/hostagent: Add GuestIPAddress field to GET /v1/info endpoint`
Update to use guestIPAddress: - `limactl shell` - `limactl show-ssh` - `limactl tunnel` pkg/limatype: - Add `GuestIPAddress string` to `Instance` - Add `Instance.SSHAddressPort()` helper to provide ipAddress and port for SSH Signed-off-by: Norio Nomura <[email protected]>
1 parent f22e9fa commit b63b072

File tree

7 files changed

+31
-8
lines changed

7 files changed

+31
-8
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
package api
55

66
type Info struct {
7+
// Guest IP address directly accessible from the host.
8+
GuestIPAddress string `json:"guestIPAddress,omitempty"`
9+
// SSH local port on the host forwarded to the guest's port 22.
710
SSHLocalPort int `json:"sshLocalPort,omitempty"`
811
}

pkg/hostagent/hostagent.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,11 @@ func (a *HostAgent) startRoutinesAndWait(ctx context.Context, errCh <-chan error
504504
}
505505

506506
func (a *HostAgent) Info(_ context.Context) (*hostagentapi.Info, error) {
507+
a.guestIPAddressMu.RLock()
508+
defer a.guestIPAddressMu.RUnlock()
507509
info := &hostagentapi.Info{
508-
SSHLocalPort: a.sshLocalPort,
510+
GuestIPAddress: a.guestIPAddress,
511+
SSHLocalPort: a.sshLocalPort,
509512
}
510513
return info, nil
511514
}

pkg/limatype/lima_instance.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ type Instance struct {
4747
Protected bool `json:"protected"`
4848
LimaVersion string `json:"limaVersion"`
4949
Param map[string]string `json:"param,omitempty"`
50+
// Guest IP address directly accessible from the host.
51+
GuestIPAddress string `json:"guestIPAddress,omitempty"`
5052
}
5153

5254
// Protect protects the instance to prohibit accidental removal.
@@ -107,3 +109,13 @@ func (inst *Instance) UnmarshalJSON(data []byte) error {
107109
}
108110
return nil
109111
}
112+
113+
func (inst *Instance) SSHAddressPort() (string, int) {
114+
sshAddress := inst.SSHAddress
115+
sshPort := inst.SSHLocalPort
116+
if inst.GuestIPAddress != "" {
117+
sshAddress = inst.GuestIPAddress
118+
sshPort = 22
119+
}
120+
return sshAddress, sshPort
121+
}

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.GuestIPAddress = info.GuestIPAddress
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)