Skip to content

Commit

Permalink
Merge pull request #376 from np-guard/issue375
Browse files Browse the repository at this point in the history
Added weight to edges in output dot file to avoid edge text collision
  • Loading branch information
tanyaveksler committed Jun 25, 2024
2 parents f161b0b + 476a95f commit a9f5ad0
Show file tree
Hide file tree
Showing 59 changed files with 1,816 additions and 1,790 deletions.
20 changes: 9 additions & 11 deletions pkg/netpol/connlist/conns_formatter_dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@ import (
)

const (
ipColor = "red2"
nonIPPeerColor = "blue"
ipColor = "red2"
nonIPPeerColor = "blue"
connlistEdgeColor = "gold2"
edgeFontColor = "darkgreen"
)

// formatDOT: implements the connsFormatter interface for dot output format
type formatDOT struct {
peersList []Peer // internally used peersList; in case of focusWorkload option contains only relevant peers
}

// formats an edge line from a singleConnFields struct , to be used for dot graph
func getEdgeLine(c Peer2PeerConnection) string {
connStr := common.ConnStrFromConnProperties(c.AllProtocolsAndPorts(), c.ProtocolsAndPorts())
return fmt.Sprintf("\t%q -> %q [label=%q color=\"gold2\" fontcolor=\"darkgreen\"]", c.Src().String(), c.Dst().String(), connStr)
}

// returns the peer label and color to be represented in the graph, and whether the peer is external to cluster's namespaces
func peerNameAndColorByType(peer Peer) (nameLabel, color string, isExternal bool) {
if peer.IsPeerIPType() {
Expand Down Expand Up @@ -68,9 +64,11 @@ func (d formatDOT) writeOutput(conns []Peer2PeerConnection) (string, error) {
edgeLines := make([]string, len(conns)) // list of edges lines
peersVisited := make(map[string]bool, 0) // acts as a set
for index := range conns {
edgeLines[index] = getEdgeLine(conns[index])
externalPeersLines = categorizeAndAddPeerLine(conns[index].Src(), peersVisited, externalPeersLines, nsPeers)
externalPeersLines = categorizeAndAddPeerLine(conns[index].Dst(), peersVisited, externalPeersLines, nsPeers)
c := conns[index]
connStr := common.ConnStrFromConnProperties(c.AllProtocolsAndPorts(), c.ProtocolsAndPorts())
edgeLines[index] = dotformatting.GetEdgeLine(c.Src().String(), c.Dst().String(), connStr, connlistEdgeColor, edgeFontColor)
externalPeersLines = categorizeAndAddPeerLine(c.Src(), peersVisited, externalPeersLines, nsPeers)
externalPeersLines = categorizeAndAddPeerLine(c.Dst(), peersVisited, externalPeersLines, nsPeers)
}
for _, val := range d.peersList {
if !val.IsPeerIPType() {
Expand Down
13 changes: 4 additions & 9 deletions pkg/netpol/diff/diff_formatter_dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,24 +141,19 @@ func (df *diffFormatDOT) addEdgesLines(connsPair SrcDstDiff) string {
firstConn, secondConn := getDirsConnsStrings(connsPair)
switch connsPair.DiffType() {
case UnchangedType:
return getEdgeLine(src, dst, firstConn, nonChangedConnColor)
return dotformatting.GetEdgeLine(src, dst, firstConn, nonChangedConnColor, nonChangedConnColor)
case ChangedType:
changedEdgeLabel := fmt.Sprintf("%s (%s: %s)", secondConn, df.ref1, firstConn)
return getEdgeLine(src, dst, changedEdgeLabel, changedConnColor)
return dotformatting.GetEdgeLine(src, dst, changedEdgeLabel, changedConnColor, changedConnColor)
case RemovedType:
return getEdgeLine(src, dst, firstConn, removedConnColor)
return dotformatting.GetEdgeLine(src, dst, firstConn, removedConnColor, removedConnColor)
case AddedType:
return getEdgeLine(src, dst, secondConn, addedConnColor)
return dotformatting.GetEdgeLine(src, dst, secondConn, addedConnColor, addedConnColor)
default:
return "" // should not get here
}
}

// getEdgeLine returns a single edge line string in the dot format
func getEdgeLine(src, dst, connStr, edgeColor string) string {
return fmt.Sprintf("\t%q -> %q [label=%q color=%q fontcolor=%q]", src, dst, connStr, edgeColor, edgeColor)
}

// kept empty for dot format, used to implement the diffFormatter interface in other formats
func (df *diffFormatDOT) singleDiffLine(d *singleDiffFields) string {
return ""
Expand Down
20 changes: 18 additions & 2 deletions pkg/netpol/internal/dotformatting/dot_output_formatting.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ SPDX-License-Identifier: Apache-2.0
package dotformatting

import (
"fmt"
"sort"
"strings"
)

// common dot output formatting consts and funcs
const (
DotHeader = "digraph {"
DotClosing = "}"
DotHeader = "digraph {"
DotClosing = "}"
EdgeWeightLabel = "weight"
LessWeight = "0.5"
MoreWeight = "1"
)

// AddPeerToNsGroup adds the peer line to the namespace list in the given map.
Expand Down Expand Up @@ -59,3 +63,15 @@ func sortMapKeys(nsPeersMap map[string][]string) []string {
sort.Strings(keys)
return keys
}

// returns a single edge line string in the dot format
func GetEdgeLine(src, dst, connStr, edgeColor, fontColor string) string {
var weight string
if src <= dst {
weight = LessWeight
} else {
weight = MoreWeight
}
return fmt.Sprintf("\t%q -> %q [label=%q color=%q fontcolor=%q %s=%s]",
src, dst, connStr, edgeColor, fontColor, EdgeWeightLabel, weight)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ digraph {
"default/emailservice[Deployment]" [label="emailservice[Deployment]" color="blue" fontcolor="blue"]
label="default"
}
"default/checkoutservice[Deployment]" -> "default/emailservice[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"default/checkoutservice[Deployment]" -> "default/emailservice[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=0.5]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 14 additions & 14 deletions test_outputs/connlist/acs-security-demos_connlist_output.dot
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ digraph {
label="payments"
}
"{ingress-controller}" [label="{ingress-controller}" color="blue" fontcolor="blue"]
"backend/checkout[Deployment]" -> "backend/notification[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"backend/checkout[Deployment]" -> "backend/recommendation[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"backend/checkout[Deployment]" -> "payments/gateway[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"backend/recommendation[Deployment]" -> "backend/catalog[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"backend/reports[Deployment]" -> "backend/catalog[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"backend/reports[Deployment]" -> "backend/recommendation[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"frontend/webapp[Deployment]" -> "backend/checkout[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"frontend/webapp[Deployment]" -> "backend/recommendation[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"frontend/webapp[Deployment]" -> "backend/reports[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"frontend/webapp[Deployment]" -> "backend/shipping[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"payments/gateway[Deployment]" -> "payments/mastercard-processor[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"payments/gateway[Deployment]" -> "payments/visa-processor[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"{ingress-controller}" -> "frontend/asset-cache[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"{ingress-controller}" -> "frontend/webapp[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen"]
"backend/checkout[Deployment]" -> "backend/notification[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=0.5]
"backend/checkout[Deployment]" -> "backend/recommendation[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=0.5]
"backend/checkout[Deployment]" -> "payments/gateway[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=0.5]
"backend/recommendation[Deployment]" -> "backend/catalog[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=1]
"backend/reports[Deployment]" -> "backend/catalog[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=1]
"backend/reports[Deployment]" -> "backend/recommendation[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=1]
"frontend/webapp[Deployment]" -> "backend/checkout[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=1]
"frontend/webapp[Deployment]" -> "backend/recommendation[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=1]
"frontend/webapp[Deployment]" -> "backend/reports[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=1]
"frontend/webapp[Deployment]" -> "backend/shipping[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=1]
"payments/gateway[Deployment]" -> "payments/mastercard-processor[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=0.5]
"payments/gateway[Deployment]" -> "payments/visa-processor[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=0.5]
"{ingress-controller}" -> "frontend/asset-cache[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=1]
"{ingress-controller}" -> "frontend/webapp[Deployment]" [label="TCP 8080" color="gold2" fontcolor="darkgreen" weight=1]
}
Binary file modified test_outputs/connlist/acs-security-demos_connlist_output.dot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit a9f5ad0

Please sign in to comment.