Skip to content

Commit

Permalink
improvement(k8s): better error logging for kubectl port forwards
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Sep 20, 2019
1 parent 3fb518d commit 5a5d539
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
28 changes: 26 additions & 2 deletions garden-service/src/plugins/kubernetes/port-forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { KubernetesResource } from "./types"
import { ForwardablePort } from "../../types/service"
import { isBuiltIn } from "./util"
import { LogEntry } from "../../logger/log-entry"
import { RuntimeError } from "../../exceptions"

// TODO: implement stopPortForward handler

Expand Down Expand Up @@ -90,8 +91,25 @@ export async function getPortForward(
log.silly(`Running 'kubectl ${portForwardArgs.join(" ")}'`)

const proc = await kubectl.spawn({ log, provider: k8sCtx.provider, namespace, args: portForwardArgs })
let output = ""

return new Promise((resolve, reject) => {
let resolved = false

const portForward = { targetResource, port, proc, localPort }

proc.on("close", (code) => {
if (registeredPortForwards[key]) {
delete registeredPortForwards[key]
}
if (!resolved) {
reject(new RuntimeError(
`Port forward exited with code ${code} before establishing connection:\n\n${output}`,
{ code, portForward },
))
}
})

return new Promise((resolve) => {
proc.on("error", (error) => {
!proc.killed && proc.kill()
throw error
Expand All @@ -100,13 +118,19 @@ export async function getPortForward(
proc.stdout!.on("data", (line) => {
// This is unfortunately the best indication that we have that the connection is up...
log.silly(`[${targetResource} port forwarder] ${line}`)
output += line

if (line.toString().includes("Forwarding from ")) {
const portForward = { targetResource, port, proc, localPort }
registeredPortForwards[key] = portForward
resolved = true
resolve(portForward)
}
})

proc.stderr!.on("data", (line) => {
log.silly(`[${targetResource} port forwarder] ${line}`)
output += line
})
})
}))
}
Expand Down
18 changes: 15 additions & 3 deletions garden-service/src/util/ext-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,14 @@ export class BinaryCmd extends Library {
if (!args) {
args = []
}
if (!cwd) {
cwd = dirname(path)
}

log.debug(`Execing ${path} ${args.join(" ")}`)
log.debug(`Execing '${path} ${args.join(" ")}' in ${cwd}`)

const proc = execa(path, args, {
cwd: cwd || dirname(path),
cwd,
timeout: this.getTimeout(timeout) * 1000,
env,
input,
Expand Down Expand Up @@ -302,7 +305,16 @@ export class BinaryCmd extends Library {

async spawn({ args, cwd, env, log }: ExecParams) {
const path = await this.getPath(log)
return crossSpawn(path, args || [], { cwd: cwd || dirname(path), env })

if (!args) {
args = []
}
if (!cwd) {
cwd = dirname(path)
}

log.debug(`Spawning '${path} ${args.join(" ")}' in ${cwd}`)
return crossSpawn(path, args, { cwd, env })
}

async spawnAndWait({ args, cwd, env, log, ignoreError, outputStream, timeout, tty }: SpawnParams) {
Expand Down

0 comments on commit 5a5d539

Please sign in to comment.