Skip to content

Commit 242c46a

Browse files
authored
Merge pull request #12 from sakura-internet/embed-gobgp
gobgpの導入
2 parents 701cec8 + 3ed6302 commit 242c46a

20 files changed

+1017
-294
lines changed

cmd/db-controller/cli.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ var (
3434
globalInterfaceNameFlag string
3535
// chainNameForDBAclFlag is a cli-flag that specifies the nftables chain name for DB access control list.
3636
chainNameForDBAclFlag string
37+
// bgpLocalAsnFlag is a cli-flag that specifies the my as number
38+
bgpLocalAsnFlag int
39+
// bgpServingPortFlag is a cli-flag that specifies the port of bgp
40+
bgpServingPortFlag int
41+
// bgpKeepaliveIntervalSecFlag is a cli-flag that specifies the interval seconds of bgp keepalive
42+
bgpKeepaliveIntervalSecFlag int
43+
// bgpPeerXAddrFlag and bgpPeerXAsn is a cli-flag that specifies neighbor address and asn of bgp peer.
44+
bgpPeer1AddrFlag string
45+
bgpPeer1AsnFlag int
46+
bgpPeer2AddrFlag string
47+
bgpPeer2AsnFlag int
48+
// gobgpGrpcPortFlag is a cli-flag that specifies port of gobgp gRPC
49+
gobgpGrpcPortFlag int
3750

3851
// mainPollingSpanSecondFlag is a cli-flag that specifies the span seconds of the loop in main.go.
3952
mainPollingSpanSecondFlag int
@@ -60,12 +73,20 @@ func parseAllFlags(args []string) error {
6073
fs.StringVar(&globalInterfaceNameFlag, "global-interface-name", "eth0", "the interface name of global")
6174
fs.StringVar(&chainNameForDBAclFlag, "chain-name-for-db-acl", "mariadb", "the chain name for DB access control")
6275
fs.StringVar(&dbReplicaUserNameFlag, "db-replica-user-name", "repl", "the username for replication")
76+
fs.StringVar(&bgpPeer1AddrFlag, "bgp-peer1-addr", "", "the address of bgp peer#1")
77+
fs.StringVar(&bgpPeer2AddrFlag, "bgp-peer2-addr", "", "the address of bgp peer#2")
6378

6479
fs.IntVar(&mainPollingSpanSecondFlag, "main-polling-span-second", 4, "the span seconds of the loop in main.go")
6580
fs.IntVar(&httpAPIServerPortFlag, "http-api-server-port", 54545, "the port the http api server listens")
6681
fs.IntVar(&prometheusExporterPortFlag, "prometheus-exporter-port", 50505, "the port the prometheus exporter listens")
6782
fs.IntVar(&dbReplicaSourcePortFlag, "db-replica-source-port", 13306, "the port of primary as replication source")
6883
fs.IntVar(&dbServingPortFlag, "db-serving-port", 3306, "the port of database service")
84+
fs.IntVar(&bgpLocalAsnFlag, "bgp-local-asn", 0, "the as number of local")
85+
fs.IntVar(&bgpPeer1AsnFlag, "bgp-peer1-asn", 0, "the asn of bgp peer#1")
86+
fs.IntVar(&bgpPeer2AsnFlag, "bgp-peer2-asn", 0, "the asn of bgp peer#2")
87+
fs.IntVar(&bgpServingPortFlag, "bgp-serving-port", 179, "the port of bgp")
88+
fs.IntVar(&bgpKeepaliveIntervalSecFlag, "bgp-keepalive-interval-sec", 3, "the interval seconds of bgp keepalive")
89+
fs.IntVar(&gobgpGrpcPortFlag, "gobgp-grpc-port", 50051, "the listen port of gobgp gRPC")
6990

7091
fs.BoolVar(&enablePrometheusExporterFlag, "prometheus-exporter", true, "enables the prometheus exporter")
7192
fs.BoolVar(&enableHTTPAPIFlag, "http-api", true, "enables the http api server")
@@ -83,6 +104,14 @@ func validateAllFlags() error {
83104
return fmt.Errorf("--prometheus-exporter-port must be the range of uint16(tcp port)")
84105
}
85106

107+
if bgpLocalAsnFlag == 0 {
108+
return fmt.Errorf("--bgp-local-asan must be specified")
109+
}
110+
111+
if bgpPeer1AddrFlag == "" || bgpPeer1AsnFlag == 0 || bgpPeer2AddrFlag == "" || bgpPeer2AsnFlag == 0 {
112+
return fmt.Errorf("insufficient bgp peer")
113+
}
114+
86115
return nil
87116
}
88117

cmd/db-controller/main.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/labstack/gommon/log"
3434
"github.com/prometheus/client_golang/prometheus/promhttp"
3535
apiv0 "github.com/sakura-internet/distributed-mariadb-controller/cmd/db-controller/api/v0"
36+
"github.com/sakura-internet/distributed-mariadb-controller/pkg/bgpserver"
3637
"github.com/sakura-internet/distributed-mariadb-controller/pkg/controller"
3738
"github.com/sakura-internet/distributed-mariadb-controller/pkg/nftables"
3839
"github.com/vishvananda/netlink"
@@ -59,14 +60,16 @@ func main() {
5960
}
6061
defer lockf.Close()
6162

63+
logger.Info("Hello, Starting db-controller.")
64+
6265
// for controlling the traffics that they're to the DB server port.
6366
// the function returns nil if the expected chain is already exist.
6467
nftConnect := nftables.NewDefaultConnector(logger)
6568
if err := nftConnect.CreateChain(chainNameForDBAclFlag); err != nil {
6669
panic(err)
6770
}
6871

69-
// prepare controller instance
72+
// get my global ip address
7073
myHostAddress, err := getNetIFAddress(globalInterfaceNameFlag)
7174
if err != nil {
7275
panic(err)
@@ -78,6 +81,33 @@ func main() {
7881
panic(err)
7982
}
8083

84+
var bgpPeers []bgpserver.Peer
85+
{
86+
bgpPeers = append(bgpPeers, bgpserver.Peer{
87+
Neighbor: bgpPeer1AddrFlag,
88+
RemoteAS: uint32(bgpPeer1AsnFlag),
89+
RemotePort: uint32(bgpServingPortFlag),
90+
KeepaliveIntervalSec: uint64(bgpKeepaliveIntervalSecFlag),
91+
})
92+
}
93+
{
94+
bgpPeers = append(bgpPeers, bgpserver.Peer{
95+
Neighbor: bgpPeer2AddrFlag,
96+
RemoteAS: uint32(bgpPeer2AsnFlag),
97+
RemotePort: uint32(bgpServingPortFlag),
98+
KeepaliveIntervalSec: uint64(bgpKeepaliveIntervalSecFlag),
99+
})
100+
}
101+
102+
bgpServerConnect := bgpserver.NewDefaultConnector(
103+
logger,
104+
bgpserver.WithLocalAsn(uint32(bgpLocalAsnFlag)),
105+
bgpserver.WithRouterId(myHostAddress),
106+
bgpserver.WithListenPort(int32(bgpServingPortFlag)),
107+
bgpserver.WithGrpcPort(gobgpGrpcPortFlag),
108+
bgpserver.WithPeers(bgpPeers),
109+
)
110+
81111
c := controller.NewController(
82112
logger,
83113
controller.WithGlobalInterfaceName(globalInterfaceNameFlag),
@@ -87,6 +117,7 @@ func main() {
87117
controller.WithDBReplicaPassword(dbReplicaPassword),
88118
controller.WithDBReplicaSourcePort(uint16(dbReplicaSourcePortFlag)),
89119
controller.WithDBAclChainName(chainNameForDBAclFlag),
120+
controller.WithBgpServerConnector(bgpServerConnect),
90121
)
91122

92123
// start goroutines
@@ -98,7 +129,10 @@ func main() {
98129
wg.Add(1)
99130
go func(ctx context.Context, wg *sync.WaitGroup, c *controller.Controller) {
100131
defer wg.Done()
101-
c.Start(ctx, time.Second*time.Duration(mainPollingSpanSecondFlag))
132+
err := c.Start(ctx, time.Second*time.Duration(mainPollingSpanSecondFlag))
133+
if err != nil {
134+
panic(err)
135+
}
102136
}(ctx, wg, c)
103137

104138
if enablePrometheusExporterFlag {

go.mod

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,52 @@ go 1.24.1
55
require (
66
github.com/labstack/echo/v4 v4.13.3
77
github.com/labstack/gommon v0.4.2
8-
github.com/prometheus/client_golang v1.21.1
8+
github.com/osrg/gobgp/v3 v3.36.0
9+
github.com/prometheus/client_golang v1.22.0
910
github.com/stretchr/testify v1.10.0
1011
github.com/vishvananda/netlink v1.3.0
12+
google.golang.org/protobuf v1.36.6
1113
)
1214

1315
require (
1416
github.com/beorn7/perks v1.0.1 // indirect
1517
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1618
github.com/davecgh/go-spew v1.1.1 // indirect
17-
github.com/klauspost/compress v1.17.11 // indirect
18-
github.com/kr/text v0.2.0 // indirect
19+
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
20+
github.com/eapache/channels v1.1.0 // indirect
21+
github.com/eapache/queue v1.1.0 // indirect
22+
github.com/fsnotify/fsnotify v1.6.0 // indirect
23+
github.com/golang/protobuf v1.5.3 // indirect
24+
github.com/google/uuid v1.6.0 // indirect
25+
github.com/hashicorp/hcl v1.0.0 // indirect
26+
github.com/k-sone/critbitgo v1.4.0 // indirect
27+
github.com/magiconair/properties v1.8.7 // indirect
1928
github.com/mattn/go-colorable v0.1.13 // indirect
2029
github.com/mattn/go-isatty v0.0.20 // indirect
30+
github.com/mitchellh/mapstructure v1.5.0 // indirect
2131
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
32+
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
2233
github.com/pmezard/go-difflib v1.0.0 // indirect
2334
github.com/prometheus/client_model v0.6.1 // indirect
2435
github.com/prometheus/common v0.62.0 // indirect
2536
github.com/prometheus/procfs v0.15.1 // indirect
37+
github.com/sirupsen/logrus v1.9.3 // indirect
38+
github.com/spf13/afero v1.9.5 // indirect
39+
github.com/spf13/cast v1.5.1 // indirect
40+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
41+
github.com/spf13/pflag v1.0.5 // indirect
42+
github.com/spf13/viper v1.16.0 // indirect
43+
github.com/subosito/gotenv v1.4.2 // indirect
2644
github.com/valyala/bytebufferpool v1.0.0 // indirect
2745
github.com/valyala/fasttemplate v1.2.2 // indirect
2846
github.com/vishvananda/netns v0.0.4 // indirect
29-
golang.org/x/crypto v0.31.0 // indirect
30-
golang.org/x/net v0.33.0 // indirect
31-
golang.org/x/sys v0.28.0 // indirect
32-
golang.org/x/text v0.21.0 // indirect
47+
golang.org/x/crypto v0.36.0 // indirect
48+
golang.org/x/net v0.37.0 // indirect
49+
golang.org/x/sys v0.31.0 // indirect
50+
golang.org/x/text v0.23.0 // indirect
3351
golang.org/x/time v0.8.0 // indirect
34-
google.golang.org/protobuf v1.36.1 // indirect
52+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
53+
google.golang.org/grpc v1.56.3 // indirect
54+
gopkg.in/ini.v1 v1.67.0 // indirect
3555
gopkg.in/yaml.v3 v3.0.1 // indirect
3656
)

0 commit comments

Comments
 (0)