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

Lines changed: 7 additions & 6 deletions
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

Lines changed: 6 additions & 5 deletions
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

Lines changed: 1 addition & 3 deletions
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

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

pkg/hostagent/hostagent.go

Lines changed: 18 additions & 19 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 7 deletions
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

Lines changed: 3 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)