diff --git a/main.go b/main.go index f885aa11ce32..ab670dd907a8 100644 --- a/main.go +++ b/main.go @@ -68,25 +68,27 @@ func newNetworkManager() (controller.Controller, error) { host = strings.TrimSpace(string(output)) } - return controller.NewController(sub, string(host), opts.ip, opts.containerNetwork, opts.containerSubnetLength), nil + return controller.NewController(sub, string(host), opts.ip), nil } func newSubnetRegistry() (registry.SubnetRegistry, error) { peers := strings.Split(opts.etcdEndpoints, ",") subnetPath := path.Join(opts.etcdPath, "subnets") + subnetConfigPath := path.Join(opts.etcdPath, "config") minionPath := "/registry/minions/" if opts.sync { minionPath = path.Join(opts.etcdPath, "minions") } cfg := ®istry.EtcdConfig{ - Endpoints: peers, - Keyfile: opts.etcdKeyfile, - Certfile: opts.etcdCertfile, - CAFile: opts.etcdCAFile, - SubnetPath: subnetPath, - MinionPath: minionPath, + Endpoints: peers, + Keyfile: opts.etcdKeyfile, + Certfile: opts.etcdCertfile, + CAFile: opts.etcdCAFile, + SubnetPath: subnetPath, + SubnetConfigPath: subnetConfigPath, + MinionPath: minionPath, } return registry.NewEtcdSubnetRegistry(cfg) @@ -121,7 +123,7 @@ func main() { log.Fatalf("Failed to start openshift sdn in node mode: %v", err) } } else if opts.master { - err := be.StartMaster(opts.sync) + err := be.StartMaster(opts.sync, opts.containerNetwork, opts.containerSubnetLength) if err != nil { log.Fatalf("Failed to start openshift sdn in master mode: %v", err) } diff --git a/ovs-simple/controller/controller.go b/ovs-simple/controller/controller.go index 65a06f8ccd5e..2e76132d3bad 100644 --- a/ovs-simple/controller/controller.go +++ b/ovs-simple/controller/controller.go @@ -14,13 +14,8 @@ import ( "github.com/openshift/openshift-sdn/pkg/registry" ) -var ( - ContainerNetwork string = "10.1.0.0/16" - ContainerSubnetLength uint = 8 -) - type Controller interface { - StartMaster(sync bool) error + StartMaster(sync bool, containerNetwork string, containerSubnetLength uint) error StartNode(sync, skipsetup bool) error AddNode(minionIP string) error DeleteNode(minionIP string) error @@ -36,9 +31,7 @@ type OvsController struct { sig chan struct{} } -func NewController(sub registry.SubnetRegistry, hostname string, selfIP string, containerNetwork string, containerSubnetLength uint) Controller { - ContainerNetwork = containerNetwork - ContainerSubnetLength = containerSubnetLength +func NewController(sub registry.SubnetRegistry, hostname string, selfIP string) Controller { if selfIP == "" { addrs, err := net.LookupIP(hostname) if err != nil { @@ -58,7 +51,7 @@ func NewController(sub registry.SubnetRegistry, hostname string, selfIP string, } } -func (oc *OvsController) StartMaster(sync bool) error { +func (oc *OvsController) StartMaster(sync bool, containerNetwork string, containerSubnetLength uint) error { // wait a minute for etcd to come alive status := oc.subnetRegistry.CheckEtcdIsAlive(60) if !status { @@ -87,7 +80,12 @@ func (oc *OvsController) StartMaster(sync bool) error { } } - oc.subnetAllocator, err = netutils.NewSubnetAllocator(ContainerNetwork, ContainerSubnetLength, subrange) + err = oc.subnetRegistry.WriteNetworkConfig(containerNetwork, containerSubnetLength) + if err != nil { + return err + } + + oc.subnetAllocator, err = netutils.NewSubnetAllocator(containerNetwork, containerSubnetLength, subrange) if err != nil { return err } @@ -186,7 +184,12 @@ func (oc *OvsController) StartNode(sync, skipsetup bool) error { if !skipsetup { // Assume we are working with IPv4 subnetMaskLength, _ := ipnet.Mask.Size() - out, err := exec.Command("openshift-sdn-simple-setup-node.sh", netutils.GenerateDefaultGateway(ipnet).String(), ipnet.String(), ContainerNetwork, strconv.Itoa(subnetMaskLength)).CombinedOutput() + containerNetwork, err := oc.subnetRegistry.GetContainerNetwork() + if err != nil { + log.Errorf("Failed to obtain ContainerNetwork: %v", err) + return err + } + out, err := exec.Command("openshift-sdn-simple-setup-node.sh", netutils.GenerateDefaultGateway(ipnet).String(), ipnet.String(), containerNetwork, strconv.Itoa(subnetMaskLength)).CombinedOutput() log.Infof("Output of setup script:\n%s", out) if err != nil { log.Errorf("Error executing setup script. \n\tOutput: %s\n\tError: %v\n", out, err) diff --git a/pkg/netutils/common.go b/pkg/netutils/common.go index 27f08d51b43b..70e05c898edd 100644 --- a/pkg/netutils/common.go +++ b/pkg/netutils/common.go @@ -18,5 +18,5 @@ func Uint32ToIP(u uint32) net.IP { // Generate the default gateway IP Address for a subnet func GenerateDefaultGateway(sna *net.IPNet) net.IP { ip := sna.IP.To4() - return net.IPv4(ip[0], ip[1], ip[2], 1) + return net.IPv4(ip[0], ip[1], ip[2], ip[3]|0x1) } diff --git a/pkg/registry/registry.go b/pkg/registry/registry.go index 490c3a9264c0..0fbd877ecee0 100644 --- a/pkg/registry/registry.go +++ b/pkg/registry/registry.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "path" + "strconv" "sync" "time" @@ -30,16 +31,20 @@ type SubnetRegistry interface { WatchSubnets(rev uint64, receiver chan *SubnetEvent, stop chan bool) error GetMinions() (*[]string, error) WatchMinions(rev uint64, receiver chan *MinionEvent, stop chan bool) error + WriteNetworkConfig(network string, subnetLength uint) error + GetContainerNetwork() (string, error) + GetSubnetLength() (uint64, error) CheckEtcdIsAlive(seconds uint64) bool } type EtcdConfig struct { - Endpoints []string - Keyfile string - Certfile string - CAFile string - SubnetPath string - MinionPath string + Endpoints []string + Keyfile string + Certfile string + CAFile string + SubnetPath string + SubnetConfigPath string + MinionPath string } type SubnetEvent struct { @@ -147,6 +152,11 @@ func NewEtcdSubnetRegistry(config *EtcdConfig) (SubnetRegistry, error) { func (sub *EtcdSubnetRegistry) InitSubnets() error { key := sub.etcdCfg.SubnetPath _, err := sub.client().SetDir(key, 0) + if err != nil { + return err + } + key = sub.etcdCfg.SubnetConfigPath + _, err = sub.client().SetDir(key, 0) return err } @@ -225,6 +235,49 @@ func (sub *EtcdSubnetRegistry) DeleteSubnet(minion string) error { return err } +func (sub *EtcdSubnetRegistry) WriteNetworkConfig(network string, subnetLength uint) error { + key := path.Join(sub.etcdCfg.SubnetConfigPath, "ContainerNetwork") + _, err := sub.client().Create(key, network, 0) + if err != nil { + log.Warningf("Found existing network configuration, overwriting it.") + _, err = sub.client().Update(key, network, 0) + if err != nil { + log.Errorf("Failed to write Network configuration to etcd: %v", err) + return err + } + } + + key = path.Join(sub.etcdCfg.SubnetConfigPath, "SubnetLength") + data := strconv.FormatUint(uint64(subnetLength), 10) + _, err = sub.client().Create(key, data, 0) + if err != nil { + _, err = sub.client().Update(key, data, 0) + if err != nil { + log.Errorf("Failed to write Network configuration to etcd: %v", err) + return err + } + } + return nil +} + +func (sub *EtcdSubnetRegistry) GetContainerNetwork() (string, error) { + key := path.Join(sub.etcdCfg.SubnetConfigPath, "ContainerNetwork") + resp, err := sub.client().Get(key, false, false) + if err != nil { + return "", err + } + return resp.Node.Value, err +} + +func (sub *EtcdSubnetRegistry) GetSubnetLength() (uint64, error) { + key := path.Join(sub.etcdCfg.SubnetConfigPath, "SubnetLength") + resp, err := sub.client().Get(key, false, false) + if err == nil { + return strconv.ParseUint(resp.Node.Value, 10, 0) + } + return 0, err +} + func (sub *EtcdSubnetRegistry) CreateMinion(minion string, data string) error { key := path.Join(sub.etcdCfg.MinionPath, minion) _, err := sub.client().Get(key, false, false)