Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split evpn folder #270

Merged
merged 2 commits into from
Oct 19, 2023
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
18 changes: 12 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import (

pc "github.com/opiproject/opi-api/inventory/v1/gen/go"
pe "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"
"github.com/opiproject/opi-evpn-bridge/pkg/evpn"
"github.com/opiproject/opi-evpn-bridge/pkg/bridge"
"github.com/opiproject/opi-evpn-bridge/pkg/port"
"github.com/opiproject/opi-evpn-bridge/pkg/svi"
"github.com/opiproject/opi-evpn-bridge/pkg/utils"
"github.com/opiproject/opi-evpn-bridge/pkg/vrf"
"github.com/opiproject/opi-smbios-bridge/pkg/inventory"

"github.com/philippgille/gokv"
Expand Down Expand Up @@ -105,12 +108,15 @@ func runGrpcServer(grpcPort int, tlsFiles string, store gokv.Store) {
)
s := grpc.NewServer(serverOptions...)

opi := evpn.NewServer(store)
bridgeServer := bridge.NewServer(store)
portServer := port.NewServer(store)
vrfServer := vrf.NewServer(store)
sviServer := svi.NewServer(store)

pe.RegisterLogicalBridgeServiceServer(s, opi)
pe.RegisterBridgePortServiceServer(s, opi)
pe.RegisterVrfServiceServer(s, opi)
pe.RegisterSviServiceServer(s, opi)
pe.RegisterLogicalBridgeServiceServer(s, bridgeServer)
pe.RegisterBridgePortServiceServer(s, portServer)
pe.RegisterVrfServiceServer(s, vrfServer)
pe.RegisterSviServiceServer(s, sviServer)
pc.RegisterInventorySvcServer(s, &inventory.Server{})

reflection.Register(s)
Expand Down
4 changes: 2 additions & 2 deletions pkg/evpn/bridge.go → pkg/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Copyright (c) 2022-2023 Intel Corporation, or its subsidiaries.
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries.

// Package evpn is the main package of the application
package evpn
// Package bridge is the main package of the application
package bridge

import (
"context"
Expand Down
4 changes: 2 additions & 2 deletions pkg/evpn/bridge_netlink.go → pkg/bridge/bridge_netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Copyright (c) 2022-2023 Intel Corporation, or its subsidiaries.
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries.

// Package evpn is the main package of the application
package evpn
// Package bridge is the main package of the application
package bridge

import (
"context"
Expand Down
4 changes: 2 additions & 2 deletions pkg/evpn/bridge_test.go → pkg/bridge/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Copyright (c) 2022-2023 Intel Corporation, or its subsidiaries.
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries.

// Package evpn is the main package of the application
package evpn
// Package bridge is the main package of the application
package bridge

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries.

// Package evpn is the main package of the application
package evpn
// Package bridge is the main package of the application
package bridge

import (
"fmt"
Expand Down
103 changes: 103 additions & 0 deletions pkg/bridge/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries.

// Package bridge is the main package of the application
package bridge

import (
"context"
"fmt"
"log"
"net"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc/test/bufconn"
"google.golang.org/protobuf/proto"

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

// TODO: move all of this to a common place
const (
tenantbridgeName = "br-tenant"
)

func resourceIDToFullName(_ string, resourceID string) string {
return fmt.Sprintf("//network.opiproject.org/bridges/%s", resourceID)
}

func protoClone[T proto.Message](protoStruct T) T {
return proto.Clone(protoStruct).(T)
}

func extractPagination(pageSize int32, pageToken string, pagination map[string]int) (size int, offset int, err error) {
const (
maxPageSize = 250
defaultPageSize = 50
)
switch {
case pageSize < 0:
return -1, -1, status.Error(codes.InvalidArgument, "negative PageSize is not allowed")
case pageSize == 0:
size = defaultPageSize
case pageSize > maxPageSize:
size = maxPageSize
default:
size = int(pageSize)
}
// fetch offset from the database using opaque token
offset = 0
if pageToken != "" {
var ok bool
offset, ok = pagination[pageToken]
if !ok {
return -1, -1, status.Errorf(codes.NotFound, "unable to find pagination token %s", pageToken)
}
log.Printf("Found offset %d from pagination token: %s", offset, pageToken)
}
return size, offset, nil
}

func limitPagination[T any](result []T, offset int, size int) ([]T, bool) {
end := offset + size
hasMoreElements := false
if end < len(result) {
hasMoreElements = true

Check warning on line 67 in pkg/bridge/common.go

View check run for this annotation

Codecov / codecov/patch

pkg/bridge/common.go#L67

Added line #L67 was not covered by tests
} else {
end = len(result)
}
return result[offset:end], hasMoreElements
}

func dialer(opi *Server) func(context.Context, string) (net.Conn, error) {
listener := bufconn.Listen(1024 * 1024)
server := grpc.NewServer()

pb.RegisterLogicalBridgeServiceServer(server, opi)

go func() {
if err := server.Serve(listener); err != nil {
log.Fatal(err)
}

Check warning on line 83 in pkg/bridge/common.go

View check run for this annotation

Codecov / codecov/patch

pkg/bridge/common.go#L82-L83

Added lines #L82 - L83 were not covered by tests
}()

return func(context.Context, string) (net.Conn, error) {
return listener.Dial()
}
}

func equalProtoSlices[T proto.Message](x, y []T) bool {
if len(x) != len(y) {
return false
}

Check warning on line 94 in pkg/bridge/common.go

View check run for this annotation

Codecov / codecov/patch

pkg/bridge/common.go#L93-L94

Added lines #L93 - L94 were not covered by tests

for i := 0; i < len(x); i++ {
if !proto.Equal(x[i], y[i]) {
return false
}

Check warning on line 99 in pkg/bridge/common.go

View check run for this annotation

Codecov / codecov/patch

pkg/bridge/common.go#L98-L99

Added lines #L98 - L99 were not covered by tests
}

return true
}
58 changes: 58 additions & 0 deletions pkg/bridge/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries.

// Package bridge is the main package of the application
package bridge

import (
"log"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"

"github.com/philippgille/gokv"

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

"github.com/opiproject/opi-evpn-bridge/pkg/utils"
)

// Server represents the Server object
type Server struct {
pb.UnimplementedLogicalBridgeServiceServer
Pagination map[string]int
ListHelper map[string]bool
nLink utils.Netlink
frr utils.Frr
tracer trace.Tracer
store gokv.Store
}

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

Check warning on line 35 in pkg/bridge/server.go

View check run for this annotation

Codecov / codecov/patch

pkg/bridge/server.go#L32-L35

Added lines #L32 - L35 were not covered by tests
}

// NewServerWithArgs creates initialized instance of EVPN server
// with externally created Netlink
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{
ListHelper: make(map[string]bool),
Pagination: make(map[string]int),
nLink: nLink,
frr: frr,
tracer: otel.Tracer(""),
store: store,
}
}
64 changes: 64 additions & 0 deletions pkg/bridge/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries.

// Package bridge is the main package of the application
package bridge

import (
"testing"

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

"github.com/opiproject/opi-evpn-bridge/pkg/utils"
)

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,
},
}

for testName, tt := range tests {
t.Run(testName, func(t *testing.T) {
defer func() {
r := recover()
if (r != nil) != tt.wantPanic {
t.Errorf("NewServerWithArgs() recover = %v, wantPanic = %v", r, tt.wantPanic)
}
}()

server := NewServerWithArgs(tt.nLink, tt.frr, tt.store)
if server == nil && !tt.wantPanic {
t.Error("expected non nil server or panic")
}
})
}
}
Loading
Loading