Skip to content

Commit

Permalink
Merge pull request #1 from rajatchopra/config_subnet
Browse files Browse the repository at this point in the history
store network config in etcd
  • Loading branch information
Mrunal Patel committed Mar 3, 2015
2 parents 0a053f8 + 84b999e commit 99be945
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 27 deletions.
18 changes: 10 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := &registry.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)
Expand Down Expand Up @@ -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)
}
Expand Down
27 changes: 15 additions & 12 deletions ovs-simple/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/netutils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
65 changes: 59 additions & 6 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"path"
"strconv"
"sync"
"time"

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 99be945

Please sign in to comment.