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

domain objects abstraction #279

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (
"context"
"flag"
"fmt"
"github.com/opiproject/opi-evpn-bridge/pkg/objects/bridge"
"github.com/opiproject/opi-evpn-bridge/pkg/objects/port"
"github.com/opiproject/opi-evpn-bridge/pkg/objects/svi"
"github.com/opiproject/opi-evpn-bridge/pkg/objects/vrf"
"log"
"net"
"net/http"
"time"

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/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
58 changes: 0 additions & 58 deletions pkg/bridge/server.go

This file was deleted.

24 changes: 15 additions & 9 deletions pkg/models/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

// Bridge object, separate from protobuf for decoupling
type Bridge struct {
Name string
Vni uint32
VlanID uint32
VtepIP net.IPNet
Expand All @@ -21,17 +22,18 @@ type Bridge struct {
// build time check that struct implements interface
var _ EvpnObject[*pb.LogicalBridge] = (*Bridge)(nil)

// NewBridge creates new SVI object from protobuf message
func NewBridge(in *pb.LogicalBridge) *Bridge {
// vtepip := make(net.IP, 4)
// binary.BigEndian.PutUint32(vtepip, in.Spec.VtepIpPrefix.Addr.GetV4Addr())
// vip := net.IPNet{IP: vtepip, Mask: net.CIDRMask(int(in.Spec.VtepIpPrefix.Len), 32)}
// TODO: Vni: *in.Spec.Vni
return &Bridge{VlanID: in.Spec.VlanId}
func NewBridge(i *pb.LogicalBridge) EvpnObject[*pb.LogicalBridge] {
return &Bridge{
// vtepip := make(net.IP, 4)
// binary.BigEndian.PutUint32(vtepip, in.Spec.VtepIpPrefix.Addr.GetV4Addr())
// vip := net.IPNet{IP: vtepip, Mask: net.CIDRMask(int(in.Spec.VtepIpPrefix.Len), 32)}
// TODO: Vni: *in.Spec.Vni
VlanID: i.Spec.VlanId,
}
}

// ToPb transforms SVI object to protobuf message
func (in *Bridge) ToPb() (*pb.LogicalBridge, error) {
func (in *Bridge) ToPb() *pb.LogicalBridge {
bridge := &pb.LogicalBridge{
Spec: &pb.LogicalBridgeSpec{
Vni: &in.Vni,
Expand All @@ -42,5 +44,9 @@ func (in *Bridge) ToPb() (*pb.LogicalBridge, error) {
},
}
// TODO: add VtepIpPrefix
return bridge, nil
return bridge
}

func (in *Bridge) GetName() string {
return in.Name
}
3 changes: 2 additions & 1 deletion pkg/models/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ package models

// EvpnObject is an interface for all domain objects in evpn-gw
type EvpnObject[T any] interface {
ToPb() (T, error)
ToPb() T
GetName() string
}
35 changes: 18 additions & 17 deletions pkg/bridge/bridge_test.go → pkg/objects/bridge/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/opiproject/opi-evpn-bridge/pkg/objects"
"log"
"net"
"reflect"
Expand All @@ -36,7 +37,7 @@ import (

var (
testLogicalBridgeID = "opi-bridge9"
testLogicalBridgeName = resourceIDToFullName(testLogicalBridgeID)
testLogicalBridgeName = objects.ResourceIDToFullName(Prefix, testLogicalBridgeID)
testLogicalBridge = pb.LogicalBridge{
Spec: &pb.LogicalBridgeSpec{
Vni: proto.Uint32(11),
Expand Down Expand Up @@ -147,10 +148,10 @@ func Test_CreateLogicalBridge(t *testing.T) {
in: &testLogicalBridge,
out: nil,
errCode: codes.NotFound,
errMsg: fmt.Sprintf("unable to find key %v", tenantbridgeName),
errMsg: fmt.Sprintf("unable to find key %v", objects.TenantbridgeName),
exist: false,
on: func(mockNetlink *mocks.Netlink, mockFrr *mocks.Frr, errMsg string) {
mockNetlink.EXPECT().LinkByName(mock.Anything, tenantbridgeName).Return(nil, errors.New(errMsg)).Once()
mockNetlink.EXPECT().LinkByName(mock.Anything, objects.TenantbridgeName).Return(nil, errors.New(errMsg)).Once()
},
},
"failed LinkAdd call": {
Expand All @@ -166,8 +167,8 @@ func Test_CreateLogicalBridge(t *testing.T) {
binary.BigEndian.PutUint32(myip, 167772162)
vxlanName := fmt.Sprintf("vni%d", *testLogicalBridge.Spec.Vni)
vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(*testLogicalBridge.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip}
bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: tenantbridgeName}}
mockNetlink.EXPECT().LinkByName(mock.Anything, tenantbridgeName).Return(bridge, nil).Once()
bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: objects.TenantbridgeName}}
mockNetlink.EXPECT().LinkByName(mock.Anything, objects.TenantbridgeName).Return(bridge, nil).Once()
mockNetlink.EXPECT().LinkAdd(mock.Anything, vxlan).Return(errors.New(errMsg)).Once()
},
},
Expand All @@ -183,8 +184,8 @@ func Test_CreateLogicalBridge(t *testing.T) {
binary.BigEndian.PutUint32(myip, 167772162)
vxlanName := fmt.Sprintf("vni%d", *testLogicalBridge.Spec.Vni)
vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(*testLogicalBridge.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip}
bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: tenantbridgeName}}
mockNetlink.EXPECT().LinkByName(mock.Anything, tenantbridgeName).Return(bridge, nil).Once()
bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: objects.TenantbridgeName}}
mockNetlink.EXPECT().LinkByName(mock.Anything, objects.TenantbridgeName).Return(bridge, nil).Once()
mockNetlink.EXPECT().LinkAdd(mock.Anything, vxlan).Return(nil).Once()
mockNetlink.EXPECT().LinkSetMaster(mock.Anything, vxlan, bridge).Return(errors.New(errMsg)).Once()
},
Expand All @@ -201,8 +202,8 @@ func Test_CreateLogicalBridge(t *testing.T) {
binary.BigEndian.PutUint32(myip, 167772162)
vxlanName := fmt.Sprintf("vni%d", *testLogicalBridge.Spec.Vni)
vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(*testLogicalBridge.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip}
bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: tenantbridgeName}}
mockNetlink.EXPECT().LinkByName(mock.Anything, tenantbridgeName).Return(bridge, nil).Once()
bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: objects.TenantbridgeName}}
mockNetlink.EXPECT().LinkByName(mock.Anything, objects.TenantbridgeName).Return(bridge, nil).Once()
mockNetlink.EXPECT().LinkAdd(mock.Anything, vxlan).Return(nil).Once()
mockNetlink.EXPECT().LinkSetMaster(mock.Anything, vxlan, bridge).Return(nil).Once()
mockNetlink.EXPECT().LinkSetUp(mock.Anything, vxlan).Return(errors.New(errMsg)).Once()
Expand All @@ -220,8 +221,8 @@ func Test_CreateLogicalBridge(t *testing.T) {
binary.BigEndian.PutUint32(myip, 167772162)
vxlanName := fmt.Sprintf("vni%d", *testLogicalBridge.Spec.Vni)
vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(*testLogicalBridge.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip}
bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: tenantbridgeName}}
mockNetlink.EXPECT().LinkByName(mock.Anything, tenantbridgeName).Return(bridge, nil).Once()
bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: objects.TenantbridgeName}}
mockNetlink.EXPECT().LinkByName(mock.Anything, objects.TenantbridgeName).Return(bridge, nil).Once()
mockNetlink.EXPECT().LinkAdd(mock.Anything, vxlan).Return(nil).Once()
mockNetlink.EXPECT().LinkSetMaster(mock.Anything, vxlan, bridge).Return(nil).Once()
mockNetlink.EXPECT().LinkSetUp(mock.Anything, vxlan).Return(nil).Once()
Expand All @@ -241,8 +242,8 @@ func Test_CreateLogicalBridge(t *testing.T) {
binary.BigEndian.PutUint32(myip, 167772162)
vxlanName := fmt.Sprintf("vni%d", *testLogicalBridge.Spec.Vni)
vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(*testLogicalBridge.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip}
bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: tenantbridgeName}}
mockNetlink.EXPECT().LinkByName(mock.Anything, tenantbridgeName).Return(bridge, nil).Once()
bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: objects.TenantbridgeName}}
mockNetlink.EXPECT().LinkByName(mock.Anything, objects.TenantbridgeName).Return(bridge, nil).Once()
mockNetlink.EXPECT().LinkAdd(mock.Anything, vxlan).Return(nil).Once()
mockNetlink.EXPECT().LinkSetMaster(mock.Anything, vxlan, bridge).Return(nil).Once()
mockNetlink.EXPECT().LinkSetUp(mock.Anything, vxlan).Return(nil).Once()
Expand Down Expand Up @@ -320,7 +321,7 @@ func Test_DeleteLogicalBridge(t *testing.T) {
in: "unknown-id",
out: nil,
errCode: codes.NotFound,
errMsg: fmt.Sprintf("unable to find key %v", resourceIDToFullName("unknown-id")),
errMsg: fmt.Sprintf("unable to find key %v", objects.ResourceIDToFullName(Prefix, "unknown-id")),
missing: false,
on: nil,
},
Expand Down Expand Up @@ -445,7 +446,7 @@ func Test_DeleteLogicalBridge(t *testing.T) {
}(conn)
client := pb.NewLogicalBridgeServiceClient(conn)

fname1 := resourceIDToFullName(tt.in)
fname1 := objects.ResourceIDToFullName(Prefix, tt.in)
_ = opi.store.Set(testLogicalBridgeName, &testLogicalBridgeWithStatus)

if tt.on != nil {
Expand Down Expand Up @@ -502,12 +503,12 @@ func Test_UpdateLogicalBridge(t *testing.T) {
"valid request with unknown key": {
mask: nil,
in: &pb.LogicalBridge{
Name: resourceIDToFullName("unknown-id"),
Name: objects.ResourceIDToFullName(Prefix, "unknown-id"),
Spec: spec,
},
out: nil,
errCode: codes.NotFound,
errMsg: fmt.Sprintf("unable to find key %v", resourceIDToFullName("unknown-id")),
errMsg: fmt.Sprintf("unable to find key %v", objects.ResourceIDToFullName(Prefix, "unknown-id")),
start: false,
exist: true,
},
Expand Down
File renamed without changes.
21 changes: 2 additions & 19 deletions pkg/bridge/common.go → pkg/objects/bridge/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,15 @@ package bridge

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

"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"
"log"
"net"

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

func sortLogicalBridges(bridges []*pb.LogicalBridge) {
sort.Slice(bridges, func(i int, j int) bool {
return bridges[i].Name < bridges[j].Name
})
}

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

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

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