Skip to content

Commit

Permalink
refactor: extracted helper method to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
vvagaytsev committed Oct 14, 2022
1 parent 9b50102 commit bf31f78
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions core/src/plugins/kubernetes/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,14 +810,6 @@ export class PodRunner extends PodRunnerParams {
let mainContainerLogs = ""
const mainContainerName = this.getMainContainerName()

const getDebugLogs = async () => {
try {
return this.getMainContainerLogs()
} catch (err) {
return ""
}
}

const stream = new Stream<RunLogEntry>()
void stream.forEach((entry) => {
const { msg, timestamp } = entry
Expand Down Expand Up @@ -853,13 +845,13 @@ export class PodRunner extends PodRunnerParams {
const exitReason = terminated?.reason
const exitCode = terminated?.exitCode

// We've seen instances were Pods are OOMKilled but the exit code is 0 and the state that
// Garden computes is "stopped". However in those instances the exitReason is still "OOMKilled"
// We've seen instances where Pods are OOMKilled but the exit code is 0 and the state that
// Garden computes is "stopped". However, in those instances the exitReason is still "OOMKilled"
// and we handle that case specifically here.
if (exitCode === 137 || exitReason === "OOMKilled") {
const msg = `Pod container was OOMKilled.`
throw new OutOfMemoryError(msg, {
logs: (await getDebugLogs()) || msg,
logs: (await this.getDebugLogs()) || msg,
serverPod,
})
}
Expand Down Expand Up @@ -898,7 +890,7 @@ export class PodRunner extends PodRunnerParams {
if (timeoutSec && elapsed > timeoutSec) {
const msg = `Command timed out after ${timeoutSec} seconds.`
throw new TimeoutError(msg, {
logs: (await getDebugLogs()) || msg,
logs: (await this.getDebugLogs()) || msg,
serverPod,
})
}
Expand Down Expand Up @@ -988,17 +980,9 @@ export class PodRunner extends PodRunnerParams {
}

if (result.exitCode === 137) {
const getDebugLogs = async () => {
try {
return this.getMainContainerLogs()
} catch (err) {
return ""
}
}

const msg = `Pod container was OOMKilled.`
throw new OutOfMemoryError(msg, {
logs: (await getDebugLogs()) || msg,
logs: (await this.getDebugLogs()) || msg,
execParams: params,
})
}
Expand Down Expand Up @@ -1035,6 +1019,14 @@ export class PodRunner extends PodRunnerParams {
return allLogs.find((l) => l.containerName === this.getMainContainerName())?.log || ""
}

async getDebugLogs() {
try {
return this.getMainContainerLogs()
} catch (err) {
return ""
}
}

/**
* Removes the Pod from the cluster, if it's running. You can safely call this even
* if the process is no longer active.
Expand Down

0 comments on commit bf31f78

Please sign in to comment.