Skip to content

Commit

Permalink
add: node sysctl
Browse files Browse the repository at this point in the history
  • Loading branch information
ak1ra24 committed Dec 14, 2019
1 parent b818040 commit d860fb6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
30 changes: 26 additions & 4 deletions shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Node struct {
NetBase string `yaml:"net_base"`
Image string `yaml:"image"`
Interfaces []Interface `yaml:"interfaces" mapstructure:"interfaces"`
Sysctls []Sysctl `yaml:"sysctls" mapstructure:"sysctls"`
}

// Interface
Expand All @@ -59,6 +60,11 @@ type Interface struct {
Args string `yaml:"args"`
}

// Sysctl
type Sysctl struct {
Sysctl string `yaml:"string"`
}

// Switch
type Switch struct {
Name string `yaml:"name"`
Expand Down Expand Up @@ -352,13 +358,23 @@ func CreateNode(node Node) []string {
node.NetBase = "none"
}
if node.Type == "docker" {
createNodeCmd = fmt.Sprintf("docker run -td --hostname %s --net %s --name %s --rm --privileged %s", node.Name, node.NetBase, node.Name, node.Image)

createNodeCmd = fmt.Sprintf("docker run -td --hostname %s --net %s --name %s --rm --privileged ", node.Name, node.NetBase, node.Name)
if len(node.Sysctls) != 0 {
for _, sysctl := range node.Sysctls {
createNodeCmd += fmt.Sprintf("--sysctl %s ", sysctl.Sysctl)
}
}
createNodeCmd += node.Image
} else if node.Type == "netns" {
createNodeCmd = fmt.Sprintf("ip netns add %s", node.Name)
} else if node.Type == "" {
createNodeCmd = fmt.Sprintf("docker run -td --hostname %s --net %s --name %s --rm --privileged %s", node.Name, node.NetBase, node.Name, node.Image)

createNodeCmd = fmt.Sprintf("docker run -td --hostname %s --net %s --name %s --rm --privileged ", node.Name, node.NetBase, node.Name)
if len(node.Sysctls) != 0 {
for _, sysctl := range node.Sysctls {
createNodeCmd += fmt.Sprintf("--sysctl %s ", sysctl.Sysctl)
}
}
createNodeCmd += node.Image
} else {
// err := fmt.Errorf("unknown nodetype %s", node.Type)
// log.Fatal(err)
Expand All @@ -368,6 +384,12 @@ func CreateNode(node Node) []string {
createNodeCmds = append(createNodeCmds, createNodeCmd)

if node.Type == "netns" {
if len(node.Sysctls) != 0 {
for _, sysctl := range node.Sysctls {
sysctlNsCmd := fmt.Sprintf("ip netns exec %s sysctl -w %s", node.Name, sysctl.Sysctl)
createNodeCmds = append(createNodeCmds, sysctlNsCmd)
}
}
infloUpCmd := fmt.Sprintf("ip netns exec %s ip link set lo up", node.Name)
createNodeCmds = append(createNodeCmds, infloUpCmd)
}
Expand Down
51 changes: 51 additions & 0 deletions shell/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,31 @@ func TestCreateNode(t *testing.T) {
},
want: []string{"docker run -td --hostname R1 --net none --name R1 --rm --privileged slankdev/frr"},
},
{
name: "create docker node net None with sysctls",
args: args{
node: Node{
Name: "R1",
Image: "slankdev/frr",
Interfaces: []Interface{
Interface{
Name: "net0",
Type: "direct",
Args: "C1#net0",
},
},
Sysctls: []Sysctl{
Sysctl{
Sysctl: "net.ipv4.ip_forward=1",
},
Sysctl{
Sysctl: "net.ipv6.conf.all.forwarding=1",
},
},
},
},
want: []string{"docker run -td --hostname R1 --net none --name R1 --rm --privileged --sysctl net.ipv4.ip_forward=1 --sysctl net.ipv6.conf.all.forwarding=1 slankdev/frr"},
},
{
name: "create docker node net bridge",
args: args{
Expand Down Expand Up @@ -502,6 +527,32 @@ func TestCreateNode(t *testing.T) {
},
want: []string{"ip netns add C1", "ip netns exec C1 ip link set lo up"},
},
{
name: "create netns node with sysctls",
args: args{
node: Node{
Name: "C1",
Type: "netns",
Image: "",
Interfaces: []Interface{
Interface{
Name: "net0",
Type: "direct",
Args: "R1#net0",
},
},
Sysctls: []Sysctl{
Sysctl{
Sysctl: "net.ipv4.ip_forward=1",
},
Sysctl{
Sysctl: "net.ipv6.conf.all.forwarding=1",
},
},
},
},
want: []string{"ip netns add C1", "ip netns exec C1 sysctl -w net.ipv4.ip_forward=1", "ip netns exec C1 sysctl -w net.ipv6.conf.all.forwarding=1", "ip netns exec C1 ip link set lo up"},
},
{
name: "create not support node",
args: args{
Expand Down

0 comments on commit d860fb6

Please sign in to comment.