Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions k8s-deployer/scripts/tail-container-log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 6 additions & 9 deletions k8s-deployer/src/pod-log-tail.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'
}
)

Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions k8s-deployer/test/test-suite-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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
})

Expand Down
Loading