Configure Kubernetes waits, Jobs, timeouts, and cleanup
Use public HelmExecutionOptions, HelmUpgradeInstallRequest, and HelmUninstallRequest settings together so namespace creation, upgrade ownership, Job completion, operation timeouts, caller cancellation, and cleanup stay explicit.
Pin execution defaults
public sealed class DeploymentOptionsProvider : IHelmOptionsProvider
{
public ValueTask<HelmExecutionOptions> GetHelmAsync(CancellationToken cancellationToken = default)
=> ValueTask.FromResult(new HelmExecutionOptions
{
DefaultNamespace = "platform",
FieldManager = "my-deployment-service",
TimeoutSeconds = 300,
ServerSideApply = false
});
}ServerSideApply = false keeps the supported client-side apply path (lifecycle operations reject ServerSideApply = true before mutation). On upgrades with a prior deployed manifest, HelmClient performs a three-way merge that preserves live-only fields while applying chart updates. FieldManager is forwarded on dynamically discovered create/replace paths such as custom resources.
Deploy with Job waits, timeout, cancellation, and recovery
public static async Task<string> DeployWithWaitAndCleanupAsync(
string chartPath,
string valuesPath,
CancellationToken cancellationToken)
{
var client = new HelmClient(new DeploymentOptionsProvider());
var request = new HelmUpgradeInstallRequest
{
ReleaseName = "payments",
Namespace = "platform",
CreateNamespace = true,
Chart = chartPath,
ValuesFiles = [valuesPath],
Wait = true,
WaitForJobs = true,
TimeoutSeconds = 300,
Atomic = true,
CleanupOnFail = true,
DisableHooks = false
};
try
{
var result = await client.UpgradeInstallAsync(request, cancellationToken);
if (!result.Succeeded)
throw new InvalidOperationException($"Release failed: {result.StandardError}");
return result.StandardOutput;
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
// Caller cancellation propagates; never treat it as readiness success.
throw;
}
catch (TimeoutException ex)
{
// Wait deadline expired, including Pods that stayed in CrashLoopBackOff (#259)
// instead of reaching Ready=True; ex.Message carries the last observed status.
return $"Timed out waiting for readiness: {ex.Message}";
}
catch (OperationCanceledException ex)
{
// Operation-wide TimeoutSeconds expired during an API read or poll delay.
return $"Operation timed out before readiness completed: {ex.Message}";
}
catch (InvalidOperationException ex)
{
// Terminal failure (for example: Failed Pod phase, Job exceeding backoffLimit,
// Lost PVC, or autoscaling/v2 HPA with ScalingActive=False).
return $"Terminal workload or hook failure: {ex.Message}";
}
}- Namespace:
Namespacesets the default namespace for namespaced manifests withoutmetadata.namespace, whileCreateNamespace = truecreates it if absent. Explicit manifest namespaces still win, and cluster-scoped resources remain cluster-scoped. - Waits and Jobs:
Wait = truepolls the 12 supported readiness kinds on a 3-second cadence; other kinds (such asConfigMap,Secret,Ingress,CronJob,autoscaling/v1HPA, and custom resource instances) are accepted once applied without readiness polling.WaitForJobs = trueadditionally waits forbatch/v1Jobs and requiresWait = trueorAtomic = true. - CrashLoopBackOff status (#259): A Pod reporting
CrashLoopBackOffstays pending while polling forReady=True. If it recovers before the deadline, the wait succeeds; if not, the wait times out with the last observed status (Pod in CrashLoopBackOff). Only aFailedPod phase is a terminal Pod failure. - Timeout vs. caller cancellation: One positive
TimeoutSecondsvalue covers chart loading, hooks, apply, and readiness waiting. Caller cancellation always propagates asOperationCanceledException. An elapsed timeout surfaces asTimeoutException(from the waiter deadline) orOperationCanceledExceptionwithcancellationToken.IsCancellationRequested == false(when the linked operation timeout expires during an API read or poll delay). - Failure recovery:
Atomic = trueandCleanupOnFail = truerecord afailedrevision and run recovery before rethrowing. On a failed upgrade with a prior deployed revision,CleanupOnFaildeletes resources introduced only by the failed attempt andAtomicalso restores the previous deployed manifest. On a failed initial install, both delete the attempted manifest.
Uninstall with foreground propagation and deletion wait
public static Task<CommandResult> UninstallWithCleanupWaitAsync(
IHelmClient client,
CancellationToken cancellationToken)
=> client.UninstallAsync(new HelmUninstallRequest
{
ReleaseName = "payments",
Namespace = "platform",
KeepHistory = true,
Wait = true,
TimeoutSeconds = 120,
DeletionPropagation = HelmDeletionPropagation.Foreground
}, cancellationToken);UninstallAsync deletes release resources in reverse manifest order, honors helm.sh/resource-policy: keep for direct delete requests, and with Wait = true polls through KubernetesResourceWaiter.WaitForDeletedAsync until every deleted resource is observed absent. Specify a positive TimeoutSeconds (or leave it null to fall back to HelmExecutionOptions.TimeoutSeconds for hooks and deletion waiting; a non-positive value throws ArgumentOutOfRangeException when Wait = true runs after deletion). Setting KeepHistory = true records an uninstalled revision instead of purging release history.
For the full per-kind readiness predicates, hook wait and cleanup bounds, and test evidence, see the Kubernetes compatibility contract, the Action API, and the Kube API.