示例#1
0
// AllocSubnet allocates a new subnet. Returns a CIDR.
func (gc *Cfg) AllocSubnet(ra core.ResourceManager) (string, error) {
	pair, err := ra.AllocateResourceVal(gc.Tenant, resources.AutoSubnetResource)
	if err != nil {
		return "", err
	}

	return pair.(resources.SubnetIPLenPair).IP.String(), err
}
示例#2
0
// AllocVLAN allocates a new VLAN resource. Returns an ID.
func (gc *Cfg) AllocVLAN(ra core.ResourceManager) (uint, error) {
	vlan, err := ra.AllocateResourceVal(gc.Tenant, resources.AutoVLANResource)
	if err != nil {
		log.Errorf("alloc vlan failed: %q", err)
		return 0, err
	}

	return vlan.(uint), err
}
示例#3
0
// FreeVXLAN returns a VXLAN id to the pool.
func (gc *Cfg) FreeVXLAN(ra core.ResourceManager, vxlan uint, localVLAN uint) error {
	g := &Oper{}
	g.StateDriver = gc.StateDriver
	err := g.Read(gc.Tenant)
	if err != nil {
		return nil
	}

	return ra.DeallocateResourceVal(gc.Tenant, resources.AutoVXLANResource,
		resources.VXLANVLANPair{
			VXLAN: vxlan - g.FreeVXLANsStart,
			VLAN:  localVLAN})
}
示例#4
0
// AllocVXLAN allocates a new vxlan; ids for both the vxlan and vlan are returned.
func (gc *Cfg) AllocVXLAN(ra core.ResourceManager) (vxlan uint,
	localVLAN uint, err error) {

	pair, err1 := ra.AllocateResourceVal(gc.Tenant, resources.AutoVXLANResource)
	if err1 != nil {
		return 0, 0, err1
	}

	g := &Oper{}
	g.StateDriver = gc.StateDriver
	err = g.Read(gc.Tenant)
	if err != nil {
		return 0, 0, err
	}

	vxlan = pair.(resources.VXLANVLANPair).VXLAN + g.FreeVXLANsStart
	localVLAN = pair.(resources.VXLANVLANPair).VLAN

	return
}
示例#5
0
// DeleteResources deletes associated resources
func (gc *Cfg) DeleteResources(ra core.ResourceManager) error {
	tenant := gc.Tenant
	if tenant == "" {
		return core.Errorf("null tenant")
	}

	err := ra.UndefineResource(tenant, resources.AutoSubnetResource)
	if err != nil {
		log.Errorf("Error deleting subnet resource. Err: %v", err)
	}

	err = ra.UndefineResource(tenant, resources.AutoVLANResource)
	if err != nil {
		log.Errorf("Error deleting vlan resource. Err: %v", err)
	}

	err = ra.UndefineResource(tenant, resources.AutoVXLANResource)
	if err != nil {
		log.Errorf("Error deleting vxlan resource. Err: %v", err)
	}

	return err
}
示例#6
0
// Process validates, implements, and writes the state.
func (gc *Cfg) Process(ra core.ResourceManager) error {
	var err error

	if gc.Version != VersionBeta1 {
		return core.Errorf("unsupported version %s", gc.Version)
	}

	err = gc.checkErrors()
	if err != nil {
		return core.Errorf("process failed on error checks %s", err)
	}

	tenant := gc.Tenant
	if tenant == "" {
		return core.Errorf("null tenant")
	}

	subnetRsrcCfg := &resources.AutoSubnetCfgResource{
		SubnetPool:     net.ParseIP(gc.Auto.SubnetPool),
		SubnetPoolLen:  gc.Auto.SubnetLen,
		AllocSubnetLen: gc.Auto.AllocSubnetLen}
	err = ra.DefineResource(tenant, resources.AutoSubnetResource, subnetRsrcCfg)
	if err != nil {
		return err
	}

	// Only define a vlan resource if a valid range was specified
	if gc.Auto.VLANs != "" {
		var vlanRsrcCfg *bitset.BitSet
		vlanRsrcCfg, err = gc.initVLANBitset(gc.Auto.VLANs)
		if err != nil {
			return err
		}
		err = ra.DefineResource(tenant, resources.AutoVLANResource, vlanRsrcCfg)
		if err != nil {
			return err
		}
	}

	// Only define a vxlan resource if a valid range was specified
	var freeVXLANsStart uint
	if gc.Auto.VXLANs != "" {
		var vxlanRsrcCfg *resources.AutoVXLANCfgResource
		vxlanRsrcCfg, freeVXLANsStart, err = gc.initVXLANBitset(gc.Auto.VXLANs)
		if err != nil {
			return err
		}
		err = ra.DefineResource(tenant, resources.AutoVXLANResource, vxlanRsrcCfg)
		if err != nil {
			return err
		}
	}

	g := &Oper{Tenant: gc.Tenant,
		FreeVXLANsStart: freeVXLANsStart}
	g.StateDriver = gc.StateDriver
	err = g.Write()
	if err != nil {
		log.Errorf("error '%s' updating goper state %v \n", err, g)
		return err
	}

	log.Debugf("updating the global config to new state %v \n", gc)
	return nil
}
示例#7
0
// FreeSubnet releases a subnet derived from it's CIDR.
func (gc *Cfg) FreeSubnet(ra core.ResourceManager, subnetIP string) error {
	return ra.DeallocateResourceVal(gc.Tenant, resources.AutoSubnetResource,
		resources.SubnetIPLenPair{
			IP:  net.ParseIP(subnetIP),
			Len: gc.Auto.AllocSubnetLen})
}
示例#8
0
// FreeVLAN releases a VLAN for a given ID.
func (gc *Cfg) FreeVLAN(ra core.ResourceManager, vlan uint) error {
	return ra.DeallocateResourceVal(gc.Tenant, resources.AutoVLANResource, vlan)
}