Skip to content

Commit

Permalink
feat: use optional keyword
Browse files Browse the repository at this point in the history
Fixes #112

See https://google.aip.dev/149

Now can check for nil so `0` is legal value

Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
  • Loading branch information
glimchb committed Aug 14, 2023
1 parent a2b7572 commit b0376a5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/opiproject/opi-evpn-bridge
go 1.19

require (
github.com/opiproject/opi-api v0.0.0-20230812010918-f241fade9773
github.com/opiproject/opi-api v0.0.0-20230814154012-8e3a291121b9
github.com/vishvananda/netlink v1.2.1-beta.2
go.einride.tech/aip v0.62.0
google.golang.org/grpc v1.57.0
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/opiproject/opi-api v0.0.0-20230811153555-425899c0550b h1:S93TcEyc6vDg3lbLXFSTihpEtcEoubFg7T6OOpVg8uI=
github.com/opiproject/opi-api v0.0.0-20230811153555-425899c0550b/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/opiproject/opi-api v0.0.0-20230812010918-f241fade9773 h1:WD15a4rKs6UYaLvbgAlINcW40plx7OJgBTNIbr9WRS0=
github.com/opiproject/opi-api v0.0.0-20230812010918-f241fade9773/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/opiproject/opi-api v0.0.0-20230814154012-8e3a291121b9 h1:4CmLT/PIl9BY65KgXQjXwwYl8Ka7CzY03jphYeu+6mg=
github.com/opiproject/opi-api v0.0.0-20230814154012-8e3a291121b9/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/vishvananda/netlink v1.2.1-beta.2 h1:Llsql0lnQEbHj0I1OuKyp8otXp0r3q0mPkuhwHfStVs=
github.com/vishvananda/netlink v1.2.1-beta.2/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhgX83tXhKS2B/PRMpOho=
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
go.einride.tech/aip v0.61.0 h1:H7r59BtQDcj8kGNa0Dytw88so1iWrzp6mSOEQgcIJWI=
go.einride.tech/aip v0.61.0/go.mod h1:YVrCQRL7SCB5Mv7i2ZF1R6vkLPh844RQBCLrrLcefaU=
go.einride.tech/aip v0.62.0 h1:DVHT0kgIhHfEqcbTUZ/tKTc+YButvOuTVT4JQFWDGo0=
go.einride.tech/aip v0.62.0/go.mod h1:YVrCQRL7SCB5Mv7i2ZF1R6vkLPh844RQBCLrrLcefaU=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
Expand Down
8 changes: 4 additions & 4 deletions pkg/evpn/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *Server) CreateLogicalBridge(_ context.Context, in *pb.CreateLogicalBrid
return nil, status.Errorf(codes.InvalidArgument, msg)
}
// create vxlan only if VNI is not empty
if in.LogicalBridge.Spec.Vni > 0 {
if in.LogicalBridge.Spec.Vni != nil {
bridge, err := netlink.LinkByName(tenantbridgeName)
if err != nil {
err := status.Errorf(codes.NotFound, "unable to find key %s", tenantbridgeName)
Expand All @@ -71,7 +71,7 @@ func (s *Server) CreateLogicalBridge(_ context.Context, in *pb.CreateLogicalBrid
myip := make(net.IP, 4)
// TODO: remove hard-coded 167772260 == "10.0.0.100"
binary.BigEndian.PutUint32(myip, 167772260)
vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: resourceID}, VxlanId: int(in.LogicalBridge.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip}
vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: resourceID}, VxlanId: int(*in.LogicalBridge.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip}
log.Printf("Creating Vxlan %v", vxlan)
// TODO: take Port from proto instead of hard-coded
if err := netlink.LinkAdd(vxlan); err != nil {
Expand Down Expand Up @@ -127,7 +127,7 @@ func (s *Server) DeleteLogicalBridge(_ context.Context, in *pb.DeleteLogicalBrid
}
resourceID := path.Base(obj.Name)
// only if VNI is not empty
if obj.Spec.Vni > 0 {
if obj.Spec.Vni != nil {
// use netlink to find vxlan device
vxlan, err := netlink.LinkByName(resourceID)
if err != nil {
Expand Down Expand Up @@ -225,7 +225,7 @@ func (s *Server) GetLogicalBridge(_ context.Context, in *pb.GetLogicalBridgeRequ
}
resourceID := path.Base(bridge.Name)
// only if VNI is not empty
if bridge.Spec.Vni > 0 {
if bridge.Spec.Vni != nil {
_, err := netlink.LinkByName(resourceID)
if err != nil {
err := status.Errorf(codes.NotFound, "unable to find key %s", resourceID)
Expand Down
10 changes: 5 additions & 5 deletions pkg/evpn/vrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *Server) CreateVrf(_ context.Context, in *pb.CreateVrfRequest) (*pb.Vrf,
}
// not found, so create a new one
vrfName := resourceID
tableID := uint32(1000 + math.Mod(float64(in.Vrf.Spec.Vni), 10.0))
tableID := uint32(1000 + math.Mod(float64(*in.Vrf.Spec.Vni), 10.0))
// Example: ip link add blue type vrf table 1000
vrf := &netlink.Vrf{LinkAttrs: netlink.LinkAttrs{Name: vrfName}, Table: tableID}
log.Printf("Creating VRF %v", vrf)
Expand Down Expand Up @@ -90,9 +90,9 @@ func (s *Server) CreateVrf(_ context.Context, in *pb.CreateVrfRequest) (*pb.Vrf,
}

// create bridge and vxlan only if VNI value is not empty
if in.Vrf.Spec.Vni > 0 {
if in.Vrf.Spec.Vni != nil {
// Example: ip link add br100 type bridge
bridgeName := fmt.Sprintf("br%d", in.Vrf.Spec.Vni)
bridgeName := fmt.Sprintf("br%d", *in.Vrf.Spec.Vni)
bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: bridgeName}}
log.Printf("Creating Linux Bridge %v", bridge)
if err := netlink.LinkAdd(bridge); err != nil {
Expand All @@ -115,11 +115,11 @@ func (s *Server) CreateVrf(_ context.Context, in *pb.CreateVrfRequest) (*pb.Vrf,
return nil, err
}
// Example: ip link add vni100 type vxlan local 10.0.0.4 dstport 4789 id 100 nolearning
vxlanName := fmt.Sprintf("vni%d", in.Vrf.Spec.Vni)
vxlanName := fmt.Sprintf("vni%d", *in.Vrf.Spec.Vni)
myip := make(net.IP, 4)
binary.BigEndian.PutUint32(myip, in.Vrf.Spec.VtepIpPrefix.Addr.GetV4Addr())
// TODO: take Port from proto instead of hard-coded
vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(in.Vrf.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip}
vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(*in.Vrf.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip}
log.Printf("Creating VXLAN %v", vxlan)
if err := netlink.LinkAdd(vxlan); err != nil {
fmt.Printf("Failed to create Vxlan link: %v", err)
Expand Down

0 comments on commit b0376a5

Please sign in to comment.