Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
Merge branch 'aermakov-zalando-cluster-name-instead-of-id'
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Oct 26, 2018
2 parents 4d2a3ea + 6e5c470 commit 9cfb359
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 29 deletions.
16 changes: 9 additions & 7 deletions kube_resource_report/cluster_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ class Cluster:
def __init__(
self,
id,
name,
api_server_url,
ssl_ca_cert=None,
auth=None,
cert_file=None,
key_file=None,
):
self.id = id
self.name = name
self.api_server_url = api_server_url
self.ssl_ca_cert = ssl_ca_cert
self.auth = auth
Expand All @@ -81,31 +83,28 @@ def __init__(self, api_server_urls: list):
# we are not running inside a cluster
# => assume default kubectl proxy URL
cluster = Cluster(
generate_cluster_id(DEFAULT_CLUSTERS), DEFAULT_CLUSTERS
generate_cluster_id(DEFAULT_CLUSTERS), "cluster", DEFAULT_CLUSTERS
)
else:
# "load_incluster_config" set defaults in the config class
config = kubernetes.client.configuration.Configuration()
cluster = Cluster(
generate_cluster_id(config.host),
"cluster",
config.host,
ssl_ca_cert=config.ssl_ca_cert,
auth=StaticAuthorizationHeaderAuth(config.api_key["authorization"]),
)
self._clusters.append(cluster)
else:
for api_server_url in api_server_urls:

if "localhost" not in api_server_url:
# TODO: hacky way of detecting whether we need a token or not
auth = OAuthTokenAuth("read-only")
else:
auth = None
self._clusters.append(
Cluster(
generate_cluster_id(api_server_url), api_server_url, auth=auth
)
)
generated_id = generate_cluster_id(api_server_url)
self._clusters.append(Cluster(generated_id, generated_id, api_server_url, auth=auth))

def get_clusters(self):
return self._clusters
Expand Down Expand Up @@ -133,6 +132,7 @@ def refresh(self):
clusters.append(
Cluster(
row["id"],
row["alias"],
row["api_server_url"],
auth=OAuthTokenAuth("read-only"),
)
Expand Down Expand Up @@ -176,6 +176,7 @@ def get_clusters(self):
else:
auth = None
cluster = Cluster(
context["name"],
context["name"],
config.host,
ssl_ca_cert=config.ssl_ca_cert,
Expand All @@ -190,6 +191,7 @@ class MockDiscoverer:
def get_clusters(self):
for i in range(3):
yield Cluster(
f"mock-cluster-{i}",
f"mock-cluster-{i}",
api_server_url="https://kube-{}.example.org".format(i),
)
14 changes: 12 additions & 2 deletions kube_resource_report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ def get_cluster_summaries(
["error", f"Failed to query cluster {cluster.id}: {e}"]
)
logger.exception(e)
return cluster_summaries

sorted_by_name = sorted(cluster_summaries.values(), key=lambda summary: summary["cluster"].name)
return {summary["cluster"].id: summary for summary in sorted_by_name}


def resolve_application_ids(applications: dict, teams: dict, application_registry: str):
Expand Down Expand Up @@ -573,12 +575,20 @@ def generate_report(
namespace["cost"] += pod["cost"]
namespace["slack_cost"] += pod.get("slack_cost", 0)
namespace["pods"] += 1
namespace["cluster"] = cluster_id
namespace["cluster"] = summary["cluster"]
namespace_usage[(ns_pod[0], cluster_id)] = namespace

if application_registry:
resolve_application_ids(applications, teams, application_registry)

for team in teams.values():
def cluster_name(cluster_id):
try:
return cluster_summaries[cluster_id]["cluster"].name
except KeyError:
return None
team["clusters"] = sorted(team["clusters"], key=cluster_name)

for cluster_id, summary in sorted(cluster_summaries.items()):
for k, pod in summary["pods"].items():
app = applications.get(pod["application"])
Expand Down
8 changes: 4 additions & 4 deletions kube_resource_report/templates/cluster.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% extends "base.html" %}
{% block title %}Cluster {{ cluster_id }}{% endblock %}
{% block title %}Cluster {{ summary.cluster.name }}{% endblock %}
{% block content %}
<h1 class="title">Cluster {{ cluster_id }}</h1>
<h2 class="subtitle">{{ summary.cluster.api_server_url }}</h1>
<h1 class="title">Cluster {{ summary.cluster.name }}</h1>
<h2 class="subtitle">{{ summary.cluster.id if summary.cluster.id != summary.cluster.name else summary.cluster.api_server_url }}</h1>

<nav class="level">
<div class="level-item has-text-centered">
Expand Down Expand Up @@ -157,7 +157,7 @@ <h2 class="title is-5">Namespaces</h2>
</thead>
<tbody>
{% for namespace_id, namespace_data in namespace_usage.items()|sort: %}
{% if namespace_data.cluster == cluster_id %}
{% if namespace_data.cluster.id == cluster_id %}
<tr>
<td>{{ namespace_id[0] }}</td>
<td>
Expand Down
8 changes: 3 additions & 5 deletions kube_resource_report/templates/clusters.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<table class="table is-striped" data-sortable>
<thead>
<tr>
<th>ID</th>
<th>API URL</th>
<th>Cluster</th>
<th><abbr title="Master Nodes">MN</abbr></th>
<th><abbr title="Worker Nodes">WN</abbr></th>
<th>Inst. Types</th>
Expand All @@ -20,10 +19,9 @@
</tr>
</thead>
<tbody>
{% for cluster_id, summary in cluster_summaries.items()|sort: %}
{% for cluster_id, summary in cluster_summaries.items() %}
<tr>
<td><a href="./cluster-{{ cluster_id }}.html">{{ cluster_id }}</a></td>
<td>{{ summary.cluster.api_server_url }}</td>
<td><a href="./cluster-{{ cluster_id }}.html">{{ summary.cluster.name }}</a></td>
<td class="has-text-right">{{ summary.master_nodes }}</td>
<td class="has-text-right">{{ summary.worker_nodes }}</td>
<td>{{ summary.worker_instance_types|join(', ') }}</td>
Expand Down
4 changes: 2 additions & 2 deletions kube_resource_report/templates/ingresses.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<table class="table is-striped" data-sortable>
<thead>
<tr>
<th>Cluster ID</th>
<th>Cluster</th>
<th>Namespace</th>
<th>Name</th>
<th>Application</th>
Expand All @@ -17,7 +17,7 @@
{% for cluster_id, summary in cluster_summaries.items(): %}
{% for ingress in summary.ingresses: %}
<tr>
<td><a href="./cluster-{{ cluster_id }}.html">{{ cluster_id }}</a></td>
<td><a href="./cluster-{{ cluster_id }}.html">{{ summary["cluster"].name }}</a></td>
<td>{{ ingress[0] }}</td>
<td>{{ ingress[1] }}</td>
<td>{{ ingress[2] }}</td>
Expand Down
2 changes: 1 addition & 1 deletion kube_resource_report/templates/namespaces.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
{% endif %}

</td>
<td><a href="./cluster-{{ namespace_data.cluster }}.html">{{ namespace_data.cluster }}</a></td>
<td><a href="./cluster-{{ namespace_data.cluster.id }}.html">{{ namespace_data.cluster.name }}</a></td>
<td>{{ namespace_data.pods }}</td>
<td>{{ namespace_data.requests.cpu|round(3) }}</td>
<td data-value="{{ namespace_data.requests.memory }}">{{ namespace_data.requests.memory|filesizeformat(True) }}</td>
Expand Down
4 changes: 2 additions & 2 deletions kube_resource_report/templates/pods.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<table class="table is-striped" data-sortable>
<thead>
<tr>
<th>Cluster ID</th>
<th>Cluster</th>
<th>Namespace</th>
<th>Name</th>
<th>Application</th>
Expand All @@ -20,7 +20,7 @@
{% for cluster_id, summary in cluster_summaries.items(): %}
{% for namespace_name, pod in summary.pods.items(): %}
<tr>
<td><a href="./cluster-{{ cluster_id }}.html">{{ cluster_id }}</a></td>
<td><a href="./cluster-{{ cluster_id }}.html">{{ summary.cluster.name }}</a></td>
<td>{{ namespace_name[0] }}</td>
<td>{{ namespace_name[1] }}</td>
<td>{{ pod.application }}</td>
Expand Down
8 changes: 3 additions & 5 deletions kube_resource_report/templates/team.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ <h2 class="title is-5">Clusters</h2>
<table class="table is-striped" data-sortable>
<thead>
<tr>
<th>ID</th>
<th>API URL</th>
<th>Cluster</th>
<th><abbr title="Master Nodes">MN</abbr></th>
<th><abbr title="Worker Nodes">WN</abbr></th>
<th>Inst. Types</th>
Expand All @@ -57,11 +56,10 @@ <h2 class="title is-5">Clusters</h2>
</tr>
</thead>
<tbody>
{% for cluster_id in team.clusters|sort: %}
{% for cluster_id in team.clusters: %}
{% set summary = cluster_summaries[cluster_id] %}
<tr>
<td><a href="./cluster-{{ cluster_id }}.html">{{ cluster_id }}</a></td>
<td>{{ summary.cluster.api_server_url }}</td>
<td><a href="./cluster-{{ cluster_id }}.html">{{ summary.cluster.name }}</a></td>
<td>{{ summary.master_nodes }}</td>
<td>{{ summary.worker_nodes }}</td>
<td>{{ summary.worker_instance_types|join(', ') }}</td>
Expand Down
2 changes: 1 addition & 1 deletion tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_generate_report(tmpdir, monkeypatch):
monkeypatch.setattr("kube_resource_report.cluster_discovery.tokens.get", lambda x: "mytok")
monkeypatch.setattr(
"kube_resource_report.cluster_discovery.ClusterRegistryDiscoverer.get_clusters",
lambda x: [Cluster("test-cluster-1", "https://test-cluster-1.example.org")],
lambda x: [Cluster("test-cluster-1", "test-cluster-1", "https://test-cluster-1.example.org")],
)

responses = {
Expand Down

0 comments on commit 9cfb359

Please sign in to comment.