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

Commit

Permalink
Application JSON (#86)
Browse files Browse the repository at this point in the history
* generate application JSON

* include ingresses
  • Loading branch information
hjacobs authored May 18, 2019
1 parent 90e4e98 commit 286b3d3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 36 deletions.
60 changes: 57 additions & 3 deletions kube_resource_report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,14 +991,68 @@ def write_report(output_path: Path, start, notifications, cluster_summaries, nam
default=json_default
)

with (output_path / "application-metrics.json").open("w") as fd:
json.dump(applications, fd, default=json_default)

ingresses_by_application = collections.defaultdict(list)
for cluster_id, summary in cluster_summaries.items():
for ingress in summary['ingresses']:
ingresses_by_application[ingress[2]].append({
'cluster_id': cluster_id, 'cluster_summary': summary,
'namespace': ingress[0],
'name': ingress[1],
'host': ingress[3],
'status': ingress[4]}
)

pods_by_application = collections.defaultdict(list)
for cluster_id, summary in cluster_summaries.items():
for namespace_name, pod in summary['pods'].items():
namespace, name = namespace_name
pods_by_application[pod['application']].append({
'cluster_id': cluster_id, 'cluster_summary': summary,
'namespace': namespace,
'name': name,
'pod': pod}
)

for app_id, application in applications.items():
file_name = f"application-{app_id}.json"
with (output_path / file_name).open("w") as fd:
json.dump(
{
**application,
"ingresses": [
{
"cluster": row['cluster_id'],
"namespace": row['namespace'],
"name": row["name"],
"host": row["host"],
"status": row["status"]
}
for row in ingresses_by_application[app_id]
],
"pods": [
{
**row['pod'],
"cluster": row['cluster_id'],
"namespace": row['namespace'],
"name": row["name"]
}
for row in pods_by_application[app_id]
]
},
fd,
default=json_default
)

for app_id, application in applications.items():
page = "applications"
file_name = f"application-{app_id}.html"
logger.info(f"Generating {file_name}..")
template = env.get_template("application.html")
context["page"] = page
context["application"] = application
context["ingresses_by_application"] = ingresses_by_application
context["pods_by_application"] = pods_by_application
template.stream(**context).dump(str(output_path / file_name))

with (output_path / "application-metrics.json").open("w") as fd:
json.dump(applications, fd, default=json_default)
58 changes: 25 additions & 33 deletions kube_resource_report/templates/application.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,24 @@ <h2 class="title is-5">Ingresses</h2>
</tr>
</thead>
<tbody>
{% for cluster_id, summary in cluster_summaries.items(): %}
{% for ingress in summary.ingresses: %}
{% if ingress[2] == application.id: %}
{% for ingress in ingresses_by_application[application.id]: %}
<tr>
<td><a href="./cluster-{{ cluster_id }}.html">{{ summary.cluster.name }}</a></td>
<td>{{ ingress[0] }}</td>
<td>{{ ingress[1] }}</td>
<td>{{ ingress[3] }}</td>
<td><a href="./cluster-{{ ingress.cluster_id }}.html">{{ ingress.cluster_summary.cluster.name }}</a></td>
<td>{{ ingress.namespace }}</td>
<td>{{ ingress.name }}</td>
<td>{{ ingress.host }}</td>
<td><span class="tag
{% if ingress[4] >= 500 %}
{% if ingress.status >= 500 %}
is-danger
{% elif ingress[4] >= 400 %}
{% elif ingress.status >= 400 %}
is-info
{% elif ingress[4] >= 200 %}
{% elif ingress.status >= 200 %}
is-success
{% else: %}
is-info
{% endif %}
">{{ ingress[4] }}</span></td>
</tr>
{%endif %}
{%endfor %}
{%endfor %}
</tbody>

Expand All @@ -107,41 +103,37 @@ <h2 class="title is-5">Pods</h2>
</tr>
</thead>
<tbody>
{% for cluster_id, summary in cluster_summaries.items(): %}
{% for namespace_name, pod in summary.pods.items(): %}
{% if pod.application == application.id: %}
{% for row in pods_by_application[application.id]: %}
<tr>
<td><a href="./cluster-{{ cluster_id }}.html">{{ summary.cluster.name }}</a></td>
<td>{{ namespace_name[0] }}</td>
<td>{{ pod.component }}</td>
<td>{{ namespace_name[1] }}</td>
<td title="{{ pod.container_images|join(', ') }}">{{ pod.container_images|count }}</td>
<td>{{ pod.requests.cpu|round(3) }}</td>
<td data-value="{{ pod.requests.memory }}">{{ pod.requests.memory|filesizeformat(True) }}</td>
<td><a href="./cluster-{{ row.cluster_id }}.html">{{ row.cluster_summary.cluster.name }}</a></td>
<td>{{ row.namespace }}</td>
<td>{{ row.pod.component }}</td>
<td>{{ row.name }}</td>
<td title="{{ row.pod.container_images|join(', ') }}">{{ row.pod.container_images|count }}</td>
<td>{{ row.pod.requests.cpu|round(3) }}</td>
<td data-value="{{ row.pod.requests.memory }}">{{ row.pod.requests.memory|filesizeformat(True) }}</td>

<td style="font-size: 0.75rem" data-value="{{ pod.usage.cpu }}">
<td style="font-size: 0.75rem" data-value="{{ row.pod.usage.cpu }}">
<div class="resource-labels">
<span>{{ pod.usage.cpu|round(2) }}</span> <span>{{ pod.requests.cpu|round(2) }}</span>
<span>{{ row.pod.usage.cpu|round(2) }}</span> <span>{{ row.pod.requests.cpu|round(2) }}</span>
</div>
<div class="resource-bar">
<progress class="progress" value="{{ pod.requests.cpu }}" max="{{ pod.requests.cpu }}"></progress>
<progress class="progress {{ 'is-danger' if pod.usage.cpu > pod.requests.cpu else 'is-primary' }}" value="{{ pod.usage.cpu }}" max="{{ pod.requests.cpu }}"></progress>
<progress class="progress" value="{{ row.pod.requests.cpu }}" max="{{ row.pod.requests.cpu }}"></progress>
<progress class="progress {{ 'is-danger' if row.pod.usage.cpu > row.pod.requests.cpu else 'is-primary' }}" value="{{ row.pod.usage.cpu }}" max="{{ row.pod.requests.cpu }}"></progress>
</div>
</td>
<td style="font-size: 0.75rem" data-value="{{ pod.usage.memory }}">
<td style="font-size: 0.75rem" data-value="{{ row.pod.usage.memory }}">
<div class="resource-labels">
<span>{{ pod.usage.memory|memory('MiB') }}</span> <span>{{ pod.requests.memory|memory('MiB') }}</span>
<span>{{ row.pod.usage.memory|memory('MiB') }}</span> <span>{{ row.pod.requests.memory|memory('MiB') }}</span>
</div>
<div class="resource-bar">
<progress class="progress" value="{{ pod.requests.memory }}" max="{{ pod.requests.memory }}"></progress>
<progress class="progress {{ 'is-danger' if pod.usage.memory > pod.requests.memory else 'is-primary' }}" value="{{ pod.usage.memory }}" max="{{ pod.requests.memory }}"></progress>
<progress class="progress" value="{{ row.pod.requests.memory }}" max="{{ row.pod.requests.memory }}"></progress>
<progress class="progress {{ 'is-danger' if row.pod.usage.memory > row.pod.requests.memory else 'is-primary' }}" value="{{ row.pod.usage.memory }}" max="{{ row.pod.requests.memory }}"></progress>
</div>
</td>

<td class="has-text-right">{{ pod.cost|money }}</td>
<td class="has-text-right">{{ row.pod.cost|money }}</td>
</tr>
{%endif %}
{%endfor %}
{%endfor %}
</tbody>

Expand Down

0 comments on commit 286b3d3

Please sign in to comment.