Skip to content

Commit

Permalink
feat: add atomic flag for aws-eks HelmChart
Browse files Browse the repository at this point in the history
fixes: aws#22254
  • Loading branch information
michaelfedell authored Sep 27, 2022
1 parent d87a651 commit 86025da
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
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

0 comments on commit 86025da

Please sign in to comment.