Skip to content

Commit 8a8738f

Browse files
committed
optimize
1 parent 4cf6ffb commit 8a8738f

File tree

9 files changed

+15
-36
lines changed

9 files changed

+15
-36
lines changed

packages/@aws-cdk/aws-eks-v2-alpha/lib/access-entry.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ICluster } from './cluster';
33
import { CfnAccessEntry } from 'aws-cdk-lib/aws-eks';
44
import {
55
Resource, IResource, Aws, Lazy,
6+
RemovalPolicy,
67
} from 'aws-cdk-lib/core';
78
import { MethodMetadata, addConstructMetadata } from 'aws-cdk-lib/core/lib/metadata-resource';
89

@@ -346,8 +347,8 @@ export class AccessEntry extends Resource implements IAccessEntry {
346347
policyArn: p.policy,
347348
})),
348349
}),
349-
350350
});
351+
resource.applyRemovalPolicy(RemovalPolicy.RETAIN);
351352
this.accessEntryName = this.getResourceNameAttribute(resource.ref);
352353
this.accessEntryArn = this.getResourceArnAttribute(resource.attrAccessEntryArn, {
353354
service: 'eks',

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

+3
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,9 @@ export class Cluster extends ClusterBase {
14601460
accessPolicies: policies,
14611461
});
14621462
this.accessEntries.set(principal, newEntry);
1463+
// if (this.kubectlProvider) {
1464+
// Node.of(this.kubectlProvider).addDependency(newEntry);
1465+
// }
14631466
}
14641467
}
14651468

packages/aws-cdk-lib/aws-eks/README.md

-13
Original file line numberDiff line numberDiff line change
@@ -515,14 +515,11 @@ cluster.connectAutoScalingGroupCapacity(asg, {});
515515
To connect a self-managed node group to an imported cluster, use the `cluster.connectAutoScalingGroupCapacity()` method:
516516

517517
```ts
518-
import { KubectlV32Layer } from '@aws-cdk/lambda-layer-kubectl-v32';
519-
520518
declare const cluster: eks.Cluster;
521519
declare const asg: autoscaling.AutoScalingGroup;
522520
const importedCluster = eks.Cluster.fromClusterAttributes(this, 'ImportedCluster', {
523521
clusterName: cluster.clusterName,
524522
clusterSecurityGroupId: cluster.clusterSecurityGroupId,
525-
kubectlLayer: new KubectlV32Layer(this, 'kubectl'),
526523
});
527524

528525
importedCluster.connectAutoScalingGroupCapacity(asg, {});
@@ -843,8 +840,6 @@ The resources are created in the cluster by running `kubectl apply` from a pytho
843840
By default, CDK will create a new python lambda function to apply your k8s manifests. If you want to use an existing kubectl provider function, for example with tight trusted entities on your IAM Roles - you can import the existing provider and then use the imported provider when importing the cluster:
844841

845842
```ts
846-
import { KubectlV32Layer } from '@aws-cdk/lambda-layer-kubectl-v32';
847-
848843
const handlerRole = iam.Role.fromRoleArn(this, 'HandlerRole', 'arn:aws:iam::123456789012:role/lambda-role');
849844
// get the serviceToken from the custom resource provider
850845
const functionArn = lambda.Function.fromFunctionName(this, 'ProviderOnEventFunc', 'ProviderframeworkonEvent-XXX').functionArn;
@@ -857,7 +852,6 @@ const kubectlProvider = eks.KubectlProvider.fromKubectlProviderAttributes(this,
857852
const cluster = eks.Cluster.fromClusterAttributes(this, 'Cluster', {
858853
clusterName: 'cluster',
859854
kubectlProvider,
860-
kubectlLayer: new KubectlV32Layer(this, 'kubectl'),
861855
});
862856
```
863857

@@ -959,7 +953,6 @@ eks.Cluster.fromClusterAttributes(this, 'MyCluster', {
959953
kubectlMemory: Size.gibibytes(4),
960954
vpc,
961955
clusterName: 'cluster-name',
962-
kubectlLayer: new KubectlV32Layer(this, 'kubectl'),
963956
});
964957
```
965958

@@ -1372,8 +1365,6 @@ You can also add service accounts to existing clusters.
13721365
To do so, pass the `openIdConnectProvider` property when you import the cluster into the application.
13731366

13741367
```ts
1375-
import { KubectlV32Layer } from '@aws-cdk/lambda-layer-kubectl-v32';
1376-
13771368
// you can import an existing provider
13781369
const provider = eks.OpenIdConnectProvider.fromOpenIdConnectProviderArn(this, 'Provider', 'arn:aws:iam::123456:oidc-provider/oidc.eks.eu-west-1.amazonaws.com/id/AB123456ABC');
13791370

@@ -1387,7 +1378,6 @@ const cluster = eks.Cluster.fromClusterAttributes(this, 'MyCluster', {
13871378
clusterName: 'Cluster',
13881379
openIdConnectProvider: provider,
13891380
kubectlRoleArn: 'arn:aws:iam::123456:role/service-role/k8sservicerole',
1390-
kubectlLayer: new KubectlV32Layer(this, 'kubectl'),
13911381
});
13921382

13931383
const serviceAccount = cluster.addServiceAccount('MyServiceAccount');
@@ -1930,12 +1920,9 @@ First, you'll need to "import" a cluster to your CDK app. To do that, use the
19301920
`eks.Cluster.fromClusterAttributes()` static method:
19311921

19321922
```ts
1933-
import { KubectlV32Layer } from '@aws-cdk/lambda-layer-kubectl-v32';
1934-
19351923
const cluster = eks.Cluster.fromClusterAttributes(this, 'MyCluster', {
19361924
clusterName: 'my-cluster-name',
19371925
kubectlRoleArn: 'arn:aws:iam::1111111:role/iam-role-that-has-masters-access',
1938-
kubectlLayer: new KubectlV32Layer(this, 'kubectl'),
19391926
});
19401927
```
19411928

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export interface ICluster extends IResource, ec2.IConnectable {
144144
* An AWS Lambda layer that includes `kubectl` and `helm`
145145
*
146146
*/
147-
readonly kubectlLayer: lambda.ILayerVersion;
147+
readonly kubectlLayer?: lambda.ILayerVersion;
148148

149149
/**
150150
* Specify which IP family is used to assign Kubernetes pod and service IP addresses.
@@ -383,8 +383,10 @@ export interface ClusterAttributes {
383383
* /opt/helm/helm
384384
* /opt/kubectl/kubectl
385385
* ```
386+
*
387+
* @default - No default layer will be provided
386388
*/
387-
readonly kubectlLayer: lambda.ILayerVersion;
389+
readonly kubectlLayer?: lambda.ILayerVersion;
388390

389391
/**
390392
* An AWS Lambda layer that contains the `aws` CLI.
@@ -1085,7 +1087,7 @@ abstract class ClusterBase extends Resource implements ICluster {
10851087
public abstract readonly ipFamily?: IpFamily;
10861088
public abstract readonly kubectlRole?: iam.IRole;
10871089
public abstract readonly kubectlLambdaRole?: iam.IRole;
1088-
public abstract readonly kubectlLayer: lambda.ILayerVersion;
1090+
public abstract readonly kubectlLayer?: lambda.ILayerVersion;
10891091
public abstract readonly kubectlEnvironment?: { [key: string]: string };
10901092
public abstract readonly kubectlSecurityGroup?: ec2.ISecurityGroup;
10911093
public abstract readonly kubectlPrivateSubnets?: ec2.ISubnet[];
@@ -1477,7 +1479,7 @@ export class Cluster extends ClusterBase {
14771479
* An AWS Lambda layer that includes `kubectl` and `helm`
14781480
*
14791481
*/
1480-
readonly kubectlLayer: lambda.ILayerVersion;
1482+
readonly kubectlLayer?: lambda.ILayerVersion;
14811483

14821484
/**
14831485
* An AWS Lambda layer that contains the `aws` CLI.
@@ -2398,7 +2400,7 @@ class ImportedCluster extends ClusterBase {
23982400
public readonly kubectlEnvironment?: { [key: string]: string } | undefined;
23992401
public readonly kubectlSecurityGroup?: ec2.ISecurityGroup | undefined;
24002402
public readonly kubectlPrivateSubnets?: ec2.ISubnet[] | undefined;
2401-
public readonly kubectlLayer: lambda.ILayerVersion;
2403+
public readonly kubectlLayer?: lambda.ILayerVersion;
24022404
public readonly ipFamily?: IpFamily;
24032405
public readonly awscliLayer?: lambda.ILayerVersion;
24042406
public readonly kubectlProvider?: IKubectlProvider;

packages/aws-cdk-lib/aws-eks/lib/kubectl-provider.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ export class KubectlProvider extends NestedStack implements IKubectlProvider {
149149

150150
// allow user to customize the layers with the tools we need
151151
handler.addLayers(props.cluster.awscliLayer ?? new AwsCliLayer(this, 'AwsCliLayer'));
152-
handler.addLayers(props.cluster.kubectlLayer);
152+
if (props.cluster.kubectlLayer) {
153+
handler.addLayers(props.cluster.kubectlLayer);
154+
}
153155

154156
this.handlerRole = handler.role!;
155157

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

-11
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ describe('cluster', () => {
174174

175175
const cluster = eks.Cluster.fromClusterAttributes(stack, 'Cluster', {
176176
clusterName: 'cluster',
177-
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
178177
});
179178

180179
expect(() => cluster.clusterSecurityGroup).toThrow(/"clusterSecurityGroup" is not defined for this imported cluster/);
@@ -215,7 +214,6 @@ describe('cluster', () => {
215214
const cluster = eks.Cluster.fromClusterAttributes(stack, 'Cluster', {
216215
clusterName: 'cluster',
217216
clusterSecurityGroupId: clusterSgId,
218-
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
219217
});
220218

221219
const clusterSg = cluster.clusterSecurityGroup;
@@ -281,7 +279,6 @@ describe('cluster', () => {
281279
const importedCluster = eks.Cluster.fromClusterAttributes(stack, 'ImportedCluster', {
282280
clusterName: cluster.clusterName,
283281
clusterSecurityGroupId: cluster.clusterSecurityGroupId,
284-
kubectlLayer: new KubectlV31Layer(stack, 'ImportKubectlLayer'),
285282
});
286283

287284
const selfManaged = new asg.AutoScalingGroup(stack, 'self-managed', {
@@ -981,7 +978,6 @@ describe('cluster', () => {
981978
const cluster = eks.Cluster.fromClusterAttributes(stack, 'Cluster', {
982979
clusterName: 'cluster',
983980
kubectlProvider: kubectlProvider,
984-
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
985981
});
986982

987983
expect(cluster.kubectlProvider).toEqual(kubectlProvider);
@@ -1001,7 +997,6 @@ describe('cluster', () => {
1001997
const cluster = eks.Cluster.fromClusterAttributes(stack, 'Cluster', {
1002998
clusterName: 'cluster',
1003999
kubectlProvider: kubectlProvider,
1004-
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
10051000
});
10061001

10071002
new eks.HelmChart(stack, 'Chart', {
@@ -1028,7 +1023,6 @@ describe('cluster', () => {
10281023
const cluster = eks.Cluster.fromClusterAttributes(stack, 'Cluster', {
10291024
clusterName: 'cluster',
10301025
kubectlProvider: kubectlProvider,
1031-
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
10321026
});
10331027

10341028
new eks.HelmChart(stack, 'Chart', {
@@ -1062,7 +1056,6 @@ describe('cluster', () => {
10621056
const cluster = eks.Cluster.fromClusterAttributes(stack, 'Cluster', {
10631057
clusterName: 'cluster',
10641058
kubectlProvider: kubectlProvider,
1065-
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
10661059
});
10671060

10681061
new eks.HelmChart(stack, 'Chart', {
@@ -1104,7 +1097,6 @@ describe('cluster', () => {
11041097
const cluster = eks.Cluster.fromClusterAttributes(stack, 'Cluster', {
11051098
clusterName: 'cluster',
11061099
kubectlPrivateSubnetIds: vpc.privateSubnets.map(s => s.subnetId),
1107-
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
11081100
});
11091101

11101102
expect(cluster.kubectlPrivateSubnets?.map(s => stack.resolve(s.subnetId))).toEqual([
@@ -1139,7 +1131,6 @@ describe('cluster', () => {
11391131
clusterCertificateAuthorityData: cluster.clusterCertificateAuthorityData,
11401132
clusterSecurityGroupId: cluster.clusterSecurityGroupId,
11411133
clusterEncryptionConfigKeyArn: cluster.clusterEncryptionConfigKeyArn,
1142-
kubectlLayer: new KubectlV31Layer(stack2, 'KubectlLayer'),
11431134
});
11441135

11451136
// this should cause an export/import
@@ -2605,7 +2596,6 @@ describe('cluster', () => {
26052596
clusterName,
26062597
kubectlRoleArn: 'arn:aws:iam::1111111:role/iam-role-that-has-masters-access',
26072598
kubectlLambdaRole: kubectlLambdaRole,
2608-
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
26092599
});
26102600

26112601
const chart = 'hello-world';
@@ -3338,7 +3328,6 @@ describe('cluster', () => {
33383328
clusterName: 'my-cluster',
33393329
kubectlRoleArn: 'arn:aws:iam::123456789012:role/MyRole',
33403330
kubectlMemory: cdk.Size.gibibytes(4),
3341-
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
33423331
});
33433332

33443333
cluster.addManifest('foo', { bar: 123 });

packages/aws-cdk-lib/aws-eks/test/k8s-manifest.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ describe('k8s manifest', () => {
8888
const cluster = Cluster.fromClusterAttributes(stack, 'MyCluster', {
8989
clusterName: 'my-cluster-name',
9090
kubectlRoleArn: 'arn:aws:iam::1111111:role/iam-role-that-has-masters-access',
91-
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
9291
});
9392

9493
// WHEN
@@ -116,7 +115,6 @@ describe('k8s manifest', () => {
116115
const cluster = Cluster.fromClusterAttributes(stack, 'MyCluster', {
117116
clusterName: 'my-cluster-name',
118117
kubectlRoleArn: 'arn:aws:iam::1111111:role/iam-role-that-has-masters-access',
119-
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
120118
});
121119

122120
const manifest = cluster.addManifest('foo', { bar: 2334 });

packages/aws-cdk-lib/aws-eks/test/service-account.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ describe('service account', () => {
3333
clusterName: 'Cluster',
3434
openIdConnectProvider: oidcProvider,
3535
kubectlRoleArn: 'arn:aws:iam::123456:role/service-role/k8sservicerole',
36-
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
3736
});
3837

3938
cluster.addServiceAccount('MyServiceAccount');

packages/aws-cdk-lib/aws-eks/test/user-data.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ describe('user data', () => {
5454
clusterName: cluster.clusterName,
5555
openIdConnectProvider: cluster.openIdConnectProvider,
5656
clusterCertificateAuthorityData: cluster.clusterCertificateAuthorityData,
57-
kubectlLayer: new KubectlV31Layer(stack, 'ImportKubectlLayer'),
5857
});
5958

6059
// WHEN
@@ -93,7 +92,6 @@ describe('user data', () => {
9392
clusterName: cluster.clusterName,
9493
openIdConnectProvider: cluster.openIdConnectProvider,
9594
clusterEndpoint: cluster.clusterEndpoint,
96-
kubectlLayer: new KubectlV31Layer(stack, 'ImportKubectlLayer'),
9795
});
9896

9997
// WHEN

0 commit comments

Comments
 (0)