Skip to content

Commit 6705fb6

Browse files
authored
Merge pull request #50 from kindredgroup/feature/improve-pod-log-tail
feat: [KSBP-101406] Improve log tailing to log all pods
2 parents 38c6987 + 820c415 commit 6705fb6

3 files changed

Lines changed: 30 additions & 25 deletions

File tree

k8s-deployer/scripts/tail-container-log.sh

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,27 @@ namespace=$1
44
service=$2
55
containerName=$3
66
filter=$4
7+
logFileBase=$5
78

8-
podId=$(kubectl get pods -n $namespace -l "app.kubernetes.io/name=${service}" --field-selector="status.phase=Running" -o jsonpath="{.items[].metadata.name}")
9+
podIds=$(kubectl get pods -n "${namespace}" -l "app.kubernetes.io/name=${service}" --field-selector="status.phase=Running" -o jsonpath="{.items[*].metadata.name}")
910

10-
args="-f -n ${namespace} ${podId}"
11+
for podId in $podIds; do
12+
args=("-f" "-n" "${namespace}" "${podId}")
1113

12-
if [ "${containerName}" != "" ];
13-
then
14-
args="${args} -c ${containerName}"
15-
fi
14+
if [ "${containerName}" != "" ]; then
15+
args+=("-c" "${containerName}")
16+
fi
1617

17-
if [ "${filter}" != "" ];
18-
then
19-
args="${args} -l ${filter}"
20-
fi
18+
if [ "${filter}" != "" ]; then
19+
args+=("-l" "${filter}")
20+
fi
2121

22-
# the output will be caught by child node process whose parent is k8s-deployer
23-
kubectl logs $args
22+
if [ "${logFileBase}" != "" ]; then
23+
kubectl logs "${args[@]}" >> "${logFileBase}-${podId}.log" 2>&1 &
24+
else
25+
# the output will be caught by child node process whose parent is k8s-deployer
26+
kubectl logs "${args[@]}" &
27+
fi
28+
done
29+
30+
wait

k8s-deployer/src/pod-log-tail.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { logger } from "./logger.js"
22
import * as NodeShell from "node:child_process"
3-
import * as fs from "node:fs"
43
import { Namespace } from "./model.js"
54

5+
export const TAIL_SCRIPT = "k8s-deployer/scripts/tail-container-log.sh"
6+
67
export class PodLogTail {
78

89
private tailer?: NodeShell.ChildProcess
@@ -16,16 +17,12 @@ export class PodLogTail {
1617
start(containerName = ""): PodLogTail {
1718
if (this.tailer) throw new Error(`Tailer is already attached to process with PID: ${this.tailer.pid}`)
1819

19-
// creating streams
20-
const out = fs.openSync(this.logFilePath, 'a')
21-
const err = fs.openSync(this.logFilePath, 'a')
22-
2320
this.tailer = NodeShell.spawn(
24-
"k8s-deployer/scripts/tail-container-log.sh",
25-
[ this.namespace, this.service, containerName ],
21+
TAIL_SCRIPT,
22+
[ this.namespace, this.service, containerName, "", this.logFilePath.replace(/\.log$/, "") ],
2623
{
2724
detached: true,
28-
stdio: [ 'ignore', out, err ]
25+
stdio: 'ignore'
2926
}
3027
)
3128

@@ -43,7 +40,7 @@ export class PodLogTail {
4340
if (wasStopped) {
4441
logger.info("PodLogTail.stop(): The log tailer with PID %s has been stopped", pid)
4542
} else {
46-
logger.warn("PodLogTail.stop(): Unable to stop the log tailer with PID %s. Has it been stopped already?", pid)
43+
logger.warn("PodLogTail.stop(): Unable to stop the log tailer with PID %s. Has it been stopped already?", pid)
4744
}
4845

4946
return wasStopped

k8s-deployer/test/test-suite-handler.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ShellOptions } from "../src/shell-facade.js"
1313
import { ScalarMetric, TestOutcomeType, TestStream } from "../src/test-app-client/report/schema-v1.js"
1414
import * as webapi from "../src/test-app-client/web-api/schema-v1.js"
1515
import { generatePrefixByDate } from "../src/test-suite-handler.js"
16+
import { TAIL_SCRIPT } from "../src/pod-log-tail.js"
1617

1718
describe("Helper functions", () => {
1819
it ("should generate readable date prefix", () => {
@@ -286,13 +287,13 @@ describe("Deployment happy path", async () => {
286287
chai.expect(nodeShellSpawnStub.callCount).eq(2)
287288

288289
chai.expect(nodeShellSpawnStub.getCall(0).calledWith(
289-
"k8s-deployer/scripts/tail-container-log.sh",
290-
[ namespace, "comp-1", "comp-1-specific-container" ]
290+
TAIL_SCRIPT,
291+
[ namespace, "comp-1", "comp-1-specific-container", "", "12345_t1/logs/pod-comp-1-nsChild-comp-1-specific-container" ]
291292
)).be.true
292293

293294
chai.expect(nodeShellSpawnStub.getCall(1).calledWith(
294-
"k8s-deployer/scripts/tail-container-log.sh",
295-
[ namespace, "comp-1-test-app", "" ]
295+
TAIL_SCRIPT,
296+
[ namespace, "comp-1-test-app", "", "", "12345_t1/logs/pod-comp-1-test-app-nsChild" ]
296297
)).be.true
297298
})
298299

0 commit comments

Comments
 (0)