@@ -21,6 +21,41 @@ import (
21
21
"github.com/projectcalico/calico/goldmane/proto"
22
22
)
23
23
24
+ const (
25
+ pub = "pub"
26
+ pvt = "pvt"
27
+ global = "Global"
28
+ )
29
+
30
+ // The UI displays some values differently than they are stored within Goldmane. As such,
31
+ // users may sends filters for the UI displayed values, but we need to match against the
32
+ // actual stored values. For example, the UI displays "PUBLIC NETWORK" but the stored value
33
+ // is "pub".
34
+ func names (valFn func () string ) func () []string {
35
+ return func () []string {
36
+ n := valFn ()
37
+ switch n {
38
+ case pub :
39
+ return []string {"PUBLIC NETWORK" , pub }
40
+ case pvt :
41
+ return []string {"PRIVATE NETWORK" , pvt }
42
+
43
+ }
44
+ return []string {n }
45
+ }
46
+ }
47
+
48
+ func namespaces (valFn func () string ) func () []string {
49
+ return func () []string {
50
+ n := valFn ()
51
+ switch n {
52
+ case global :
53
+ return []string {"-" , global }
54
+ }
55
+ return []string {n }
56
+ }
57
+ }
58
+
24
59
// Matches returns true if the given flow Matches the given filter.
25
60
func Matches (filter * proto.Filter , key * FlowKey ) bool {
26
61
if filter == nil {
@@ -29,11 +64,11 @@ func Matches(filter *proto.Filter, key *FlowKey) bool {
29
64
}
30
65
31
66
comps := []matcher {
32
- & stringComparison {filter : filter .SourceNames , valFn : key .SourceName },
33
- & stringComparison {filter : filter .DestNames , valFn : key .DestName },
34
- & stringComparison {filter : filter .SourceNamespaces , valFn : key .SourceNamespace },
35
- & stringComparison {filter : filter .DestNamespaces , valFn : key .DestNamespace },
36
- & stringComparison {filter : filter .Protocols , valFn : key .Proto },
67
+ & stringComparison {filter : filter .SourceNames , genVals : names ( key .SourceName ) },
68
+ & stringComparison {filter : filter .DestNames , genVals : names ( key .DestName ) },
69
+ & stringComparison {filter : filter .SourceNamespaces , genVals : namespaces ( key .SourceNamespace ) },
70
+ & stringComparison {filter : filter .DestNamespaces , genVals : namespaces ( key .DestNamespace ) },
71
+ & stringComparison {filter : filter .Protocols , genVals : func () [] string { return [] string { key .Proto ()} } },
37
72
& actionMatch {filter : filter .Actions , key : key },
38
73
& portComparison {filter : filter .DestPorts , key : key },
39
74
& policyComparison {filter : filter .Policies , key : key },
@@ -86,7 +121,10 @@ func (p *portComparison) matches() bool {
86
121
87
122
type stringComparison struct {
88
123
filter []* proto.StringMatch
89
- valFn func () string
124
+
125
+ // genVals returns a list of values on the underlying object to compare against the filter.
126
+ // If any of the values match, the comparison is considered a match.
127
+ genVals func () []string
90
128
}
91
129
92
130
func (c stringComparison ) matches () bool {
@@ -99,14 +137,19 @@ func (c stringComparison) matches() bool {
99
137
}
100
138
101
139
func (c stringComparison ) matchFilter (filter * proto.StringMatch ) bool {
102
- val := c .valFn ()
140
+ vals := c .genVals ()
103
141
104
142
if filter .Type == proto .MatchType_Exact {
105
- return val == filter .Value
143
+ return slices . Contains ( vals , filter .Value )
106
144
}
107
145
108
146
// Match type is not exact, so we need to do a substring match.
109
- return strings .Contains (val , filter .Value )
147
+ for _ , val := range vals {
148
+ if strings .Contains (val , filter .Value ) {
149
+ return true
150
+ }
151
+ }
152
+ return false
110
153
}
111
154
112
155
type policyComparison struct {
0 commit comments