Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid crash during kubectl retry #5098

Merged
merged 2 commits into from
Sep 16, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion core/src/plugins/kubernetes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,13 @@ function getGroupBasePath(apiVersion: string) {
return apiVersion.includes("/") ? `/apis/${apiVersion}` : `/api/${apiVersion}`
}

export const KUBECTL_RETRY_OPTS: RetryOpts = {
maxRetries: 3,
minTimeoutMs: 300,
// forceRetry is important, because shouldRetry cannot handle ChildProcessError.
forceRetry: true,
}

export async function getKubeConfig(log: Log, ctx: PluginContext, provider: KubernetesProvider) {
let kubeConfigStr: string

Expand All @@ -978,7 +985,7 @@ export async function getKubeConfig(log: Log, ctx: PluginContext, provider: Kube
log,
args,
}),
{ forceRetry: true }
KUBECTL_RETRY_OPTS
)
}
return load(kubeConfigStr)!
Expand Down
15 changes: 7 additions & 8 deletions core/src/plugins/kubernetes/kubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { dedent, gardenAnnotationKey } from "../../util/string"
import { getResourceKey, hashManifest } from "./util"
import { PluginToolSpec } from "../../plugin/tools"
import { PluginContext } from "../../plugin-context"
import { KubeApi, KubernetesError } from "./api"
import { KUBECTL_RETRY_OPTS, KubeApi, KubernetesError } from "./api"
import { pathExists } from "fs-extra"
import { ChildProcessError, ConfigurationError } from "../../exceptions"
import { requestWithRetry, RetryOpts } from "./retry"
Expand Down Expand Up @@ -58,8 +58,6 @@ export interface ApplyParams {

export const KUBECTL_DEFAULT_TIMEOUT = 300

const KUBECTL_DEFAULT_RETRY_OPTS: RetryOpts = { maxRetries: 3, minTimeoutMs: 300 }

export async function apply({
log,
ctx,
Expand Down Expand Up @@ -111,7 +109,8 @@ export async function apply({

const input = Buffer.from(encodeYamlMulti(manifests))

log.debug(`Applying Kubernetes manifests:\n${input.toString()}`)
const manifestLogLevel = "debug" as const
log[manifestLogLevel](`Applying Kubernetes manifests:\n${input.toString()}`)

let args = ["apply"]
dryRun && args.push("--dry-run")
Expand All @@ -130,7 +129,7 @@ export async function apply({
args,
input,
}),
KUBECTL_DEFAULT_RETRY_OPTS
KUBECTL_RETRY_OPTS
)
} catch (e) {
if (e instanceof ChildProcessError) {
Expand All @@ -140,7 +139,7 @@ export async function apply({

${e.details.output}

Use the option "--log-level verbose" to see the kubernetes manifests that we attempted to apply through "kubectl apply".
Use the option "--log-level ${manifestLogLevel}" to see the kubernetes manifests that we attempted to apply through "kubectl apply".
`,
})
}
Expand Down Expand Up @@ -199,7 +198,7 @@ export async function deleteResourceKeys({
log,
`kubectl ${args.join(" ")}`,
() => kubectl(ctx, provider).stdout({ namespace, args, log }),
KUBECTL_DEFAULT_RETRY_OPTS
KUBECTL_RETRY_OPTS
)
}

Expand Down Expand Up @@ -228,7 +227,7 @@ export async function deleteObjectsBySelector({
log,
`kubectl ${args.join(" ")}`,
() => kubectl(ctx, provider).stdout({ namespace, args, log }),
KUBECTL_DEFAULT_RETRY_OPTS
KUBECTL_RETRY_OPTS
)
}

Expand Down