Skip to content

Commit

Permalink
Normalise selectors.
Browse files Browse the repository at this point in the history
  • Loading branch information
fasaxc committed Dec 6, 2023
1 parent 2398b53 commit 663aac1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
18 changes: 16 additions & 2 deletions felix/calc/rule_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,15 @@ func (rs *RuleScanner) OnProfileInactive(key model.ProfileRulesKey) {
}

func (rs *RuleScanner) OnPolicyActive(key model.PolicyKey, policy *model.Policy) {
parsedRules := rs.updateRules(key, policy.InboundRules, policy.OutboundRules, policy.DoNotTrack, policy.PreDNAT, policy.Namespace, policy.Selector)
parsedRules := rs.updateRules(
key,
policy.InboundRules,
policy.OutboundRules,
policy.DoNotTrack,
policy.PreDNAT,
policy.Namespace,
selector.Normalise(policy.Selector),
)
rs.RulesUpdateCallbacks.OnPolicyActive(key, parsedRules)
}

Expand All @@ -191,7 +199,13 @@ func (rs *RuleScanner) OnPolicyInactive(key model.PolicyKey) {
rs.RulesUpdateCallbacks.OnPolicyInactive(key)
}

func (rs *RuleScanner) updateRules(key interface{}, inbound, outbound []model.Rule, untracked, preDNAT bool, origNamespace string, origSelector string) (parsedRules *ParsedRules) {
func (rs *RuleScanner) updateRules(
key interface{},
inbound, outbound []model.Rule,
untracked, preDNAT bool,
origNamespace string,
origSelector string,
) (parsedRules *ParsedRules) {
log.Debugf("Scanning rules (%v in, %v out) for key %v",
len(inbound), len(outbound), key)
// Extract all the new selectors/named ports.
Expand Down
8 changes: 5 additions & 3 deletions felix/calc/rule_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ var _ = DescribeTable("RuleScanner rule conversion should generate correct Parse
Namespace: "namespace",
InboundRules: []model.Rule{modelRule},
OutboundRules: []model.Rule{},
Selector: "a == 'A' ",
}
rs.OnPolicyActive(policyKey, policy)
Expect(ur.activeRules).To(Equal(map[model.Key]*ParsedRules{
policyKey: {
Namespace: "namespace",
InboundRules: []*ParsedRule{&expectedParsedRule},
OutboundRules: []*ParsedRule{},
Namespace: "namespace",
InboundRules: []*ParsedRule{&expectedParsedRule},
OutboundRules: []*ParsedRule{},
OriginalSelector: "a == \"A\"",
},
}))
rs.OnPolicyInactive(policyKey)
Expand Down
2 changes: 0 additions & 2 deletions felix/dataplane/linux/endpoint_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,6 @@ func (m *endpointManager) OnUpdate(protoBufMsg interface{}) {
}
m.hostEndpointsDirty = true
case *proto.ActivePolicyUpdate:
// FIXME normalise selectors somewhere? Here on in calc graph.
// want "" to be equal to "all()" and for whitespace to be ignored.
newSel := msg.Policy.OriginalSelector
if oldSel, ok := m.activePolicySelectors[*msg.Id]; ok && oldSel == newSel {
// No change that we care about.
Expand Down
18 changes: 17 additions & 1 deletion libcalico-go/lib/selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package selector

import "github.com/projectcalico/calico/libcalico-go/lib/selector/parser"
import (
"strings"

"github.com/projectcalico/calico/libcalico-go/lib/selector/parser"
)

// Selector represents a label selector.
type Selector interface {
Expand All @@ -38,3 +42,15 @@ type Selector interface {
func Parse(selector string) (sel Selector, err error) {
return parser.Parse(selector)
}

// Normalise converts the given selector to the form returned by
// Selector.String(), i.e. "" is converted to "all()" and whitespace is
// tidied up. If the input string cannot be parsed, it is returned unaltered.
func Normalise(selector string) string {
selector = strings.TrimSpace(selector)
parsed, err := Parse(selector)
if err != nil {
return selector
}
return parsed.String()
}

0 comments on commit 663aac1

Please sign in to comment.