Skip to content

Commit faa564f

Browse files
committed
sort more tables for explain output
1 parent 69308e9 commit faa564f

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

pkg/matcher/policy.go

+12-24
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package matcher
22

33
import (
44
"fmt"
5+
"github.com/mattfenwick/collections/pkg/builtins"
6+
"github.com/mattfenwick/collections/pkg/slices"
57
"github.com/mattfenwick/cyclonus/pkg/kube"
68
"github.com/olekukonko/tablewriter"
7-
"sort"
9+
"golang.org/x/exp/maps"
810
"strings"
911
)
1012

@@ -26,19 +28,9 @@ func NewPolicyWithTargets(ingress []*Target, egress []*Target) *Policy {
2628
}
2729

2830
func (p *Policy) SortedTargets() ([]*Target, []*Target) {
29-
var ingress, egress []*Target
30-
for _, rule := range p.Ingress {
31-
ingress = append(ingress, rule)
32-
}
33-
sort.Slice(ingress, func(i, j int) bool {
34-
return ingress[i].GetPrimaryKey() < ingress[j].GetPrimaryKey()
35-
})
36-
for _, rule := range p.Egress {
37-
egress = append(egress, rule)
38-
}
39-
sort.Slice(egress, func(i, j int) bool {
40-
return egress[i].GetPrimaryKey() < egress[j].GetPrimaryKey()
41-
})
31+
key := func(t *Target) string { return t.GetPrimaryKey() }
32+
ingress := slices.SortOnBy(key, builtins.CompareOrdered[string], maps.Values(p.Ingress))
33+
egress := slices.SortOnBy(key, builtins.CompareOrdered[string], maps.Values(p.Egress))
4234
return ingress, egress
4335
}
4436

@@ -114,7 +106,8 @@ func (ar *AllowedResult) Table() string {
114106
}
115107

116108
func addTargetsToTable(table *tablewriter.Table, ruleType string, action string, targets []*Target) {
117-
for _, t := range targets {
109+
sortedTargets := slices.SortOnBy(func(t *Target) string { return t.GetPrimaryKey() }, builtins.CompareOrdered[string], targets)
110+
for _, t := range sortedTargets {
118111
targetString := fmt.Sprintf("namespace: %s\n%s", t.Namespace, kube.LabelSelectorTableLines(t.PodSelector))
119112
table.Append([]string{ruleType, action, targetString})
120113
}
@@ -160,15 +153,10 @@ func (p *Policy) IsIngressOrEgressAllowed(traffic *Traffic, isIngress bool) *Dir
160153
}
161154

162155
// 3. Check if any matching targets allow this traffic
163-
var allowers []*Target
164-
var deniers []*Target
165-
for _, target := range matchingTargets {
166-
if target.Allows(peer, traffic.ResolvedPort, traffic.ResolvedPortName, traffic.Protocol) {
167-
allowers = append(allowers, target)
168-
} else {
169-
deniers = append(deniers, target)
170-
}
171-
}
156+
pair := slices.Partition(func(t *Target) bool {
157+
return t.Allows(peer, traffic.ResolvedPort, traffic.ResolvedPortName, traffic.Protocol)
158+
}, matchingTargets)
159+
allowers, deniers := pair.Fst, pair.Snd
172160

173161
return &DirectionResult{AllowingTargets: allowers, DenyingTargets: deniers}
174162
}

pkg/matcher/traffic.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package matcher
22

33
import (
44
"fmt"
5+
"github.com/mattfenwick/collections/pkg/builtins"
6+
"github.com/mattfenwick/collections/pkg/slices"
57
"github.com/olekukonko/tablewriter"
8+
"golang.org/x/exp/maps"
69
v1 "k8s.io/api/core/v1"
710
"strings"
811
)
@@ -48,11 +51,8 @@ func (t *Traffic) Table() string {
4851
}
4952

5053
func labelsToString(labels map[string]string) string {
51-
var kvs []string
52-
for k, v := range labels {
53-
kvs = append(kvs, fmt.Sprintf("%s: %s", k, v))
54-
}
55-
return strings.Join(kvs, "\n")
54+
format := func(k string) string { return fmt.Sprintf("%s: %s", k, labels[k]) }
55+
return strings.Join(slices.Map(format, slices.SortBy(builtins.CompareOrdered[string], maps.Keys(labels))), "\n")
5656
}
5757

5858
type TrafficPeer struct {

0 commit comments

Comments
 (0)