Skip to content

Commit 1aa65b6

Browse files
authored
simplify_output flag (#576)
* simplify_output Signed-off-by: haim-kermany <[email protected]> * simplify_output -> simplify_graph Signed-off-by: haim-kermany <[email protected]> * simplify_output -> simplify_graph Signed-off-by: haim-kermany <[email protected]> * update md Signed-off-by: haim-kermany <[email protected]> * lint Signed-off-by: haim-kermany <[email protected]> * update scheme md Signed-off-by: haim-kermany <[email protected]> --------- Signed-off-by: haim-kermany <[email protected]>
1 parent 3c48544 commit 1aa65b6

File tree

10 files changed

+37
-26
lines changed

10 files changed

+37
-26
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ The arguments to `--resource_list` and to `--base_resource_list` should be one o
9595
*shorthand* `-f`
9696
- `--expected_output <file name>`\
9797
A file path to the expected query output (for connectivity or semantic_diff queries).\
98+
- `--simplify_graph`\
99+
simplify the connectivity graph, (relevant only when output_format is dot or jpg)
98100
- `--pr_url <URL>`\
99101
Write output as GitHub PR comment. URL points to the relevant `comments` resource in the GitHub API.\
100102
e.g., https://api.github.com/repos/shift-left-netconfig/online-boutique/issues/1/comments

docs/SchemeFileFormat.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ The supported entries in the outputConfiguration object are as follows:
8282
|------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------|
8383
| outputFormat | Output format specification. | string [ txt / yaml / csv / md / dot / jpg/ txt_no_fw_rules] |
8484
| outputPath | A file path to redirect output into. | string |
85+
| simplifyGraph | Choose if to simplify the connectivity graph. | bool [default: False] |
8586
| outputEndpoints | Choose endpoints type in output. | string [ pods / deployments ] |
8687
| subset | A dict object with the defined subset elements to display in the output | [subset](#subset) object |
8788
| fullExplanation | Choose if to print all counterexamples causing the query result in the output | bool |

nca/FWRules/ConnectivityGraph.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,18 +330,19 @@ def get_connections_without_fw_rules_txt_format(self, connectivity_msg=None, exc
330330
lines_list.extend(sorted(list(lines)))
331331
return '\n'.join(lines_list)
332332

333-
def get_connectivity_dot_format_str(self, connectivity_restriction=None):
333+
def get_connectivity_dot_format_str(self, connectivity_restriction=None, simplify_graph=False):
334334
"""
335335
:param Union[str,None] connectivity_restriction: specify if connectivity is restricted to
336336
TCP / non-TCP , or not
337+
:param simplify_graph[bool, False] whether to simplify the dot output graph
337338
:rtype str
338339
:return: a string with content of dot format for connectivity graph
339340
"""
340341
restriction_title = f', for {connectivity_restriction} connections' if connectivity_restriction else ''
341342
query_title = f'{self.output_config.queryName}/' if self.output_config.queryName else ''
342343
name = f'{query_title}{self.output_config.configName}{restriction_title}'
343344

344-
dot_graph = DotGraph(name)
345+
dot_graph = DotGraph(name, do_not_subgraph=simplify_graph)
345346
peers_groups = self._get_equals_groups()
346347
# we are going to treat a peers_group as one peer.
347348
# the first peer in the peers_group is representing the group

nca/FWRules/DotGraph.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Node:
3030
name: str
3131
node_type: int
3232
label: str
33+
title: str
3334

3435
@dataclass
3536
class Edge:
@@ -38,13 +39,14 @@ class Edge:
3839
label: str
3940
is_dir: bool
4041

41-
def __init__(self, name):
42+
def __init__(self, name, do_not_subgraph):
4243
self.subgraphs = {}
4344
self.name = name
4445
self.edges = []
4546
self.all_nodes = {}
4647
self.labels = set()
4748
self.labels_dict = {}
49+
self.do_not_subgraph = do_not_subgraph
4850
self.node_styles = \
4951
{self.NodeType.IPBlock: 'shape=box fontcolor=red2',
5052
self.NodeType.Pod: 'shape=box fontcolor=blue',
@@ -74,9 +76,11 @@ def add_node(self, subgraph, name, node_type, label):
7476
param label: node label
7577
"""
7678
label = [tok.strip() for tok in label if tok != '']
79+
title = subgraph if self.do_not_subgraph else ''
80+
subgraph = '' if self.do_not_subgraph else subgraph
7781
if subgraph not in self.subgraphs:
7882
self.subgraphs[subgraph] = self.Subgraph(subgraph)
79-
node = self.Node(name, node_type, label)
83+
node = self.Node(name, node_type, label, title)
8084
self.subgraphs[subgraph].nodes.append(node)
8185
self.all_nodes[name] = node
8286
if node_type in {self.NodeType.Clique, self.NodeType.BiClique}:
@@ -191,7 +195,11 @@ def _node_to_str(self, node):
191195
table = f'<<table border="{border}" cellspacing="0">'
192196
for line in node.label:
193197
if line:
194-
table += f'<tr><td>{line}</td></tr>'
198+
if node.title:
199+
table += f'<tr><td>{node.title}/{line}</td></tr>'
200+
else:
201+
table += f'<tr><td>{line}</td></tr>'
202+
195203
table += '</table>>'
196204
label = f'label={table}'
197205
node_desc = f'{label} {self.node_styles[node.node_type]} tooltip=\"{self.node_tooltip[node.node_type]}\"'

nca/NetworkConfig/NetworkConfigQuery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ def dot_format_from_connections_dict(self, connections, peers, connectivity_rest
10311031
:return the connectivity map in dot-format, considering connectivity_restriction if required
10321032
"""
10331033
conn_graph = self._get_conn_graph(connections, peers)
1034-
return conn_graph.get_connectivity_dot_format_str(connectivity_restriction)
1034+
return conn_graph.get_connectivity_dot_format_str(connectivity_restriction, self.output_config.simplifyGraph)
10351035

10361036
def dot_format_from_props(self, props, peers, connectivity_restriction=None):
10371037
"""

nca/Resources/NetworkPolicy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def __str__(self):
116116
return self.full_name()
117117

118118
def __eq__(self, other):
119-
if type(self) == type(other):
119+
if type(self) is type(other):
120120
self.sync_opt_props()
121121
other.sync_opt_props()
122122
return \

nca/Utils/OutputConfiguration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self, output_config_dict=None, query_name=''):
1919
default_output_config = {'fwRulesRunInTestMode': False, 'fwRulesDebug': False,
2020
'fwRulesGroupByLabelSinglePod': False, 'fwRulesFilterSystemNs': False,
2121
'fwRulesMaxIter': 10, 'outputFormat': 'txt', 'outputPath': None,
22-
'fwRulesOverrideAllowedLabels': None, 'prURL': None,
22+
'simplifyGraph': False, 'fwRulesOverrideAllowedLabels': None, 'prURL': None,
2323
'connectivityFilterIstioEdges': True, 'outputEndpoints': 'deployments',
2424
'subset': {}, 'explain': None, 'fullExplanation': False, 'excludeIPv6Range': True}
2525

nca/nca_cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def run_args(args): # noqa: C901
153153

154154
output_config = OutputConfiguration({'outputFormat': args.output_format or 'txt',
155155
'outputPath': args.file_out or None,
156+
'simplifyGraph': args.simplify_graph or False,
156157
'prURL': args.pr_url or None,
157158
'outputEndpoints': args.output_endpoints,
158159
'subset': {},
@@ -326,6 +327,9 @@ def nca_main(argv=None):
326327
parser.add_argument('--file_out', '-f', type=str, help='A file path to which output is redirected')
327328
parser.add_argument('--expected_output', type=str, help='A file path of the expected query output,'
328329
'relevant only with --connectivity and --semantic_diff')
330+
parser.add_argument('--simplify_graph', action='store_true',
331+
help='simplify the connectivity graph,'
332+
'relevant only when output_format is dot or jpg')
329333
parser.add_argument('--pr_url', type=str, help='The full api url for adding a PR comment')
330334
parser.add_argument('--return_0', action='store_true', help='Force a return value 0')
331335
parser.add_argument('--version', '-v', action='store_true', help='Print version and exit')

tests/fw_rules_tests/policies/expected_output/poc1-scheme_output.dot

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,18 @@ digraph {
66
subgraph cluster_map_explanation {
77
dict_box [label=<<table border="0" cellspacing="0"><tr><td align="text">Connectivity legend<br align="left" /></td></tr><tr><td align="text" tooltip="TCP 3550" href="bogus">tcp3550 TCP 3550<br align="left" /></td></tr><tr><td align="text" tooltip="TCP 50051" href="bogus">tcp50051 TCP 50051<br align="left" /></td></tr><tr><td align="text" tooltip="TCP 5050" href="bogus">tcp5050 TCP 5050<br align="left" /></td></tr><tr><td align="text" tooltip="TCP 6379" href="bogus">tcp6379 TCP 6379<br align="left" /></td></tr><tr><td align="text" tooltip="TCP 7000" href="bogus">tcp7000 TCP 7000<br align="left" /></td></tr><tr><td align="text" tooltip="TCP 7070" href="bogus">tcp7070 TCP 7070<br align="left" /></td></tr><tr><td align="text" tooltip="TCP 8080" href="bogus">tcp8080 TCP 8080<br align="left" /></td></tr><tr><td align="text" tooltip="TCP 9555" href="bogus">tcp9555 TCP 9555<br align="left" /></td></tr></table>> shape=box]
88
"0.0.0.0/0" [label=<<table border="0" cellspacing="0"><tr><td>0.0.0.0/0</td></tr></table>> shape=box fontcolor=red2 tooltip="IP Block"]
9-
subgraph cluster_default_namespace{
10-
label="default"
11-
fontsize=20
12-
fontcolor=blue
13-
tooltip="Namespace"
14-
"default/adservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>adservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
15-
"default/cartservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>cartservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
16-
"default/checkoutservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>checkoutservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
17-
"default/currencyservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>currencyservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
18-
"default/emailservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>emailservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
19-
"default/frontend(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>frontend(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
20-
"default/loadgenerator(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>loadgenerator(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
21-
"default/paymentservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>paymentservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
22-
"default/productcatalogservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>productcatalogservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
23-
"default/recommendationservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>recommendationservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
24-
"default/redis-cart(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>redis-cart(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
25-
"default/shippingservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>shippingservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
26-
}
9+
"default/adservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>default/adservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
10+
"default/cartservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>default/cartservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
11+
"default/checkoutservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>default/checkoutservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
12+
"default/currencyservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>default/currencyservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
13+
"default/emailservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>default/emailservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
14+
"default/frontend(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>default/frontend(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
15+
"default/loadgenerator(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>default/loadgenerator(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
16+
"default/paymentservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>default/paymentservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
17+
"default/productcatalogservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>default/productcatalogservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
18+
"default/recommendationservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>default/recommendationservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
19+
"default/redis-cart(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>default/redis-cart(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
20+
"default/shippingservice(Deployment)" [label=<<table border="0" cellspacing="0"><tr><td>default/shippingservice(Deployment)</td></tr></table>> shape=box fontcolor=blue tooltip="Workload"]
2721
"0.0.0.0/0" -> "default/frontend(Deployment)"[label="tcp8080" labeltooltip="TCP 8080" color=darkorange4 fontcolor=darkgreen dir=both arrowhead=normal arrowtail=none]
2822
"default/cartservice(Deployment)" -> "default/redis-cart(Deployment)"[label="tcp6379" labeltooltip="TCP 6379" color=darkorange4 fontcolor=darkgreen dir=both arrowhead=normal arrowtail=none]
2923
"default/checkoutservice(Deployment)" -> "default/cartservice(Deployment)"[label="tcp7070" labeltooltip="TCP 7070" color=darkorange4 fontcolor=darkgreen dir=both arrowhead=normal arrowtail=none]

tests/fw_rules_tests/policies/poc1-scheme.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ queries:
3131
outputConfiguration:
3232
outputFormat: dot
3333
outputPath: null
34+
simplifyGraph: true
3435
fwRulesRunInTestMode: false
3536
expectedOutput: expected_output/poc1-scheme_output.dot
3637
- name: connectivity_map_csv

0 commit comments

Comments
 (0)