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

Commit

Permalink
Merge pull request #42 from nyanpassu/feature/enchance-helper
Browse files Browse the repository at this point in the history
Feature/enchance helper
  • Loading branch information
jschwinger233 authored Nov 26, 2021
2 parents 168ac27 + 33a6190 commit c6e5347
Show file tree
Hide file tree
Showing 43 changed files with 731 additions and 582 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ dist
cert/
barrel
eru-barrel
eru-barrel.debug
eru-barrel.release
eru-barrel-utils
vendor/*
.mypy_cache
Expand Down
3 changes: 2 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ builds:
env:
- CGO_ENABLED=0
ldflags:
- -s -w
- -X github.com/projecteru2/barrel/versioninfo.REVISION={{.Commit}}
- -X github.com/projecteru2/barrel/versioninfo.VERSION={{.Env.VERSION}}
- -X github.com/projecteru2/barrel/versioninfo.BUILTAT={{.Date}}
Expand Down Expand Up @@ -38,6 +37,8 @@ archives:
windows: Windows
386: i386
amd64: x86_64
files:
- strip_debug.sh

checksum:
name_template: 'checksums.txt'
Expand Down
31 changes: 21 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
.PHONY: deps build test binary
.PHONY: deps build test binary mocks

REPO_PATH := github.com/projecteru2/barrel
REVISION := $(shell git rev-parse HEAD || unknown)
BUILTAT := $(shell date +%Y-%m-%dT%H:%M:%S)
BUILD_PATH := target
VERSION := $(shell git tag --contains $(shell git rev-list --tags --max-count=1) | sed '/-/!{s/$$/_/}' | sort -V -r | sed 's/_$$//' | head -1)
GO_LDFLAGS ?= -s -X $(REPO_PATH)/versioninfo.REVISION=$(REVISION) \
GO_LDFLAGS ?= -X $(REPO_PATH)/versioninfo.REVISION=$(REVISION) \
-X $(REPO_PATH)/versioninfo.BUILTAT=$(BUILTAT) \
-X $(REPO_PATH)/versioninfo.VERSION=$(VERSION)

deps:
env GO111MODULE=on go mod download
env GO111MODULE=on go mod vendor
binary:
go build -ldflags "$(GO_LDFLAGS)" -a -tags "netgo osusergo" -installsuffix netgo -o eru-barrel
go build -ldflags "-s -w $(GO_LDFLAGS)" -a -tags "netgo osusergo" -installsuffix netgo -o eru-barrel-utils cmd/ctr/ctr.go

strip: binary
objcopy --only-keep-debug eru-barrel eru-barrel.debug
objcopy --strip-debug --add-gnu-debuglink=eru-barrel.debug eru-barrel eru-barrel.release

test:
go vet `go list ./... | grep -v '/vendor/' | grep -v '/tools'`
Expand All @@ -22,14 +26,21 @@ test:
./utils/... \
./resources/...

binary:
go build -ldflags "$(GO_LDFLAGS)" -a -tags "netgo osusergo" -installsuffix netgo -o eru-barrel
go build -ldflags "$(GO_LDFLAGS)" -a -tags "netgo osusergo" -installsuffix netgo -o eru-barrel-utils cmd/ctr/ctr.go

cloc:
cloc --exclude-dir=vendor,3rdmocks,mocks,tools --not-match-f=test .

lint:
golangci-lint run

build: test binary
build: test binary

deps:
env GO111MODULE=on go mod download
env GO111MODULE=on go mod vendor

mocks:
cd vessel; \
mockery --name FixedIPAllocator && \
mockery --name CalicoIPAllocator && \
mockery --name ContainerVessel && \
mockery --name DockerNetworkManager
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (app Application) networkPluginOnlyMode() ([]service.Service, error) {
if dockerCli, err = app.getDockerClient(); err != nil {
return nil, err
}
allocator = vessel.NewIPPoolManager(client, dockerCli, app.DriverName, app.Hostname)
allocator = vessel.NewCalicoIPAllocator(client, app.Hostname)
return []service.Service{
pluginService{
ipam: calicoDriver.NewIpam(allocator, app.RequestTimeout),
Expand Down
35 changes: 17 additions & 18 deletions cmd/ctr/commands/assign/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type IPAssign struct {
*ctrtypes.Flags
c ctr.Ctr
poolFlag string
ipFlag string
hostFlag string
nodeFlag string
ipArg string
fixedIPFlag bool
}

Expand All @@ -26,21 +26,23 @@ func IPCommand(flags *ctrtypes.Flags) *cli.Command {
assignIP := IPAssign{Flags: flags}

return &cli.Command{
Name: "ip",
Usage: "assign a ip",
Action: assignIP.run,
Before: assignIP.init,
Name: "ip",
Usage: "assign a ip",
ArgsUsage: "ADDRESSV4",
Action: assignIP.run,
Before: assignIP.init,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pool",
Usage: "pool",
Usage: "use poolname to specific from which pool to allocate the ip address",
Required: true,
Destination: &assignIP.poolFlag,
},
&cli.StringFlag{
Name: "host",
Usage: "hostname",
Destination: &assignIP.hostFlag,
Name: "node",
Usage: "use nodename to specific on which host to allocate the ip address",
Required: true,
Destination: &assignIP.nodeFlag,
},
&cli.BoolFlag{
Name: "fixed-ip",
Expand All @@ -52,15 +54,12 @@ func IPCommand(flags *ctrtypes.Flags) *cli.Command {
}

func (d *IPAssign) init(ctx *cli.Context) error {
if err := ctr.InitHost(&d.hostFlag); err != nil {
return err
}
d.ipFlag = ctx.Args().First()
if d.ipFlag == "" {
d.ipArg = ctx.Args().First()
if d.ipArg == "" {
return errors.New("must provide ip address")
}
return ctr.InitCtr(&d.c, func(init *ctr.Init) {
init.InitAllocator(d.DockerHostFlag, d.DockerVersionFlag, "")
init.InitAllocator(d.nodeFlag)
})
}

Expand All @@ -76,11 +75,11 @@ func (d *IPAssign) assignIP(ctx context.Context) error {
if d.fixedIPFlag {
return d.c.AssignFixedIP(ctx, types.IP{
PoolID: d.poolFlag,
Address: d.ipFlag,
Address: d.ipArg,
})
}
return d.c.AssignIP(ctx, types.IP{
PoolID: d.poolFlag,
Address: d.ipFlag,
Address: d.ipArg,
})
}
2 changes: 1 addition & 1 deletion cmd/ctr/commands/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func DiagCommands(flags *ctrtypes.Flags) *cli.Command {
Subcommands: []*cli.Command{
diag.PoolCommand(flags),
diag.IPCommand(flags),
diag.HostCommand(flags),
diag.NodeCommand(flags),
},
}
}
85 changes: 0 additions & 85 deletions cmd/ctr/commands/diag/host.go

This file was deleted.

23 changes: 12 additions & 11 deletions cmd/ctr/commands/diag/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type DiagnoseIP struct {
*ctrtypes.Flags
c ctr.Ctr
poolFlag string
ip string
ipArg string
}

// IPCommand .
Expand All @@ -28,12 +28,13 @@ func IPCommand(flags *ctrtypes.Flags) *cli.Command {
}

return &cli.Command{
Name: "ip",
Usage: "diagnose ip problem",
Name: "ip",
Usage: "diagnose ip address problem",
ArgsUsage: "ADDRESSV4",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pool",
Usage: "pool",
Usage: "use poolname to specific which pool the address belongs to",
Destination: &diagIP.poolFlag,
Required: true,
},
Expand All @@ -44,8 +45,8 @@ func IPCommand(flags *ctrtypes.Flags) *cli.Command {
}

func (diag *DiagnoseIP) init(ctx *cli.Context) (err error) {
diag.ip = ctx.Args().First()
if diag.ip == "" {
diag.ipArg = ctx.Args().First()
if diag.ipArg == "" {
return errors.New("must provide ip address")
}
return ctr.InitCtr(&diag.c, func(init *ctr.Init) {
Expand All @@ -56,14 +57,14 @@ func (diag *DiagnoseIP) init(ctx *cli.Context) (err error) {
}

func (diag *DiagnoseIP) run(ctx *cli.Context) error {
cnetIP := cnet.ParseIP(diag.ip)
cnetIP := cnet.ParseIP(diag.ipArg)
if cnetIP == nil {
return errors.New("arg0 is not a valid ip address")
}

ipInfo, exists, err := diag.c.InspectFixedIP(ctx.Context, types.IP{
PoolID: diag.poolFlag,
Address: diag.ip,
Address: diag.ipArg,
})
if err != nil {
return err
Expand Down Expand Up @@ -96,7 +97,7 @@ func (diag *DiagnoseIP) run(ctx *cli.Context) error {
wepAssigned := false
for _, wep := range weps {
for _, ipNet := range wep.Spec.IPNetworks {
if strings.HasPrefix(ipNet, diag.ip) {
if strings.HasPrefix(ipNet, diag.ipArg) {
wepAssigned = true
ctr.Fprintlnf("the ip is assigned to WorkloadEndpoint %s, namespace = %s",
wep.Name, wep.Namespace,
Expand All @@ -108,13 +109,13 @@ func (diag *DiagnoseIP) run(ctx *cli.Context) error {
ctr.Fprintlnf("the ip is not assigned to WorkloadEndpoint")
}

containers, err := diag.c.ListContainerByPool(ctx.Context, diag.poolFlag)
containers, err := diag.c.ListContainersByPool(ctx.Context, diag.poolFlag)
if err != nil {
return errors.Annotate(err, "list containers on subnet error")
}
occupied := false
for containerID, container := range containers {
if strings.HasPrefix(container.IPv4Address, diag.ip) {
if strings.HasPrefix(container.IPv4Address, diag.ipArg) {
occupied = true
ctr.Fprintlnf("the ip is occupied by container %s(id = %s), workloadendpointID = %s",
container.Name, containerID, container.EndpointID,
Expand Down
Loading

0 comments on commit c6e5347

Please sign in to comment.