Skip to content

Commit 50e14cf

Browse files
Switch exec pod promise to reject on websocket error (#127)
* Switch exec pod promise to reject on websocket error * Fix incorrectly resolved merge conflict * Apply suggestions from code review Co-authored-by: Ferenc Hammerl <31069338+fhammerl@users.noreply.github.com> --------- Co-authored-by: Ferenc Hammerl <31069338+fhammerl@users.noreply.github.com>
1 parent 921be5b commit 50e14cf

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

packages/k8s/src/k8s/index.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -228,31 +228,36 @@ export async function execPodStep(
228228
): Promise<void> {
229229
const exec = new k8s.Exec(kc)
230230
command = fixArgs(command)
231-
await new Promise(async function (resolve, reject) {
232-
await exec.exec(
233-
namespace(),
234-
podName,
235-
containerName,
236-
command,
237-
process.stdout,
238-
process.stderr,
239-
stdin ?? null,
240-
false /* tty */,
241-
resp => {
242-
// kube.exec returns an error if exit code is not 0, but we can't actually get the exit code
243-
if (resp.status === 'Success') {
244-
resolve(resp.code)
245-
} else {
246-
core.debug(
247-
JSON.stringify({
248-
message: resp?.message,
249-
details: resp?.details
250-
})
251-
)
252-
reject(resp?.message)
231+
// Exec returns a websocket. If websocket fails, we should reject the promise. Otherwise, websocket will call a callback. Since at that point, websocket is not failing, we can safely resolve or reject the promise.
232+
await new Promise(function (resolve, reject) {
233+
exec
234+
.exec(
235+
namespace(),
236+
podName,
237+
containerName,
238+
command,
239+
process.stdout,
240+
process.stderr,
241+
stdin ?? null,
242+
false /* tty */,
243+
resp => {
244+
// kube.exec returns an error if exit code is not 0, but we can't actually get the exit code
245+
if (resp.status === 'Success') {
246+
resolve(resp.code)
247+
} else {
248+
core.debug(
249+
JSON.stringify({
250+
message: resp?.message,
251+
details: resp?.details
252+
})
253+
)
254+
reject(resp?.message)
255+
}
253256
}
254-
}
255-
)
257+
)
258+
// If exec.exec fails, explicitly reject the outer promise
259+
// eslint-disable-next-line github/no-then
260+
.catch(e => reject(e))
256261
})
257262
}
258263

0 commit comments

Comments
 (0)