Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

[BUGFIX] Fix some bugs in command utils #39

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions cmd/ctr/commands/inspect/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package inspect

import (
"encoding/json"
"fmt"
"os"

"github.com/juju/errors"
cli "github.com/urfave/cli/v2"
Expand Down Expand Up @@ -54,8 +52,8 @@ func (g *IPInspect) init(ctx *cli.Context) error {

func (g *IPInspect) run(ctx *cli.Context) error {
ip, exists, err := g.c.InspectFixedIP(ctx.Context, types.IP{
PoolID: ctx.String("poolname"),
Address: ctx.String("ip"),
PoolID: g.poolFlag,
Address: g.ipFlag,
})
if err != nil {
return err
Expand All @@ -67,6 +65,5 @@ func (g *IPInspect) run(ctx *cli.Context) error {
if err != nil {
return err
}
_, err = fmt.Fprint(os.Stdout, content)
return err
return ctr.Fprintln(string(content))
}
1 change: 1 addition & 0 deletions cmd/ctr/commands/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func ReleaseCommands(flags *ctrtypes.Flags) *cli.Command {
Usage: "release network resources",
Subcommands: []*cli.Command{
release.BlockCommand(flags),
release.BlocksCommand(flags),
release.IPCommand(flags),
release.WEPCommand(flags),
},
Expand Down
16 changes: 11 additions & 5 deletions cmd/ctr/commands/release/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
// DelIP .
type DelIP struct {
*ctrtypes.Flags
c ctr.Ctr
poolFlag string
ipFlag string
hostFlag string
c ctr.Ctr
poolFlag string
ipFlag string
hostFlag string
unallocFlag bool
}

// IPCommand .
Expand All @@ -41,6 +42,11 @@ func IPCommand(flags *ctrtypes.Flags) *cli.Command {
Usage: "hostname",
Destination: &delIP.hostFlag,
},
&cli.BoolFlag{
Name: "unalloc",
Usage: "unalloc fixed ip",
Destination: &delIP.unallocFlag,
},
},
}
}
Expand All @@ -62,7 +68,7 @@ func (d *DelIP) run(ctx *cli.Context) error {
if err := d.c.UnassignFixedIP(ctx.Context, types.IP{
PoolID: d.poolFlag,
Address: d.ipFlag,
}); err != nil {
}, d.unallocFlag); err != nil {
ctr.Fprintln("unassign fixed ip success")
}
return nil
Expand Down
30 changes: 13 additions & 17 deletions cmd/ctr/ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,38 @@ import (
"fmt"
"os"

"github.com/juju/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
cli "github.com/urfave/cli/v2"

"github.com/projecteru2/barrel/cmd/ctr/commands"
ctrtypes "github.com/projecteru2/barrel/cmd/ctr/types"
"github.com/projecteru2/barrel/types"
"github.com/projecteru2/barrel/versioninfo"
)

const envETCDEndpoints = "ETCD_ENDPOINTS"

func main() {
cli.VersionPrinter = func(c *cli.Context) {
fmt.Print(versioninfo.VersionString())
}

flags := ctrtypes.Flags{}
var configPath string
var etcdEndpoints string

app := &cli.App{
Name: "Barrel Ctr",
Version: versioninfo.VERSION,
Before: func(c *cli.Context) error {
viper.AddConfigPath(configPath)
viper.SetConfigName("ETCD_ENDPOINTS")
viper.AutomaticEnv()

if err := viper.ReadInConfig(); err != nil {
return err
if os.Getenv(envETCDEndpoints) != "" {
return nil
}

config := types.Config{}
if err := viper.Unmarshal(&config); err != nil {
return err
if etcdEndpoints != "" {
return os.Setenv("ETCD_ENDPOINTS", etcdEndpoints)
}
return os.Setenv("ETCD_ENDPOINTS", config.ETCDEndpoints)

return errors.New("must specific etcd endpoints from options or environment variable")
},
Commands: []*cli.Command{
commands.AssignCommands(&flags),
Expand All @@ -49,10 +46,9 @@ func main() {
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "environment config file for barrel",
Value: "/etc/eru/barrel.conf",
Destination: &configPath,
Name: "etcd-endpoints",
Usage: "etcd endpoints",
Destination: &etcdEndpoints,
},
&cli.StringFlag{
Name: "docker-host",
Expand Down
9 changes: 6 additions & 3 deletions ctr/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ func (c *Ctr) ListFixedIP(ctx context.Context, poolname string) ([]*types.IPInfo
}

// UnassignFixedIP .
func (c *Ctr) UnassignFixedIP(ctx context.Context, ip types.IP) error {
func (c *Ctr) UnassignFixedIP(ctx context.Context, ip types.IP, unalloc bool) error {
if unalloc {
return c.ipAllocator.UnallocFixedIP(ctx, ip)
}
if err := c.ipAllocator.UnassignFixedIP(
ctx,
ip,
); err != nil {
if err == types.ErrFixedIPNotAllocated {
if err == types.ErrFixedIPNotAllocated && unalloc {
return c.ipAllocator.UnallocIP(ctx, ip)
}
return err
Expand Down Expand Up @@ -113,7 +116,7 @@ func (c *Ctr) ListBlocks(ctx context.Context, opt ListBlockOpt) (result []*model

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

if ipPool == nil {
blocks = append(blocks, k.CIDR)
Expand Down
2 changes: 1 addition & 1 deletion ctr/wep.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *Ctr) RecycleWorkloadEndpoint(ctx context.Context, wepName string, pooln
} else if err := c.UnassignFixedIP(ctx, types.IP{
PoolID: poolname,
Address: ip.String(),
}); err != nil {
}, false); err != nil {
log.WithError(err).Errorf("Failed to recycle ip %s", ipCidr)
}
}
Expand Down
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ require (
github.com/Azure/go-autorest/autorest/adal v0.9.10 // indirect
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/containerd/containerd v1.4.3 // indirect
github.com/coreos/bbolt v1.3.2 // indirect
github.com/coreos/etcd v3.3.25+incompatible
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v20.10.2+incompatible
github.com/docker/go-connections v0.4.0
Expand All @@ -24,7 +27,9 @@ require (
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d
github.com/gophercloud/gophercloud v0.1.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.4.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.9.6 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
Expand All @@ -51,11 +56,13 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.6.0
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect
github.com/urfave/cli/v2 v2.3.0
github.com/vishvananda/netlink v1.1.0
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.etcd.io/bbolt v1.3.2 // indirect
go.uber.org/automaxprocs v1.3.0
go.uber.org/zap v1.16.0 // indirect
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 // indirect
Expand Down
Loading