Skip to content

Commit 1978a22

Browse files
committed
style: PeerMatcherAdmin using only PodPeerMatcher + minor UI changes
1 parent 4649ace commit 1978a22

File tree

7 files changed

+48
-41
lines changed

7 files changed

+48
-41
lines changed

cmd/cyclonus/pkg/matcher/builder.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ func BuildTargetANP(anp *v1alpha1.AdminNetworkPolicy) (*Target, *Target) {
224224
v := AdminActionToVerdict(r.Action)
225225
matchers := BuildPeerMatcherAdmin(r.From, r.Ports)
226226
for _, m := range matchers {
227-
matcherV2 := NewPeerMatcherANP(m, v, int(anp.Spec.Priority))
228-
ingress.Peers = append(ingress.Peers, matcherV2)
227+
matcherAdmin := NewPeerMatcherANP(m, v, int(anp.Spec.Priority))
228+
ingress.Peers = append(ingress.Peers, matcherAdmin)
229229
}
230230
}
231231
}
@@ -240,8 +240,8 @@ func BuildTargetANP(anp *v1alpha1.AdminNetworkPolicy) (*Target, *Target) {
240240
v := AdminActionToVerdict(r.Action)
241241
matchers := BuildPeerMatcherAdmin(r.To, r.Ports)
242242
for _, m := range matchers {
243-
matcherV2 := NewPeerMatcherANP(m, v, int(anp.Spec.Priority))
244-
egress.Peers = append(egress.Peers, matcherV2)
243+
matcherAdmin := NewPeerMatcherANP(m, v, int(anp.Spec.Priority))
244+
egress.Peers = append(egress.Peers, matcherAdmin)
245245
}
246246
}
247247
}
@@ -267,8 +267,8 @@ func BuildTargetBANP(banp *v1alpha1.BaselineAdminNetworkPolicy) (*Target, *Targe
267267
v := BaselineAdminActionToVerdict(r.Action)
268268
matchers := BuildPeerMatcherAdmin(r.From, r.Ports)
269269
for _, m := range matchers {
270-
matcherV2 := NewPeerMatcherBANP(m, v)
271-
ingress.Peers = append(ingress.Peers, matcherV2)
270+
matcherAdmin := NewPeerMatcherBANP(m, v)
271+
ingress.Peers = append(ingress.Peers, matcherAdmin)
272272
}
273273
}
274274
}
@@ -283,16 +283,16 @@ func BuildTargetBANP(banp *v1alpha1.BaselineAdminNetworkPolicy) (*Target, *Targe
283283
v := BaselineAdminActionToVerdict(r.Action)
284284
matchers := BuildPeerMatcherAdmin(r.To, r.Ports)
285285
for _, m := range matchers {
286-
matcherV2 := NewPeerMatcherBANP(m, v)
287-
egress.Peers = append(egress.Peers, matcherV2)
286+
matcherAdmin := NewPeerMatcherBANP(m, v)
287+
egress.Peers = append(egress.Peers, matcherAdmin)
288288
}
289289
}
290290
}
291291

292292
return ingress, egress
293293
}
294294

295-
func BuildPeerMatcherAdmin(peers []v1alpha1.AdminNetworkPolicyPeer, ports *[]v1alpha1.AdminNetworkPolicyPort) []PeerMatcher {
295+
func BuildPeerMatcherAdmin(peers []v1alpha1.AdminNetworkPolicyPeer, ports *[]v1alpha1.AdminNetworkPolicyPort) []*PodPeerMatcher {
296296
if len(peers) == 0 {
297297
panic(errors.Errorf("invalid admin to/from field: must have at least one peer"))
298298
}
@@ -306,7 +306,7 @@ func BuildPeerMatcherAdmin(peers []v1alpha1.AdminNetworkPolicyPeer, ports *[]v1a
306306
}
307307

308308
// 2. build Peers
309-
var peerMatchers []PeerMatcher
309+
var peerMatchers []*PodPeerMatcher
310310
for _, peer := range peers {
311311
if (peer.Namespaces == nil && peer.Pods == nil) || (peer.Namespaces != nil && peer.Pods != nil) {
312312
panic(errors.Errorf("invalid admin peer: must have exactly one of Namespaces or Pods"))

cmd/cyclonus/pkg/matcher/explain.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,17 @@ func (s *SliceBuilder) TargetsTableLines(targets []*Target, isIngress bool) {
6363
} else {
6464
for _, peer := range slice.SortOn(func(p PeerMatcher) string { return json.MustMarshalToString(p) }, target.Peers) {
6565
switch a := peer.(type) {
66+
case *PeerMatcherAdmin:
67+
s.PodPeerMatcherTableLines(a.PodPeerMatcher, a.effectFromMatch)
6668
case *AllPeersMatcher:
6769
s.Append("all pods, all ips", "all ports, all protocols")
6870
case *PortsForAllPeersMatcher:
69-
pps := PortMatcherTableLines(a.Port)
71+
pps := PortMatcherTableLines(a.Port, NetworkPolicyV1)
7072
s.Append("all pods, all ips", strings.Join(pps, "\n"))
7173
case *IPPeerMatcher:
7274
s.IPPeerMatcherTableLines(a)
7375
case *PodPeerMatcher:
74-
s.PodPeerMatcherTableLines(a)
76+
s.PodPeerMatcherTableLines(a, NewV1Effect(true))
7577
default:
7678
panic(errors.Errorf("invalid PeerMatcher type %T", a))
7779
}
@@ -82,12 +84,12 @@ func (s *SliceBuilder) TargetsTableLines(targets []*Target, isIngress bool) {
8284

8385
func (s *SliceBuilder) IPPeerMatcherTableLines(ip *IPPeerMatcher) {
8486
peer := ip.IPBlock.CIDR + "\n" + fmt.Sprintf("except %+v", ip.IPBlock.Except)
85-
pps := PortMatcherTableLines(ip.Port)
87+
pps := PortMatcherTableLines(ip.Port, NetworkPolicyV1)
8688
s.Append(peer, strings.Join(pps, "\n"))
8789
}
8890

89-
func (s *SliceBuilder) PodPeerMatcherTableLines(nsPodMatcher *PodPeerMatcher) {
90-
// FIXME add action/priority column
91+
func (s *SliceBuilder) PodPeerMatcherTableLines(nsPodMatcher *PodPeerMatcher, e Effect) {
92+
// FIXME add action/priority column using e
9193
var namespaces string
9294
switch ns := nsPodMatcher.Namespace.(type) {
9395
case *AllNamespaceMatcher:
@@ -109,10 +111,10 @@ func (s *SliceBuilder) PodPeerMatcherTableLines(nsPodMatcher *PodPeerMatcher) {
109111
default:
110112
panic(errors.Errorf("invalid PodMatcher type %T", p))
111113
}
112-
s.Append("namespace: "+namespaces+"\n"+"pods: "+pods, strings.Join(PortMatcherTableLines(nsPodMatcher.Port), "\n"))
114+
s.Append("namespace: "+namespaces+"\n"+"pods: "+pods, strings.Join(PortMatcherTableLines(nsPodMatcher.Port, e.PolicyKind), "\n"))
113115
}
114116

115-
func PortMatcherTableLines(pm PortMatcher) []string {
117+
func PortMatcherTableLines(pm PortMatcher, kind PolicyKind) []string {
116118
switch port := pm.(type) {
117119
case *AllPortMatcher:
118120
return []string{"all ports, all protocols"}
@@ -122,7 +124,11 @@ func PortMatcherTableLines(pm PortMatcher) []string {
122124
if portProtocol.Port == nil {
123125
lines = append(lines, "all ports on protocol "+string(portProtocol.Protocol))
124126
} else if portProtocol.Port.StrVal != "" {
125-
lines = append(lines, fmt.Sprintf("namedport '%s'", portProtocol.Port.StrVal))
127+
if kind == NetworkPolicyV1 {
128+
lines = append(lines, fmt.Sprintf("namedport '%s' on protocol %s", portProtocol.Port.StrVal, portProtocol.Protocol))
129+
} else {
130+
lines = append(lines, fmt.Sprintf("namedport '%s'", portProtocol.Port.StrVal))
131+
}
126132
} else {
127133
lines = append(lines, fmt.Sprintf("port %d on protocol %s", portProtocol.Port.IntVal, portProtocol.Protocol))
128134
}

cmd/cyclonus/pkg/matcher/peermatcher.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ These are the original PeerMatcher implementations made for v1 NetPol:
1919
- IPPeerMatcher
2020
- PodPeerMatcher
2121
22-
Now we also have PeerMatcherV2, a wrapper for the above to model ANP and BANP,
23-
as well as NamespaceMatcher objects for SameLabels and NotSameLabels.
24-
25-
All of these (except AllPeersMatcher) use a PortMatcher.
22+
All PeerMatcher implementations (except AllPeersMatcher) use a PortMatcher.
2623
If the traffic doesn't match the port matcher, then Matches() will be false.
24+
25+
Now we also have PeerMatcherAdmin, a wrapper for PodPeerMatcher to model ANP and BANP.
26+
We also made NamespaceMatcher objects for SameLabels and NotSameLabels.
2727
*/
2828
type PeerMatcher interface {
2929
Matches(subject, peer *TrafficPeer, portInt int, portName string, protocol v1.Protocol) bool

cmd/cyclonus/pkg/matcher/peermatcherv2.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import (
55
"sigs.k8s.io/network-policy-api/apis/v1alpha1"
66
)
77

8-
// PeerMatcherV2 models an ANP or BANP rule, incorporating an ANP/BANP action and an ANP priority.
9-
// NOTE: best approach right now is to only use PodPeerMatcher as the PeerMatcher.
10-
type PeerMatcherV2 struct {
11-
PeerMatcher
8+
// PeerMatcherAdmin models an ANP or BANP rule, incorporating an ANP/BANP action and an ANP priority.
9+
// NOTE: we only use the PodPeerMatcher out of all the PeerMatcher imlementations.
10+
// This is because ANP and BANP only deal with Pod to Pod traffic, and do not deal with external IPs.
11+
type PeerMatcherAdmin struct {
12+
*PodPeerMatcher
1213
effectFromMatch Effect
1314
}
1415

15-
// NewPeerMatcherANP creates a PeerMatcherV2 for an ANP rule
16-
func NewPeerMatcherANP(peer PeerMatcher, v Verdict, priority int) *PeerMatcherV2 {
17-
return &PeerMatcherV2{
18-
PeerMatcher: peer,
16+
// NewPeerMatcherANP creates a PeerMatcherAdmin for an ANP rule
17+
func NewPeerMatcherANP(peer *PodPeerMatcher, v Verdict, priority int) *PeerMatcherAdmin {
18+
return &PeerMatcherAdmin{
19+
PodPeerMatcher: peer,
1920
effectFromMatch: Effect{
2021
PolicyKind: AdminNetworkPolicy,
2122
Priority: priority,
@@ -24,10 +25,10 @@ func NewPeerMatcherANP(peer PeerMatcher, v Verdict, priority int) *PeerMatcherV2
2425
}
2526
}
2627

27-
// NewPeerMatcherBANP creates a new PeerMatcherV2 for a BANP rule
28-
func NewPeerMatcherBANP(peer PeerMatcher, v Verdict) *PeerMatcherV2 {
29-
return &PeerMatcherV2{
30-
PeerMatcher: peer,
28+
// NewPeerMatcherBANP creates a new PeerMatcherAdmin for a BANP rule
29+
func NewPeerMatcherBANP(peer *PodPeerMatcher, v Verdict) *PeerMatcherAdmin {
30+
return &PeerMatcherAdmin{
31+
PodPeerMatcher: peer,
3132
effectFromMatch: Effect{
3233
PolicyKind: BaselineAdminNetworkPolicy,
3334
Verdict: v,

cmd/cyclonus/pkg/matcher/policy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,11 @@ func (p *Policy) IsIngressOrEgressAllowed(traffic *Traffic, isIngress bool) Dire
301301
effects := make([]Effect, 0)
302302
for _, target := range matchingTargets {
303303
for _, m := range target.Peers {
304-
// check if m is a PeerMatcherV2
304+
// check if m is a PeerMatcherAdmin
305305
e := NewV1Effect(true)
306-
matcherV2, ok := m.(*PeerMatcherV2)
306+
matcherAdmin, ok := m.(*PeerMatcherAdmin)
307307
if ok {
308-
e = matcherV2.effectFromMatch
308+
e = matcherAdmin.effectFromMatch
309309
}
310310

311311
if !m.Matches(subject, peer, traffic.ResolvedPort, traffic.ResolvedPortName, traffic.Protocol) {

cmd/cyclonus/pkg/matcher/simplifier.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ func Simplify(matchers []PeerMatcher) []PeerMatcher {
1515

1616
result := make([]PeerMatcher, 0)
1717
for _, m := range matchers {
18-
if matcherV2, ok := m.(*PeerMatcherV2); ok {
19-
result = append(result, matcherV2)
18+
if matcherAdmin, ok := m.(*PeerMatcherAdmin); ok {
19+
result = append(result, matcherAdmin)
2020
}
2121
}
2222

@@ -28,7 +28,7 @@ func Simplify(matchers []PeerMatcher) []PeerMatcher {
2828
func SimplifyV1(matchers []PeerMatcher) []PeerMatcher {
2929
v1Matchers := make([]PeerMatcher, 0)
3030
for _, m := range matchers {
31-
if _, ok := m.(*PeerMatcherV2); !ok {
31+
if _, ok := m.(*PeerMatcherAdmin); !ok {
3232
v1Matchers = append(v1Matchers, m)
3333
}
3434
}

cmd/cyclonus/pkg/matcher/target.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func (s *SubjectAdmin) Matches(candidate *InternalPeer) bool {
172172

173173
func (s *SubjectAdmin) TargetString() string {
174174
// FIXME
175-
return "FIXME: implement target string for admin network policies"
175+
return "FIXME: implement target string like v1's except it supports namespace selector and (not) same labels"
176176
}
177177

178178
func (s *SubjectAdmin) GetPrimaryKey() string {

0 commit comments

Comments
 (0)