Skip to content

Commit 25baa9d

Browse files
authored
Merge pull request #2868 from jandubois/metric
Add interface metrics to network settings
2 parents adc5a71 + 651bbce commit 25baa9d

File tree

7 files changed

+19
-10
lines changed

7 files changed

+19
-10
lines changed

pkg/cidata/cidata.TEMPLATE.d/network-config

+1-6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ ethernets:
77
dhcp4: true
88
set-name: {{$nw.Interface}}
99
dhcp4-overrides:
10-
{{- if (eq $nw.Interface $.SlirpNICName) }}
11-
route-metric: 200
12-
{{- else }}
13-
route-metric: 100
14-
{{- end }}
10+
route-metric: {{$nw.Metric}}
1511
{{- if and (eq $nw.Interface $.SlirpNICName) (gt (len $.DNSAddresses) 0) }}
1612
nameservers:
1713
addresses:
@@ -20,4 +16,3 @@ ethernets:
2016
{{- end }}
2117
{{- end }}
2218
{{- end }}
23-

pkg/cidata/cidata.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,12 @@ func templateArgs(bootScripts bool, instDir, name string, instConfig *limayaml.L
256256
})
257257
}
258258

259-
args.Networks = append(args.Networks, Network{MACAddress: limayaml.MACAddress(instDir), Interface: networks.SlirpNICName})
259+
args.Networks = append(args.Networks, Network{MACAddress: limayaml.MACAddress(instDir), Interface: networks.SlirpNICName, Metric: 200})
260260
for i, nw := range instConfig.Networks {
261261
if i == firstUsernetIndex {
262262
continue
263263
}
264-
args.Networks = append(args.Networks, Network{MACAddress: nw.MACAddress, Interface: nw.Interface})
264+
args.Networks = append(args.Networks, Network{MACAddress: nw.MACAddress, Interface: nw.Interface, Metric: *nw.Metric})
265265
}
266266

267267
args.Env, err = setupEnv(instConfig.Env, *instConfig.PropagateProxyEnv, args.SlirpGateway)

pkg/cidata/template.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Containerd struct {
3535
type Network struct {
3636
MACAddress string
3737
Interface string
38+
Metric uint32
3839
}
3940
type Mount struct {
4041
Tag string

pkg/limayaml/defaults.go

+6
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
609609
if nw.MACAddress != "" {
610610
networks[i].MACAddress = nw.MACAddress
611611
}
612+
if nw.Metric != nil {
613+
networks[i].Metric = nw.Metric
614+
}
612615
} else {
613616
// unnamed network definitions are not combined/overwritten
614617
if nw.Interface != "" {
@@ -627,6 +630,9 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
627630
if nw.Interface == "" {
628631
nw.Interface = "lima" + strconv.Itoa(i)
629632
}
633+
if nw.Metric == nil {
634+
nw.Metric = ptr.Of(uint32(100))
635+
}
630636
}
631637

632638
y.MountTypesUnsupported = append(append(o.MountTypesUnsupported, y.MountTypesUnsupported...), d.MountTypesUnsupported...)

pkg/limayaml/defaults_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ func TestFillDefault(t *testing.T) {
255255
expect.Networks = slices.Clone(y.Networks)
256256
expect.Networks[0].MACAddress = MACAddress(fmt.Sprintf("%s#%d", filePath, 0))
257257
expect.Networks[0].Interface = "lima0"
258+
expect.Networks[0].Metric = ptr.Of(uint32(100))
258259

259260
expect.DNS = slices.Clone(y.DNS)
260261
expect.PortForwards = []PortForward{
@@ -401,6 +402,7 @@ func TestFillDefault(t *testing.T) {
401402
{
402403
MACAddress: "11:22:33:44:55:66",
403404
Interface: "def0",
405+
Metric: ptr.Of(uint32(50)),
404406
},
405407
},
406408
DNS: []net.IP{
@@ -622,6 +624,7 @@ func TestFillDefault(t *testing.T) {
622624
Lima: "shared",
623625
MACAddress: "10:20:30:40:50:60",
624626
Interface: "def1",
627+
Metric: ptr.Of(uint32(25)),
625628
},
626629
{
627630
Lima: "bridged",

pkg/limayaml/limayaml.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,9 @@ type Network struct {
274274
// VZNAT uses VZNATNetworkDeviceAttachment. Needs VZ. No root privilege is required.
275275
VZNAT *bool `yaml:"vzNAT,omitempty" json:"vzNAT,omitempty"`
276276

277-
MACAddress string `yaml:"macAddress,omitempty" json:"macAddress,omitempty"`
278-
Interface string `yaml:"interface,omitempty" json:"interface,omitempty"`
277+
MACAddress string `yaml:"macAddress,omitempty" json:"macAddress,omitempty"`
278+
Interface string `yaml:"interface,omitempty" json:"interface,omitempty"`
279+
Metric *uint32 `yaml:"metric,omitempty" json:"metric,omitempty"`
279280
}
280281

281282
type HostResolver struct {

templates/default.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ networks:
384384
# macAddress: ""
385385
# # Interface name, defaults to "lima0", "lima1", etc.
386386
# interface: ""
387+
# # Interface metric, lowest metric becomes the preferred route.
388+
# # Defaults to 100. Builtin SLIRP network uses 200.
389+
# metric: 100
387390
#
388391
# Lima can also connect to "unmanaged" networks addressed by "socket". This
389392
# means that the daemons will not be controlled by Lima, but must be started

0 commit comments

Comments
 (0)