Skip to content

Commit 7e824ab

Browse files
authored
fix(eks): Cluster.FromClusterAttributes ignores KubectlLambdaRole (#20373)
This PR will fix #20008. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 85604d9 commit 7e824ab

File tree

2 files changed

+65
-29
lines changed

2 files changed

+65
-29
lines changed

packages/@aws-cdk/aws-eks/lib/cluster.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,6 +2054,7 @@ class ImportedCluster extends ClusterBase {
20542054
this.clusterName = props.clusterName;
20552055
this.clusterArn = this.stack.formatArn(clusterArnComponents(props.clusterName));
20562056
this.kubectlRole = props.kubectlRoleArn ? iam.Role.fromRoleArn(this, 'KubectlRole', props.kubectlRoleArn) : undefined;
2057+
this.kubectlLambdaRole = props.kubectlLambdaRole;
20572058
this.kubectlSecurityGroup = props.kubectlSecurityGroupId ? ec2.SecurityGroup.fromSecurityGroupId(this, 'KubectlSecurityGroup', props.kubectlSecurityGroupId) : undefined;
20582059
this.kubectlEnvironment = props.kubectlEnvironment;
20592060
this.kubectlPrivateSubnets = props.kubectlPrivateSubnetIds ? props.kubectlPrivateSubnetIds.map((subnetid, index) => ec2.Subnet.fromSubnetId(this, `KubectlSubnet${index}`, subnetid)) : undefined;

packages/@aws-cdk/aws-eks/test/cluster.test.ts

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as cdk8s from 'cdk8s';
1111
import * as constructs from 'constructs';
1212
import * as YAML from 'yaml';
1313
import * as eks from '../lib';
14+
import { HelmChart } from '../lib';
1415
import { KubectlProvider } from '../lib/kubectl-provider';
1516
import { BottleRocketImage } from '../lib/private/bottlerocket';
1617
import { testFixture, testFixtureNoVpc } from './util';
@@ -2422,43 +2423,77 @@ describe('cluster', () => {
24222423

24232424
});
24242425

2425-
test('kubectl provider passes iam role environment to kube ctl lambda', () => {
2426+
describe('kubectl provider passes iam role environment to kube ctl lambda', ()=>{
2427+
test('new cluster', () => {
24262428

2427-
const { stack } = testFixture();
2429+
const { stack } = testFixture();
24282430

2429-
const kubectlRole = new iam.Role(stack, 'KubectlIamRole', {
2430-
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
2431-
});
2431+
const kubectlRole = new iam.Role(stack, 'KubectlIamRole', {
2432+
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
2433+
});
24322434

2433-
// using _ syntax to silence warning about _cluster not being used, when it is
2434-
const cluster = new eks.Cluster(stack, 'Cluster1', {
2435-
version: CLUSTER_VERSION,
2436-
prune: false,
2437-
endpointAccess: eks.EndpointAccess.PRIVATE,
2438-
kubectlLambdaRole: kubectlRole,
2439-
});
2435+
// using _ syntax to silence warning about _cluster not being used, when it is
2436+
const cluster = new eks.Cluster(stack, 'Cluster1', {
2437+
version: CLUSTER_VERSION,
2438+
prune: false,
2439+
endpointAccess: eks.EndpointAccess.PRIVATE,
2440+
kubectlLambdaRole: kubectlRole,
2441+
});
24402442

2441-
cluster.addManifest('resource', {
2442-
kind: 'ConfigMap',
2443-
apiVersion: 'v1',
2444-
data: {
2445-
hello: 'world',
2446-
},
2447-
metadata: {
2448-
name: 'config-map',
2449-
},
2450-
});
2443+
cluster.addManifest('resource', {
2444+
kind: 'ConfigMap',
2445+
apiVersion: 'v1',
2446+
data: {
2447+
hello: 'world',
2448+
},
2449+
metadata: {
2450+
name: 'config-map',
2451+
},
2452+
});
2453+
2454+
// the kubectl provider is inside a nested stack.
2455+
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2456+
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2457+
Role: {
2458+
Ref: 'referencetoStackKubectlIamRole02F8947EArn',
2459+
},
2460+
});
24512461

2452-
// the kubectl provider is inside a nested stack.
2453-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2454-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2455-
Role: {
2456-
Ref: 'referencetoStackKubectlIamRole02F8947EArn',
2457-
},
24582462
});
2463+
test('imported cluster', ()=> {
24592464

2460-
});
2465+
const clusterName = 'my-cluster';
2466+
const stack = new cdk.Stack();
2467+
const kubectlLambdaRole = new iam.Role(stack, 'KubectlLambdaRole', {
2468+
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
2469+
});
2470+
const cluster = eks.Cluster.fromClusterAttributes(stack, 'Imported', {
2471+
clusterName,
2472+
kubectlRoleArn: 'arn:aws:iam::1111111:role/iam-role-that-has-masters-access',
2473+
kubectlLambdaRole: kubectlLambdaRole,
2474+
});
2475+
2476+
const chart = 'hello-world';
2477+
cluster.addHelmChart('test-chart', {
2478+
chart,
2479+
});
24612480

2481+
const nested = stack.node.tryFindChild('Imported-KubectlProvider') as cdk.NestedStack;
2482+
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2483+
Role: {
2484+
Ref: 'referencetoKubectlLambdaRole7D084D94Arn',
2485+
},
2486+
});
2487+
Template.fromStack(stack).hasResourceProperties(HelmChart.RESOURCE_TYPE, {
2488+
ClusterName: clusterName,
2489+
RoleArn: 'arn:aws:iam::1111111:role/iam-role-that-has-masters-access',
2490+
Release: 'importedcharttestchartf3acd6e5',
2491+
Chart: chart,
2492+
Namespace: 'default',
2493+
CreateNamespace: true,
2494+
});
2495+
});
2496+
});
24622497
describe('endpoint access', () => {
24632498

24642499
test('public restricted', () => {

0 commit comments

Comments
 (0)