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

Add Type to string consts #15

Merged
merged 1 commit into from
Jul 27, 2022
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
2 changes: 1 addition & 1 deletion pkg/policyrules/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func (r *rendererImpl) getPorts(ports []multiv1beta1.MultiNetworkPolicyPort) []P
switch *p.Protocol {
case corev1.ProtocolTCP:
break
case ProtocolUDP:
case corev1.ProtocolUDP:
policyPort.Protocol = ProtocolUDP
default:
r.log.Error(fmt.Errorf("unsupported protocol"), "", "protocol", p.Protocol)
Expand Down
17 changes: 10 additions & 7 deletions pkg/policyrules/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

const (
PolicyTypeIngress = "Ingress"
PolicyTypeEgress = "Egress"
PolicyTypeIngress PolicyType = "Ingress"
PolicyTypeEgress PolicyType = "Egress"

PolicyActionPass = "Pass"
PolicyActionDrop = "Drop"
PolicyActionPass PolicyAction = "Pass"
PolicyActionDrop PolicyAction = "Drop"

ProtocolTCP = "TCP"
ProtocolUDP = "UDP"
ProtocolTCP PolicyPortProtocol = "TCP"
ProtocolUDP PolicyPortProtocol = "UDP"
)

// PolicyType is the type of policy either PolicyTypeIngress or PolicyTypeEgress
Expand All @@ -22,6 +22,9 @@ type PolicyType string
// PolicyAction is Action needed to be performed for the given Rule
type PolicyAction string

// PolicyPortProtocol is the Port Protocol
type PolicyPortProtocol string

// InterfaceInfo holds information about the interface
type InterfaceInfo struct {
// Network is the network interfaceInfo is associated with
Expand All @@ -42,7 +45,7 @@ func (i *InterfaceInfo) GetUID() string {

// Port holds port information
type Port struct {
Protocol string
Protocol PolicyPortProtocol
Number uint16
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/tc/driver/cmdline/tc_cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"

"github.com/pkg/errors"
"k8s.io/utils/exec"
"k8s.io/klog/v2"
"k8s.io/utils/exec"

"github.com/Mellanox/multi-networkpolicy-tc/pkg/tc/types"
)
Expand Down Expand Up @@ -84,7 +84,7 @@ func (t *TcCmdLineImpl) QDiscList() ([]types.QDisc, error) {

var objs []types.QDisc
for _, q := range cQdiscs {
if q.Kind != types.QDiscIngressType {
if q.Kind != string(types.QDiscIngressType) {
// skip non-ingress qdiscs
continue
}
Expand Down Expand Up @@ -141,13 +141,13 @@ func (t *TcCmdLineImpl) FilterList(qdisc types.QDisc) ([]types.Filter, error) {
if f.Options == nil {
continue
}
if f.Kind != types.FilterKindFlower {
if f.Kind != string(types.FilterKindFlower) {
return nil, fmt.Errorf("unexpected filter Kind: %s", f.Kind)
}

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

Expand All @@ -163,10 +163,10 @@ func (t *TcCmdLineImpl) FilterList(qdisc types.QDisc) ([]types.Filter, error) {

for _, a := range f.Options.Actions {
// TODO(adrianc): sort first by Order, ATM only one action is expected
if a.Kind != types.ActionTypeGeneric {
if a.Kind != string(types.ActionTypeGeneric) {
return nil, fmt.Errorf("unexpected action: %s", a.Kind)
}
act := types.NewGenericAction(a.ControlAction.Type)
act := types.NewGenericAction(types.ActionGenericType(a.ControlAction.Type))
fb.WithAction(act)
}
objs = append(objs, fb.Build())
Expand Down
4 changes: 2 additions & 2 deletions pkg/tc/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *SimpleTCGenerator) genFilters(ipCidrs []*net.IPNet, ports []policyrules
WithProtocol(tctypes.FilterProtocolIP).
WithPriority(prio).
WithMatchKeyDstIP(ipCidr.String()).
WithMatchKeyIPProto(port.Protocol).
WithMatchKeyIPProto(string(port.Protocol)).
WithMatchKeyDstPort(port.Number).
WithAction(action).
Build())
Expand All @@ -141,7 +141,7 @@ func (s *SimpleTCGenerator) genFilters(ipCidrs []*net.IPNet, ports []policyrules
tctypes.NewFlowerFilterBuilder().
WithProtocol(tctypes.FilterProtocolIP).
WithPriority(prio).
WithMatchKeyIPProto(port.Protocol).
WithMatchKeyIPProto(string(port.Protocol)).
WithMatchKeyDstPort(port.Number).
WithAction(action).
Build())
Expand Down
28 changes: 18 additions & 10 deletions pkg/tc/types/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,51 @@ package types

const (
// Action type
ActionTypeGeneric = "gact"
ActionTypeGeneric ActionType = "gact"

// Generic control actions
ActionGenericPass = "pass"
ActionGenericDrop = "drop"
ActionGenericPass ActionGenericType = "pass"
ActionGenericDrop ActionGenericType = "drop"
)

// ActionType is the TC Action type
type ActionType string

// ActionGenericType is the Generic Action control action type
type ActionGenericType string

// Action is an interface which represents a TC action
type Action interface {
CmdLineGenerator
// Type returns the action type
Type() string
Type() ActionType
// Spec returns Action Specification
Spec() map[string]string
// Equals compares this Action with other, returns true if they are equal or false otherwise
Equals(other Action) bool

// Driver Specific related Interfaces
CmdLineGenerator
}

// NewGenericAction creates a new GenericAction
func NewGenericAction(controlAction string) *GenericAction {
func NewGenericAction(controlAction ActionGenericType) *GenericAction {
return &GenericAction{controlAction: controlAction}
}

// GenericAction is a struct representing TC generic action (gact)
type GenericAction struct {
controlAction string
controlAction ActionGenericType
}

// Type implements Action interface, it returns the type of the action
func (a *GenericAction) Type() string {
func (a *GenericAction) Type() ActionType {
return ActionTypeGeneric
}

// Spec implements Action interface, it returns the specification of the action
func (a *GenericAction) Spec() map[string]string {
m := make(map[string]string)
m["control_action"] = a.controlAction
m["control_action"] = string(a.controlAction)
return m
}

Expand All @@ -56,7 +64,7 @@ func (a *GenericAction) Equals(other Action) bool {

// GenCmdLineArgs implements CmdLineGenerator interface
func (a *GenericAction) GenCmdLineArgs() []string {
return []string{"action", ActionTypeGeneric, a.controlAction}
return []string{"action", string(ActionTypeGeneric), string(a.controlAction)}
}

// Builer
Expand Down
10 changes: 6 additions & 4 deletions pkg/tc/types/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import (

const (
// ChainDefaultParent is the default parent of a chain which is the ingress qdisc
ChainDefaultParent = 0xfffffff1
ChainDefaultParent uint32 = 0xfffffff1
// ChainDefaultChain is the default chain number
ChainDefaultChain = 0
ChainDefaultChain uint16 = 0
)

// Chain is an interface which represents a TC chain
type Chain interface {
CmdLineGenerator
// Attrs returns chain attributes
Attrs() *ChainAttrs

// Driver Specific related Interfaces
CmdLineGenerator
}

// ChainAttrs are the attributes of a Chain
Expand Down Expand Up @@ -87,7 +89,7 @@ func (cb *ChainBuilder) WithChain(chain uint16) *ChainBuilder {
// to create several objects, different builders should be used.
func (cb *ChainBuilder) Build() *ChainImpl {
if cb.chain.Chain == nil {
defChain := uint16(ChainDefaultChain)
defChain := ChainDefaultChain
cb.chain.Chain = &defChain

}
Expand Down
50 changes: 31 additions & 19 deletions pkg/tc/types/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,50 @@ import (

const (
// Values for FilterAttrs.Protocol
FilterProtocolAll = "all"
FilterProtocolIP = "ip" // note ip == ipv4
FilterProtocolAll FilterProtocol = "all"
FilterProtocolIP FilterProtocol = "ip" // note ip == ipv4

// FlowerFilter.Kind
FilterKindFlower = "flower"
FilterKindFlower FilterKind = "flower"

// FlowerKeys
FlowerKeyIPProto = "ip_proto"
FlowerKeyDstIP = "dst_ip"
FlowerKeyDstPort = "dst_port"
FlowerKeyIPProto FlowerKey = "ip_proto"
FlowerKeyDstIP FlowerKey = "dst_ip"
FlowerKeyDstPort FlowerKey = "dst_port"
)

// FilterProtocol is the type of filter protocol
type FilterProtocol string

// FilterKind is the type of filter
type FilterKind string

// FlowerKey is the type of flower key
type FlowerKey string

// Filter represent a tc filter object
type Filter interface {
CmdLineGenerator
// Attrs returns FilterAttrs
Attrs() *FilterAttrs
// Equals compares this Filter with other, returns true if they are equal or false otherwise
Equals(other Filter) bool

// Driver Specific related Interfaces
CmdLineGenerator
}

// FilterAttrs holds filter object attributes
type FilterAttrs struct {
Kind string
Protocol string
Kind FilterKind
Protocol FilterProtocol
Chain *uint16
Handle *uint32
Priority *uint16
}

// NewFilterAttrs creates new FilterAttrs instance
func NewFilterAttrs(kind string, protocol string, chain *uint16, handle *uint32, priority *uint16) *FilterAttrs {
func NewFilterAttrs(
kind FilterKind, protocol FilterProtocol, chain *uint16, handle *uint32, priority *uint16) *FilterAttrs {
return &FilterAttrs{
Kind: kind,
Protocol: protocol,
Expand All @@ -53,7 +65,7 @@ func (fa *FilterAttrs) GenCmdLineArgs() []string {
args := []string{}

if fa.Protocol != "" {
args = append(args, "protocol", fa.Protocol)
args = append(args, "protocol", string(fa.Protocol))
}

if fa.Handle != nil {
Expand All @@ -69,7 +81,7 @@ func (fa *FilterAttrs) GenCmdLineArgs() []string {
}

// must be last as next are filter type specific params
args = append(args, fa.Kind)
args = append(args, string(fa.Kind))

return args
}
Expand Down Expand Up @@ -116,15 +128,15 @@ func (ff *FlowerSpec) GenCmdLineArgs() []string {
}

if ff.IpProto != nil {
args = append(args, FlowerKeyIPProto, *ff.IpProto)
args = append(args, string(FlowerKeyIPProto), *ff.IpProto)
}

if ff.DstIP != nil {
args = append(args, FlowerKeyDstIP, *ff.DstIP)
args = append(args, string(FlowerKeyDstIP), *ff.DstIP)
}

if ff.DstPort != nil {
args = append(args, FlowerKeyDstPort, strconv.FormatUint(uint64(*ff.DstPort), 10))
args = append(args, string(FlowerKeyDstPort), strconv.FormatUint(uint64(*ff.DstPort), 10))
}

return args
Expand Down Expand Up @@ -229,13 +241,13 @@ type FilterAttrsBuilder struct {
}

// WithKind adds Kind to FilterAttrsBuilder
func (fb *FilterAttrsBuilder) WithKind(k string) *FilterAttrsBuilder {
func (fb *FilterAttrsBuilder) WithKind(k FilterKind) *FilterAttrsBuilder {
fb.filterAttrs.Kind = k
return fb
}

// WithProtocol adds Protocol to FilterAttrsBuilder
func (fb *FilterAttrsBuilder) WithProtocol(p string) *FilterAttrsBuilder {
func (fb *FilterAttrsBuilder) WithProtocol(p FilterProtocol) *FilterAttrsBuilder {
fb.filterAttrs.Protocol = p
return fb
}
Expand Down Expand Up @@ -284,13 +296,13 @@ type FlowerFilterBuilder struct {
}

// WithKind adds Kind to FlowerFilterBuilder
func (fb *FlowerFilterBuilder) WithKind(k string) *FlowerFilterBuilder {
func (fb *FlowerFilterBuilder) WithKind(k FilterKind) *FlowerFilterBuilder {
fb.filterAttrsBuilder = fb.filterAttrsBuilder.WithKind(k)
return fb
}

// WithProtocol adds Protocol to FlowerFilterBuilder
func (fb *FlowerFilterBuilder) WithProtocol(p string) *FlowerFilterBuilder {
func (fb *FlowerFilterBuilder) WithProtocol(p FilterProtocol) *FlowerFilterBuilder {
fb.filterAttrsBuilder = fb.filterAttrsBuilder.WithProtocol(p)
return fb
}
Expand Down
17 changes: 11 additions & 6 deletions pkg/tc/types/qdisc.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package types

const (
QDiscIngressType = "ingress"
QDiscIngressType QDiscType = "ingress"
)

// QDiscType is the type of qdisc
type QDiscType string

// QDiscAttr holds QDisc object attributes
type QDiscAttr struct {
Handle *uint32
Expand All @@ -12,17 +15,19 @@ type QDiscAttr struct {

// QDisc is an interface which represents a TC qdisc object
type QDisc interface {
CmdLineGenerator
// Attrs returns QDiscAttr for a qdisc
Attrs() *QDiscAttr
// Type returns the QDisc type
Type() string
Type() QDiscType

// Driver Specific related Interfaces
CmdLineGenerator
}

// GenericQDisc is a generic qdisc of an arbitrary type
type GenericQDisc struct {
QDiscAttr
QdiscType string
QdiscType QDiscType
}

// Attrs implements QDisc interface
Expand All @@ -31,14 +36,14 @@ func (g *GenericQDisc) Attrs() *QDiscAttr {
}

// Type implements QDisc interface
func (g *GenericQDisc) Type() string {
func (g *GenericQDisc) Type() QDiscType {
return g.QdiscType
}

// GenCmdLineArgs implements CmdLineGenerator interface
func (g *GenericQDisc) GenCmdLineArgs() []string {
// for now we can just use qdisc type without attrs (parent, handle)
return []string{QDiscIngressType}
return []string{string(g.QdiscType)}
}

// NewIngressQdisc creates a new Ingress QDisc object
Expand Down