Skip to content

Commit 13ebdd2

Browse files
authored
Merge pull request #2623 from alexandear/refactor/improve-var-field-name
pkg: improve field and variable names
2 parents f6a59b0 + 41102b6 commit 13ebdd2

File tree

17 files changed

+153
-153
lines changed

17 files changed

+153
-153
lines changed

pkg/driver/driver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ type Driver interface {
7272
}
7373

7474
type BaseDriver struct {
75-
Instance *store.Instance
76-
Yaml *limayaml.LimaYAML
75+
Instance *store.Instance
76+
InstConfig *limayaml.LimaYAML
7777

7878
SSHLocalPort int
7979
VSockPort int

pkg/driverutil/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func CreateTargetDriverInstance(base *driver.BaseDriver) driver.Driver {
12-
limaDriver := base.Yaml.VMType
12+
limaDriver := base.InstConfig.VMType
1313
if *limaDriver == limayaml.VZ {
1414
return vz.New(base)
1515
}

pkg/hostagent/hostagent.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
)
4141

4242
type HostAgent struct {
43-
y *limayaml.LimaYAML
43+
instConfig *limayaml.LimaYAML
4444
sshLocalPort int
4545
udpDNSLocalPort int
4646
tcpDNSLocalPort int
@@ -97,22 +97,22 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
9797
return nil, err
9898
}
9999

100-
y, err := inst.LoadYAML()
100+
instConfig, err := inst.LoadYAML()
101101
if err != nil {
102102
return nil, err
103103
}
104104
// y is loaded with FillDefault() already, so no need to care about nil pointers.
105105

106-
sshLocalPort, err := determineSSHLocalPort(y, instName)
106+
sshLocalPort, err := determineSSHLocalPort(instConfig, instName)
107107
if err != nil {
108108
return nil, err
109109
}
110-
if *y.VMType == limayaml.WSL2 {
110+
if *instConfig.VMType == limayaml.WSL2 {
111111
sshLocalPort = inst.SSHLocalPort
112112
}
113113

114114
var udpDNSLocalPort, tcpDNSLocalPort int
115-
if *y.HostResolver.Enabled {
115+
if *instConfig.HostResolver.Enabled {
116116
udpDNSLocalPort, err = findFreeUDPLocalPort()
117117
if err != nil {
118118
return nil, err
@@ -125,24 +125,24 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
125125

126126
vSockPort := 0
127127
virtioPort := ""
128-
if *y.VMType == limayaml.VZ {
128+
if *instConfig.VMType == limayaml.VZ {
129129
vSockPort = 2222
130-
} else if *y.VMType == limayaml.WSL2 {
130+
} else if *instConfig.VMType == limayaml.WSL2 {
131131
port, err := getFreeVSockPort()
132132
if err != nil {
133133
logrus.WithError(err).Error("failed to get free VSock port")
134134
}
135135
vSockPort = port
136-
} else if *y.VMType == limayaml.QEMU {
136+
} else if *instConfig.VMType == limayaml.QEMU {
137137
// virtserialport doesn't seem to work reliably: https://github.com/lima-vm/lima/issues/2064
138138
virtioPort = "" // filenames.VirtioPort
139139
}
140140

141-
if err := cidata.GenerateISO9660(inst.Dir, instName, y, udpDNSLocalPort, tcpDNSLocalPort, o.nerdctlArchive, vSockPort, virtioPort); err != nil {
141+
if err := cidata.GenerateISO9660(inst.Dir, instName, instConfig, udpDNSLocalPort, tcpDNSLocalPort, o.nerdctlArchive, vSockPort, virtioPort); err != nil {
142142
return nil, err
143143
}
144144

145-
sshOpts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
145+
sshOpts, err := sshutil.SSHOpts(inst.Dir, *instConfig.SSH.LoadDotSSHPubKeys, *instConfig.SSH.ForwardAgent, *instConfig.SSH.ForwardX11, *instConfig.SSH.ForwardX11Trusted)
146146
if err != nil {
147147
return nil, err
148148
}
@@ -155,7 +155,7 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
155155

156156
ignoreTCP := false
157157
ignoreUDP := false
158-
for _, rule := range y.PortForwards {
158+
for _, rule := range instConfig.PortForwards {
159159
if rule.Ignore && rule.GuestPortRange[0] == 1 && rule.GuestPortRange[1] == 65535 {
160160
switch rule.Proto {
161161
case limayaml.ProtoTCP:
@@ -173,14 +173,14 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
173173
break
174174
}
175175
}
176-
rules := make([]limayaml.PortForward, 0, 3+len(y.PortForwards))
176+
rules := make([]limayaml.PortForward, 0, 3+len(instConfig.PortForwards))
177177
// Block ports 22 and sshLocalPort on all IPs
178178
for _, port := range []int{sshGuestPort, sshLocalPort} {
179179
rule := limayaml.PortForward{GuestIP: net.IPv4zero, GuestPort: port, Ignore: true}
180180
limayaml.FillPortForwardDefaults(&rule, inst.Dir, inst.Param)
181181
rules = append(rules, rule)
182182
}
183-
rules = append(rules, y.PortForwards...)
183+
rules = append(rules, instConfig.PortForwards...)
184184
// Default forwards for all non-privileged ports from "127.0.0.1" and "::1"
185185
rule := limayaml.PortForward{}
186186
limayaml.FillPortForwardDefaults(&rule, inst.Dir, inst.Param)
@@ -193,14 +193,14 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
193193

194194
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
195195
Instance: inst,
196-
Yaml: y,
196+
InstConfig: instConfig,
197197
SSHLocalPort: sshLocalPort,
198198
VSockPort: vSockPort,
199199
VirtioPort: virtioPort,
200200
})
201201

202202
a := &HostAgent{
203-
y: y,
203+
instConfig: instConfig,
204204
sshLocalPort: sshLocalPort,
205205
udpDNSLocalPort: udpDNSLocalPort,
206206
tcpDNSLocalPort: tcpDNSLocalPort,
@@ -333,16 +333,16 @@ func (a *HostAgent) Run(ctx context.Context) error {
333333
}()
334334
adjustNofileRlimit()
335335

336-
if limayaml.FirstUsernetIndex(a.y) == -1 && *a.y.HostResolver.Enabled {
337-
hosts := a.y.HostResolver.Hosts
336+
if limayaml.FirstUsernetIndex(a.instConfig) == -1 && *a.instConfig.HostResolver.Enabled {
337+
hosts := a.instConfig.HostResolver.Hosts
338338
hosts["host.lima.internal"] = networks.SlirpGateway
339339
hosts[fmt.Sprintf("lima-%s", a.instName)] = networks.SlirpIPAddress
340340
srvOpts := dns.ServerOptions{
341341
UDPPort: a.udpDNSLocalPort,
342342
TCPPort: a.tcpDNSLocalPort,
343343
Address: "127.0.0.1",
344344
HandlerOptions: dns.HandlerOptions{
345-
IPv6: *a.y.HostResolver.IPv6,
345+
IPv6: *a.instConfig.HostResolver.IPv6,
346346
StaticHosts: hosts,
347347
},
348348
}
@@ -359,16 +359,16 @@ func (a *HostAgent) Run(ctx context.Context) error {
359359
}
360360

361361
// WSL instance SSH address isn't known until after VM start
362-
if *a.y.VMType == limayaml.WSL2 {
362+
if *a.instConfig.VMType == limayaml.WSL2 {
363363
sshAddr, err := store.GetSSHAddress(a.instName)
364364
if err != nil {
365365
return err
366366
}
367367
a.instSSHAddress = sshAddr
368368
}
369369

370-
if a.y.Video.Display != nil && *a.y.Video.Display == "vnc" {
371-
vncdisplay, vncoptions, _ := strings.Cut(*a.y.Video.VNC.Display, ",")
370+
if a.instConfig.Video.Display != nil && *a.instConfig.Video.Display == "vnc" {
371+
vncdisplay, vncoptions, _ := strings.Cut(*a.instConfig.Video.VNC.Display, ",")
372372
vnchost, vncnum, err := net.SplitHostPort(vncdisplay)
373373
if err != nil {
374374
return err
@@ -469,7 +469,7 @@ func (a *HostAgent) Info(_ context.Context) (*hostagentapi.Info, error) {
469469
}
470470

471471
func (a *HostAgent) startHostAgentRoutines(ctx context.Context) error {
472-
if *a.y.Plain {
472+
if *a.instConfig.Plain {
473473
logrus.Info("Running in plain mode. Mounts, port forwarding, containerd, etc. will be ignored. Guest agent will not be running.")
474474
}
475475
a.onClose = append(a.onClose, func() error {
@@ -483,7 +483,7 @@ func (a *HostAgent) startHostAgentRoutines(ctx context.Context) error {
483483
if err := a.waitForRequirements("essential", a.essentialRequirements()); err != nil {
484484
errs = append(errs, err)
485485
}
486-
if *a.y.SSH.ForwardAgent {
486+
if *a.instConfig.SSH.ForwardAgent {
487487
faScript := `#!/bin/bash
488488
set -eux -o pipefail
489489
sudo mkdir -p -m 700 /run/host-services
@@ -496,7 +496,7 @@ sudo chown -R "${USER}" /run/host-services`
496496
errs = append(errs, fmt.Errorf("stdout=%q, stderr=%q: %w", stdout, stderr, err))
497497
}
498498
}
499-
if *a.y.MountType == limayaml.REVSSHFS && !*a.y.Plain {
499+
if *a.instConfig.MountType == limayaml.REVSSHFS && !*a.instConfig.Plain {
500500
mounts, err := a.setupMounts()
501501
if err != nil {
502502
errs = append(errs, err)
@@ -511,10 +511,10 @@ sudo chown -R "${USER}" /run/host-services`
511511
return errors.Join(unmountErrs...)
512512
})
513513
}
514-
if len(a.y.AdditionalDisks) > 0 {
514+
if len(a.instConfig.AdditionalDisks) > 0 {
515515
a.onClose = append(a.onClose, func() error {
516516
var unlockErrs []error
517-
for _, d := range a.y.AdditionalDisks {
517+
for _, d := range a.instConfig.AdditionalDisks {
518518
disk, inspectErr := store.InspectDisk(d.Name)
519519
if inspectErr != nil {
520520
unlockErrs = append(unlockErrs, inspectErr)
@@ -528,13 +528,13 @@ sudo chown -R "${USER}" /run/host-services`
528528
return errors.Join(unlockErrs...)
529529
})
530530
}
531-
if !*a.y.Plain {
531+
if !*a.instConfig.Plain {
532532
go a.watchGuestAgentEvents(ctx)
533533
}
534534
if err := a.waitForRequirements("optional", a.optionalRequirements()); err != nil {
535535
errs = append(errs, err)
536536
}
537-
if !*a.y.Plain {
537+
if !*a.instConfig.Plain {
538538
logrus.Info("Waiting for the guest agent to be running")
539539
select {
540540
case <-a.guestAgentAliveCh:
@@ -547,14 +547,14 @@ sudo chown -R "${USER}" /run/host-services`
547547
errs = append(errs, err)
548548
}
549549
// Copy all config files _after_ the requirements are done
550-
for _, rule := range a.y.CopyToHost {
550+
for _, rule := range a.instConfig.CopyToHost {
551551
if err := copyToHost(ctx, a.sshConfig, a.sshLocalPort, rule.HostFile, rule.GuestFile); err != nil {
552552
errs = append(errs, err)
553553
}
554554
}
555555
a.onClose = append(a.onClose, func() error {
556556
var rmErrs []error
557-
for _, rule := range a.y.CopyToHost {
557+
for _, rule := range a.instConfig.CopyToHost {
558558
if rule.DeleteOnStop {
559559
logrus.Infof("Deleting %s", rule.HostFile)
560560
if err := os.RemoveAll(rule.HostFile); err != nil {
@@ -583,9 +583,9 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
583583
// TODO: use vSock (when QEMU for macOS gets support for vSock)
584584

585585
// Setup all socket forwards and defer their teardown
586-
if *a.y.VMType != limayaml.WSL2 {
586+
if *a.instConfig.VMType != limayaml.WSL2 {
587587
logrus.Debugf("Forwarding unix sockets")
588-
for _, rule := range a.y.PortForwards {
588+
for _, rule := range a.instConfig.PortForwards {
589589
if rule.GuestSocket != "" {
590590
local := hostAddress(rule, &guestagentapi.IPPort{})
591591
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
@@ -599,7 +599,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
599599
a.onClose = append(a.onClose, func() error {
600600
logrus.Debugf("Stop forwarding unix sockets")
601601
var errs []error
602-
for _, rule := range a.y.PortForwards {
602+
for _, rule := range a.instConfig.PortForwards {
603603
if rule.GuestSocket != "" {
604604
local := hostAddress(rule, &guestagentapi.IPPort{})
605605
// using ctx.Background() because ctx has already been cancelled
@@ -617,7 +617,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
617617
})
618618

619619
go func() {
620-
if a.y.MountInotify != nil && *a.y.MountInotify {
620+
if a.instConfig.MountInotify != nil && *a.instConfig.MountInotify {
621621
if a.client == nil || !isGuestAgentSocketAccessible(ctx, a.client) {
622622
if a.driver.ForwardGuestAgent() {
623623
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)

pkg/hostagent/inotify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (a *HostAgent) startInotify(ctx context.Context) error {
6767
}
6868

6969
func (a *HostAgent) setupWatchers(events chan notify.EventInfo) error {
70-
for _, m := range a.y.Mounts {
70+
for _, m := range a.instConfig.Mounts {
7171
if !*m.Writable {
7272
continue
7373
}

pkg/hostagent/mount.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (a *HostAgent) setupMounts() ([]*mount, error) {
2020
res []*mount
2121
errs []error
2222
)
23-
for _, f := range a.y.Mounts {
23+
for _, f := range a.instConfig.Mounts {
2424
m, err := a.setupMount(f)
2525
if err != nil {
2626
errs = append(errs, err)

pkg/hostagent/requirements.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Make sure that the YAML field "ssh.localPort" is not used by other processes on
114114
If any private key under ~/.ssh is protected with a passphrase, you need to have ssh-agent to be running.
115115
`,
116116
})
117-
if *a.y.Plain {
117+
if *a.instConfig.Plain {
118118
return req
119119
}
120120
req = append(req,
@@ -134,7 +134,7 @@ it must not be created until the session reset is done.
134134
`,
135135
})
136136

137-
if *a.y.MountType == limayaml.REVSSHFS && len(a.y.Mounts) > 0 {
137+
if *a.instConfig.MountType == limayaml.REVSSHFS && len(a.instConfig.Mounts) > 0 {
138138
req = append(req, requirement{
139139
description: "sshfs binary to be installed",
140140
script: `#!/bin/bash
@@ -167,7 +167,7 @@ fi
167167

168168
func (a *HostAgent) optionalRequirements() []requirement {
169169
req := make([]requirement, 0)
170-
if (*a.y.Containerd.System || *a.y.Containerd.User) && !*a.y.Plain {
170+
if (*a.instConfig.Containerd.System || *a.instConfig.Containerd.User) && !*a.instConfig.Plain {
171171
req = append(req,
172172
requirement{
173173
description: "systemd must be available",
@@ -189,7 +189,7 @@ are set to 'false' in the config file.
189189
description: "containerd binaries to be installed",
190190
script: `#!/bin/bash
191191
set -eux -o pipefail
192-
if ! timeout 30s bash -c "until command -v nerdctl || test -x ` + *a.y.GuestInstallPrefix + `/bin/nerdctl; do sleep 3; done"; then
192+
if ! timeout 30s bash -c "until command -v nerdctl || test -x ` + *a.instConfig.GuestInstallPrefix + `/bin/nerdctl; do sleep 3; done"; then
193193
echo >&2 "nerdctl is not installed yet"
194194
exit 1
195195
fi
@@ -200,7 +200,7 @@ Also see "/var/log/cloud-init-output.log" in the guest.
200200
`,
201201
})
202202
}
203-
for _, probe := range a.y.Probes {
203+
for _, probe := range a.instConfig.Probes {
204204
if probe.Mode == limayaml.ProbeModeReadiness {
205205
req = append(req, requirement{
206206
description: probe.Description,

pkg/instance/create.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import (
1616
"github.com/lima-vm/lima/pkg/version"
1717
)
1818

19-
func Create(ctx context.Context, instName string, yBytes []byte, saveBrokenYAML bool) (*store.Instance, error) {
19+
func Create(ctx context.Context, instName string, instConfig []byte, saveBrokenYAML bool) (*store.Instance, error) {
2020
if instName == "" {
2121
return nil, errors.New("got empty instName")
2222
}
23-
if len(yBytes) == 0 {
24-
return nil, errors.New("got empty yBytes")
23+
if len(instConfig) == 0 {
24+
return nil, errors.New("got empty instConfig")
2525
}
2626

2727
instDir, err := store.InstanceDir(instName)
@@ -40,24 +40,24 @@ func Create(ctx context.Context, instName string, yBytes []byte, saveBrokenYAML
4040
}
4141
// limayaml.Load() needs to pass the store file path to limayaml.FillDefault() to calculate default MAC addresses
4242
filePath := filepath.Join(instDir, filenames.LimaYAML)
43-
y, err := limayaml.Load(yBytes, filePath)
43+
loadedInstConfig, err := limayaml.Load(instConfig, filePath)
4444
if err != nil {
4545
return nil, err
4646
}
47-
if err := limayaml.Validate(y, true); err != nil {
47+
if err := limayaml.Validate(loadedInstConfig, true); err != nil {
4848
if !saveBrokenYAML {
4949
return nil, err
5050
}
5151
rejectedYAML := "lima.REJECTED.yaml"
52-
if writeErr := os.WriteFile(rejectedYAML, yBytes, 0o644); writeErr != nil {
52+
if writeErr := os.WriteFile(rejectedYAML, instConfig, 0o644); writeErr != nil {
5353
return nil, fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w", rejectedYAML, writeErr, err)
5454
}
5555
return nil, fmt.Errorf("the YAML is invalid, saved the buffer as %q: %w", rejectedYAML, err)
5656
}
5757
if err := os.MkdirAll(instDir, 0o700); err != nil {
5858
return nil, err
5959
}
60-
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
60+
if err := os.WriteFile(filePath, instConfig, 0o644); err != nil {
6161
return nil, err
6262
}
6363
if err := os.WriteFile(filepath.Join(instDir, filenames.LimaVersion), []byte(version.Version), 0o444); err != nil {
@@ -70,8 +70,8 @@ func Create(ctx context.Context, instName string, yBytes []byte, saveBrokenYAML
7070
}
7171

7272
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
73-
Instance: inst,
74-
Yaml: y,
73+
Instance: inst,
74+
InstConfig: loadedInstConfig,
7575
})
7676

7777
if err := limaDriver.Register(ctx); err != nil {

0 commit comments

Comments
 (0)