Skip to content

Commit 9c6d7cb

Browse files
committed
pkg/hostagent/events: Add GuestIPAddress string field to Status
Support printing "Guest IP Address: %s" on `limactl start` Signed-off-by: Norio Nomura <[email protected]>
1 parent b63b072 commit 9c6d7cb

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

pkg/hostagent/events/events.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ type Status struct {
1616

1717
Errors []string `json:"errors,omitempty"`
1818

19+
// Guest IP address directly accessible from the host.
20+
GuestIPAddress string `json:"guestIPAddress,omitempty"`
21+
// SSH local port on the host forwarded to the guest's port 22.
1922
SSHLocalPort int `json:"sshLocalPort,omitempty"`
2023

2124
// Cloud-init progress information

pkg/hostagent/hostagent.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,17 @@ func (a *HostAgent) emitCloudInitProgressEvent(ctx context.Context, progress *ev
362362
a.emitEvent(ctx, ev)
363363
}
364364

365+
func (a *HostAgent) emitGuestIPAddressEvent(ctx context.Context, ip string) {
366+
a.statusMu.RLock()
367+
currentStatus := a.currentStatus
368+
a.statusMu.RUnlock()
369+
370+
currentStatus.GuestIPAddress = ip
371+
372+
ev := events.Event{Status: currentStatus}
373+
a.emitEvent(ctx, ev)
374+
}
375+
365376
func generatePassword(length int) (string, error) {
366377
// avoid any special symbols, to make it easier to copy/paste
367378
return password.Generate(length, length/4, 0, false, false)

pkg/hostagent/requirements.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,9 @@ func (a *HostAgent) detectGuestIPAddress(stdout string) error {
359359
a.guestIPAddressMu.Lock()
360360
a.guestIPAddress = addr.Local
361361
a.guestIPAddressMu.Unlock()
362-
a.WriteSSHConfigFile(context.Background())
362+
ctx := context.Background()
363+
a.WriteSSHConfigFile(ctx)
364+
a.emitGuestIPAddressEvent(ctx, addr.Local)
363365
logrus.Infof("The guest IP address on the interface %q is %q", a.guestIfnameOnSameSubnetAsHost, addr.Local)
364366
return nil
365367
}

pkg/instance/start.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,11 @@ func watchHostAgentEvents(ctx context.Context, inst *limatype.Instance, haStdout
277277
defer cancel()
278278

279279
var (
280-
printedSSHLocalPort bool
281-
receivedRunningEvent bool
282-
cloudInitCompleted bool
283-
err error
280+
printedSSHLocalPort bool
281+
printedGuestIPAddress bool
282+
receivedRunningEvent bool
283+
cloudInitCompleted bool
284+
err error
284285
)
285286

286287
onEvent := func(ev hostagentevents.Event) bool {
@@ -292,6 +293,14 @@ func watchHostAgentEvents(ctx context.Context, inst *limatype.Instance, haStdout
292293
inst.SSHLocalPort = ev.Status.SSHLocalPort
293294
}
294295

296+
if !printedGuestIPAddress && ev.Status.GuestIPAddress != "" {
297+
logrus.Infof("Guest IP Address: %s", ev.Status.GuestIPAddress)
298+
printedGuestIPAddress = true
299+
300+
// Update the instance's Guest IP address
301+
inst.GuestIPAddress = ev.Status.GuestIPAddress
302+
}
303+
295304
if showProgress && ev.Status.CloudInitProgress != nil {
296305
progress := ev.Status.CloudInitProgress
297306
if progress.Active && progress.LogLine == "" {

0 commit comments

Comments
 (0)