Skip to content

Commit 9e94993

Browse files
committed
Apply reviews
- Rename `GuestIPAddres` to `GuestIP` - Change type of `GuestIP` from `string` to `net.IP` Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
1 parent 156f115 commit 9e94993

File tree

7 files changed

+29
-25
lines changed

7 files changed

+29
-25
lines changed

pkg/hostagent/api/api.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
package api
55

6+
import "net"
7+
68
type Info struct {
79
// Guest IP address directly accessible from the host.
8-
GuestIPAddress string `json:"guestIPAddress,omitempty"`
10+
GuestIP net.IP `json:"guestIPAddress,omitempty"`
911
// SSH local port on the host forwarded to the guest's port 22.
1012
SSHLocalPort int `json:"sshLocalPort,omitempty"`
1113
}

pkg/hostagent/events/events.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package events
55

66
import (
7+
"net"
78
"time"
89
)
910

@@ -17,7 +18,7 @@ type Status struct {
1718
Errors []string `json:"errors,omitempty"`
1819

1920
// Guest IP address directly accessible from the host.
20-
GuestIPAddress string `json:"guestIPAddress,omitempty"`
21+
GuestIP net.IP `json:"guestIPAddress,omitempty"`
2122
// SSH local port on the host forwarded to the guest's port 22.
2223
SSHLocalPort int `json:"sshLocalPort,omitempty"`
2324

pkg/hostagent/hostagent.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ type HostAgent struct {
8686
// Guest interface name on the same subnet as the host,
8787
guestIfnameOnSameSubnetAsHost string
8888
// Guest IP address on the same subnet as the host.
89-
guestIPAddress string
90-
guestIPAddressMu sync.RWMutex
89+
guestIP net.IP
90+
guestIPMu sync.RWMutex
9191
}
9292

9393
type options struct {
@@ -367,7 +367,7 @@ func (a *HostAgent) emitGuestIPAddressEvent(ctx context.Context, ip string) {
367367
currentStatus := a.currentStatus
368368
a.statusMu.RUnlock()
369369

370-
currentStatus.GuestIPAddress = ip
370+
currentStatus.GuestIP = net.ParseIP(ip)
371371

372372
ev := events.Event{Status: currentStatus}
373373
a.emitEvent(ctx, ev)
@@ -515,22 +515,22 @@ func (a *HostAgent) startRoutinesAndWait(ctx context.Context, errCh <-chan error
515515
}
516516

517517
func (a *HostAgent) Info(_ context.Context) (*hostagentapi.Info, error) {
518-
a.guestIPAddressMu.RLock()
519-
defer a.guestIPAddressMu.RUnlock()
518+
a.guestIPMu.RLock()
519+
defer a.guestIPMu.RUnlock()
520520
info := &hostagentapi.Info{
521-
GuestIPAddress: a.guestIPAddress,
522-
SSHLocalPort: a.sshLocalPort,
521+
GuestIP: a.guestIP,
522+
SSHLocalPort: a.sshLocalPort,
523523
}
524524
return info, nil
525525
}
526526

527527
func (a *HostAgent) sshAddressPort() (sshAddress string, sshPort int) {
528-
a.guestIPAddressMu.RLock()
529-
defer a.guestIPAddressMu.RUnlock()
528+
a.guestIPMu.RLock()
529+
defer a.guestIPMu.RUnlock()
530530
sshAddress = a.instSSHAddress
531531
sshPort = a.sshLocalPort
532-
if a.guestIPAddress != "" {
533-
sshAddress = a.guestIPAddress
532+
if a.guestIP != nil {
533+
sshAddress = a.guestIP.String()
534534
sshPort = 22
535535
logrus.Debugf("Using the guest IP address %q directly", sshAddress)
536536
}

pkg/hostagent/requirements.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,9 @@ func (a *HostAgent) detectGuestIfnameOnSameSubnetAtHost(stdout string) error {
326326
if ifi.HardwareAddr.String() != neighbor.LLADDR {
327327
continue
328328
}
329-
a.guestIPAddressMu.Lock()
329+
a.guestIPMu.Lock()
330330
a.guestIfnameOnSameSubnetAsHost = neighbor.DEV
331-
a.guestIPAddressMu.Unlock()
331+
a.guestIPMu.Unlock()
332332
logrus.Infof("Detected the guest has interface %q in same subnet on the host", a.guestIfnameOnSameSubnetAsHost)
333333
return nil
334334
}
@@ -359,9 +359,9 @@ func (a *HostAgent) detectGuestIPAddress(stdout string) error {
359359
if addr.Family != "inet" {
360360
continue
361361
}
362-
a.guestIPAddressMu.Lock()
363-
a.guestIPAddress = addr.Local
364-
a.guestIPAddressMu.Unlock()
362+
a.guestIPMu.Lock()
363+
a.guestIP = net.ParseIP(addr.Local)
364+
a.guestIPMu.Unlock()
365365
logrus.Infof("The guest IP address on the interface %q is %q", a.guestIfnameOnSameSubnetAsHost, addr.Local)
366366
ctx := context.Background()
367367
a.emitGuestIPAddressEvent(ctx, addr.Local)

pkg/instance/start.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,12 @@ func watchHostAgentEvents(ctx context.Context, inst *limatype.Instance, haStdout
293293
inst.SSHLocalPort = ev.Status.SSHLocalPort
294294
}
295295

296-
if !printedGuestIPAddress && ev.Status.GuestIPAddress != "" {
297-
logrus.Infof("Guest IP Address: %s", ev.Status.GuestIPAddress)
296+
if !printedGuestIPAddress && ev.Status.GuestIP != nil {
297+
logrus.Infof("Guest IP Address: %s", ev.Status.GuestIP.String())
298298
printedGuestIPAddress = true
299299

300300
// Update the instance's Guest IP address
301-
inst.GuestIPAddress = ev.Status.GuestIPAddress
301+
inst.GuestIP = ev.Status.GuestIP
302302
}
303303

304304
if showProgress && ev.Status.CloudInitProgress != nil {

pkg/limatype/lima_instance.go

Lines changed: 4 additions & 3 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,7 +49,7 @@ type Instance struct {
4849
LimaVersion string `json:"limaVersion"`
4950
Param map[string]string `json:"param,omitempty"`
5051
// Guest IP address directly accessible from the host.
51-
GuestIPAddress string `json:"guestIPAddress,omitempty"`
52+
GuestIP net.IP `json:"guestIPAddress,omitempty"`
5253
}
5354

5455
// Protect protects the instance to prohibit accidental removal.
@@ -113,8 +114,8 @@ func (inst *Instance) UnmarshalJSON(data []byte) error {
113114
func (inst *Instance) SSHAddressPort() (sshAddress string, sshPort int) {
114115
sshAddress = inst.SSHAddress
115116
sshPort = inst.SSHLocalPort
116-
if inst.GuestIPAddress != "" {
117-
sshAddress = inst.GuestIPAddress
117+
if inst.GuestIP != nil {
118+
sshAddress = inst.GuestIP.String()
118119
sshPort = 22
119120
}
120121
return sshAddress, sshPort

pkg/store/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +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
85+
inst.GuestIP = info.GuestIP
8686
inst.SSHLocalPort = info.SSHLocalPort
8787
}
8888
}

0 commit comments

Comments
 (0)