From 4668a3dbcefc292b1142156cb844d61454e05210 Mon Sep 17 00:00:00 2001 From: Adrian Chiris Date: Tue, 26 Jul 2022 19:24:24 +0300 Subject: [PATCH] Add Type to string consts This commit adds type for string constants in tc and policyrules packages. Signed-off-by: Adrian Chiris --- pkg/policyrules/renderer.go | 2 +- pkg/policyrules/types.go | 17 ++++++---- pkg/tc/driver/cmdline/tc_cmdline.go | 12 +++---- pkg/tc/generator.go | 4 +-- pkg/tc/types/action.go | 28 ++++++++++------ pkg/tc/types/chain.go | 10 +++--- pkg/tc/types/filter.go | 50 ++++++++++++++++++----------- pkg/tc/types/qdisc.go | 17 ++++++---- 8 files changed, 85 insertions(+), 55 deletions(-) diff --git a/pkg/policyrules/renderer.go b/pkg/policyrules/renderer.go index 8443819..1060a8d 100644 --- a/pkg/policyrules/renderer.go +++ b/pkg/policyrules/renderer.go @@ -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) diff --git a/pkg/policyrules/types.go b/pkg/policyrules/types.go index 450ea05..83a1fb5 100644 --- a/pkg/policyrules/types.go +++ b/pkg/policyrules/types.go @@ -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 @@ -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 @@ -42,7 +45,7 @@ func (i *InterfaceInfo) GetUID() string { // Port holds port information type Port struct { - Protocol string + Protocol PolicyPortProtocol Number uint16 } diff --git a/pkg/tc/driver/cmdline/tc_cmdline.go b/pkg/tc/driver/cmdline/tc_cmdline.go index 1f955d8..5b847db 100644 --- a/pkg/tc/driver/cmdline/tc_cmdline.go +++ b/pkg/tc/driver/cmdline/tc_cmdline.go @@ -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" ) @@ -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 } @@ -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) @@ -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()) diff --git a/pkg/tc/generator.go b/pkg/tc/generator.go index 7adabdd..414e60d 100644 --- a/pkg/tc/generator.go +++ b/pkg/tc/generator.go @@ -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()) @@ -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()) diff --git a/pkg/tc/types/action.go b/pkg/tc/types/action.go index 9840209..30334b9 100644 --- a/pkg/tc/types/action.go +++ b/pkg/tc/types/action.go @@ -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 } @@ -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 diff --git a/pkg/tc/types/chain.go b/pkg/tc/types/chain.go index 7b98992..60853dc 100644 --- a/pkg/tc/types/chain.go +++ b/pkg/tc/types/chain.go @@ -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 @@ -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 } diff --git a/pkg/tc/types/filter.go b/pkg/tc/types/filter.go index de97c7f..610fb0b 100644 --- a/pkg/tc/types/filter.go +++ b/pkg/tc/types/filter.go @@ -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, @@ -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 { @@ -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 } @@ -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 @@ -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 } @@ -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 } diff --git a/pkg/tc/types/qdisc.go b/pkg/tc/types/qdisc.go index e4e906d..233200e 100644 --- a/pkg/tc/types/qdisc.go +++ b/pkg/tc/types/qdisc.go @@ -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 @@ -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 @@ -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