Skip to content

Commit f55da76

Browse files
author
Tycho Andersen
committed
lxdbr0 intialization: handle cases where config has been edited
Signed-off-by: Tycho Andersen <[email protected]>
1 parent af7d453 commit f55da76

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

container/lxd/initialisation.go

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -185,35 +185,45 @@ func detectSubnet(ipAddrOutput string) (string, error) {
185185
func editLXDBridgeFile(input string, subnet string) string {
186186
buffer := bytes.Buffer{}
187187

188+
newValues := map[string]string{
189+
"USE_LXD_BRIDGE": "true",
190+
"EXISTING_BRIDGE": "",
191+
"LXD_BRIDGE": "lxdbr0",
192+
"LXD_IPV4_ADDR": fmt.Sprintf("10.0.%s.1", subnet),
193+
"LXD_IPV4_NETMASK": "255.255.255.0",
194+
"LXD_IPV4_NETWORK": fmt.Sprintf("10.0.%s.1/24", subnet),
195+
"LXD_IPV4_DHCP_RANGE": fmt.Sprintf("10.0.%s.2,10.0.%s.254", subnet, subnet),
196+
"LXD_IPV4_DHCP_MAX": "253",
197+
"LXD_IPV4_NAT": "true",
198+
"LXD_IPV6_PROXY": "false",
199+
}
200+
found := map[string]bool{}
201+
188202
for _, line := range strings.Split(input, "\n") {
189203
out := line
190204

191-
if strings.HasPrefix(line, "USE_LXD_BRIDGE=") {
192-
out = `USE_LXD_BRIDGE="true"`
193-
} else if strings.HasPrefix(line, "EXISTING_BRIDGE=") {
194-
out = `EXISTING_BRIDGE=""`
195-
} else if strings.HasPrefix(line, "LXD_BRIDGE=") {
196-
out = `LXD_BRIDGE="lxdbr0"`
197-
} else if strings.HasPrefix(line, "LXD_IPV4_ADDR=") {
198-
out = fmt.Sprintf(`LXD_IPV4_ADDR="10.0.%s.1"`, subnet)
199-
} else if strings.HasPrefix(line, "LXD_IPV4_NETMASK=") {
200-
out = `LXD_IPV4_NETMASK="255.255.255.0"`
201-
} else if strings.HasPrefix(line, "LXD_IPV4_NETWORK=") {
202-
out = fmt.Sprintf(`LXD_IPV4_NETWORK="10.0.%s.1/24"`, subnet)
203-
} else if strings.HasPrefix(line, "LXD_IPV4_DHCP_RANGE=") {
204-
out = fmt.Sprintf(`LXD_IPV4_DHCP_RANGE="10.0.%s.2,10.0.%s.254"`, subnet, subnet)
205-
} else if strings.HasPrefix(line, "LXD_IPV4_DHCP_MAX=") {
206-
out = `LXD_IPV4_DHCP_MAX="253"`
207-
} else if strings.HasPrefix(line, "LXD_IPV4_NAT=") {
208-
out = `LXD_IPV4_NAT="true"`
209-
} else if strings.HasPrefix(line, "LXD_IPV6_PROXY=") {
210-
out = `LXD_IPV6_PROXY="false"`
205+
for prefix, value := range newValues {
206+
if strings.HasPrefix(line, prefix+"=") {
207+
out = fmt.Sprintf(`%s="%s"`, prefix, value)
208+
found[prefix] = true
209+
break
210+
}
211211
}
212212

213213
buffer.WriteString(out)
214214
buffer.WriteString("\n")
215215
}
216216

217+
for prefix, value := range newValues {
218+
if !found[prefix] {
219+
buffer.WriteString(prefix)
220+
buffer.WriteString("=")
221+
buffer.WriteString(value)
222+
buffer.WriteString("\n")
223+
found[prefix] = true // not necessary but keeps "found" logically consistent
224+
}
225+
}
226+
217227
return buffer.String()
218228
}
219229

0 commit comments

Comments
 (0)