Skip to content

Commit da15262

Browse files
authored
Merge pull request #2661 from jandubois/remove-instconfig
instConfig is redundant with inst.Config
2 parents a550e52 + e176316 commit da15262

File tree

18 files changed

+124
-170
lines changed

18 files changed

+124
-170
lines changed

cmd/limactl/shell.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ func shellAction(cmd *cobra.Command, args []string) error {
7878
if inst.Status == store.StatusStopped {
7979
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
8080
}
81-
y, err := inst.LoadYAML()
82-
if err != nil {
83-
return err
84-
}
8581

8682
// When workDir is explicitly set, the shell MUST have workDir as the cwd, or exit with an error.
8783
//
@@ -95,7 +91,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
9591
if workDir != "" {
9692
changeDirCmd = fmt.Sprintf("cd %s || exit 1", shellescape.Quote(workDir))
9793
// FIXME: check whether y.Mounts contains the home, not just len > 0
98-
} else if len(y.Mounts) > 0 {
94+
} else if len(inst.Config.Mounts) > 0 {
9995
hostCurrentDir, err := os.Getwd()
10096
if err == nil {
10197
changeDirCmd = fmt.Sprintf("cd %s", shellescape.Quote(hostCurrentDir))
@@ -169,7 +165,12 @@ func shellAction(cmd *cobra.Command, args []string) error {
169165
}
170166
}
171167

172-
sshOpts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
168+
sshOpts, err := sshutil.SSHOpts(
169+
inst.Dir,
170+
*inst.Config.SSH.LoadDotSSHPubKeys,
171+
*inst.Config.SSH.ForwardAgent,
172+
*inst.Config.SSH.ForwardX11,
173+
*inst.Config.SSH.ForwardX11Trusted)
173174
if err != nil {
174175
return err
175176
}

cmd/limactl/show-ssh.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
8888
}
8989
logrus.Warnf("`limactl show-ssh` is deprecated. Instead, use `ssh -F %s lima-%s`.",
9090
filepath.Join(inst.Dir, filenames.SSHConfig), inst.Name)
91-
y, err := inst.LoadYAML()
92-
if err != nil {
93-
return err
94-
}
95-
opts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
91+
opts, err := sshutil.SSHOpts(
92+
inst.Dir,
93+
*inst.Config.SSH.LoadDotSSHPubKeys,
94+
*inst.Config.SSH.ForwardAgent,
95+
*inst.Config.SSH.ForwardX11,
96+
*inst.Config.SSH.ForwardX11Trusted)
9697
if err != nil {
9798
return err
9899
}

pkg/driver/driver.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"net"
77

8-
"github.com/lima-vm/lima/pkg/limayaml"
98
"github.com/lima-vm/lima/pkg/store"
109
)
1110

@@ -72,8 +71,7 @@ type Driver interface {
7271
}
7372

7473
type BaseDriver struct {
75-
Instance *store.Instance
76-
InstConfig *limayaml.LimaYAML
74+
Instance *store.Instance
7775

7876
SSHLocalPort int
7977
VSockPort int

pkg/driverutil/instance.go

+1-1
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.InstConfig.VMType
12+
limaDriver := base.Instance.Config.VMType
1313
if *limaDriver == limayaml.VZ {
1414
return vz.New(base)
1515
}

pkg/hostagent/hostagent.go

+18-19
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,17 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
9797
return nil, err
9898
}
9999

100-
instConfig, err := inst.LoadYAML()
100+
// inst.Config is loaded with FillDefault() already, so no need to care about nil pointers.
101+
sshLocalPort, err := determineSSHLocalPort(*inst.Config.SSH.LocalPort, instName)
101102
if err != nil {
102103
return nil, err
103104
}
104-
// instConf is loaded with FillDefault() already, so no need to care about nil pointers.
105-
106-
sshLocalPort, err := determineSSHLocalPort(*instConfig.SSH.LocalPort, instName)
107-
if err != nil {
108-
return nil, err
109-
}
110-
if *instConfig.VMType == limayaml.WSL2 {
105+
if *inst.Config.VMType == limayaml.WSL2 {
111106
sshLocalPort = inst.SSHLocalPort
112107
}
113108

114109
var udpDNSLocalPort, tcpDNSLocalPort int
115-
if *instConfig.HostResolver.Enabled {
110+
if *inst.Config.HostResolver.Enabled {
116111
udpDNSLocalPort, err = findFreeUDPLocalPort()
117112
if err != nil {
118113
return nil, err
@@ -125,24 +120,29 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
125120

126121
vSockPort := 0
127122
virtioPort := ""
128-
if *instConfig.VMType == limayaml.VZ {
123+
if *inst.Config.VMType == limayaml.VZ {
129124
vSockPort = 2222
130-
} else if *instConfig.VMType == limayaml.WSL2 {
125+
} else if *inst.Config.VMType == limayaml.WSL2 {
131126
port, err := getFreeVSockPort()
132127
if err != nil {
133128
logrus.WithError(err).Error("failed to get free VSock port")
134129
}
135130
vSockPort = port
136-
} else if *instConfig.VMType == limayaml.QEMU {
131+
} else if *inst.Config.VMType == limayaml.QEMU {
137132
// virtserialport doesn't seem to work reliably: https://github.com/lima-vm/lima/issues/2064
138133
virtioPort = "" // filenames.VirtioPort
139134
}
140135

141-
if err := cidata.GenerateISO9660(inst.Dir, instName, instConfig, udpDNSLocalPort, tcpDNSLocalPort, o.nerdctlArchive, vSockPort, virtioPort); err != nil {
136+
if err := cidata.GenerateISO9660(inst.Dir, instName, inst.Config, udpDNSLocalPort, tcpDNSLocalPort, o.nerdctlArchive, vSockPort, virtioPort); err != nil {
142137
return nil, err
143138
}
144139

145-
sshOpts, err := sshutil.SSHOpts(inst.Dir, *instConfig.SSH.LoadDotSSHPubKeys, *instConfig.SSH.ForwardAgent, *instConfig.SSH.ForwardX11, *instConfig.SSH.ForwardX11Trusted)
140+
sshOpts, err := sshutil.SSHOpts(
141+
inst.Dir,
142+
*inst.Config.SSH.LoadDotSSHPubKeys,
143+
*inst.Config.SSH.ForwardAgent,
144+
*inst.Config.SSH.ForwardX11,
145+
*inst.Config.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 instConfig.PortForwards {
158+
for _, rule := range inst.Config.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(instConfig.PortForwards))
176+
rules := make([]limayaml.PortForward, 0, 3+len(inst.Config.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, instConfig.PortForwards...)
183+
rules = append(rules, inst.Config.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,13 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
193193

194194
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
195195
Instance: inst,
196-
InstConfig: instConfig,
197196
SSHLocalPort: sshLocalPort,
198197
VSockPort: vSockPort,
199198
VirtioPort: virtioPort,
200199
})
201200

202201
a := &HostAgent{
203-
instConfig: instConfig,
202+
instConfig: inst.Config,
204203
sshLocalPort: sshLocalPort,
205204
udpDNSLocalPort: udpDNSLocalPort,
206205
tcpDNSLocalPort: tcpDNSLocalPort,

pkg/instance/ansible.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ import (
1414
)
1515

1616
func runAnsibleProvision(ctx context.Context, inst *store.Instance) error {
17-
y, err := inst.LoadYAML()
18-
if err != nil {
19-
return err
20-
}
21-
for _, f := range y.Provision {
17+
for _, f := range inst.Config.Provision {
2218
if f.Mode == limayaml.ProvisionModeAnsible {
2319
logrus.Infof("Waiting for ansible playbook %q", f.Playbook)
2420
if err := runAnsiblePlaybook(ctx, inst, f.Playbook); err != nil {

pkg/instance/create.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ func Create(ctx context.Context, instName string, instConfig []byte, saveBrokenY
7070
}
7171

7272
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
73-
Instance: inst,
74-
InstConfig: loadedInstConfig,
73+
Instance: inst,
7574
})
7675

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

pkg/instance/delete.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,8 @@ func Delete(ctx context.Context, inst *store.Instance, force bool) error {
3232
}
3333

3434
func unregister(ctx context.Context, inst *store.Instance) error {
35-
instConfig, err := inst.LoadYAML()
36-
if err != nil {
37-
return err
38-
}
39-
4035
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
41-
Instance: inst,
42-
InstConfig: instConfig,
36+
Instance: inst,
4337
})
4438

4539
return limaDriver.Unregister(ctx)

pkg/instance/start.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,8 @@ type Prepared struct {
7878

7979
// Prepare ensures the disk, the nerdctl archive, etc.
8080
func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
81-
instConfig, err := inst.LoadYAML()
82-
if err != nil {
83-
return nil, err
84-
}
85-
8681
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
87-
Instance: inst,
88-
InstConfig: instConfig,
82+
Instance: inst,
8983
})
9084

9185
if err := limaDriver.Validate(); err != nil {
@@ -98,13 +92,13 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
9892

9993
// Check if the instance has been created (the base disk already exists)
10094
baseDisk := filepath.Join(inst.Dir, filenames.BaseDisk)
101-
_, err = os.Stat(baseDisk)
95+
_, err := os.Stat(baseDisk)
10296
created := err == nil
10397

10498
if err := limaDriver.CreateDisk(ctx); err != nil {
10599
return nil, err
106100
}
107-
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(ctx, instConfig, created)
101+
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(ctx, inst.Config, created)
108102
if err != nil {
109103
return nil, err
110104
}

pkg/networks/usernet/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (c *Client) ConfigureDriver(ctx context.Context, driver *driver.BaseDriver)
3838
if err != nil {
3939
return err
4040
}
41-
hosts := driver.InstConfig.HostResolver.Hosts
41+
hosts := driver.Instance.Config.HostResolver.Hosts
4242
hosts[fmt.Sprintf("lima-%s.internal", driver.Instance.Name)] = ipAddress
4343
err = c.AddDNSHosts(hosts)
4444
return err

pkg/qemu/qemu_driver.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func New(driver *driver.BaseDriver) *LimaQemuDriver {
4343
}
4444

4545
func (l *LimaQemuDriver) Validate() error {
46-
if *l.InstConfig.MountType == limayaml.VIRTIOFS && runtime.GOOS != "linux" {
46+
if *l.Instance.Config.MountType == limayaml.VIRTIOFS && runtime.GOOS != "linux" {
4747
return fmt.Errorf("field `mountType` must be %q or %q for QEMU driver on non-Linux, got %q",
48-
limayaml.REVSSHFS, limayaml.NINEP, *l.InstConfig.MountType)
48+
limayaml.REVSSHFS, limayaml.NINEP, *l.Instance.Config.MountType)
4949
}
5050
return nil
5151
}
@@ -54,7 +54,7 @@ func (l *LimaQemuDriver) CreateDisk(ctx context.Context) error {
5454
qCfg := Config{
5555
Name: l.Instance.Name,
5656
InstanceDir: l.Instance.Dir,
57-
LimaYAML: l.InstConfig,
57+
LimaYAML: l.Instance.Config,
5858
}
5959
return EnsureDisk(ctx, qCfg)
6060
}
@@ -70,7 +70,7 @@ func (l *LimaQemuDriver) Start(ctx context.Context) (chan error, error) {
7070
qCfg := Config{
7171
Name: l.Instance.Name,
7272
InstanceDir: l.Instance.Dir,
73-
LimaYAML: l.InstConfig,
73+
LimaYAML: l.Instance.Config,
7474
SSHLocalPort: l.SSHLocalPort,
7575
}
7676
qExe, qArgs, err := Cmdline(ctx, qCfg)
@@ -79,13 +79,13 @@ func (l *LimaQemuDriver) Start(ctx context.Context) (chan error, error) {
7979
}
8080

8181
var vhostCmds []*exec.Cmd
82-
if *l.InstConfig.MountType == limayaml.VIRTIOFS {
82+
if *l.Instance.Config.MountType == limayaml.VIRTIOFS {
8383
vhostExe, err := FindVirtiofsd(qExe)
8484
if err != nil {
8585
return nil, err
8686
}
8787

88-
for i := range l.InstConfig.Mounts {
88+
for i := range l.Instance.Config.Mounts {
8989
args, err := VirtiofsdCmdline(qCfg, i)
9090
if err != nil {
9191
return nil, err
@@ -189,8 +189,8 @@ func (l *LimaQemuDriver) Start(ctx context.Context) (chan error, error) {
189189
}()
190190
l.vhostCmds = vhostCmds
191191
go func() {
192-
if usernetIndex := limayaml.FirstUsernetIndex(l.InstConfig); usernetIndex != -1 {
193-
client := usernet.NewClientByName(l.InstConfig.Networks[usernetIndex].Lima)
192+
if usernetIndex := limayaml.FirstUsernetIndex(l.Instance.Config); usernetIndex != -1 {
193+
client := usernet.NewClientByName(l.Instance.Config.Networks[usernetIndex].Lima)
194194
err := client.ConfigureDriver(ctx, l.BaseDriver)
195195
if err != nil {
196196
l.qWaitCh <- err
@@ -297,8 +297,8 @@ func (l *LimaQemuDriver) killVhosts() error {
297297

298298
func (l *LimaQemuDriver) shutdownQEMU(ctx context.Context, timeout time.Duration, qCmd *exec.Cmd, qWaitCh <-chan error) error {
299299
logrus.Info("Shutting down QEMU with ACPI")
300-
if usernetIndex := limayaml.FirstUsernetIndex(l.InstConfig); usernetIndex != -1 {
301-
client := usernet.NewClientByName(l.InstConfig.Networks[usernetIndex].Lima)
300+
if usernetIndex := limayaml.FirstUsernetIndex(l.Instance.Config); usernetIndex != -1 {
301+
client := usernet.NewClientByName(l.Instance.Config.Networks[usernetIndex].Lima)
302302
err := client.UnExposeSSH(l.SSHLocalPort)
303303
if err != nil {
304304
logrus.Warnf("Failed to remove SSH binding for port %d", l.SSHLocalPort)
@@ -348,7 +348,7 @@ func (l *LimaQemuDriver) killQEMU(_ context.Context, _ time.Duration, qCmd *exec
348348
} else {
349349
logrus.Info("QEMU has already exited")
350350
}
351-
qemuPIDPath := filepath.Join(l.Instance.Dir, filenames.PIDFile(*l.InstConfig.VMType))
351+
qemuPIDPath := filepath.Join(l.Instance.Dir, filenames.PIDFile(*l.Instance.Config.VMType))
352352
_ = os.RemoveAll(qemuPIDPath)
353353
_ = l.removeVNCFiles()
354354
return errors.Join(qWaitErr, l.killVhosts())
@@ -366,7 +366,7 @@ func (l *LimaQemuDriver) DeleteSnapshot(_ context.Context, tag string) error {
366366
qCfg := Config{
367367
Name: l.Instance.Name,
368368
InstanceDir: l.Instance.Dir,
369-
LimaYAML: l.InstConfig,
369+
LimaYAML: l.Instance.Config,
370370
}
371371
return Del(qCfg, l.Instance.Status == store.StatusRunning, tag)
372372
}
@@ -375,7 +375,7 @@ func (l *LimaQemuDriver) CreateSnapshot(_ context.Context, tag string) error {
375375
qCfg := Config{
376376
Name: l.Instance.Name,
377377
InstanceDir: l.Instance.Dir,
378-
LimaYAML: l.InstConfig,
378+
LimaYAML: l.Instance.Config,
379379
}
380380
return Save(qCfg, l.Instance.Status == store.StatusRunning, tag)
381381
}
@@ -384,7 +384,7 @@ func (l *LimaQemuDriver) ApplySnapshot(_ context.Context, tag string) error {
384384
qCfg := Config{
385385
Name: l.Instance.Name,
386386
InstanceDir: l.Instance.Dir,
387-
LimaYAML: l.InstConfig,
387+
LimaYAML: l.Instance.Config,
388388
}
389389
return Load(qCfg, l.Instance.Status == store.StatusRunning, tag)
390390
}
@@ -393,7 +393,7 @@ func (l *LimaQemuDriver) ListSnapshots(_ context.Context) (string, error) {
393393
qCfg := Config{
394394
Name: l.Instance.Name,
395395
InstanceDir: l.Instance.Dir,
396-
LimaYAML: l.InstConfig,
396+
LimaYAML: l.Instance.Config,
397397
}
398398
return List(qCfg, l.Instance.Status == store.StatusRunning)
399399
}

0 commit comments

Comments
 (0)