Skip to content

Commit 3a73c4e

Browse files
authored
Livesim - Add missing elements while analyzing in the left - issue #262 (#393)
Signed-off-by: adisos <[email protected]>
1 parent 3879e30 commit 3a73c4e

File tree

65 files changed

+2248
-223
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2248
-223
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ The arguments to `--resource_list` and to `--base_resource_list` should be one o
100100

101101
For more information on command-line switches combinations, see [Common Query Patterns](docs/CommonQueryPatterns.md#cmdline-queries)
102102

103+
##### Simulating live cluster missing resources:
104+
- There are several key elements that may be assumed to exist in the live cluster and be missing form the topology configurations in the repo.
105+
In those case, nca will add complementary configurations to make the topology and the connectivity whole.
106+
- Fine-tuning instructions can be found [here](docs/SimulatingLiveClusterMissingResources.md)
107+
103108
#### Exit Code Meaning:
104109
The exit value of running a command-line without a scheme is the combination of three factors:
105110
1. The result of running the query (0/1) as specified [here](docs/CmdLineQueriesResults.md)

docs/CommonQueryPatterns.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,3 @@ otherwise, an empty peer container is created
5858
- otherwise, global namespaces will be used if existed else cluster has empty namespaces container
5959
- If any specific key is specified it will override the relevant contents in resourceList
6060

61-
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Simulating live cluster missing resources:
2+
- There are several key elements that may be assumed to exist in the live cluster and be missing form the topology configurations in the repo.
3+
At those case, nca will add complementary configurations to make the topology and the connectivity whole.
4+
- Fine-tune of the configurations can be made in dedicated yaml files.
5+
6+
| Missing Element | Element name when added to the topology |
7+
|------------------------|-------------------------------------------------------------------------------|
8+
| kube-dns | [kube-dns-livesim](../nca/NetworkConfig/LiveSim/dns) |
9+
| ingress controller | [ingress-controller-livesim](../nca/NetworkConfig/LiveSim/ingress_controller) |
10+
| Istio ingress gateway | [istio-ingressgateway-livesim](../nca/NetworkConfig/LiveSim/istio_gateway) |

nca/FWRules/ConnectivityGraph.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,35 @@ def _get_peer_name(self, peer):
6262
return peer.workload_name, False
6363
return str(peer), False
6464

65+
@staticmethod
66+
def _is_peer_livesim(peer):
67+
"""
68+
check if peer name indicates that this is a peer related to "livesim": resources added
69+
during parsing, since they are required for the analysis but were missing from the input config
70+
71+
current convention is that such peers suffix is "-livesim"
72+
73+
:param Peer peer: the peer object
74+
:rtype bool
75+
"""
76+
livesim_peer_name_suffix = "-livesim"
77+
return peer.full_name().endswith(livesim_peer_name_suffix)
78+
79+
@staticmethod
80+
def _get_peer_color(is_livesim, is_ip_block):
81+
"""
82+
determine peer color for connectivity graph
83+
:param is_livesim: is peer added from "livesim" (missing resource at input config)
84+
:param is_ip_block: is peer of type ip-block
85+
:return: str of the peer color in the dot format
86+
:rtype str
87+
"""
88+
if is_livesim:
89+
return "coral4"
90+
elif is_ip_block:
91+
return "red2"
92+
return "blue"
93+
6594
def get_connectivity_dot_format_str(self, connectivity_restriction=None):
6695
"""
6796
:param Union[str,None] connectivity_restriction: specify if connectivity is restricted to
@@ -79,7 +108,7 @@ def get_connectivity_dot_format_str(self, connectivity_restriction=None):
79108
peer_lines = set()
80109
for peer in self.cluster_info.all_peers:
81110
peer_name, is_ip_block = self._get_peer_name(peer)
82-
peer_color = "red2" if is_ip_block else "blue"
111+
peer_color = self._get_peer_color(self._is_peer_livesim(peer), is_ip_block)
83112
peer_lines.add(f'\t\"{peer_name}\" [label=\"{peer_name}\" color=\"{peer_color}\" fontcolor=\"{peer_color}\"]\n')
84113

85114
edge_lines = set()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: kube-system
5+
labels:
6+
name: kube-system
7+
kubernetes.io/metadata.name: kube-system
8+
---
9+
apiVersion: apps/v1
10+
kind: Pod
11+
metadata:
12+
name: kube-dns-livesim
13+
namespace: kube-system
14+
labels:
15+
k8s-app: kube-dns
16+
projectcalico.org/namespace: kube-system
17+
spec:
18+
containers:
19+
- name: nginx
20+
image: kube-dns
21+
---
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: ingress-controller-ns
5+
labels:
6+
name: ingress-controller-ns
7+
---
8+
apiVersion: v1
9+
kind: Service
10+
metadata:
11+
name: ingress-nginx
12+
namespace: ingress-controller-ns
13+
spec:
14+
selector:
15+
app: ingress-nginx
16+
ports:
17+
- port: 5678
18+
---
19+
apiVersion: v1
20+
kind: Pod
21+
metadata:
22+
name: ingress-controller-livesim
23+
namespace: ingress-controller-ns
24+
labels:
25+
app: ingress-nginx
26+
spec:
27+
containers:
28+
- name: ingress-nginx
29+
image: ingress-nginx:1.2.3
30+
---
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: custom-ingressgateway-livesim
5+
namespace: custom-gateways
6+
labels:
7+
istio: custom-ingressgateway
8+
spec:
9+
serviceAccountName: custom-ingressgateway-livesim
10+
containers:
11+
- name: istio-proxy
12+
image: auto
13+
---
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: istio-ingressgateway-livesim
5+
namespace: istio-ingressgateway-ns
6+
labels:
7+
app: istio-ingressgateway
8+
istio: ingressgateway
9+
spec:
10+
serviceAccountName: istio-ingressgateway
11+
containers:
12+
- name: istio-proxy
13+
image: auto
14+
---

nca/NetworkConfig/PoliciesFinder.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def __init__(self):
2525
self.policies_container = PoliciesContainer()
2626
self._parse_queue = deque()
2727
self.peer_container = None
28+
# following missing resources fields are relevant for "livesim" mode,
29+
# where certain resources are added to enable the analysis
30+
self.missing_istio_gw_pods_with_labels = {}
31+
self.missing_k8s_ingress_peers = False
32+
self.missing_dns_pods_with_labels = {}
2833

2934
def set_peer_container(self, peer_container):
3035
"""
@@ -70,6 +75,8 @@ def parse_policies_in_parse_queue(self):
7075
elif policy_type == NetworkPolicy.PolicyType.K8sNetworkPolicy:
7176
parsed_element = K8sPolicyYamlParser(policy, self.peer_container, file_name)
7277
self._add_policy(parsed_element.parse_policy())
78+
# add info about missing resources
79+
self.missing_dns_pods_with_labels.update(parsed_element.missing_pods_with_labels)
7380
elif policy_type == NetworkPolicy.PolicyType.IstioAuthorizationPolicy:
7481
parsed_element = IstioPolicyYamlParser(policy, self.peer_container, file_name)
7582
self._add_policy(parsed_element.parse_policy())
@@ -79,10 +86,14 @@ def parse_policies_in_parse_queue(self):
7986
elif policy_type == NetworkPolicy.PolicyType.Ingress:
8087
parsed_element = IngressPolicyYamlParser(policy, self.peer_container, file_name)
8188
self._add_policy(parsed_element.parse_policy())
89+
# add info about missing resources
90+
self.missing_k8s_ingress_peers |= parsed_element.missing_k8s_ingress_peers
8291
elif policy_type == NetworkPolicy.PolicyType.Gateway:
8392
if not istio_traffic_parser:
8493
istio_traffic_parser = IstioTrafficResourcesYamlParser(self.peer_container)
8594
istio_traffic_parser.parse_gateway(policy, file_name)
95+
# add info about missing resources
96+
self.missing_istio_gw_pods_with_labels.update(istio_traffic_parser.missing_istio_gw_pods_with_labels)
8697
elif policy_type == NetworkPolicy.PolicyType.VirtualService:
8798
if not istio_traffic_parser:
8899
istio_traffic_parser = IstioTrafficResourcesYamlParser(self.peer_container)

0 commit comments

Comments
 (0)