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

feat(aws-eks): add atomic flag for HelmChart construct #22272

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-eks/lib/helm-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ export interface HelmChartOptions {
*/
readonly timeout?: Duration;

/**
* Whether or not Helm should treat this operation as atomic; if set, upgrade process rolls back changes
* made in case of failed upgrade. The --wait flag will be set automatically if --atomic is used.
* @default false
*/
readonly atomic?: boolean;

/**
* create namespace if not exist
* @default true
Expand Down Expand Up @@ -129,6 +136,8 @@ export class HelmChart extends Construct {
const wait = props.wait ?? false;
// default to create new namespace
const createNamespace = props.createNamespace ?? true;
// default to not use atomic operations
const atomic = props.atomic ?? false;

props.chartAsset?.grantRead(provider.handlerRole);

Expand All @@ -143,6 +152,7 @@ export class HelmChart extends Construct {
ChartAssetURL: props.chartAsset?.s3ObjectUrl,
Version: props.version,
Wait: wait || undefined, // props are stringified so we encode “false” as undefined
Atomic: atomic || undefined, // props are stringified so we encode “false” as undefined
Timeout: timeout ? `${timeout.toString()}s` : undefined, // Helm v3 expects duration instead of integer
Values: (props.values ? stack.toJsonString(props.values) : undefined),
Namespace: props.namespace ?? 'default',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def helm_handler(event, context):
chart_asset_url = props.get('ChartAssetURL', None)
version = props.get('Version', None)
wait = props.get('Wait', False)
atomic = props.get('Atomic', False)
timeout = props.get('Timeout', None)
namespace = props.get('Namespace', None)
create_namespace = props.get('CreateNamespace', None)
Expand Down Expand Up @@ -146,7 +147,7 @@ def get_chart_from_oci(tmpdir, release, repository = None, version = None):
raise Exception(f'Operation failed after {maxAttempts} attempts: {output}')


def helm(verb, release, chart = None, repo = None, file = None, namespace = None, version = None, wait = False, timeout = None, create_namespace = None):
def helm(verb, release, chart = None, repo = None, file = None, namespace = None, version = None, wait = False, atomic = False, timeout = None, create_namespace = None):
import subprocess

cmnd = ['helm', verb, release]
Expand All @@ -166,6 +167,8 @@ def helm(verb, release, chart = None, repo = None, file = None, namespace = None
cmnd.extend(['--namespace', namespace])
if wait:
cmnd.append('--wait')
if atomic:
cmnd.append("--atomic")
if not timeout is None:
cmnd.extend(['--timeout', timeout])
cmnd.extend(['--kubeconfig', kubeconfig])
Expand Down
23 changes: 23 additions & 0 deletions packages/@aws-cdk/aws-eks/test/helm-chart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,29 @@ describe('helm chart', () => {
// THEN
Template.fromStack(stack).hasResourceProperties(eks.HelmChart.RESOURCE_TYPE, { Wait: true });
});

test('should disable atomic operations by default', () => {
// GIVEN
const { stack, cluster } = testFixtureCluster();

// WHEN
new eks.HelmChart(stack, 'MyChart', { cluster, chart: 'chart' });

// THEN
const charts = Template.fromStack(stack).findResources(eks.HelmChart.RESOURCE_TYPE, { Atomic: true });
expect(Object.keys(charts).length).toEqual(0);
});
test('should enable atomic operations when specified', () => {
// GIVEN
const { stack, cluster } = testFixtureCluster();

// WHEN
new eks.HelmChart(stack, 'MyAtomicChart', { cluster, chart: 'chart', atomic: true });

// THEN
Template.fromStack(stack).hasResourceProperties(eks.HelmChart.RESOURCE_TYPE, { Atomic: true });
});

test('should disable waiting when specified as false', () => {
// GIVEN
const { stack, cluster } = testFixtureCluster();
Expand Down