Skip to content

Commit

Permalink
extends list blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
nyanpassu committed Sep 23, 2021
1 parent c022fcf commit 7cf363e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 10 deletions.
59 changes: 49 additions & 10 deletions cmd/ctr/commands/calico/block/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package block
import (
log "github.com/sirupsen/logrus"

"github.com/projectcalico/libcalico-go/lib/backend/model"
cli "github.com/urfave/cli/v2"

"github.com/projecteru2/barrel/ctr"
Expand All @@ -19,30 +20,59 @@ func ListCommand() *cli.Command {
Usage: "hostname",
},
&cli.StringFlag{
Name: "poolname",
Usage: "poolname",
Name: "pool",
Usage: "pool",
Required: true,
},
&cli.BoolFlag{
Name: "all-host",
Usage: "list blocks of all hosts",
},
&cli.BoolFlag{
Name: "list-empty",
Usage: "list empty blocks only",
},
},
}
}

func list(ctx *cli.Context) error {
func list(ctx *cli.Context) (err error) {
c := ctr.Ctr{}
if err := c.InitCalico(); err != nil {
return err
}
if err := c.InitCalicoBackend(); err != nil {
return err
}

hostname, err := ctr.GetHostname(ctx)
if err != nil {
return err
var blocks []*model.AllocationBlock
if ctx.Bool("all-host") {
blocks, err = c.ListBlocks(ctx.Context, ctx.String("pool"))
if err != nil {
return err
}
} else {
hostname, err := ctr.GetHostname(ctx)
if err != nil {
return err
}
blocks, err = c.ListAffinityBlocks(ctx.Context, hostname, ctx.String("pool"))
if err != nil {
return err
}
}
blocks, err := c.ListAffinityBlocks(ctx.Context, hostname, ctx.String("poolname"))
if err != nil {
return err
log.Infof("there are %d blocks in pool", len(blocks))
if ctx.Bool("list-empty") {
var newBlocks []*model.AllocationBlock
for _, block := range blocks {
if isEmpty(block) {
newBlocks = append(newBlocks, block)
}
}
blocks = newBlocks
}
if len(blocks) == 0 {
log.Info("no block presents")
return
}
for _, block := range blocks {
log.Infof("%v", block.CIDR)
Expand All @@ -51,3 +81,12 @@ func list(ctx *cli.Context) error {
}
return nil
}

func isEmpty(block *model.AllocationBlock) bool {
for _, val := range block.Allocations {
if val != nil {
return false
}
}
return true
}
58 changes: 58 additions & 0 deletions ctr/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,64 @@ func (c *Ctr) UnassignIP(ctx context.Context, ip types.IP) error {
return nil
}

func (c *Ctr) ListBlocks(ctx context.Context, poolname string) (result []*model.AllocationBlock, err error) {
var (
blocksInPool = []cnet.IPNet{}
)

ipPool, err := c.calico.IPPools().Get(ctx, poolname, options.GetOptions{})
if err != nil {
log.Errorf("Invalid Pool - %v", poolname)
return nil, err
}

// Lookup blocks affine to the specified host.
opts := model.BlockListOptions{IPVersion: 4}
datastoreObjs, err := c.backend.List(ctx, opts, "")

if _, ok := err.(cerrors.ErrorResourceDoesNotExist); ok {
// The block path does not exist yet. This is OK - it means
// there are no affine blocks.
return result, nil
} else if err != nil {
log.Errorf("Error getting affine blocks: %v", err)
return nil, err
}

// Iterate through and extract the block CIDRs.
for _, o := range datastoreObjs.KVPairs {
k := o.Key.(model.BlockKey)

// Add the block if no IP pools were specified, or if IP pools were specified
// and the block falls within the given IP pools.
var poolNet *cnet.IPNet
_, poolNet, err = cnet.ParseCIDR(ipPool.Spec.CIDR)
if err != nil {
log.Errorf("Error parsing CIDR: %s from pool: %s %v", ipPool.Spec.CIDR, ipPool.Name, err)
return nil, err
}

if poolNet.Contains(k.CIDR.IPNet.IP) {
blocksInPool = append(blocksInPool, k.CIDR)
}
}

var blocks []*model.AllocationBlock
for _, cidr := range blocksInPool {
block, err := c.backend.Get(ctx, model.BlockKey{CIDR: cidr}, "")
if err != nil {
if _, ok := err.(cerrors.ErrorResourceDoesNotExist); ok {
// the block doesn't exists
continue
}
return nil, err
}
blocks = append(blocks, block.Value.(*model.AllocationBlock))
}

return blocks, nil
}

func (c *Ctr) ListAffinityBlocks(ctx context.Context, hostname string, poolname string) (result []*model.AllocationBlock, err error) {
var (
blocksInPool = []cnet.IPNet{}
Expand Down

0 comments on commit 7cf363e

Please sign in to comment.