From 5033cba9c871bcbe1891c22e54cf32b6a8010661 Mon Sep 17 00:00:00 2001 From: mtrspringer Date: Thu, 4 Apr 2024 12:37:16 +0100 Subject: [PATCH] add support for passing helm chart values to aws-load-balancer-controller --- .../aws-cdk-lib/aws-eks/lib/alb-controller.ts | 22 +++++ .../aws-eks/test/alb-controller.test.ts | 84 +++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/packages/aws-cdk-lib/aws-eks/lib/alb-controller.ts b/packages/aws-cdk-lib/aws-eks/lib/alb-controller.ts index 5dfe0ab11dbcf..42a6088d31b2b 100644 --- a/packages/aws-cdk-lib/aws-eks/lib/alb-controller.ts +++ b/packages/aws-cdk-lib/aws-eks/lib/alb-controller.ts @@ -241,6 +241,27 @@ export interface AlbControllerOptions { * @default - Corresponds to the predefined version. */ readonly policy?: any; + + /** + * Override values to be used by the chart. + * For nested values use a nested dictionary. For example: + * values: { + * autoscaling: false, + * ingressClassParams: { create: true } + * } + * + * Note that the following values are set by the controller and cannot be overridden: + * - clusterName + * - serviceAccount.create + * - serviceAccount.name + * - region + * - vpcId + * - image.repository + * - image.tag + * + * @default - No values are provided to the chart. + */ + readonly values?: {[key: string]: any}; } /** @@ -312,6 +333,7 @@ export class AlbController extends Construct { wait: true, timeout: Duration.minutes(15), values: { + ...(props.values ?? {}), clusterName: props.cluster.clusterName, serviceAccount: { create: false, diff --git a/packages/aws-cdk-lib/aws-eks/test/alb-controller.test.ts b/packages/aws-cdk-lib/aws-eks/test/alb-controller.test.ts index 3459b772eb8f7..b6006ca835a13 100644 --- a/packages/aws-cdk-lib/aws-eks/test/alb-controller.test.ts +++ b/packages/aws-cdk-lib/aws-eks/test/alb-controller.test.ts @@ -118,3 +118,87 @@ test('correct helm chart version is set for selected alb controller version', () }, }); }); + +test('can pass values to the aws-load-balancer-controller helm chart', () => { + const { stack } = testFixture(); + + const cluster = new Cluster(stack, 'Cluster', { + version: KubernetesVersion.V1_27, + }); + + AlbController.create(stack, { + cluster, + version: AlbControllerVersion.V2_6_2, + repository: 'custom', + values: { + enableWafv2: false, + }, + }); + + Template.fromStack(stack).hasResourceProperties(HelmChart.RESOURCE_TYPE, { + Version: '1.6.2', // The helm chart version associated with AlbControllerVersion.V2_6_2 + Values: { + 'Fn::Join': [ + '', + [ + '{"enableWafv2":false,"clusterName":"', + { + Ref: 'Cluster9EE0221C', + }, + '","serviceAccount":{"create":false,"name":"aws-load-balancer-controller"},"region":"us-east-1","vpcId":"', + { + Ref: 'ClusterDefaultVpcFA9F2722', + }, + '","image":{"repository":"custom","tag":"v2.6.2"}}', + ], + ], + }, + }); +}); + +test('values passed to the aws-load-balancer-controller do not override values set by construct', () => { + const { stack } = testFixture(); + + const cluster = new Cluster(stack, 'Cluster', { + version: KubernetesVersion.V1_27, + }); + + AlbController.create(stack, { + cluster, + version: AlbControllerVersion.V2_6_2, + repository: 'custom', + values: { + clusterName: 'test-cluster', + serviceAccount: { + create: true, + name: 'test-service-account', + }, + region: 'test-region', + vpcId: 'test-vpc-id', + image: { + repository: 'test-repository', + tag: 'test-tag', + }, + }, + }); + + Template.fromStack(stack).hasResourceProperties(HelmChart.RESOURCE_TYPE, { + Version: '1.6.2', // The helm chart version associated with AlbControllerVersion.V2_6_2 + Values: { + 'Fn::Join': [ + '', + [ + '{"clusterName":"', + { + Ref: 'Cluster9EE0221C', + }, + '","serviceAccount":{"create":false,"name":"aws-load-balancer-controller"},"region":"us-east-1","vpcId":"', + { + Ref: 'ClusterDefaultVpcFA9F2722', + }, + '","image":{"repository":"custom","tag":"v2.6.2"}}', + ], + ], + }, + }); +});