diff --git a/k8s-deployer/scripts/tail-container-log.sh b/k8s-deployer/scripts/tail-container-log.sh index 0942083..c1cc285 100755 --- a/k8s-deployer/scripts/tail-container-log.sh +++ b/k8s-deployer/scripts/tail-container-log.sh @@ -4,20 +4,27 @@ namespace=$1 service=$2 containerName=$3 filter=$4 +logFileBase=$5 -podId=$(kubectl get pods -n $namespace -l "app.kubernetes.io/name=${service}" --field-selector="status.phase=Running" -o jsonpath="{.items[].metadata.name}") +podIds=$(kubectl get pods -n "${namespace}" -l "app.kubernetes.io/name=${service}" --field-selector="status.phase=Running" -o jsonpath="{.items[*].metadata.name}") -args="-f -n ${namespace} ${podId}" +for podId in $podIds; do + args=("-f" "-n" "${namespace}" "${podId}") -if [ "${containerName}" != "" ]; -then - args="${args} -c ${containerName}" -fi + if [ "${containerName}" != "" ]; then + args+=("-c" "${containerName}") + fi -if [ "${filter}" != "" ]; -then - args="${args} -l ${filter}" -fi + if [ "${filter}" != "" ]; then + args+=("-l" "${filter}") + fi -# the output will be caught by child node process whose parent is k8s-deployer -kubectl logs $args + if [ "${logFileBase}" != "" ]; then + kubectl logs "${args[@]}" >> "${logFileBase}-${podId}.log" 2>&1 & + else + # the output will be caught by child node process whose parent is k8s-deployer + kubectl logs "${args[@]}" & + fi +done + +wait diff --git a/k8s-deployer/src/pod-log-tail.ts b/k8s-deployer/src/pod-log-tail.ts index f53db33..95c34d8 100644 --- a/k8s-deployer/src/pod-log-tail.ts +++ b/k8s-deployer/src/pod-log-tail.ts @@ -1,8 +1,9 @@ import { logger } from "./logger.js" import * as NodeShell from "node:child_process" -import * as fs from "node:fs" import { Namespace } from "./model.js" +export const TAIL_SCRIPT = "k8s-deployer/scripts/tail-container-log.sh" + export class PodLogTail { private tailer?: NodeShell.ChildProcess @@ -16,16 +17,12 @@ export class PodLogTail { start(containerName = ""): PodLogTail { if (this.tailer) throw new Error(`Tailer is already attached to process with PID: ${this.tailer.pid}`) - // creating streams - const out = fs.openSync(this.logFilePath, 'a') - const err = fs.openSync(this.logFilePath, 'a') - this.tailer = NodeShell.spawn( - "k8s-deployer/scripts/tail-container-log.sh", - [ this.namespace, this.service, containerName ], + TAIL_SCRIPT, + [ this.namespace, this.service, containerName, "", this.logFilePath.replace(/\.log$/, "") ], { detached: true, - stdio: [ 'ignore', out, err ] + stdio: 'ignore' } ) @@ -43,7 +40,7 @@ export class PodLogTail { if (wasStopped) { logger.info("PodLogTail.stop(): The log tailer with PID %s has been stopped", pid) } else { - logger.warn("PodLogTail.stop(): Unable to stop the log tailer with PID %s. Has it been stopped already?", pid) + logger.warn("PodLogTail.stop(): Unable to stop the log tailer with PID %s. Has it been stopped already?", pid) } return wasStopped diff --git a/k8s-deployer/test/test-suite-handler.spec.ts b/k8s-deployer/test/test-suite-handler.spec.ts index 134e5fc..1c5ed34 100644 --- a/k8s-deployer/test/test-suite-handler.spec.ts +++ b/k8s-deployer/test/test-suite-handler.spec.ts @@ -13,6 +13,7 @@ import { ShellOptions } from "../src/shell-facade.js" import { ScalarMetric, TestOutcomeType, TestStream } from "../src/test-app-client/report/schema-v1.js" import * as webapi from "../src/test-app-client/web-api/schema-v1.js" import { generatePrefixByDate } from "../src/test-suite-handler.js" +import { TAIL_SCRIPT } from "../src/pod-log-tail.js" describe("Helper functions", () => { it ("should generate readable date prefix", () => { @@ -286,13 +287,13 @@ describe("Deployment happy path", async () => { chai.expect(nodeShellSpawnStub.callCount).eq(2) chai.expect(nodeShellSpawnStub.getCall(0).calledWith( - "k8s-deployer/scripts/tail-container-log.sh", - [ namespace, "comp-1", "comp-1-specific-container" ] + TAIL_SCRIPT, + [ namespace, "comp-1", "comp-1-specific-container", "", "12345_t1/logs/pod-comp-1-nsChild-comp-1-specific-container" ] )).be.true chai.expect(nodeShellSpawnStub.getCall(1).calledWith( - "k8s-deployer/scripts/tail-container-log.sh", - [ namespace, "comp-1-test-app", "" ] + TAIL_SCRIPT, + [ namespace, "comp-1-test-app", "", "", "12345_t1/logs/pod-comp-1-test-app-nsChild" ] )).be.true })