1
0
mirror of https://github.com/rancher/os.git synced 2025-09-14 14:11:18 +00:00

Networking implementation and fixes

This commit is contained in:
Darren Shepherd
2015-03-06 21:20:46 -07:00
parent cdd9ede94a
commit b62fd539bd
4 changed files with 42 additions and 24 deletions

View File

@@ -35,15 +35,19 @@ func applyNetworkConfigs(cfg *config.Config) error {
}
//apply network config
for _, netConf := range cfg.Network.Interfaces {
for _, netConf := range cfg.Network.Interfaces {
for _, link := range links {
err := applyNetConf(link, netConf)
if err != nil {
log.Fatal(err)
log.Errorf("Failed to apply settings to %s : %v", link.Attrs().Name, err)
}
}
}
if err != nil {
return err
}
//post run
if cfg.Network.PostRun != nil {
return docker.StartAndWait(config.DOCKER_HOST, cfg.Network.PostRun)
@@ -54,7 +58,8 @@ func applyNetworkConfigs(cfg *config.Config) error {
func applyNetConf(link netlink.Link, netConf config.InterfaceConfig) error {
if matches(link.Attrs().Name, netConf.Match) {
if netConf.DHCP {
cmd := exec.Command("udhcpc", "-i", link.Attrs().Name)
log.Infof("Running DHCP on %s", link.Attrs().Name)
cmd := exec.Command("udhcpc", "-i", link.Attrs().Name, "-t", "20", "-n")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
@@ -72,23 +77,37 @@ func applyNetConf(link netlink.Link, netConf config.InterfaceConfig) error {
log.Error("addr add failed")
return err
}
log.Infof("Set %s on %s", netConf.Address, link.Attrs().Name)
}
if netConf.MTU > 0 {
if err := netlink.LinkSetMTU(link, netConf.MTU); err != nil {
log.Error("set MTU Failed")
return err
}
}
if err := netlink.LinkSetUp(link); err != nil {
log.Error("failed to setup link")
return err
}
if netConf.Gateway != "" {
route := netlink.Route{LinkIndex: link.Attrs().Index, Scope: netlink.SCOPE_LINK, Gw: net.ParseIP(netConf.Gateway)}
gatewayIp := net.ParseIP(netConf.Gateway)
if gatewayIp == nil {
return errors.New("Invalid gateway address " + netConf.Gateway)
}
route := netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
Gw: net.ParseIP(netConf.Gateway),
}
if err := netlink.RouteAdd(&route); err != nil {
log.Error("gateway set failed")
return err
}
}
if err := netlink.LinkSetUp(link); err != nil {
log.Error("failed to setup link")
return err
log.Infof("Set default gateway %s", netConf.Gateway)
}
}
return nil
@@ -97,4 +116,3 @@ func applyNetConf(link netlink.Link, netConf config.InterfaceConfig) error {
func matches(link, conf string) bool {
return glob.Glob(conf, link)
}

View File

@@ -17,11 +17,15 @@ func NewConfig() *Config {
Userdocker: UserDockerInfo{
UseTLS: true,
},
Network: NetworkConfig {
Interfaces: []InterfaceConfig {
Network: NetworkConfig{
Interfaces: []InterfaceConfig{
{
Match: "*",
DHCP: true,
Match: "eth*",
DHCP: true,
},
{
Match: "lo",
Address: "127.0.0.1/8",
},
},
},
@@ -80,9 +84,11 @@ func NewConfig() *Config {
{
Id: "network",
Cmd: "--name=network " +
"--rm " +
"--cap-add=NET_ADMIN " +
"--net=host " +
"--rm " +
"--volumes-from=command-volumes " +
"--volumes-from=system-volumes " +
"network",
},
{

View File

@@ -1,3 +1,2 @@
FROM base
COPY scripts/dockerimages/scripts/network.sh /
CMD ["/network.sh"]
CMD ["netconf"]

View File

@@ -1,5 +0,0 @@
#!/bin/sh
/sbin/ip addr add 127.0.0.1/8 dev lo
/sbin/ip link set up dev lo
udhcpc -i eth0