Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add probe for Kubernetes ingress #78

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions orca/common/clients/k8s/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ def get(kind):
kind='horizontal_pod_autoscaler',
list_fn=client.AutoscalingV1Api().list_horizontal_pod_autoscaler_for_all_namespaces
)
elif kind == 'ingress':
return ResourceProxy(
kind='ingress',
list_fn=client.ExtensionsV1beta1Api().list_ingress_for_all_namespaces
)
else:
raise Exception("Unknown kind %s" % kind)

Expand Down
20 changes: 18 additions & 2 deletions orca/topology/infra/k8s/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def get_probes():
linker.PodToServiceLinker,
linker.EndpointsToServiceLinker,
istio_linker.VirtualServiceToServiceLinker,
istio_linker.DestinationRuleToServiceLinker
istio_linker.DestinationRuleToServiceLinker,
linker.IngressToServiceLinker
]
),

Expand All @@ -63,7 +64,8 @@ def get_probes():
linker.PodToServiceLinker,
linker.EndpointsToServiceLinker,
istio_linker.VirtualServiceToServiceLinker,
istio_linker.DestinationRuleToServiceLinker
istio_linker.DestinationRuleToServiceLinker,
linker.IngressToServiceLinker
]
),

Expand Down Expand Up @@ -253,6 +255,20 @@ def get_probes():
]
),

bundle.ProbeBundle(
probe=probe.IngressPullProbe,
linkers=[
linker.IngressToServiceLinker
]
),

bundle.ProbeBundle(
probe=probe.IngressPushProbe,
linkers=[
linker.IngressToServiceLinker
]
),

bundle.ProbeBundle(
probe=cluster.ClusterProbe,
linkers=[
Expand Down
35 changes: 35 additions & 0 deletions orca/topology/infra/k8s/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,38 @@ def _extract_properties(self, entity):
properties['labels'] = entity.metadata.labels.copy()
properties['phase'] = entity.status.phase
return properties


class IngressExtractor(Extractor):

"""Extractor for Ingress entities."""

@property
def kind(self):
return 'ingress'

def _extract_properties(self, entity):
properties = {}
properties['name'] = entity.metadata.name
properties['namespace'] = entity.metadata.namespace
properties['rules'] = self._extract_rules(entity)
return properties

def _extract_rules(self, entity):
rules = []
for rule in entity.spec.rules:
properties = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add rule host property as well.

properties['host'] = rule.host
properties['paths'] = self._extract_paths(rule)
rules.append(properties)
return rules

def _extract_paths(self, rule):
paths = []
for path in rule.http.paths:
properties = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add path property as well.

properties['service_name'] = path.backend.service_name
properties['service_port'] = path.backend.service_port
properties['path'] = path.path
paths.append(properties)
return paths
13 changes: 13 additions & 0 deletions orca/topology/infra/k8s/linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,16 @@ def get(cls, graph):
source_spec=utils.NodeSpec(origin='kubernetes', kind='node'),
target_spec=utils.NodeSpec(origin='kubernetes', kind='cluster'),
matcher=matcher.NodeToClusterMatcher())


class IngressToServiceLinker(Linker):

"""Links Ingress entities to Service entities."""

@classmethod
def get(cls, graph):
return cls(
graph=graph,
source_spec=utils.NodeSpec(origin='kubernetes', kind='ingress'),
target_spec=utils.NodeSpec(origin='kubernetes', kind='service'),
matcher=matcher.IngressToServiceMatcher())
17 changes: 17 additions & 0 deletions orca/topology/infra/k8s/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ def are_linked(self, obj, namespace):
return namespace.properties.name == obj.properties.namespace


class IngressToServiceMatcher(Matcher):

"""Matcher for links between Ingress and Service entities."""

def are_linked(self, ingress, service):
matched_namespace = match_namespace(ingress, service)
matched_service = self._match_rules(ingress, service)
return matched_namespace and matched_service

def _match_rules(self, ingress, service):
for rule in ingress.properties.rules:
for path in rule.paths:
if path.service_name == service.properties.name:
return True
return False


def match_namespace(obj_a, obj_b):
return obj_a.properties.namespace == obj_b.properties.namespace

Expand Down
18 changes: 18 additions & 0 deletions orca/topology/infra/k8s/probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,21 @@ class NamespacePushProbe(PushProbe):
@classmethod
def get(cls, graph):
return super().get(graph, 'namespace', extractor.NamespaceExtractor())


class IngressPullProbe(PullProbe):

"""Ingress pull probe."""

@classmethod
def get(cls, graph):
return super().get(graph, 'ingress', extractor.IngressExtractor())


class IngressPushProbe(PushProbe):

"""Ingress push probe."""

@classmethod
def get(cls, graph):
return super().get(graph, 'ingress', extractor.IngressExtractor())