Skip to content

Commit

Permalink
takes them up to make them fit
Browse files Browse the repository at this point in the history
  • Loading branch information
anrs committed Feb 19, 2022
1 parent 232951f commit 277e11f
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 31 deletions.
25 changes: 25 additions & 0 deletions meta/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,28 @@ func SubnetKey(subnet int64) string {
var v = strconv.FormatInt(subnet, 10) //nolint
return filepath.Join(config.Conf.EtcdPrefix, ipPrefix, v)
}

// IPBlockKey /<prefix>/ippool/<ipp name>/blocks/<subnet>
func IPBlockKey(ipp, subnet string) string {
return filepath.Join(IPBlocksPrefix(ipp), subnet)
}

// IPBlocksPrefix /<prefix>/ippool/<ipp name>/blocks/
func IPBlocksPrefix(ipp string) string {
return filepath.Join(IPPoolKey(ipp), ipblockPrefix)
}

// IPPoolLockKey /<prefix>/ippools/<name>/lock
func IPPoolLockKey(name string) string {
return filepath.Join(IPPoolKey(name), "lock")
}

// IPPoolKey /<prefix>/ippools/<name>
func IPPoolKey(name string) string {
return filepath.Join(IPPoolsPrefix(), name)
}

// IPPoolsPrefix /<prefix>/ippools/
func IPPoolsPrefix() string {
return filepath.Join(config.Conf.EtcdPrefix, ippPrefix)
}
163 changes: 163 additions & 0 deletions model/ipblock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package model

import (
"net"

"github.com/projecteru2/yavirt/errors"
"github.com/projecteru2/yavirt/meta"
"github.com/projecteru2/yavirt/netx"
"github.com/projecteru2/yavirt/util"
)

// IPBlocks .
type IPBlocks []*IPBlock

// Len .
func (bs IPBlocks) Len() int { return len(bs) }

// Append .
func (bs *IPBlocks) Append(block ...*IPBlock) {
*bs = append(*bs, block...)
}

// IPBlock .
type IPBlock struct {
*Generic

IPs *util.Bitmap32 `json:"ips"`

ippool *IPPool
ipnet *net.IPNet
}

func newIPBlock(ipp *IPPool, ipn *net.IPNet) *IPBlock {
block := &IPBlock{
Generic: newGeneric(),
ippool: ipp,
ipnet: ipn,
}

block.Status = StatusRunning
block.IPs = util.NewBitmap32(block.ipCount())

return block
}

// Release .
func (b *IPBlock) Release(ipn *net.IPNet) error {
if !b.ippool.Contains(ipn) {
return errors.Annotatef(errors.ErrInsufficientIP, "IP %s not found", ipn.IP)
}

offset := b.getIPIndex(ipn.IP)
if err := b.IPs.Unset(offset); err != nil {
return errors.Annotatef(err, "release %d IP %s failed", offset, ipn)
}

if err := b.save(); err != nil {
b.IPs.Set(offset) //nolint
return errors.Trace(err)
}

return nil
}

func (b *IPBlock) isAssigned(ipn *net.IPNet) (bool, error) {
offset := b.getIPIndex(ipn.IP)
return b.IPs.Has(offset)
}

func (b *IPBlock) getIPIndex(ip net.IP) int {
i64 := netx.IP2int(ip)

i64 &= (1 << uint(32-b.MaskBits())) - 1 //nolint

i64 %= MaxBlockIPCount

return int(i64)
}

// Assign .
func (b *IPBlock) Assign() (ipn *net.IPNet, err error) {
b.IPs.Range(func(offset int, set bool) bool {
if set {
return true
}

ipn, err = b.assign(offset)

return false
})

if err == nil && ipn == nil {
err = errors.Annotatef(errors.ErrInsufficientIP,
"block %s hasn't free IP", b.ipnet)
}

return
}

func (b *IPBlock) assign(offset int) (*net.IPNet, error) {
ipn := &net.IPNet{
IP: netx.Int2ip(b.intIP() + int64(offset)),
Mask: b.ipnet.Mask,
}

if err := b.IPs.Set(offset); err != nil {
return nil, errors.Annotatef(err, "assign %d IP %s failed", offset, ipn)
}

if err := b.save(); err != nil {
b.IPs.Unset(offset) //nolint
return nil, errors.Trace(err)
}

return ipn, nil
}

// HasFreeIP .
func (b *IPBlock) HasFreeIP() bool {
return b.IPs.Available()
}

func (b *IPBlock) save() error {
return meta.Save(meta.Resources{b})
}

func (b *IPBlock) intIP() int64 {
return netx.IP2int(b.BlockIP())
}

// MetaKey .
func (b *IPBlock) MetaKey() string {
return meta.IPBlockKey(b.ippool.Name, b.BlockIP().String())
}

// BlockIP .
func (b *IPBlock) BlockIP() net.IP {
return b.ipnet.IP
}

// Marshal .
func (b *IPBlock) Marshal() ([]byte, error) {
return util.JSONEncode(b)
}

// CIDR .
func (b *IPBlock) CIDR() string {
return b.ipnet.String()
}

func (b *IPBlock) ipCount() int {
return 1 << uint(b.ipBits())
}

func (b *IPBlock) ipBits() int {
return util.Min(8, 32-b.MaskBits()) //nolint
}

// MaskBits .
func (b *IPBlock) MaskBits() (ones int) {
ones, _ = b.ipnet.Mask.Size()
return
}
1 change: 0 additions & 1 deletion virt/guest/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/projecteru2/yavirt/virt/nic"
"github.com/projecteru2/yavirt/virt/types"
"github.com/projecteru2/yavirt/virt/volume"
"github.com/projecteru2/yavirt/vnet"
)

// Bot .
Expand Down
2 changes: 1 addition & 1 deletion virt/guest/guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sync"
"time"

"github.com/projecteru2/yavirt/config"
"github.com/projecteru2/yavirt/errors"
"github.com/projecteru2/yavirt/libvirt"
"github.com/projecteru2/yavirt/log"
Expand Down Expand Up @@ -587,6 +586,7 @@ func (g *Guest) botOperate(fn func(bot Bot) error, skipLock ...bool) error {
// CacheImage downloads the image from hub.
func (g *Guest) CacheImage(lock sync.Locker) error {
// not implemented
return nil
}

func (g *Guest) sysVolume() (vol volume.Virt, err error) {
Expand Down
20 changes: 1 addition & 19 deletions virt/guest/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/projecteru2/yavirt/log"
"github.com/projecteru2/yavirt/meta"
"github.com/projecteru2/yavirt/model"
"github.com/projecteru2/yavirt/netx"
"github.com/projecteru2/yavirt/sh"
"github.com/projecteru2/yavirt/util"
"github.com/projecteru2/yavirt/vnet"
Expand Down Expand Up @@ -249,24 +248,7 @@ func (g *Guest) DeleteNetwork() error {
}

func (g *Guest) deleteExtraNetworks() (err error) {
var args map[string]*deleteExtraNetworkArgs
if args, err = g.getDeleteExtraNetworksArgs(); err != nil {
return
}

for _, a := range args {
if err = a.hand.DeleteEndpointNetwork(a.args); err != nil {
return
}
}

for _, netw := range g.ExtraNetworks {
a := args[netw.Name]
if err = a.hand.ReleaseIPs(a.args.IPs...); err != nil {
return errors.Trace(err)
}
}

// todo
return
}

Expand Down
2 changes: 0 additions & 2 deletions yavirtd.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ func Run(c *cli.Context) error {
}
defer guest.GetCurrentEpoller().Close() // nolint

svc.BootGuestCh = mon.BootGuestCh

grpcSrv, err := grpcserver.Listen(svc)
if err != nil {
return errors.Trace(err)
Expand Down
10 changes: 2 additions & 8 deletions yavirtd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,7 @@ func (svc *Service) Wait(ctx virt.Context, id string, block bool) (msg string, c
}

func (svc *Service) PushImage(ctx virt.Context, imgName, user string) (err error) {
if err = svc.guest.PushImage(ctx, imgName, user); err != nil {
log.ErrorStack(err)
metric.IncrError()
}
// todo
return
}

Expand Down Expand Up @@ -485,10 +482,7 @@ func (svc *Service) ListImage(ctx virt.Context, filter string) ([]types.SysImage
}

func (svc *Service) PullImage(ctx virt.Context, imgName string, all bool) (msg string, err error) {
if msg, err = svc.guest.PullImage(ctx, imgName, all); err != nil {
log.ErrorStack(err)
metric.IncrError()
}
// todo
return
}

Expand Down

0 comments on commit 277e11f

Please sign in to comment.