Skip to content

Commit f4558c8

Browse files
authored
Merge pull request #241 from huntergregory/display-enhancements
[Policy Assistant] display enhancements from KubeCon demo (PR 1/2)
2 parents a8b203d + ec80379 commit f4558c8

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

cmd/policy-assistant/pkg/connectivity/probe/connectivity.go

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const (
1111
ConnectivityInvalidPortProtocol Connectivity = "invalidportprotocol"
1212
ConnectivityBlocked Connectivity = "blocked"
1313
ConnectivityAllowed Connectivity = "allowed"
14+
// ConnectivityUndefined e.g. for loopback traffic
15+
ConnectivityUndefined Connectivity = "undefined"
1416
)
1517

1618
var AllConnectivity = []Connectivity{
@@ -36,6 +38,8 @@ func (p Connectivity) ShortString() string {
3638
return "P"
3739
case ConnectivityInvalidPortProtocol:
3840
return "N"
41+
case ConnectivityUndefined:
42+
return "#"
3943
default:
4044
panic(errors.Errorf("invalid Connectivity value: %+v", p))
4145
}

cmd/policy-assistant/pkg/connectivity/probe/jobrunner.go

+5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ func (s *SimulatedJobRunner) RunJobs(jobs []*Job) []*JobResult {
7676
}
7777

7878
func (s *SimulatedJobRunner) RunJob(job *Job) *JobResult {
79+
if job.FromKey == job.ToKey {
80+
connUndefined := ConnectivityUndefined
81+
return &JobResult{Job: job, Ingress: &connUndefined, Egress: &connUndefined, Combined: ConnectivityUndefined}
82+
}
83+
7984
allowed := s.Policies.IsTrafficAllowed(job.Traffic())
8085
// TODO could also keep the whole `allowed` struct somewhere
8186

cmd/policy-assistant/pkg/matcher/explain.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (p *peerProtocolGroup) Matches(subject, peer *TrafficPeer, portInt int, por
2727
}
2828

2929
type anpGroup struct {
30-
name string
30+
ruleName string
3131
priority int
3232
effects []string
3333
kind PolicyKind
@@ -55,8 +55,10 @@ func (p *Policy) ExplainTable() string {
5555
ingresses, egresses := p.SortedTargets()
5656
builder.TargetsTableLines(ingresses, true)
5757

58-
builder.Elements = append(builder.Elements, []string{"", "", "", "", "", ""})
59-
builder.TargetsTableLines(egresses, false)
58+
if len(egresses) > 0 {
59+
builder.Elements = append(builder.Elements, []string{"", "", "", "", "", ""})
60+
builder.TargetsTableLines(egresses, false)
61+
}
6062

6163
table.AppendBulk(builder.Elements)
6264

@@ -132,9 +134,9 @@ func (s *SliceBuilder) peerProtocolGroupTableLines(t *peerProtocolGroup) {
132134
})
133135
for _, v := range anps {
134136
if len(v.effects) > 1 {
135-
actions = append(actions, fmt.Sprintf(" pri=%d (%s): %s (ineffective rules: %s)", v.priority, v.name, v.effects[0], strings.Join(v.effects[1:], ", ")))
137+
actions = append(actions, fmt.Sprintf(" pri=%d (%s): %s (ineffective rules: %s)", v.priority, v.ruleName, v.effects[0], strings.Join(v.effects[1:], ", ")))
136138
} else {
137-
actions = append(actions, fmt.Sprintf(" pri=%d (%s): %s", v.priority, v.name, v.effects[0]))
139+
actions = append(actions, fmt.Sprintf(" pri=%d (%s): %s", v.priority, v.ruleName, v.effects[0]))
138140
}
139141
}
140142
}
@@ -202,10 +204,10 @@ func groupAnbAndBanp(p []PeerMatcher) []PeerMatcher {
202204
policies: map[string]*anpGroup{},
203205
}
204206
}
205-
kg := t.Name
207+
kg := t.PolicyName
206208
if _, ok := groups[k].policies[kg]; !ok {
207209
groups[k].policies[kg] = &anpGroup{
208-
name: t.Name,
210+
ruleName: t.RuleName,
209211
priority: t.effectFromMatch.Priority,
210212
effects: []string{},
211213
kind: t.effectFromMatch.PolicyKind,

cmd/policy-assistant/pkg/matcher/peermatcherv2.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ import (
1010
// This is because ANP and BANP only deal with Pod to Pod traffic, and do not deal with external IPs.
1111
type PeerMatcherAdmin struct {
1212
*PodPeerMatcher
13-
Name string
13+
PolicyName string
14+
RuleName string
1415
effectFromMatch Effect
1516
}
1617

1718
// NewPeerMatcherANP creates a PeerMatcherAdmin for an ANP rule
18-
func NewPeerMatcherANP(peer *PodPeerMatcher, v Verdict, priority int, source string) *PeerMatcherAdmin {
19+
func NewPeerMatcherANP(peer *PodPeerMatcher, v Verdict, priority int, policyName, ruleName string) *PeerMatcherAdmin {
1920
return &PeerMatcherAdmin{
2021
PodPeerMatcher: peer,
21-
Name: source,
22+
PolicyName: policyName,
23+
RuleName: ruleName,
2224
effectFromMatch: Effect{
25+
RuleName: ruleName,
2326
PolicyKind: AdminNetworkPolicy,
2427
Priority: priority,
2528
Verdict: v,
@@ -28,11 +31,13 @@ func NewPeerMatcherANP(peer *PodPeerMatcher, v Verdict, priority int, source str
2831
}
2932

3033
// NewPeerMatcherBANP creates a new PeerMatcherAdmin for a BANP rule
31-
func NewPeerMatcherBANP(peer *PodPeerMatcher, v Verdict, source string) *PeerMatcherAdmin {
34+
func NewPeerMatcherBANP(peer *PodPeerMatcher, v Verdict, policyName, ruleName string) *PeerMatcherAdmin {
3235
return &PeerMatcherAdmin{
3336
PodPeerMatcher: peer,
34-
Name: source,
37+
PolicyName: policyName,
38+
RuleName: ruleName,
3539
effectFromMatch: Effect{
40+
RuleName: ruleName,
3641
PolicyKind: BaselineAdminNetworkPolicy,
3742
Verdict: v,
3843
},
@@ -41,6 +46,7 @@ func NewPeerMatcherBANP(peer *PodPeerMatcher, v Verdict, source string) *PeerMat
4146

4247
// Effect models the effect of one or more v1/v2 NetPol rules on a peer
4348
type Effect struct {
49+
RuleName string
4450
PolicyKind
4551
// Priority is only used for ANP (there can only be one BANP)
4652
Priority int
@@ -57,9 +63,9 @@ const (
5763

5864
func NewV1Effect(allow bool) Effect {
5965
if allow {
60-
return Effect{NetworkPolicyV1, 0, Allow}
66+
return Effect{"", NetworkPolicyV1, 0, Allow}
6167
}
62-
return Effect{NetworkPolicyV1, 0, None}
68+
return Effect{"", NetworkPolicyV1, 0, None}
6369
}
6470

6571
type Verdict string

cmd/policy-assistant/pkg/matcher/policy.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -120,35 +120,35 @@ func (d DirectionResult) Flow() string {
120120
flows := make([]string, 0)
121121
if anp != nil {
122122
if anp.Verdict == Allow {
123-
return "[ANP] Allow"
123+
return fmt.Sprintf("[ANP] Allow (%s)", anp.RuleName)
124124
}
125125

126126
if anp.Verdict == Deny {
127-
return "[ANP] Deny"
127+
return fmt.Sprintf("[ANP] Deny (%s)", anp.RuleName)
128128
}
129129

130130
if anp.Verdict == Pass {
131-
flows = append(flows, "[ANP] Pass")
131+
flows = append(flows, fmt.Sprintf("[ANP] Pass (%s)", anp.RuleName))
132132
} else {
133133
flows = append(flows, "[ANP] No-Op")
134134
}
135135
}
136136

137137
if npv1 != nil {
138138
if npv1.Verdict == Allow {
139-
flows = append(flows, "[NPv1] Allow")
139+
flows = append(flows, fmt.Sprintf("[NPv1] Allow (%s)", npv1.RuleName))
140140
} else {
141-
flows = append(flows, "[NPv1] Dropped")
141+
flows = append(flows, fmt.Sprintf("[NPv1] Dropped (%s)", npv1.RuleName))
142142
}
143143

144144
return strings.Join(flows, " -> ")
145145
}
146146

147147
if banp != nil {
148148
if banp.Verdict == Allow {
149-
flows = append(flows, "[BANP] Allow")
149+
flows = append(flows, fmt.Sprintf("[BANP] Allow (%s)", banp.RuleName))
150150
} else if banp.Verdict == Deny {
151-
flows = append(flows, "[BANP] Deny")
151+
flows = append(flows, fmt.Sprintf("[BANP] Deny (%s)", banp.RuleName))
152152
} else {
153153
flows = append(flows, "[BANP] No-Op")
154154
}

0 commit comments

Comments
 (0)