Skip to content

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 ​

csharp
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 ​

csharp
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: Namespace sets the default namespace for namespaced manifests without metadata.namespace, while CreateNamespace = true creates it if absent. Explicit manifest namespaces still win, and cluster-scoped resources remain cluster-scoped.
  • Waits and Jobs: Wait = true polls the 12 supported readiness kinds on a 3-second cadence; other kinds (such as ConfigMap, Secret, Ingress, CronJob, autoscaling/v1 HPA, and custom resource instances) are accepted once applied without readiness polling. WaitForJobs = true additionally waits for batch/v1 Jobs and requires Wait = true or Atomic = true.
  • CrashLoopBackOff status (#259): A Pod reporting CrashLoopBackOff stays pending while polling for Ready=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 a Failed Pod phase is a terminal Pod failure.
  • Timeout vs. caller cancellation: One positive TimeoutSeconds value covers chart loading, hooks, apply, and readiness waiting. Caller cancellation always propagates as OperationCanceledException. An elapsed timeout surfaces as TimeoutException (from the waiter deadline) or OperationCanceledException with cancellationToken.IsCancellationRequested == false (when the linked operation timeout expires during an API read or poll delay).
  • Failure recovery: Atomic = true and CleanupOnFail = true record a failed revision and run recovery before rethrowing. On a failed upgrade with a prior deployed revision, CleanupOnFail deletes resources introduced only by the failed attempt and Atomic also restores the previous deployed manifest. On a failed initial install, both delete the attempted manifest.

Uninstall with foreground propagation and deletion wait ​

csharp
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.

Released under the MIT License.