Skip to content

Commit c77566f

Browse files
committed
Include metal-networker
1 parent 44eae80 commit c77566f

File tree

143 files changed

+10533
-28
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+10533
-28
lines changed

Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LINKMODE := -extldflags=-static \
1111
-X 'github.com/metal-stack/v.GitSHA1=$(SHA)' \
1212
-X 'github.com/metal-stack/v.BuildDate=$(BUILDDATE)'
1313

14-
all: test binary
14+
all: test validate binary
1515

1616
.PHONY: binary
1717
binary:
@@ -26,4 +26,11 @@ binary:
2626

2727
.PHONY: test
2828
test:
29-
GO_ENV=testing go test -race -cover ./...
29+
GO_ENV=testing go test -race -cover ./...
30+
31+
32+
.PHONY: validate
33+
validate:
34+
cd pkg/network
35+
./validate.sh
36+
cd -

go.mod

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ module github.com/metal-stack/os-installer
33
go 1.26
44

55
require (
6+
github.com/Masterminds/semver/v3 v3.4.0
7+
github.com/coreos/go-systemd/v22 v22.7.0
68
github.com/flatcar/ignition v0.36.2
79
github.com/google/go-cmp v0.7.0
810
github.com/metal-stack/metal-go v0.43.0
9-
github.com/metal-stack/metal-networker v0.46.3
11+
github.com/metal-stack/metal-lib v0.24.0
1012
github.com/metal-stack/v v1.0.3
1113
github.com/spf13/afero v1.15.0
1214
github.com/stretchr/testify v1.11.1
1315
gopkg.in/yaml.v3 v3.0.1
1416
)
1517

1618
require (
17-
github.com/Masterminds/semver/v3 v3.4.0 // indirect
1819
github.com/ajeddeloh/go-json v0.0.0-20160803184958-73d058cf8437 // indirect
1920
github.com/coreos/go-semver v0.3.1 // indirect
2021
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
21-
github.com/coreos/go-systemd/v22 v22.7.0 // indirect
2222
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2323
github.com/go-openapi/analysis v0.24.2 // indirect
2424
github.com/go-openapi/errors v0.22.6 // indirect
@@ -43,9 +43,6 @@ require (
4343
github.com/go-viper/mapstructure/v2 v2.5.0 // indirect
4444
github.com/godbus/dbus/v5 v5.2.2 // indirect
4545
github.com/google/uuid v1.6.0 // indirect
46-
github.com/kr/text v0.2.0 // indirect
47-
github.com/metal-stack/metal-hammer v0.14.1 // indirect
48-
github.com/metal-stack/metal-lib v0.24.0 // indirect
4946
github.com/oklog/ulid v1.3.1 // indirect
5047
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
5148
github.com/vincent-petithory/dataurl v1.0.0 // indirect

go.sum

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pq
1111
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
1212
github.com/coreos/go-systemd/v22 v22.7.0 h1:LAEzFkke61DFROc7zNLX/WA2i5J8gYqe0rSj9KI28KA=
1313
github.com/coreos/go-systemd/v22 v22.7.0/go.mod h1:xNUYtjHu2EDXbsxz1i41wouACIwT7Ybq9o0BQhMwD0w=
14-
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
1514
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1615
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
1716
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -76,18 +75,10 @@ github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
7675
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
7776
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
7877
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
79-
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
80-
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
81-
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
82-
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
8378
github.com/metal-stack/metal-go v0.43.0 h1:uODD0YCwnAYzyvFxWNakZrymBoMz1FAvP5hkhsR83VQ=
8479
github.com/metal-stack/metal-go v0.43.0/go.mod h1:GSfXrAj55LGsUSMHWGDsmq5n056NG0yb1JM8bgfvKOw=
85-
github.com/metal-stack/metal-hammer v0.14.1 h1:4Zuh7jdK/EUfJGZcrYjVAqRmgnZBf2S4WuL2mPqvexs=
86-
github.com/metal-stack/metal-hammer v0.14.1/go.mod h1:RLwtW2d02Ghv8fwFLNQNJD/bfYL+4M0qzVehM1/WadQ=
8780
github.com/metal-stack/metal-lib v0.24.0 h1:wvQQPWIXcA2tP+I6zAHUNdtVLLJfQnnV9yG2SoqUkz4=
8881
github.com/metal-stack/metal-lib v0.24.0/go.mod h1:oITaqj/BtB9vDKM66jCXkeA+4D0eTZElgIKal5vtiNY=
89-
github.com/metal-stack/metal-networker v0.46.3 h1:0suUDm6trfwMElZsf9cuewJmrYmsBSpxlVYcOAGWsPk=
90-
github.com/metal-stack/metal-networker v0.46.3/go.mod h1:FFER+4gOERGz3eTCoWqyfQ5eXwHNlYC00j5hdEJhqDU=
9182
github.com/metal-stack/v v1.0.3 h1:Sh2oBlnxrCUD+mVpzfC8HiqL045YWkxs0gpTvkjppqs=
9283
github.com/metal-stack/v v1.0.3/go.mod h1:YTahEu7/ishwpYKnp/VaW/7nf8+PInogkfGwLcGPdXg=
9384
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
@@ -97,8 +88,6 @@ github.com/pin/tftp v2.1.0+incompatible/go.mod h1:xVpZOMCXTy+A5QMjEVN0Glwa1sUvaJ
9788
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9889
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
9990
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
100-
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
101-
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
10291
github.com/sigma/bdoor v0.0.0-20160202064022-babf2a4017b0/go.mod h1:WBu7REWbxC/s/J06jsk//d+9DOz9BbsmcIrimuGRFbs=
10392
github.com/sigma/vmw-guestinfo v0.0.0-20160204083807-95dd4126d6e8/go.mod h1:JrRFFC0veyh0cibh0DAhriSY7/gV3kDdNaVUOmfx01U=
10493
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
@@ -134,9 +123,8 @@ golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
134123
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=
135124
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
136125
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
126+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
137127
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
138-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
139-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
140128
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
141129
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
142130
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

install.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414

1515
ignitionConfig "github.com/flatcar/ignition/config/v2_4"
1616
"github.com/metal-stack/metal-go/api/models"
17-
"github.com/metal-stack/metal-networker/pkg/netconf"
1817
v1 "github.com/metal-stack/os-installer/api/v1"
18+
"github.com/metal-stack/os-installer/pkg/network"
1919
"github.com/metal-stack/os-installer/templates"
2020
"github.com/metal-stack/v"
2121
"github.com/spf13/afero"
@@ -411,17 +411,17 @@ func (i *installer) createMetalUser() error {
411411

412412
func (i *installer) configureNetwork() error {
413413
i.log.Info("configure network")
414-
kb, err := netconf.New(i.log.WithGroup("networker"), installYAML)
414+
kb, err := network.New(i.log.WithGroup("networker"), installYAML)
415415
if err != nil {
416416
return err
417417
}
418418

419-
var kind netconf.BareMetalType
419+
var kind network.BareMetalType
420420
switch i.config.Role {
421421
case models.V1MachineAllocationRoleFirewall:
422-
kind = netconf.Firewall
422+
kind = network.Firewall
423423
case models.V1MachineAllocationRoleMachine:
424-
kind = netconf.Machine
424+
kind = network.Machine
425425
default:
426426
return fmt.Errorf("unknown role:%s", i.config.Role)
427427
}
@@ -431,11 +431,11 @@ func (i *installer) configureNetwork() error {
431431
return err
432432
}
433433

434-
c, err := netconf.NewConfigurator(kind, *kb, false)
434+
c, err := network.NewConfigurator(kind, *kb, false)
435435
if err != nil {
436436
return err
437437
}
438-
c.Configure(netconf.ForwardPolicyDrop)
438+
c.Configure(network.ForwardPolicyDrop)
439439
return nil
440440
}
441441

pkg/exec/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Package exec groups functionality related for command execution.
3+
*/
4+
package exec

pkg/exec/verbosecmd.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package exec
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os/exec"
7+
)
8+
9+
// VerboseCmd represents a system command with verbose output to be able to get an idea of the issue in case the cmd
10+
// fails.
11+
type VerboseCmd struct {
12+
Cmd exec.Cmd
13+
}
14+
15+
// NewVerboseCmd creates a new instance of VerboseCmd.
16+
func NewVerboseCmd(name string, args ...string) VerboseCmd {
17+
cmd := exec.Command(name, args...)
18+
return VerboseCmd{*cmd}
19+
}
20+
21+
//Run executes the command and returns any errors in case exist.
22+
func (v VerboseCmd) Run() error {
23+
var stderr bytes.Buffer
24+
v.Cmd.Stderr = &stderr
25+
26+
err := v.Cmd.Run()
27+
if err != nil {
28+
return fmt.Errorf("%w: %s", err, stderr.String())
29+
}
30+
31+
return nil
32+
}

pkg/net/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# network
2+
3+
Network can apply changes to `/etc/network/interfaces` and `/etc/frr/frr.conf`.
4+
5+
It was intentionally created to provide a common means to:
6+
7+
- apply validation
8+
- render interfaces/frr.conf files
9+
- reload required services to apply changes
10+
11+
## Requirements
12+
13+
Network lib relies on `ifupdown2` and `systemd`. It also is assumed frr is installed as systemd service.
14+
15+
## Usage
16+
17+
Make use network lib:
18+
19+
```go
20+
package main
21+
22+
import "github.com/metal-stack/os-installer/pkg/net"
23+
24+
func main() {
25+
// TODO
26+
}
27+
28+
```

pkg/net/applier.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package net
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"crypto/sha256"
7+
"io"
8+
"os"
9+
"text/template"
10+
)
11+
12+
// Applier is an interface to render changes and reload services to apply them.
13+
type Applier interface {
14+
Apply(tpl template.Template, tmpFile, destFile string, reload bool) (bool, error)
15+
Render(writer io.Writer, tpl template.Template) error
16+
Reload() error
17+
Validate() error
18+
Compare(tmpFile, destFile string) bool
19+
}
20+
21+
// networkApplier holds the toolset for applying network configuration changes.
22+
type networkApplier struct {
23+
data any
24+
validator Validator
25+
reloader Reloader
26+
}
27+
28+
// NewNetworkApplier creates a new NewNetworkApplier.
29+
func NewNetworkApplier(data any, validator Validator, reloader Reloader) Applier {
30+
return &networkApplier{data: data, validator: validator, reloader: reloader}
31+
}
32+
33+
// Apply applies the current configuration with the given template.
34+
func (n *networkApplier) Apply(tpl template.Template, tmpFile, destFile string, reload bool) (bool, error) {
35+
f, err := os.OpenFile(tmpFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
36+
if err != nil {
37+
return false, err
38+
}
39+
40+
defer func() {
41+
_ = f.Close()
42+
}()
43+
44+
w := bufio.NewWriter(f)
45+
err = n.Render(w, tpl)
46+
if err != nil {
47+
return false, err
48+
}
49+
50+
err = w.Flush()
51+
if err != nil {
52+
return false, err
53+
}
54+
55+
err = n.Validate()
56+
if err != nil {
57+
return false, err
58+
}
59+
60+
equal := n.Compare(tmpFile, destFile)
61+
if equal {
62+
return false, nil
63+
}
64+
65+
err = os.Rename(tmpFile, destFile)
66+
if err != nil {
67+
return false, err
68+
}
69+
70+
if !reload {
71+
return true, nil
72+
}
73+
74+
err = n.Reload()
75+
if err != nil {
76+
return true, err
77+
}
78+
79+
return true, nil
80+
}
81+
82+
// Render renders the network interfaces to the given writer using the given template.
83+
func (n *networkApplier) Render(w io.Writer, tpl template.Template) error {
84+
return tpl.Execute(w, n.data)
85+
}
86+
87+
// Validate applies the given validator to validate current changes.
88+
func (n *networkApplier) Validate() error {
89+
return n.validator.Validate()
90+
}
91+
92+
// Reload reloads the necessary services when the network interfaces configuration was changed.
93+
func (n *networkApplier) Reload() error {
94+
return n.reloader.Reload()
95+
}
96+
97+
// Compare compare source and target for hash equality.
98+
func (n *networkApplier) Compare(source, target string) bool {
99+
sourceChecksum, err := checksum(source)
100+
if err != nil {
101+
return false
102+
}
103+
104+
targetChecksum, err := checksum(target)
105+
if err != nil {
106+
return false
107+
}
108+
109+
return bytes.Equal(sourceChecksum, targetChecksum)
110+
}
111+
112+
func checksum(file string) ([]byte, error) {
113+
f, err := os.Open(file)
114+
if err != nil {
115+
return nil, err
116+
}
117+
118+
defer func() {
119+
_ = f.Close()
120+
}()
121+
122+
h := sha256.New()
123+
if _, err := io.Copy(h, f); err != nil {
124+
return nil, err
125+
}
126+
127+
return h.Sum(nil), nil
128+
}

pkg/net/applier_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package net
2+
3+
import "testing"
4+
5+
func TestNetworkApplier_Compare(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
source string
9+
target string
10+
want bool
11+
}{
12+
{
13+
name: "simple test",
14+
source: "/etc/hostname",
15+
target: "/etc/passwd",
16+
want: false,
17+
},
18+
{
19+
name: "simple test",
20+
source: "/etc/hostname",
21+
target: "/etc/hostname",
22+
want: true,
23+
},
24+
}
25+
for _, tt := range tests {
26+
t.Run(tt.name, func(t *testing.T) {
27+
n := &networkApplier{}
28+
if got := n.Compare(tt.source, tt.target); got != tt.want {
29+
t.Errorf("NetworkApplier.Compare() = %v, want %v", got, tt.want)
30+
}
31+
})
32+
}
33+
}

pkg/net/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package net contains code to apply changes to network interfaces and FRR (Free Range Routing).
2+
package net

0 commit comments

Comments
 (0)