Skip to content

Commit 2db42b1

Browse files
authored
Merge pull request #20 from paulbouwer/improve_info_displayed
refactor: improve displayed information
2 parents e9c6f44 + cacb3a0 commit 2db42b1

File tree

8 files changed

+69
-11
lines changed

8 files changed

+69
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
This container image can be deployed on a Kubernetes cluster. It runs a web app, that displays the following:
66

77
- a default **Hello world!** message
8-
- the pod name
9-
- node os information
8+
- namespace, pod, and node details
9+
- container image details
1010

1111
![Hello world! from the hello-kubernetes image](hello-kubernetes.png)
1212

deploy/helm/hello-kubernetes/templates/deployment.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ spec:
5050
- name: RENDER_PATH_PREFIX
5151
value: "{{ .Values.ingress.pathPrefix }}"
5252
{{- end }}
53+
- name: KUBERNETES_NAMESPACE
54+
valueFrom:
55+
fieldRef:
56+
fieldPath: metadata.namespace
57+
- name: KUBERNETES_POD_NAME
58+
valueFrom:
59+
fieldRef:
60+
fieldPath: metadata.name
61+
- name: KUBERNETES_NODE_NAME
62+
valueFrom:
63+
fieldRef:
64+
fieldPath: spec.nodeName
65+
- name: CONTAINER_IMAGE
66+
value: "{{ .Values.deployment.container.image.repository }}:{{ .Values.deployment.container.image.tag | default .Chart.AppVersion }}"
5367
{{- with .Values.deployment.resources }}
5468
resources:
5569
{{- toYaml . | nindent 12 }}

hello-kubernetes.png

20.7 KB
Loading

src/app/README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Hello Kubernetes demo app
22

3-
A demo app that can be deployd to a Kubernetes cluster. It displays a message, the name of the pod and details of the node it's deployed to.
3+
A demo app that can be deployd to a Kubernetes cluster. It displays a message, and also namespace, pod, node and image details.
44

55
## Paths
66

@@ -17,5 +17,15 @@ The application can be configured via the following environment variables.
1717
| PORT | No | 8080 | The port that the app listens on. |
1818
| MESSAGE | No | "Hello world!" | The message displayed by the app. |
1919
| RENDER_PATH_PREFIX | No | "" | The path prefix to use when rendering the urls for the static assets in the handlebar templates. <br/> Must be used when app is deployed with an ingress using a backend path for traffic to app. |
20-
| HANDLER_PATH_PREFIX | No | "" | The path prefix to use by handlers when serving the static asset and services. <br/> Note: Must be used when app is deployed with an ingress that has a backend path for traffic to the app, but which does not rewrite the backend paths to '/'. |
20+
| HANDLER_PATH_PREFIX | No | "" | The path prefix to use by handlers when serving the dynamic and static assets. <br/> Note: Must be used when app is deployed with an ingress that has a backend path for traffic to the app, but which does not rewrite the backend paths to '/'. |
21+
| KUBERNETES_NAMESPACE | Yes | "-" | The Kubernetes namespace that the app has been deployed to. |
22+
| KUBERNETES_POD_NAME | Yes | hostname | The name of the Kubernetes pod that the app is deployed into. |
23+
| KUBERNETES_NODE_NAME | Yes | "-" | The name of the Kubernetes node that the app is deployed on. |
24+
| CONTAINER_IMAGE | Yes | "paulbouwer/hello-kubernetes:$(cat package.json \| jq -r .version)" | The name and tag of the container image running the app. |
2125

26+
The application relies on the following files for configuration and operational information.
27+
28+
| File | Required | Information | Description |
29+
| ---- | -------- | ----------- | ----------- |
30+
| package.json | Yes | `.version` | The release version is used when the CONTAINER_IMAGE env is not provided. |
31+
| info.json | Yes | `.containerImageArch` | The container image architecture is used for display. This file will be overwritten in future versions as part of the container image build process when multi-arch images are supported. |

src/app/info.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"containerImageArch": "linux/amd64"
3+
}

src/app/server.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express');
22
const exphbs = require('express-handlebars');
33
const os = require("os");
4+
const fs = require('fs');
45

56
const pino = require('pino');
67
const expressPino = require('express-pino-logger');
@@ -27,13 +28,25 @@ var handlerPathPrefix = (
2728
''
2829
);
2930

31+
var namespace = process.env.KUBERNETES_NAMESPACE || '-';
32+
var podName = process.env.KUBERNETES_POD_NAME || os.hostname();
33+
var nodeName = process.env.KUBERNETES_NODE_NAME || '-';
34+
var nodeOS = os.type() + ' ' + os.release();
35+
var applicationVersion = JSON.parse(fs.readFileSync('package.json', 'utf8')).version;
36+
var containerImage = process.env.CONTAINER_IMAGE || 'paulbouwer/hello-kubernetes:' + applicationVersion
37+
var containerImageArch = JSON.parse(fs.readFileSync('info.json', 'utf8')).containerImageArch;
38+
3039
logger.debug();
3140
logger.debug('Configuration');
3241
logger.debug('-----------------------------------------------------');
3342
logger.debug('PORT=' + process.env.PORT);
3443
logger.debug('MESSAGE=' + process.env.MESSAGE);
3544
logger.debug('RENDER_PATH_PREFIX=' + process.env.RENDER_PATH_PREFIX);
3645
logger.debug('HANDLER_PATH_PREFIX=' + process.env.HANDLER_PATH_PREFIX);
46+
logger.debug('KUBERNETES_NAMESPACE=' + process.env.KUBERNETES_NAMESPACE);
47+
logger.debug('KUBERNETES_POD_NAME=' + process.env.KUBERNETES_POD_NAME);
48+
logger.debug('KUBERNETES_NODE_NAME=' + process.env.KUBERNETES_NODE_NAME);
49+
logger.debug('CONTAINER_IMAGE=' + process.env.CONTAINER_IMAGE);
3750

3851
// Handlers
3952

@@ -50,9 +63,10 @@ logger.debug('Serving from base path "' + handlerPathPrefix + '"');
5063
app.get(handlerPathPrefix + '/', function (req, res) {
5164
res.render('home', {
5265
message: message,
53-
platform: os.type(),
54-
release: os.release(),
55-
hostName: os.hostname(),
66+
namespace: namespace,
67+
pod: podName,
68+
node: nodeName + ' (' + nodeOS + ')',
69+
container: containerImage + ' (' + containerImageArch + ')',
5670
renderPathPrefix: renderPathPrefix
5771
});
5872
});
@@ -64,5 +78,5 @@ logger.debug('Server');
6478
logger.debug('-----------------------------------------------------');
6579

6680
app.listen(port, function () {
67-
logger.info("Listening on: http://%s:%s", os.hostname(), port);
81+
logger.info("Listening on: http://%s:%s", podName, port);
6882
});

src/app/static/css/main.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ div.main img {
1515
div.content {
1616
color:#f2f2f2;
1717
}
18+
1819
.content #message {
1920
margin: 10px 0 50px 0;
2021
padding: 30px 0;
@@ -26,6 +27,16 @@ div.content {
2627
border-bottom: 2px solid #909090;
2728
}
2829

30+
.content #footer {
31+
margin: 50px 0;
32+
padding: 30px 0;
33+
font-family: 'Ubuntu', sans-serif;
34+
font-weight: 300;
35+
font-size: 12pt;
36+
color: #909090;
37+
border-top: 2px solid #909090;
38+
}
39+
2940
.content #info {
3041
margin: 0 auto;
3142
font-family: 'Ubuntu', sans-serif;

src/app/views/home.handlebars

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
</div>
44
<div id="info">
55
<table>
6+
<tr>
7+
<th>namespace:</th>
8+
<td>{{ namespace }}</td>
9+
</tr>
610
<tr>
711
<th>pod:</th>
8-
<td>{{ hostName }}</td>
12+
<td>{{ pod }}</td>
913
</tr>
1014
<tr>
1115
<th>node:</th>
12-
<td>{{ platform }} ({{ release }})</td>
16+
<td>{{ node }}</td>
1317
</tr>
1418
</table>
15-
19+
</div>
20+
<div id="footer">
21+
{{ container }}
1622
</div>

0 commit comments

Comments
 (0)