Skip to content

Commit

Permalink
chore(eks): support create namespace with helm (#8787)
Browse files Browse the repository at this point in the history
chore(eks): support create namespace with helm 

- add `createNamespace` property for `eks.HelmChart`
- set the `wait` flag correctly for HelmChart custom resource

Fixes: #7216 #7209 #8713

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
pahud committed Jun 30, 2020
1 parent 13deb26 commit 973fa42
Show file tree
Hide file tree
Showing 5 changed files with 408 additions and 453 deletions.
14 changes: 13 additions & 1 deletion packages/@aws-cdk/aws-eks/lib/helm-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export interface HelmChartOptions {
* @default Duration.minutes(5)
*/
readonly timeout?: Duration;

/**
* create namespace if not exist
* @default true
*/
readonly createNamespace?: boolean;
}

/**
Expand Down Expand Up @@ -90,6 +96,11 @@ export class HelmChart extends Construct {
throw new Error('Helm chart timeout cannot be higher than 15 minutes.');
}

// default not to wait
const wait = props.wait ?? false;
// default to create new namespace
const createNamespace = props.createNamespace ?? true;

new CustomResource(this, 'Resource', {
serviceToken: provider.serviceToken,
resourceType: HelmChart.RESOURCE_TYPE,
Expand All @@ -99,11 +110,12 @@ export class HelmChart extends Construct {
Release: props.release ?? this.node.uniqueId.slice(-53).toLowerCase(), // Helm has a 53 character limit for the name
Chart: props.chart,
Version: props.version,
Wait: props.wait ?? false,
Wait: wait || 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',
Repository: props.repository,
CreateNamespace: createNamespace || undefined,
},
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def helm_handler(event, context):
wait = props.get('Wait', False)
timeout = props.get('Timeout', None)
namespace = props.get('Namespace', None)
create_namespace = props.get('CreateNamespace', None)
repository = props.get('Repository', None)
values_text = props.get('Values', None)

Expand All @@ -46,21 +47,23 @@ def helm_handler(event, context):
f.write(json.dumps(values, indent=2))

if request_type == 'Create' or request_type == 'Update':
helm('upgrade', release, chart, repository, values_file, namespace, version, wait, timeout)
helm('upgrade', release, chart, repository, values_file, namespace, version, wait, timeout, create_namespace)
elif request_type == "Delete":
try:
helm('uninstall', release, namespace=namespace, timeout=timeout)
except Exception as e:
logger.info("delete error: %s" % e)

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

cmnd = ['helm', verb, release]
if not chart is None:
cmnd.append(chart)
if verb == 'upgrade':
cmnd.append('--install')
if create_namespace:
cmnd.append('--create-namespace')
if not repo is None:
cmnd.extend(['--repo', repo])
if not file is None:
Expand Down

0 comments on commit 973fa42

Please sign in to comment.