Skip to content

Commit

Permalink
Switch to systemd networkd for all interfaces (#27)
Browse files Browse the repository at this point in the history
* started to move to move interface configuration of firewalls from /etc/network/interfaces and ifupdown2 to systemd-networkd

* adapted tests for systemd-networkd

* reenable go cache for tests

* use /32 in network files, vxlan config for lan0/lan1 needs real netdev names not VNIs, startup VRF interfaces by defining a network file

* remove test data validation for ifaces files because systemd-networkd does not have such things

* re-add machine.yaml comments

* address review notes from Stefan and Tobias
  • Loading branch information
mwindower authored Jul 29, 2020
1 parent d82521c commit 37781f6
Show file tree
Hide file tree
Showing 56 changed files with 554 additions and 472 deletions.
2 changes: 0 additions & 2 deletions Dockerfile.validate
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ FROM metalstack/frr:${FRR_VERSION}-${OS_NAME}-${OS_VERSION} AS frr-artifacts
FROM ${OS_NAME}:${OS_VERSION}

ENV FRR_FILES="frr.conf.firewall frr.conf.machine" \
INTERFACES_FILES="interfaces.firewall" \
TESTDATA_DIR="./internal/netconf/testdata"

WORKDIR /tmp
Expand All @@ -17,7 +16,6 @@ RUN apt-get update --quiet \
--yes \
--no-install-recommends \
--quiet \
ifupdown2 \
./frr_*.deb \
./frr-pythontools_*.deb \
./libyang*.deb
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ go 1.14

require (
github.com/coreos/go-systemd/v22 v22.1.0
github.com/google/go-cmp v0.5.1
github.com/metal-stack/v v1.0.2
github.com/pelletier/go-toml v1.8.0 // indirect
github.com/rakyll/statik v0.1.7
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/afero v1.3.2 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.7.0
github.com/stretchr/testify v1.6.1
go.uber.org/zap v1.15.0
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
)
16 changes: 12 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -116,6 +119,7 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -152,6 +156,7 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down Expand Up @@ -182,8 +187,8 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/afero v1.3.2 h1:GDarE4TJQI52kYSbSAmLiId1Elfj+xgSDqrUZxFhxlU=
github.com/spf13/afero v1.3.2/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
Expand Down Expand Up @@ -235,6 +240,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90Pveol
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -322,6 +328,8 @@ golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc h1:NCy3Ohtk6Iny5V/reW2Ktypo4zIpWBdRJ1uFMjBxdg8=
golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
Expand Down Expand Up @@ -362,8 +370,8 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
43 changes: 13 additions & 30 deletions internal/netconf/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ const (
Machine
// SystemdUnitPath is the path where systemd units will be generated.
SystemdUnitPath = "/etc/systemd/system/"
// SystemdNetworkPath is the path where systemd-networkd expects its configuration files.
SystemdNetworkPath = "/etc/systemd/network"
)

var (
// systemdNetworkPath is the path where systemd-networkd expects its configuration files.
systemdNetworkPath = "/etc/systemd/network"
// tmpPath is the path where temporary files are stored for validation before they are moved to their intended place.
tmpPath = "/etc/metal/networker/"
)

type (
Expand Down Expand Up @@ -190,17 +195,11 @@ func (configurator FirewallConfigurator) getUnits() []unitConfiguration {
}

func applyCommonConfiguration(kind BareMetalType, kb KnowledgeBase) {
src := mustTmpFile("interfaces_")
applier := NewIfacesConfigApplier(kind, kb, src)

if kind == Machine {
applyAndCleanUp(applier, TplMachineIfaces, src, SystemdNetworkPath+"/0-lo.network", FileModeSystemd)
} else {
applyAndCleanUp(applier, TplFirewallIfaces, src, "/etc/network/interfaces", FileModeDefault)
}
a := NewIfacesApplier(kind, kb)
a.Apply()

src = mustTmpFile("hosts_")
applier = NewHostsApplier(kb, src)
src := mustTmpFile("hosts_")
applier := NewHostsApplier(kb, src)
applyAndCleanUp(applier, TplHosts, src, "/etc/hosts", FileModeDefault)

src = mustTmpFile("hostname_")
Expand All @@ -216,22 +215,6 @@ func applyCommonConfiguration(kind BareMetalType, kb KnowledgeBase) {
}

applyAndCleanUp(applier, tpl, src, "/etc/frr/frr.conf", FileModeDefault)

offset := 1

for i, nic := range kb.Nics {
prefix := fmt.Sprintf("lan%d_link_", i)
src = mustTmpFile(prefix)
applier = NewSystemdLinkApplier(kind, kb.Machineuuid, i, nic, src)
dest := fmt.Sprintf("%s/%d0-lan%d.link", SystemdNetworkPath, i+offset, i)
applyAndCleanUp(applier, tplSystemdLink, src, dest, FileModeSystemd)

prefix = fmt.Sprintf("lan%d_network_", i)
src = mustTmpFile(prefix)
applier = NewSystemdNetworkApplier(kb.Machineuuid, i, src)
dest = fmt.Sprintf("%s/%d0-lan%d.network", SystemdNetworkPath, i+offset, i)
applyAndCleanUp(applier, tplSystemdNetwork, src, dest, FileModeSystemd)
}
}

func applyAndCleanUp(applier net.Applier, tpl, src, dest string, mode os.FileMode) {
Expand Down Expand Up @@ -259,7 +242,7 @@ func mustEnableUnit(unit string) {
}

func mustApply(applier net.Applier, tpl, src, dest string) {
t := template.Must(template.New(TplFirewallIfaces).Parse(tpl))
t := template.Must(template.New(src).Parse(tpl))
err := applier.Apply(*t, src, dest, false)

if err != nil {
Expand All @@ -268,7 +251,7 @@ func mustApply(applier net.Applier, tpl, src, dest string) {
}

func mustTmpFile(prefix string) string {
f, err := ioutil.TempFile("/etc/metal/networker/", prefix)
f, err := ioutil.TempFile(tmpPath, prefix)
if err != nil {
log.Panic(err)
}
Expand Down
29 changes: 24 additions & 5 deletions internal/netconf/frr_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
package netconf

import (
"bytes"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCompileFrrConf(t *testing.T) {
tests := []FileRenderInfo{
func TestFrrConfigApplier(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string
expectedOutput string
configuratorType BareMetalType
tpl string
}{
{
input: "testdata/firewall.yaml",
expectedOutput: "testdata/frr.conf.firewall",
configuratorType: Firewall,
tpl: TplFirewallFRR,
newApplierFunc: NewFrrConfigApplier,
},
{
input: "testdata/machine.yaml",
expectedOutput: "testdata/frr.conf.machine",
configuratorType: Machine,
tpl: TplMachineFRR,
newApplierFunc: NewFrrConfigApplier,
},
}
renderFilesAndVerifyExpectations(t, tests)
for _, t := range tests {
expected, err := ioutil.ReadFile(t.expectedOutput)
assert.NoError(err)

kb := NewKnowledgeBase(t.input)
assert.NoError(err)
a := NewFrrConfigApplier(t.configuratorType, kb, "")
b := bytes.Buffer{}

tpl := mustParseTpl(t.tpl)
err = a.Render(&b, *tpl)
assert.NoError(err)
assert.Equal(string(expected), b.String())
}
}

func TestFRRValidator_Validate(t *testing.T) {
Expand Down
Loading

0 comments on commit 37781f6

Please sign in to comment.