Skip to content

Commit

Permalink
pkg: improve field and variable names
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Redko <oleksandr.red+github@gmail.com>
  • Loading branch information
alexandear committed Sep 19, 2024
1 parent b5b5600 commit 41102b6
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 153 deletions.
4 changes: 2 additions & 2 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ type Driver interface {
}

type BaseDriver struct {
Instance *store.Instance
Yaml *limayaml.LimaYAML
Instance *store.Instance
InstConfig *limayaml.LimaYAML

SSHLocalPort int
VSockPort int
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverutil/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func CreateTargetDriverInstance(base *driver.BaseDriver) driver.Driver {
limaDriver := base.Yaml.VMType
limaDriver := base.InstConfig.VMType
if *limaDriver == limayaml.VZ {
return vz.New(base)
}
Expand Down
68 changes: 34 additions & 34 deletions pkg/hostagent/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
)

type HostAgent struct {
y *limayaml.LimaYAML
instConfig *limayaml.LimaYAML
sshLocalPort int
udpDNSLocalPort int
tcpDNSLocalPort int
Expand Down Expand Up @@ -97,22 +97,22 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
return nil, err
}

y, err := inst.LoadYAML()
instConfig, err := inst.LoadYAML()
if err != nil {
return nil, err
}
// y is loaded with FillDefault() already, so no need to care about nil pointers.

sshLocalPort, err := determineSSHLocalPort(y, instName)
sshLocalPort, err := determineSSHLocalPort(instConfig, instName)
if err != nil {
return nil, err
}
if *y.VMType == limayaml.WSL2 {
if *instConfig.VMType == limayaml.WSL2 {
sshLocalPort = inst.SSHLocalPort
}

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

vSockPort := 0
virtioPort := ""
if *y.VMType == limayaml.VZ {
if *instConfig.VMType == limayaml.VZ {
vSockPort = 2222
} else if *y.VMType == limayaml.WSL2 {
} else if *instConfig.VMType == limayaml.WSL2 {
port, err := getFreeVSockPort()
if err != nil {
logrus.WithError(err).Error("failed to get free VSock port")
}
vSockPort = port
} else if *y.VMType == limayaml.QEMU {
} else if *instConfig.VMType == limayaml.QEMU {
// virtserialport doesn't seem to work reliably: https://github.com/lima-vm/lima/issues/2064
virtioPort = "" // filenames.VirtioPort
}

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

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

ignoreTCP := false
ignoreUDP := false
for _, rule := range y.PortForwards {
for _, rule := range instConfig.PortForwards {
if rule.Ignore && rule.GuestPortRange[0] == 1 && rule.GuestPortRange[1] == 65535 {
switch rule.Proto {
case limayaml.TCP:
Expand All @@ -169,14 +169,14 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
break
}
}
rules := make([]limayaml.PortForward, 0, 3+len(y.PortForwards))
rules := make([]limayaml.PortForward, 0, 3+len(instConfig.PortForwards))
// Block ports 22 and sshLocalPort on all IPs
for _, port := range []int{sshGuestPort, sshLocalPort} {
rule := limayaml.PortForward{GuestIP: net.IPv4zero, GuestPort: port, Ignore: true}
limayaml.FillPortForwardDefaults(&rule, inst.Dir, inst.Param)
rules = append(rules, rule)
}
rules = append(rules, y.PortForwards...)
rules = append(rules, instConfig.PortForwards...)
// Default forwards for all non-privileged ports from "127.0.0.1" and "::1"
rule := limayaml.PortForward{}
limayaml.FillPortForwardDefaults(&rule, inst.Dir, inst.Param)
Expand All @@ -189,14 +189,14 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt

limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
Instance: inst,
Yaml: y,
InstConfig: instConfig,
SSHLocalPort: sshLocalPort,
VSockPort: vSockPort,
VirtioPort: virtioPort,
})

a := &HostAgent{
y: y,
instConfig: instConfig,
sshLocalPort: sshLocalPort,
udpDNSLocalPort: udpDNSLocalPort,
tcpDNSLocalPort: tcpDNSLocalPort,
Expand Down Expand Up @@ -329,16 +329,16 @@ func (a *HostAgent) Run(ctx context.Context) error {
}()
adjustNofileRlimit()

if limayaml.FirstUsernetIndex(a.y) == -1 && *a.y.HostResolver.Enabled {
hosts := a.y.HostResolver.Hosts
if limayaml.FirstUsernetIndex(a.instConfig) == -1 && *a.instConfig.HostResolver.Enabled {
hosts := a.instConfig.HostResolver.Hosts
hosts["host.lima.internal"] = networks.SlirpGateway
hosts[fmt.Sprintf("lima-%s", a.instName)] = networks.SlirpIPAddress
srvOpts := dns.ServerOptions{
UDPPort: a.udpDNSLocalPort,
TCPPort: a.tcpDNSLocalPort,
Address: "127.0.0.1",
HandlerOptions: dns.HandlerOptions{
IPv6: *a.y.HostResolver.IPv6,
IPv6: *a.instConfig.HostResolver.IPv6,
StaticHosts: hosts,
},
}
Expand All @@ -355,16 +355,16 @@ func (a *HostAgent) Run(ctx context.Context) error {
}

// WSL instance SSH address isn't known until after VM start
if *a.y.VMType == limayaml.WSL2 {
if *a.instConfig.VMType == limayaml.WSL2 {
sshAddr, err := store.GetSSHAddress(a.instName)
if err != nil {
return err
}
a.instSSHAddress = sshAddr
}

if a.y.Video.Display != nil && *a.y.Video.Display == "vnc" {
vncdisplay, vncoptions, _ := strings.Cut(*a.y.Video.VNC.Display, ",")
if a.instConfig.Video.Display != nil && *a.instConfig.Video.Display == "vnc" {
vncdisplay, vncoptions, _ := strings.Cut(*a.instConfig.Video.VNC.Display, ",")
vnchost, vncnum, err := net.SplitHostPort(vncdisplay)
if err != nil {
return err
Expand Down Expand Up @@ -465,7 +465,7 @@ func (a *HostAgent) Info(_ context.Context) (*hostagentapi.Info, error) {
}

func (a *HostAgent) startHostAgentRoutines(ctx context.Context) error {
if *a.y.Plain {
if *a.instConfig.Plain {
logrus.Info("Running in plain mode. Mounts, port forwarding, containerd, etc. will be ignored. Guest agent will not be running.")
}
a.onClose = append(a.onClose, func() error {
Expand All @@ -479,7 +479,7 @@ func (a *HostAgent) startHostAgentRoutines(ctx context.Context) error {
if err := a.waitForRequirements("essential", a.essentialRequirements()); err != nil {
errs = append(errs, err)
}
if *a.y.SSH.ForwardAgent {
if *a.instConfig.SSH.ForwardAgent {
faScript := `#!/bin/bash
set -eux -o pipefail
sudo mkdir -p -m 700 /run/host-services
Expand All @@ -492,7 +492,7 @@ sudo chown -R "${USER}" /run/host-services`
errs = append(errs, fmt.Errorf("stdout=%q, stderr=%q: %w", stdout, stderr, err))
}
}
if *a.y.MountType == limayaml.REVSSHFS && !*a.y.Plain {
if *a.instConfig.MountType == limayaml.REVSSHFS && !*a.instConfig.Plain {
mounts, err := a.setupMounts()
if err != nil {
errs = append(errs, err)
Expand All @@ -507,10 +507,10 @@ sudo chown -R "${USER}" /run/host-services`
return errors.Join(unmountErrs...)
})
}
if len(a.y.AdditionalDisks) > 0 {
if len(a.instConfig.AdditionalDisks) > 0 {
a.onClose = append(a.onClose, func() error {
var unlockErrs []error
for _, d := range a.y.AdditionalDisks {
for _, d := range a.instConfig.AdditionalDisks {
disk, inspectErr := store.InspectDisk(d.Name)
if inspectErr != nil {
unlockErrs = append(unlockErrs, inspectErr)
Expand All @@ -524,13 +524,13 @@ sudo chown -R "${USER}" /run/host-services`
return errors.Join(unlockErrs...)
})
}
if !*a.y.Plain {
if !*a.instConfig.Plain {
go a.watchGuestAgentEvents(ctx)
}
if err := a.waitForRequirements("optional", a.optionalRequirements()); err != nil {
errs = append(errs, err)
}
if !*a.y.Plain {
if !*a.instConfig.Plain {
logrus.Info("Waiting for the guest agent to be running")
select {
case <-a.guestAgentAliveCh:
Expand All @@ -543,14 +543,14 @@ sudo chown -R "${USER}" /run/host-services`
errs = append(errs, err)
}
// Copy all config files _after_ the requirements are done
for _, rule := range a.y.CopyToHost {
for _, rule := range a.instConfig.CopyToHost {
if err := copyToHost(ctx, a.sshConfig, a.sshLocalPort, rule.HostFile, rule.GuestFile); err != nil {
errs = append(errs, err)
}
}
a.onClose = append(a.onClose, func() error {
var rmErrs []error
for _, rule := range a.y.CopyToHost {
for _, rule := range a.instConfig.CopyToHost {
if rule.DeleteOnStop {
logrus.Infof("Deleting %s", rule.HostFile)
if err := os.RemoveAll(rule.HostFile); err != nil {
Expand Down Expand Up @@ -579,9 +579,9 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
// TODO: use vSock (when QEMU for macOS gets support for vSock)

// Setup all socket forwards and defer their teardown
if *a.y.VMType != limayaml.WSL2 {
if *a.instConfig.VMType != limayaml.WSL2 {
logrus.Debugf("Forwarding unix sockets")
for _, rule := range a.y.PortForwards {
for _, rule := range a.instConfig.PortForwards {
if rule.GuestSocket != "" {
local := hostAddress(rule, &guestagentapi.IPPort{})
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
Expand All @@ -595,7 +595,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
a.onClose = append(a.onClose, func() error {
logrus.Debugf("Stop forwarding unix sockets")
var errs []error
for _, rule := range a.y.PortForwards {
for _, rule := range a.instConfig.PortForwards {
if rule.GuestSocket != "" {
local := hostAddress(rule, &guestagentapi.IPPort{})
// using ctx.Background() because ctx has already been cancelled
Expand All @@ -613,7 +613,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
})

go func() {
if a.y.MountInotify != nil && *a.y.MountInotify {
if a.instConfig.MountInotify != nil && *a.instConfig.MountInotify {
if a.client == nil || !isGuestAgentSocketAccessible(ctx, a.client) {
if a.driver.ForwardGuestAgent() {
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
Expand Down
2 changes: 1 addition & 1 deletion pkg/hostagent/inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (a *HostAgent) startInotify(ctx context.Context) error {
}

func (a *HostAgent) setupWatchers(events chan notify.EventInfo) error {
for _, m := range a.y.Mounts {
for _, m := range a.instConfig.Mounts {
if !*m.Writable {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/hostagent/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (a *HostAgent) setupMounts() ([]*mount, error) {
res []*mount
errs []error
)
for _, f := range a.y.Mounts {
for _, f := range a.instConfig.Mounts {
m, err := a.setupMount(f)
if err != nil {
errs = append(errs, err)
Expand Down
10 changes: 5 additions & 5 deletions pkg/hostagent/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Make sure that the YAML field "ssh.localPort" is not used by other processes on
If any private key under ~/.ssh is protected with a passphrase, you need to have ssh-agent to be running.
`,
})
if *a.y.Plain {
if *a.instConfig.Plain {
return req
}
req = append(req,
Expand All @@ -134,7 +134,7 @@ it must not be created until the session reset is done.
`,
})

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

func (a *HostAgent) optionalRequirements() []requirement {
req := make([]requirement, 0)
if (*a.y.Containerd.System || *a.y.Containerd.User) && !*a.y.Plain {
if (*a.instConfig.Containerd.System || *a.instConfig.Containerd.User) && !*a.instConfig.Plain {
req = append(req,
requirement{
description: "systemd must be available",
Expand All @@ -189,7 +189,7 @@ are set to 'false' in the config file.
description: "containerd binaries to be installed",
script: `#!/bin/bash
set -eux -o pipefail
if ! timeout 30s bash -c "until command -v nerdctl || test -x ` + *a.y.GuestInstallPrefix + `/bin/nerdctl; do sleep 3; done"; then
if ! timeout 30s bash -c "until command -v nerdctl || test -x ` + *a.instConfig.GuestInstallPrefix + `/bin/nerdctl; do sleep 3; done"; then
echo >&2 "nerdctl is not installed yet"
exit 1
fi
Expand All @@ -200,7 +200,7 @@ Also see "/var/log/cloud-init-output.log" in the guest.
`,
})
}
for _, probe := range a.y.Probes {
for _, probe := range a.instConfig.Probes {
if probe.Mode == limayaml.ProbeModeReadiness {
req = append(req, requirement{
description: probe.Description,
Expand Down
18 changes: 9 additions & 9 deletions pkg/instance/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
"github.com/lima-vm/lima/pkg/version"
)

func Create(ctx context.Context, instName string, yBytes []byte, saveBrokenYAML bool) (*store.Instance, error) {
func Create(ctx context.Context, instName string, instConfig []byte, saveBrokenYAML bool) (*store.Instance, error) {
if instName == "" {
return nil, errors.New("got empty instName")
}
if len(yBytes) == 0 {
return nil, errors.New("got empty yBytes")
if len(instConfig) == 0 {
return nil, errors.New("got empty instConfig")
}

instDir, err := store.InstanceDir(instName)
Expand All @@ -40,24 +40,24 @@ func Create(ctx context.Context, instName string, yBytes []byte, saveBrokenYAML
}
// limayaml.Load() needs to pass the store file path to limayaml.FillDefault() to calculate default MAC addresses
filePath := filepath.Join(instDir, filenames.LimaYAML)
y, err := limayaml.Load(yBytes, filePath)
loadedInstConfig, err := limayaml.Load(instConfig, filePath)
if err != nil {
return nil, err
}
if err := limayaml.Validate(y, true); err != nil {
if err := limayaml.Validate(loadedInstConfig, true); err != nil {
if !saveBrokenYAML {
return nil, err
}
rejectedYAML := "lima.REJECTED.yaml"
if writeErr := os.WriteFile(rejectedYAML, yBytes, 0o644); writeErr != nil {
if writeErr := os.WriteFile(rejectedYAML, instConfig, 0o644); writeErr != nil {
return nil, fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w", rejectedYAML, writeErr, err)
}
return nil, fmt.Errorf("the YAML is invalid, saved the buffer as %q: %w", rejectedYAML, err)
}
if err := os.MkdirAll(instDir, 0o700); err != nil {
return nil, err
}
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
if err := os.WriteFile(filePath, instConfig, 0o644); err != nil {
return nil, err
}
if err := os.WriteFile(filepath.Join(instDir, filenames.LimaVersion), []byte(version.Version), 0o444); err != nil {
Expand All @@ -70,8 +70,8 @@ func Create(ctx context.Context, instName string, yBytes []byte, saveBrokenYAML
}

limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
Instance: inst,
Yaml: y,
Instance: inst,
InstConfig: loadedInstConfig,
})

if err := limaDriver.Register(ctx); err != nil {
Expand Down
Loading

0 comments on commit 41102b6

Please sign in to comment.