Skip to content

Commit

Permalink
feat(store): use gokv pkg to abstract persistant store
Browse files Browse the repository at this point in the history
Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
  • Loading branch information
glimchb committed Oct 17, 2023
1 parent 531fb23 commit eb9d97b
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 27 deletions.
20 changes: 17 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"github.com/opiproject/opi-evpn-bridge/pkg/utils"
"github.com/opiproject/opi-smbios-bridge/pkg/inventory"

"github.com/philippgille/gokv"
"github.com/philippgille/gokv/gomap"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"
Expand All @@ -42,11 +45,22 @@ func main() {

flag.Parse()

// Create KV store for persistence
options := gomap.DefaultOptions
// TODO: we can change to redis or badger at any given time
store := gomap.NewStore(options)
defer func(store gokv.Store) {
err := store.Close()
if err != nil {
log.Panic(err)
}
}(store)

go runGatewayServer(grpcPort, httpPort)
runGrpcServer(grpcPort, tlsFiles)
runGrpcServer(grpcPort, tlsFiles, store)
}

func runGrpcServer(grpcPort int, tlsFiles string) {
func runGrpcServer(grpcPort int, tlsFiles string, store gokv.Store) {
tp := utils.InitTracerProvider("opi-evpn-bridge")
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
Expand Down Expand Up @@ -88,7 +102,7 @@ func runGrpcServer(grpcPort int, tlsFiles string) {
)
s := grpc.NewServer(serverOptions...)

opi := evpn.NewServer()
opi := evpn.NewServer(store)

pe.RegisterLogicalBridgeServiceServer(s, opi)
pe.RegisterBridgePortServiceServer(s, opi)
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ require (
github.com/onsi/ginkgo/v2 v2.13.0
github.com/opiproject/opi-api v0.0.0-20231016162146-d81cc5ee60d4
github.com/opiproject/opi-smbios-bridge v0.1.3-0.20231016193849-4f8fc2771276
github.com/philippgille/gokv v0.0.0-20191001201555-5ac9a20de634
github.com/philippgille/gokv/gomap v0.6.0
github.com/stretchr/testify v1.8.4
github.com/vektra/mockery/v2 v2.35.4
github.com/vishvananda/netlink v1.2.1-beta.2
Expand Down Expand Up @@ -145,6 +147,8 @@ require (
github.com/nunnatsa/ginkgolinter v0.13.5 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/philippgille/gokv/encoding v0.0.0-20191011213304-eb77f15b9c61 // indirect
github.com/philippgille/gokv/util v0.0.0-20191011213304-eb77f15b9c61 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/polyfloyd/go-errorlint v1.4.4 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/go-toolsmith/astcast v1.1.0 h1:+JN9xZV1A+Re+95pgnMgDboWNVnIMMQXwfBwLRPgSC8=
github.com/go-toolsmith/astcast v1.1.0/go.mod h1:qdcuFWeGGS2xX5bLM/c3U9lewg7+Zu4mr+xPwZIB4ZU=
github.com/go-toolsmith/astcopy v1.1.0 h1:YGwBN0WM+ekI/6SS6+52zLDEf8Yvp3n2seZITCUBt5s=
Expand Down Expand Up @@ -447,6 +449,16 @@ github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT9
github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
github.com/philippgille/gokv v0.0.0-20191001201555-5ac9a20de634 h1:d5aWDU6fAh8bWjHrwA3YKalyZL3N010cDOkv7YGiwxU=
github.com/philippgille/gokv v0.0.0-20191001201555-5ac9a20de634/go.mod h1:OCoWPt+mbYuTO1FUVrQ2SxQU0oaaHBsn6lRhFX3JHOc=
github.com/philippgille/gokv/encoding v0.0.0-20191011213304-eb77f15b9c61 h1:IgQDuUPuEFVf22mBskeCLAtvd5c9XiiJG2UYud6eGHI=
github.com/philippgille/gokv/encoding v0.0.0-20191011213304-eb77f15b9c61/go.mod h1:SjxSrCoeYrYn85oTtroyG1ePY8aE72nvLQlw8IYwAN8=
github.com/philippgille/gokv/gomap v0.6.0 h1:h2FbYBtchscVWoaN3PhQvq5jAgRYtUPII4czP0zSF2U=
github.com/philippgille/gokv/gomap v0.6.0/go.mod h1:TlbiKOc/8KIqTNw4oEaHRB7MZ0eVCkp6syUrm0XF3OM=
github.com/philippgille/gokv/test v0.0.0-20191011213304-eb77f15b9c61 h1:4tVyBgfpK0NSqu7tNZTwYfC/pbyWUR2y+O7mxEg5BTQ=
github.com/philippgille/gokv/test v0.0.0-20191011213304-eb77f15b9c61/go.mod h1:EUc+s9ONc1+VOr9NUEd8S0YbGRrQd/gz/p+2tvwt12s=
github.com/philippgille/gokv/util v0.0.0-20191011213304-eb77f15b9c61 h1:ril/jI0JgXNjPWwDkvcRxlZ09kgHXV2349xChjbsQ4o=
github.com/philippgille/gokv/util v0.0.0-20191011213304-eb77f15b9c61/go.mod h1:2dBhsJgY/yVIkjY5V3AnDUxUbEPzT6uQ3LvoVT8TR20=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
16 changes: 11 additions & 5 deletions pkg/evpn/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"reflect"
"testing"

"github.com/philippgille/gokv/gomap"
"github.com/stretchr/testify/mock"
"github.com/vishvananda/netlink"

Expand Down Expand Up @@ -257,7 +258,8 @@ func Test_CreateLogicalBridge(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down Expand Up @@ -425,7 +427,8 @@ func Test_DeleteLogicalBridge(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down Expand Up @@ -516,7 +519,8 @@ func Test_UpdateLogicalBridge(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down Expand Up @@ -597,7 +601,8 @@ func Test_GetLogicalBridge(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down Expand Up @@ -701,7 +706,8 @@ func Test_ListLogicalBridges(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down
13 changes: 10 additions & 3 deletions pkg/evpn/evpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"

"github.com/philippgille/gokv"

pe "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"

"github.com/opiproject/opi-evpn-bridge/pkg/utils"
Expand All @@ -40,24 +42,28 @@ type Server struct {
nLink utils.Netlink
frr utils.Frr
tracer trace.Tracer
store gokv.Store
}

// NewServer creates initialized instance of EVPN server
func NewServer() *Server {
func NewServer(store gokv.Store) *Server {
nLink := utils.NewNetlinkWrapper()
frr := utils.NewFrrWrapper()
return NewServerWithArgs(nLink, frr)
return NewServerWithArgs(nLink, frr, store)
}

// NewServerWithArgs creates initialized instance of EVPN server
// with externally created Netlink
func NewServerWithArgs(nLink utils.Netlink, frr utils.Frr) *Server {
func NewServerWithArgs(nLink utils.Netlink, frr utils.Frr, store gokv.Store) *Server {
if frr == nil {
log.Panic("nil for Frr is not allowed")
}
if nLink == nil {
log.Panic("nil for Netlink is not allowed")
}
if store == nil {
log.Panic("nil for Store is not allowed")
}
return &Server{
Bridges: make(map[string]*pe.LogicalBridge),
Ports: make(map[string]*pe.BridgePort),
Expand All @@ -67,6 +73,7 @@ func NewServerWithArgs(nLink utils.Netlink, frr utils.Frr) *Server {
nLink: nLink,
frr: frr,
tracer: otel.Tracer(""),
store: store,
}
}

Expand Down
15 changes: 14 additions & 1 deletion pkg/evpn/evpn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
"google.golang.org/grpc/test/bufconn"
"google.golang.org/protobuf/proto"

"github.com/philippgille/gokv"
"github.com/philippgille/gokv/gomap"

pe "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"

"github.com/opiproject/opi-evpn-bridge/pkg/utils"
Expand Down Expand Up @@ -58,21 +61,31 @@ func TestFrontEnd_NewServerWithArgs(t *testing.T) {
tests := map[string]struct {
frr utils.Frr
nLink utils.Netlink
store gokv.Store
wantPanic bool
}{
"nil netlink argument": {
frr: &utils.FrrWrapper{},
nLink: nil,
store: gomap.NewStore(gomap.DefaultOptions),
wantPanic: true,
},
"nil store argument": {
frr: &utils.FrrWrapper{},
nLink: &utils.NetlinkWrapper{},
store: nil,
wantPanic: true,
},
"nil frr argument": {
frr: nil,
nLink: &utils.NetlinkWrapper{},
store: gomap.NewStore(gomap.DefaultOptions),
wantPanic: true,
},
"all valid arguments": {
frr: &utils.FrrWrapper{},
nLink: &utils.NetlinkWrapper{},
store: gomap.NewStore(gomap.DefaultOptions),
wantPanic: false,
},
}
Expand All @@ -86,7 +99,7 @@ func TestFrontEnd_NewServerWithArgs(t *testing.T) {
}
}()

server := NewServerWithArgs(tt.nLink, tt.frr)
server := NewServerWithArgs(tt.nLink, tt.frr, tt.store)
if server == nil && !tt.wantPanic {
t.Error("expected non nil server or panic")
}
Expand Down
16 changes: 11 additions & 5 deletions pkg/evpn/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"reflect"
"testing"

"github.com/philippgille/gokv/gomap"
"github.com/stretchr/testify/mock"
"github.com/vishvananda/netlink"

Expand Down Expand Up @@ -298,7 +299,8 @@ func Test_CreateBridgePort(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down Expand Up @@ -454,7 +456,8 @@ func Test_DeleteBridgePort(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down Expand Up @@ -546,7 +549,8 @@ func Test_UpdateBridgePort(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down Expand Up @@ -627,7 +631,8 @@ func Test_GetBridgePort(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down Expand Up @@ -731,7 +736,8 @@ func Test_ListBridgePorts(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down
16 changes: 11 additions & 5 deletions pkg/evpn/svi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"reflect"
"testing"

"github.com/philippgille/gokv/gomap"
"github.com/stretchr/testify/mock"
"github.com/vishvananda/netlink"

Expand Down Expand Up @@ -399,7 +400,8 @@ func Test_CreateSvi(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down Expand Up @@ -581,7 +583,8 @@ func Test_DeleteSvi(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down Expand Up @@ -675,7 +678,8 @@ func Test_UpdateSvi(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down Expand Up @@ -756,7 +760,8 @@ func Test_GetSvi(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down Expand Up @@ -860,7 +865,8 @@ func Test_ListSvis(t *testing.T) {
ctx := context.Background()
mockNetlink := mocks.NewNetlink(t)
mockFrr := mocks.NewFrr(t)
opi := NewServerWithArgs(mockNetlink, mockFrr)
store := gomap.NewStore(gomap.DefaultOptions)
opi := NewServerWithArgs(mockNetlink, mockFrr, store)
conn, err := grpc.DialContext(ctx,
"",
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down
Loading

0 comments on commit eb9d97b

Please sign in to comment.