Skip to content

Commit

Permalink
adding support for multiple containers in pod (#104)
Browse files Browse the repository at this point in the history
* adding support for multiple containers in pod
* update chart ver
  • Loading branch information
sepulworld authored Apr 7, 2022
1 parent f022dea commit 543e4a3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This file is used to list changes made in each version of the Dinghy-ping

## unreleased

## v1.1.0 (2022-03-29)
- [Zane]
- Adding support for multiple containers in pods

## v1.0.0 (2022-03-29)
- [Zane]
- rebuild application using Flask, dropping Responder framework
Expand Down
4 changes: 2 additions & 2 deletions charts/dinghy-ping/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apiVersion: v1
appVersion: "v1.0.0"
appVersion: "v1.1.0"
description: A Helm chart for dinghy-ping
name: dinghy-ping
version: 0.4.9
version: 0.4.10
icon: https://user-images.githubusercontent.com/538171/64288240-732bef00-cf16-11e9-830a-7b08759bffad.jpg
maintainers:
- name: sepulworld
2 changes: 1 addition & 1 deletion charts/dinghy-ping/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ spec:
- name: DINGHY_PING_HOST
value: {{ .Release.Name }}.{{- .Values.subdomain }}
- name: DD_DOGSTATSD_DISABLE
value: {{ .Values.disable_dogstatsd }}
value: "{{ .Values.disable_dogstatsd }}"
- name: MY_CLUSTER_DOMAIN
value: {{ .Values.subdomain }}
ports:
Expand Down
51 changes: 37 additions & 14 deletions dinghy_ping/services/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,10 @@ def list_pods():
)
def dinghy_get_pods():
"""Form input page for pod logs and describe, input namespace"""

resp = render_template("pods_tabbed.html", namespaces=_get_all_namespaces())
tail_lines = int(request.args.get("tail_lines", default=TAIL_LINES_DEFAULT))
resp = render_template(
"pods_tabbed.html", tail_lines=tail_lines, namespaces=_get_all_namespaces()
)

return resp

Expand All @@ -453,7 +455,7 @@ def dinghy_get_pods():
metric="dinghy_ping_events_render_namespace_event_page.timer", tags=dd_tags
)
def dinghy_get_namespace_events():
"""Render landing page to select namespace for event stream"""
"""render landing page to select namespace for event stream"""

all_namespaces = _get_all_namespaces()

Expand Down Expand Up @@ -492,11 +494,13 @@ def form_input_pod_logs():
pod = request.args.get("pod")
namespace = request.args.get("namespace", "default")
tail_lines = request.args.get("tail_lines", TAIL_LINES_DEFAULT)
container = request.args.get("container", "")

logging.debug(f"Retrieving pod logs... {pod} in namespace {namespace}")
logging.debug(f"Retrieving pods... {pod} in namespace {namespace}")
logging.debug(f"Retrieving container logs... {container} in pod {pod}")

try:
ret = _get_pod_logs(pod, namespace, tail_lines)
ret = _get_pod_logs(pod, namespace, container, tail_lines)
resp = render_template("pod_logs_output.html", logs=ret)

return resp
Expand All @@ -512,11 +516,13 @@ def form_input_pod_logs_stream(*, tail_lines=TAIL_LINES_DEFAULT):
"""List pods in namespace and click on one to display logs"""
pod = request.args["pod"]
namespace = request.args["namespace"]
container = request.args.get("container", "")

resp = render_template(
"pod_logs_output_streaming.html",
namespace=namespace,
name=pod,
container=container,
dinghy_ping_web_socket_host=dinghy_ping_web_socket_host,
)

Expand Down Expand Up @@ -552,10 +558,12 @@ def log_stream_websocket(ws):

name = request.args["name"]
namespace = request.args["namespace"]
container = request.args["container"]

resp = k8s_client.read_namespaced_pod_log(
name,
namespace,
container=container,
tail_lines=TAIL_LINES_DEFAULT,
follow=True,
_preload_content=False,
Expand Down Expand Up @@ -691,16 +699,28 @@ def _get_deployment_logs(namespace, name, tail_lines=TAIL_LINES_DEFAULT):
return logs


def _get_pod_logs(pod, namespace, tail_lines=TAIL_LINES_DEFAULT):
def _get_pod_logs(pod, namespace, container, tail_lines=TAIL_LINES_DEFAULT):
"""Read pod logs"""
k8s_client = client.CoreV1Api()
ret = []
try:
ret = k8s_client.read_namespaced_pod_log(
pod, namespace, tail_lines=TAIL_LINES_DEFAULT
)
except ApiException as e:
logging.error(f"Exception when calling CoreV1Api->read_namespaced_pod_log: {e}")
if container:
try:
ret = k8s_client.read_namespaced_pod_log(
pod, namespace, container=container, tail_lines=tail_lines
)
except ApiException as e:
logging.error(
f"Exception HERE when calling CoreV1Api->read_namespaced_pod_log: {e}"
)
else:
try:
ret = k8s_client.read_namespaced_pod_log(
pod, namespace, tail_lines=tail_lines
)
except ApiException as e:
logging.error(
f"Exception when calling CoreV1Api->read_namespaced_pod_log: {e}"
)

return ret

Expand Down Expand Up @@ -755,7 +775,7 @@ def _get_all_namespaces():


def _get_all_pods(namespace=None):
"""Get all pods"""
"""Get all pods and return dict of pods with their containers and their namespaces"""
pods = {}
k8s_client = client.CoreV1Api()

Expand All @@ -767,7 +787,10 @@ def _get_all_pods(namespace=None):
for i in ret.items:
pod = i.metadata.name
namespace = i.metadata.namespace
pods.update({pod: i.metadata.namespace})
containers = []
for container in i.spec.containers:
containers.append(container.name)
pods[pod] = dict(namespace=namespace, containers=containers)

return pods

Expand Down
16 changes: 13 additions & 3 deletions dinghy_ping/views/templates/deployment_describe_output.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ <h1 class="text-xl font-semibold hover:text-green-400"><a href="" class="no-unde
<div class="font-bold text-base mb-2"> Available Replicas: {{ deployment.status.available_replicas }}</div>
<div class="font-bold text-base mb-2"> Ready Replicas: {{ deployment.status.ready_replicas }}</div>
<div class="text-base mb-2"> <a class="font-bold no-underline hover:underline" href="/get/pod-details?namespace={{ deployment.metadata.namespace}}&filter={{ deployment.metadata.name }}">Deployment PODS </a>
<br/>
<table>
{% for pod, pod_obj in all_pods.items() %}
{{pod}} <a href="/input-pod-logs?namespace={{deployment.metadata.namespace}}&pod={{pod}}&tail_lines={{tail_lines}}" class="no-underline hover:underline text-blue-500 text-lg">logs</a> <a href="/input-pod-logs-stream?namespace={{deployment.metadata.namespace}}&pod={{pod}}&tail_lines={{tail_lines}}" class="no-underline hover:underline text-blue-500 text-lg">tail</a> <a href="/pod-describe?namespace={{deployment.metadata.namespace}}&pod={{pod}}" class="no-underline hover:underline text-blue-500 text-lg"> describe</a>
<br/>
<tr>
{{pod}}
{% for container in pod_obj.spec.containers %}
<td class="-mb-px mr-1">
<div class="bg-blue-500 text-white font-bold py-2 px-4 rounded">
<a href="/input-pod-logs?namespace={{deployment.metadata.namespace}}&pod={{pod}}&container={{container.name}}&tail_lines={{tail_lines}}" class="no-underline hover:underline text-white text-lg">
{{container.name}}</a> <a href="/input-pod-logs-stream?namespace={{deployment.metadata.namespace}}&pod={{pod}}&container={{container.name}}&tail_lines={{tail_lines}}" class="no-underline hover:underline text-white text-lg">tail</a> <a href="/pod-describe?namespace={{deployment.metadata.namespace}}&pod={{pod}}&container={{container.name}}" class="no-underline hover:underline text-white text-lg"> describe</a>
</div>
</td>
{% endfor %}
{% endfor %}
</tr>
</table>
</div>
<div class="font-bold text-base mb-2"> Created: {{ deployment.metadata.creation_timestamp }}</div>
<div class="px-6 py-4 m-4 max-w-full max-h-full rounded shadow-lg border border-green-300 bg-gray-100">
Expand Down
17 changes: 15 additions & 2 deletions dinghy_ping/views/templates/pod_logs_input.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@
<div class="px-6 py-4">
<h1 class="text-xl font-semibold hover:text-green-400"><a href="/get/pods" class="no-underline hover:underline">Dinghy Ping - Pods</a></h1>
<br>
{% for pod, namespace in all_pods.items() %}
{{namespace}} - {{pod}} <a href="/input-pod-logs?namespace={{namespace}}&pod={{pod}}&tail_lines={{tail_lines}}" class="no-underline hover:underline text-blue-500 text-lg">logs</a> <a href="/input-pod-logs-stream?namespace={{namespace}}&pod={{pod}}&tail_lines={{tail_lines}}" class="no-underline hover:underline text-blue-500 text-lg">tail</a> <a href="/pod-describe?namespace={{namespace}}&pod={{pod}}" class="no-underline hover:underline text-blue-500 text-lg"> describe</a>
{% for pod, data in all_pods.items() %}
<table>
{{data.namespace}} - {{pod}}
<tr>
{% for container in data.containers %}
</div>
<td class="-mb-px mr-1">
<div class="bg-blue-500 text-white font-bold py-2 px-4 rounded">
<a href="/input-pod-logs?namespace={{data.namespace}}&pod={{pod}}&container={{container}}&tail_lines={{tail_lines}}" class="no-underline hover:underline text-white text-lg">
{{container}}</a> <a href="/input-pod-logs-stream?namespace={{data.namespace}}&pod={{pod}}&container={{container}}&tail_lines={{tail_lines}}" class="no-underline hover:underline text-white text-lg">tail</a> <a href="/pod-describe?namespace={{data.namespace}}&pod={{pod}}" class="no-underline hover:underline text-white text-lg"> describe</a>
</div>
</td>
{% endfor %}
<br>
{% endfor %}
</tr>
</table>
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion dinghy_ping/views/templates/pod_logs_output_streaming.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}

function connect() {
var ws = new WebSocket('{{dinghy_ping_web_socket_host}}/ws/logstream?namespace={{namespace}}&name={{name}}');
var ws = new WebSocket('{{dinghy_ping_web_socket_host}}/ws/logstream?namespace={{namespace}}&name={{name}}&container={{container}}');

ws.onmessage = function(event) {
let message = event.data;
Expand Down

0 comments on commit 543e4a3

Please sign in to comment.