Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loopback configuration to network setup #26

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var applyCmd = &cobra.Command{
}
}

err := SetupLoopback(netns)
err := SetupLoopback(netns, values.Loopback)
if err != nil {
return err
}
Expand Down Expand Up @@ -184,13 +184,18 @@ func SetNetns(name string, netns string) error {
return ip.SetNetns(name, netns)
}

func SetupLoopback(netns string) error {
return SetLinkUp(ip.IntoNetns(netns), "lo")
func SetupLoopback(netns string, ethernet config.Ethernet) error {
slog.Debug("setup loopback device", "netns", netns, "addresses", ethernet.Addresses, "routes", ethernet.Routes)

n := ip.IntoNetns(netns)
return SetupDevice(n, "lo", ethernet.Addresses, ethernet.Routes)
}

func SetupEthernets(netns string, ethernets map[string]config.Ethernet) error {
n := ip.IntoNetns(netns)
for name, values := range ethernets {
slog.Debug("setup ethernet device", "netns", netns, "name", name, "addresses", values.Addresses, "routes", values.Routes)

_, err := n.ShowLink(name)
if err == nil {
slog.Debug("device is already exists in netns", "name", name, "netns", netns)
Expand All @@ -216,6 +221,8 @@ func SetupEthernets(netns string, ethernets map[string]config.Ethernet) error {
func SetupDummyDevices(netns string, devices map[string]config.Ethernet) error {
n := ip.IntoNetns(netns)
for name, values := range devices {
slog.Debug("setup dummy device", "netns", netns, "name", name, "addresses", values.Addresses, "routes", values.Routes)

_, err := n.ShowLink(name)
if err == nil {
slog.Debug("device is already exists in netns", "name", name, "netns", netns)
Expand Down Expand Up @@ -245,6 +252,9 @@ func SetupVethDevices(netns string, devices map[string]config.VethDevice) error
peerName := values.Peer.Name
peerNetns := values.Peer.Netns

slog.Debug("setup veth device", "netns", netns, "name", name, "addresses", values.Addresses, "routes", values.Routes,
"peer name", peerName, "peer netns", peerNetns, "peer addresses", values.Peer.Addresses, "peer routes", values.Peer.Routes)

// check if device is already exists in netns
_, err := n.ShowLink(name)
if err == nil {
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Config struct {
}

type Netns struct {
Loopback Ethernet `yaml:"loopback,omitempty"`
Ethernets map[string]Ethernet `yaml:"ethernets,omitempty"`
DummyDevices map[string]Ethernet `yaml:"dummy-devices,omitempty"`
VethDevices map[string]VethDevice `yaml:"veth-devices,omitempty"`
Expand Down
7 changes: 7 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func TestLoadYamlFiles(t *testing.T) {
expected := &Config{
Netns: map[string]Netns{
"sample1": {
Loopback: Ethernet{
Addresses: []string{"127.0.0.53/8"},
Routes: []Route{{
To: "10.10.0.0/24",
Via: "127.0.0.53",
}},
},
Ethernets: map[string]Ethernet{
"eth0": {
Addresses: []string{
Expand Down
6 changes: 6 additions & 0 deletions testdata/config/sample1.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
netns:
sample1:
loopback:
addresses:
- 127.0.0.53/8
routes:
- to: 10.10.0.0/24
via: 127.0.0.53
ethernets:
eth0:
addresses:
Expand Down