Skip to content

Commit

Permalink
Merge pull request #45 from adrianchiris/refactor-typed-filter-params
Browse files Browse the repository at this point in the history
Refactor - use typed flower filter params
  • Loading branch information
adrianchiris authored Oct 27, 2022
2 parents 5990ecb + c3c3496 commit 4109211
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 78 deletions.
63 changes: 63 additions & 0 deletions pkg/tc/driver/cmdline/converters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package driver

import (
"strings"

"github.com/k8snetworkplumbingwg/multi-networkpolicy-tc/pkg/tc/types"
)

const (
allStr = "all"
ipStr = "ip"
ipv6Str = "ipv6"
vlanProtoStr = "802.1q"

tcpStr = "tcp"
udpStr = "udp"
)

// sToFilterProtocol converts given string to types.FilterProtocol. returns "" in case of an invalid conversion
func sToFilterProtocol(proto string) types.FilterProtocol {
var fp types.FilterProtocol

switch strings.ToLower(proto) {
case allStr:
fp = types.FilterProtocolAll
case ipStr:
fp = types.FilterProtocolIPv4
case ipv6Str:
fp = types.FilterProtocolIPv6
case vlanProtoStr:
fp = types.FilterProtocol8021Q
}

return fp
}

// sToFlowerIPProto converts given string to types.FlowerIPProto. returns "" in case of an invalid conversion
func sToFlowerIPProto(ipp string) types.FlowerIPProto {
var fp types.FlowerIPProto

switch strings.ToLower(ipp) {
case tcpStr:
fp = types.FlowerIPProtoTCP
case udpStr:
fp = types.FlowerIPProtoUDP
}

return fp
}

// sToFilterProtocol converts given string to types.FlowerVlanEthType. returns "" in case of an invalid conversion
func sToFlowerVlanEthType(ethtype string) types.FlowerVlanEthType {
var vlanEthType types.FlowerVlanEthType

switch strings.ToLower(ethtype) {
case ipStr:
vlanEthType = types.FlowerVlanEthTypeIPv4
case ipv6Str:
vlanEthType = types.FlowerVlanEthTypeIPv6
}

return vlanEthType
}
6 changes: 3 additions & 3 deletions pkg/tc/driver/cmdline/tc_cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ func (t *TcCmdLineImpl) FilterList(qdisc types.QDisc) ([]types.Filter, error) {

fb := types.NewFlowerFilterBuilder().
WithChain(f.Chain).
WithProtocol(types.FilterProtocol(f.Protocol)).
WithProtocol(sToFilterProtocol(f.Protocol)).
WithPriority(f.Priority).
WithHandle(f.Options.Handle)

if f.Options.Keys.VlanEthType != nil {
fb.WithMatchKeyVlanEthType(*f.Options.Keys.VlanEthType)
fb.WithMatchKeyVlanEthType(sToFlowerVlanEthType(*f.Options.Keys.VlanEthType))
}
if f.Options.Keys.IPProto != nil {
fb.WithMatchKeyIPProto(*f.Options.Keys.IPProto)
fb.WithMatchKeyIPProto(sToFlowerIPProto(*f.Options.Keys.IPProto))
}
if f.Options.Keys.DstIP != nil {
fb.WithMatchKeyDstIP(*f.Options.Keys.DstIP)
Expand Down
5 changes: 2 additions & 3 deletions pkg/tc/driver/cmdline/tc_cmdline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

testingexec "k8s.io/utils/exec/testing"

"github.com/k8snetworkplumbingwg/multi-networkpolicy-tc/pkg/policyrules"
"github.com/k8snetworkplumbingwg/multi-networkpolicy-tc/pkg/tc"
driver "github.com/k8snetworkplumbingwg/multi-networkpolicy-tc/pkg/tc/driver/cmdline"
tctypes "github.com/k8snetworkplumbingwg/multi-networkpolicy-tc/pkg/tc/types"
Expand Down Expand Up @@ -387,7 +386,7 @@ var _ = Describe("TC Cmdline driver tests", func() {
WithChain(0).
WithMatchKeyDstIP("10.10.10.2/24").
WithMatchKeyDstPort(6666).
WithMatchKeyIPProto(string(policyrules.ProtocolTCP)).
WithMatchKeyIPProto(tctypes.FlowerIPProtoTCP).
WithAction(tctypes.NewGenericActionBuiler().WithPass().Build()).
Build()

Expand Down Expand Up @@ -441,7 +440,7 @@ var _ = Describe("TC Cmdline driver tests", func() {
fakeCmd.OutputScript = append(fakeCmd.OutputScript, newFakeAction([]byte(filterListOut), nil, nil))
expectedFilter := tctypes.NewFlowerFilterBuilder().
WithProtocol(tctypes.FilterProtocol8021Q).
WithMatchKeyVlanEthType("ip").
WithMatchKeyVlanEthType(tctypes.FlowerVlanEthTypeIPv4).
WithPriority(200).
WithHandle(1).
WithChain(0).
Expand Down
22 changes: 11 additions & 11 deletions pkg/tc/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (s *SimpleTCGenerator) genFilters(ipCidrs []*net.IPNet, ports []policyrules
WithProtocol(proto).
WithPriority(ipProtoPrio).
WithMatchKeyDstIP(ipCidr.String()).
WithMatchKeyIPProto(string(port.Protocol)).
WithMatchKeyIPProto(tctypes.PortProtocolToFlowerIPProto(port.Protocol)).
WithMatchKeyDstPort(port.Number).
WithAction(action).
Build())
Expand All @@ -154,9 +154,9 @@ func (s *SimpleTCGenerator) genFilters(ipCidrs []*net.IPNet, ports []policyrules
tctypes.NewFlowerFilterBuilder().
WithProtocol(tctypes.FilterProtocol8021Q).
WithPriority(vlanProtoPrio).
WithMatchKeyVlanEthType(tctypes.ProtoToVlanProto(proto)).
WithMatchKeyVlanEthType(tctypes.ProtoToFlowerVlanEthType(proto)).
WithMatchKeyDstIP(ipCidr.String()).
WithMatchKeyIPProto(string(port.Protocol)).
WithMatchKeyIPProto(tctypes.PortProtocolToFlowerIPProto(port.Protocol)).
WithMatchKeyDstPort(port.Number).
WithAction(action).
Build())
Expand All @@ -174,7 +174,7 @@ func (s *SimpleTCGenerator) genFilters(ipCidrs []*net.IPNet, ports []policyrules
tctypes.NewFlowerFilterBuilder().
WithProtocol(tctypes.FilterProtocol8021Q).
WithPriority(vlanProtoPrio).
WithMatchKeyVlanEthType(tctypes.ProtoToVlanProto(proto)).
WithMatchKeyVlanEthType(tctypes.ProtoToFlowerVlanEthType(proto)).
WithMatchKeyDstIP(ipCidr.String()).
WithAction(action).
Build())
Expand All @@ -192,17 +192,17 @@ func (s *SimpleTCGenerator) genFilters(ipCidrs []*net.IPNet, ports []policyrules
tctypes.NewFlowerFilterBuilder().
WithProtocol(proto).
WithPriority(actualPrio).
WithMatchKeyVlanEthType("ip").
WithMatchKeyIPProto(string(port.Protocol)).
WithMatchKeyVlanEthType(tctypes.FlowerVlanEthTypeIPv4).
WithMatchKeyIPProto(tctypes.PortProtocolToFlowerIPProto(port.Protocol)).
WithMatchKeyDstPort(port.Number).
WithAction(action).
Build())
filters = append(filters,
tctypes.NewFlowerFilterBuilder().
WithProtocol(proto).
WithPriority(actualPrio).
WithMatchKeyVlanEthType("ipv6").
WithMatchKeyIPProto(string(port.Protocol)).
WithMatchKeyVlanEthType(tctypes.FlowerVlanEthTypeIPv6).
WithMatchKeyIPProto(tctypes.PortProtocolToFlowerIPProto(port.Protocol)).
WithMatchKeyDstPort(port.Number).
WithAction(action).
Build())
Expand All @@ -211,7 +211,7 @@ func (s *SimpleTCGenerator) genFilters(ipCidrs []*net.IPNet, ports []policyrules
tctypes.NewFlowerFilterBuilder().
WithProtocol(proto).
WithPriority(actualPrio).
WithMatchKeyIPProto(string(port.Protocol)).
WithMatchKeyIPProto(tctypes.PortProtocolToFlowerIPProto(port.Protocol)).
WithMatchKeyDstPort(port.Number).
WithAction(action).
Build())
Expand All @@ -228,14 +228,14 @@ func (s *SimpleTCGenerator) genFilters(ipCidrs []*net.IPNet, ports []policyrules
tctypes.NewFlowerFilterBuilder().
WithProtocol(proto).
WithPriority(actualPrio).
WithMatchKeyVlanEthType("ip").
WithMatchKeyVlanEthType(tctypes.FlowerVlanEthTypeIPv4).
WithAction(action).
Build())
filters = append(filters,
tctypes.NewFlowerFilterBuilder().
WithProtocol(proto).
WithPriority(actualPrio).
WithMatchKeyVlanEthType("ipv6").
WithMatchKeyVlanEthType(tctypes.FlowerVlanEthTypeIPv6).
WithAction(action).
Build())
} else {
Expand Down
Loading

0 comments on commit 4109211

Please sign in to comment.