Skip to content

Commit e78e355

Browse files
fix: aws-sdk still used in EKS custom resources (#26756)
Removes usage of aws-sdk in eks custom resources. The remaining usage was only type references that appear to be forward compatible but this cleans up the code and makes it possible to remove aws-sdk as a dev dependency to aws-cdk-lib once the rout53 cross account zone delegation handler is updated. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 52b43fc commit e78e355

File tree

5 files changed

+56
-52
lines changed

5 files changed

+56
-52
lines changed

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

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/* eslint-disable no-console */
22

33
// eslint-disable-next-line import/no-extraneous-dependencies
4-
import { ResourceNotFoundException } from '@aws-sdk/client-eks';
5-
// eslint-disable-next-line import/no-extraneous-dependencies
6-
import * as aws from 'aws-sdk';
4+
import * as EKS from '@aws-sdk/client-eks';
75
import { EksClient, ResourceEvent, ResourceHandler } from './common';
86
import { compareLoggingProps } from './compareLogging';
97
import { IsCompleteResponse, OnEventResponse } from '../../../custom-resources/lib/provider-framework/types';
@@ -19,16 +17,16 @@ export class ClusterResourceHandler extends ResourceHandler {
1917
return this.physicalResourceId;
2018
}
2119

22-
private readonly newProps: aws.EKS.CreateClusterRequest;
23-
private readonly oldProps: Partial<aws.EKS.CreateClusterRequest>;
20+
private readonly newProps: EKS.CreateClusterCommandInput;
21+
private readonly oldProps: Partial<EKS.CreateClusterCommandInput>;
2422

2523
constructor(eks: EksClient, event: ResourceEvent) {
2624
super(eks, event);
2725

2826
this.newProps = parseProps(this.event.ResourceProperties);
2927
this.oldProps = event.RequestType === 'Update' ? parseProps(event.OldResourceProperties) : {};
3028
// compare newProps and oldProps and update the newProps by appending disabled LogSetup if any
31-
const compared: Partial<aws.EKS.CreateClusterRequest> = compareLoggingProps(this.oldProps, this.newProps);
29+
const compared: Partial<EKS.CreateClusterCommandInput> = compareLoggingProps(this.oldProps, this.newProps);
3230
this.newProps.logging = compared.logging;
3331
}
3432

@@ -71,7 +69,7 @@ export class ClusterResourceHandler extends ResourceHandler {
7169
try {
7270
await this.eks.deleteCluster({ name: this.clusterName });
7371
} catch (e: any) {
74-
if (!(e instanceof ResourceNotFoundException)) {
72+
if (!(e instanceof EKS.ResourceNotFoundException)) {
7573
throw e;
7674
} else {
7775
console.log(`cluster ${this.clusterName} not found, idempotently succeeded`);
@@ -90,7 +88,7 @@ export class ClusterResourceHandler extends ResourceHandler {
9088
console.log('describeCluster returned:', JSON.stringify(resp, undefined, 2));
9189
} catch (e: any) {
9290
// see https://aws.amazon.com/blogs/developer/service-error-handling-modular-aws-sdk-js/
93-
if (e instanceof ResourceNotFoundException) {
91+
if (e instanceof EKS.ResourceNotFoundException) {
9492
console.log('received ResourceNotFoundException, this means the cluster has been deleted (or never existed)');
9593
return { IsComplete: true };
9694
}
@@ -147,7 +145,7 @@ export class ClusterResourceHandler extends ResourceHandler {
147145
}
148146

149147
if (updates.updateLogging || updates.updateAccess) {
150-
const config: aws.EKS.UpdateClusterConfigRequest = {
148+
const config: EKS.UpdateClusterConfigCommandInput = {
151149
name: this.clusterName,
152150
};
153151
if (updates.updateLogging) {
@@ -158,9 +156,9 @@ export class ClusterResourceHandler extends ResourceHandler {
158156
// https://awscli.amazonaws.com/v2/documentation/api/latest/reference/eks/update-cluster-config.html)
159157
// will fail, therefore we take only the access fields explicitly
160158
config.resourcesVpcConfig = {
161-
endpointPrivateAccess: this.newProps.resourcesVpcConfig.endpointPrivateAccess,
162-
endpointPublicAccess: this.newProps.resourcesVpcConfig.endpointPublicAccess,
163-
publicAccessCidrs: this.newProps.resourcesVpcConfig.publicAccessCidrs,
159+
endpointPrivateAccess: this.newProps.resourcesVpcConfig?.endpointPrivateAccess,
160+
endpointPublicAccess: this.newProps.resourcesVpcConfig?.endpointPublicAccess,
161+
publicAccessCidrs: this.newProps.resourcesVpcConfig?.publicAccessCidrs,
164162
};
165163
}
166164
const updateResponse = await this.eks.updateClusterConfig(config);
@@ -241,7 +239,7 @@ export class ClusterResourceHandler extends ResourceHandler {
241239
OpenIdConnectIssuer: cluster.identity?.oidc?.issuer?.substring(8) ?? '', // Strips off https:// from the issuer url
242240

243241
// We can safely return the first item from encryption configuration array, because it has a limit of 1 item
244-
// https://docs.aws.amazon.com/eks/latest/APIReference/API_CreateCluster.html#AmazonEKS-CreateCluster-request-encryptionConfig
242+
// https://docs.amazon.com/eks/latest/APIReference/API_CreateCluster.html#AmazonEKS-CreateCluster-request-encryptionConfig
245243
EncryptionConfigKeyArn: cluster.encryptionConfig?.shift()?.provider?.keyArn ?? '',
246244
},
247245
};
@@ -283,7 +281,7 @@ export class ClusterResourceHandler extends ResourceHandler {
283281
}
284282
}
285283

286-
function parseProps(props: any): aws.EKS.CreateClusterRequest {
284+
function parseProps(props: any): EKS.CreateClusterCommandInput {
287285

288286
const parsed = props?.Config ?? {};
289287

@@ -317,7 +315,7 @@ interface UpdateMap {
317315
updateAccess: boolean; // resourcesVpcConfig.endpointPrivateAccess and endpointPublicAccess
318316
}
319317

320-
function analyzeUpdate(oldProps: Partial<aws.EKS.CreateClusterRequest>, newProps: aws.EKS.CreateClusterRequest): UpdateMap {
318+
function analyzeUpdate(oldProps: Partial<EKS.CreateClusterCommandInput>, newProps: EKS.CreateClusterCommandInput): UpdateMap {
321319
console.log('old props: ', JSON.stringify(oldProps));
322320
console.log('new props: ', JSON.stringify(newProps));
323321

packages/aws-cdk-lib/aws-eks/lib/cluster-resource-handler/common.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ export abstract class ResourceHandler {
7777

7878
export interface EksClient {
7979
configureAssumeRole(request: sts.AssumeRoleCommandInput): void;
80-
createCluster(request: _eks.CreateClusterCommandInput): Promise<_eks.CreateClusterResponse>;
81-
deleteCluster(request: _eks.DeleteClusterCommandInput): Promise<_eks.DeleteClusterResponse>;
82-
describeCluster(request: _eks.DescribeClusterCommandInput): Promise<_eks.DescribeClusterResponse>;
83-
updateClusterConfig(request: _eks.UpdateClusterConfigCommandInput): Promise<_eks.UpdateClusterConfigResponse>;
84-
updateClusterVersion(request: _eks.UpdateClusterVersionCommandInput): Promise<_eks.UpdateClusterVersionResponse>;
85-
describeUpdate(req: _eks.DescribeUpdateCommandInput): Promise<_eks.DescribeUpdateResponse>;
86-
createFargateProfile(request: _eks.CreateFargateProfileCommandInput): Promise<_eks.CreateFargateProfileResponse>;
87-
describeFargateProfile(request: _eks.DescribeFargateProfileCommandInput): Promise<_eks.DescribeFargateProfileResponse>;
88-
deleteFargateProfile(request: _eks.DeleteFargateProfileCommandInput): Promise<_eks.DeleteFargateProfileResponse>;
80+
createCluster(request: _eks.CreateClusterCommandInput): Promise<_eks.CreateClusterCommandOutput>;
81+
deleteCluster(request: _eks.DeleteClusterCommandInput): Promise<_eks.DeleteClusterCommandOutput>;
82+
describeCluster(request: _eks.DescribeClusterCommandInput): Promise<_eks.DescribeClusterCommandOutput>;
83+
updateClusterConfig(request: _eks.UpdateClusterConfigCommandInput): Promise<_eks.UpdateClusterConfigCommandOutput>;
84+
updateClusterVersion(request: _eks.UpdateClusterVersionCommandInput): Promise<_eks.UpdateClusterVersionCommandOutput>;
85+
describeUpdate(req: _eks.DescribeUpdateCommandInput): Promise<_eks.DescribeUpdateCommandOutput>;
86+
createFargateProfile(request: _eks.CreateFargateProfileCommandInput): Promise<_eks.CreateFargateProfileCommandOutput>;
87+
describeFargateProfile(request: _eks.DescribeFargateProfileCommandInput): Promise<_eks.DescribeFargateProfileCommandOutput>;
88+
deleteFargateProfile(request: _eks.DeleteFargateProfileCommandInput): Promise<_eks.DeleteFargateProfileCommandOutput>;
8989
}

packages/aws-cdk-lib/aws-eks/lib/cluster-resource-handler/compareLogging.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
* @param newProps new properties
77
* @returns result with LogSet with enabled:false if any
88
*/
9+
// eslint-disable-next-line import/no-extraneous-dependencies
10+
import * as EKS from '@aws-sdk/client-eks';
911

10-
export function compareLoggingProps(oldProps: Partial<AWS.EKS.CreateClusterRequest>,
11-
newProps: Partial<AWS.EKS.CreateClusterRequest>): Partial<AWS.EKS.CreateClusterRequest> {
12-
const result: Partial<AWS.EKS.CreateClusterRequest> = { logging: {} };
13-
let enabledTypes: AWS.EKS.LogType[] = [];
14-
let disabledTypes: AWS.EKS.LogType[] = [];
12+
export function compareLoggingProps(oldProps: Partial<EKS.CreateClusterCommandInput>,
13+
newProps: Partial<EKS.CreateClusterCommandInput>): Partial<EKS.CreateClusterCommandInput> {
14+
const result: Partial<EKS.CreateClusterCommandInput> = { logging: {} };
15+
let enabledTypes: (EKS.LogType | string)[] = [];
16+
let disabledTypes: (EKS.LogType | string)[] = [];
1517

1618
if (newProps.logging?.clusterLogging === undefined && oldProps.logging?.clusterLogging === undefined) {
1719
return newProps;

packages/aws-cdk-lib/aws-eks/lib/cluster-resource-handler/fargate.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// eslint-disable-next-line import/no-extraneous-dependencies
2-
import { ResourceNotFoundException } from '@aws-sdk/client-eks';
3-
import * as aws from 'aws-sdk'; // eslint-disable-line import/no-extraneous-dependencies
2+
import * as EKS from '@aws-sdk/client-eks';
43
import { ResourceHandler } from './common';
54

65
const MAX_NAME_LEN = 63;
@@ -9,7 +8,7 @@ export class FargateProfileResourceHandler extends ResourceHandler {
98
protected async onCreate() {
109
const fargateProfileName = this.event.ResourceProperties.Config.fargateProfileName ?? this.generateProfileName();
1110

12-
const createFargateProfile: aws.EKS.CreateFargateProfileRequest = {
11+
const createFargateProfile: EKS.CreateFargateProfileCommandInput = {
1312
fargateProfileName,
1413
...this.event.ResourceProperties.Config,
1514
};
@@ -35,7 +34,7 @@ export class FargateProfileResourceHandler extends ResourceHandler {
3534
throw new Error('Cannot delete a profile without a physical id');
3635
}
3736

38-
const deleteFargateProfile: aws.EKS.DeleteFargateProfileRequest = {
37+
const deleteFargateProfile: EKS.DeleteFargateProfileCommandInput = {
3938
clusterName: this.event.ResourceProperties.Config.clusterName,
4039
fargateProfileName: this.physicalResourceId,
4140
};
@@ -86,12 +85,12 @@ export class FargateProfileResourceHandler extends ResourceHandler {
8685
* Queries the Fargate profile's current status and returns the status or
8786
* NOT_FOUND if the profile doesn't exist (i.e. it has been deleted).
8887
*/
89-
private async queryStatus(): Promise<aws.EKS.FargateProfileStatus | 'NOT_FOUND' | undefined> {
88+
private async queryStatus(): Promise<EKS.FargateProfileStatus | 'NOT_FOUND' | string | undefined> {
9089
if (!this.physicalResourceId) {
9190
throw new Error('Unable to determine status for fargate profile without a resource name');
9291
}
9392

94-
const describeFargateProfile: aws.EKS.DescribeFargateProfileRequest = {
93+
const describeFargateProfile: EKS.DescribeFargateProfileCommandInput = {
9594
clusterName: this.event.ResourceProperties.Config.clusterName,
9695
fargateProfileName: this.physicalResourceId,
9796
};
@@ -109,7 +108,7 @@ export class FargateProfileResourceHandler extends ResourceHandler {
109108

110109
return status;
111110
} catch (describeFargateProfileError: any) {
112-
if (describeFargateProfileError instanceof ResourceNotFoundException) {
111+
if (describeFargateProfileError instanceof EKS.ResourceNotFoundException) {
113112
this.log('received ResourceNotFoundException, this means the profile has been deleted (or never existed)');
114113
return 'NOT_FOUND';
115114
}

packages/aws-cdk-lib/aws-eks/test/cluster-resource-handler-mocks.ts

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as eks from '@aws-sdk/client-eks';
22
import * as sts from '@aws-sdk/client-sts';
3-
import * as sdk from 'aws-sdk';
43
import { EksClient } from '../lib/cluster-resource-handler/common';
54

65
/**
@@ -9,15 +8,15 @@ import { EksClient } from '../lib/cluster-resource-handler/common';
98
*/
109
export let actualRequest: {
1110
configureAssumeRoleRequest?: sts.AssumeRoleRequest;
12-
createClusterRequest?: eks.CreateClusterRequest;
13-
describeClusterRequest?: eks.DescribeClusterRequest;
14-
describeUpdateRequest?: eks.DescribeUpdateRequest;
15-
deleteClusterRequest?: eks.DeleteClusterRequest;
16-
updateClusterConfigRequest?: eks.UpdateClusterConfigRequest;
17-
updateClusterVersionRequest?: eks.UpdateClusterVersionRequest;
18-
createFargateProfile?: eks.CreateFargateProfileRequest;
19-
describeFargateProfile?: eks.DescribeFargateProfileRequest;
20-
deleteFargateProfile?: eks.DeleteFargateProfileRequest;
11+
createClusterRequest?: eks.CreateClusterCommandInput;
12+
describeClusterRequest?: eks.DescribeClusterCommandInput;
13+
describeUpdateRequest?: eks.DescribeUpdateCommandInput;
14+
deleteClusterRequest?: eks.DeleteClusterCommandInput;
15+
updateClusterConfigRequest?: eks.UpdateClusterConfigCommandInput;
16+
updateClusterVersionRequest?: eks.UpdateClusterVersionCommandInput;
17+
createFargateProfile?: eks.CreateFargateProfileCommandInput;
18+
describeFargateProfile?: eks.DescribeFargateProfileCommandInput;
19+
deleteFargateProfile?: eks.DeleteFargateProfileCommandInput;
2120
} = { };
2221

2322
/**
@@ -26,7 +25,7 @@ export let actualRequest: {
2625
export let simulateResponse: {
2726
describeClusterResponseMockStatus?: string;
2827
describeUpdateResponseMockStatus?: string;
29-
describeUpdateResponseMockErrors?: sdk.EKS.ErrorDetails;
28+
describeUpdateResponseMockErrors?: eks.ErrorDetail[];
3029
deleteClusterError?: Error;
3130
describeClusterException?: Error;
3231
} = { };
@@ -47,6 +46,7 @@ export const client: EksClient = {
4746
createCluster: async req => {
4847
actualRequest.createClusterRequest = req;
4948
return {
49+
$metadata: {},
5050
cluster: {
5151
name: req.name,
5252
roleArn: req.roleArn,
@@ -64,6 +64,7 @@ export const client: EksClient = {
6464
throw simulateResponse.deleteClusterError;
6565
}
6666
return {
67+
$metadata: {},
6768
cluster: {
6869
name: req.name,
6970
},
@@ -78,6 +79,7 @@ export const client: EksClient = {
7879
}
7980

8081
return {
82+
$metadata: {},
8183
cluster: {
8284
name: req.name,
8385
version: '1.0',
@@ -94,6 +96,7 @@ export const client: EksClient = {
9496
actualRequest.describeUpdateRequest = req;
9597

9698
return {
99+
$metadata: {},
97100
update: {
98101
id: req.updateId,
99102
errors: simulateResponse.describeUpdateResponseMockErrors,
@@ -105,6 +108,7 @@ export const client: EksClient = {
105108
updateClusterConfig: async req => {
106109
actualRequest.updateClusterConfigRequest = req;
107110
return {
111+
$metadata: {},
108112
update: {
109113
id: MOCK_UPDATE_STATUS_ID,
110114
},
@@ -114,6 +118,7 @@ export const client: EksClient = {
114118
updateClusterVersion: async req => {
115119
actualRequest.updateClusterVersionRequest = req;
116120
return {
121+
$metadata: {},
117122
update: {
118123
id: MOCK_UPDATE_STATUS_ID,
119124
},
@@ -122,17 +127,17 @@ export const client: EksClient = {
122127

123128
createFargateProfile: async req => {
124129
actualRequest.createFargateProfile = req;
125-
return { };
130+
return { $metadata: {} };
126131
},
127132

128133
describeFargateProfile: async req => {
129134
actualRequest.describeFargateProfile = req;
130-
return { };
135+
return { $metadata: {} };
131136
},
132137

133138
deleteFargateProfile: async req => {
134139
actualRequest.deleteFargateProfile = req;
135-
return { };
140+
return { $metadata: {} };
136141
},
137142
};
138143

@@ -148,8 +153,8 @@ export const MOCK_ASSUME_ROLE_ARN = 'assume:role:arn';
148153

149154
export function newRequest<T extends 'Create' | 'Update' | 'Delete'>(
150155
requestType: T,
151-
props?: Partial<sdk.EKS.CreateClusterRequest>,
152-
oldProps?: Partial<sdk.EKS.CreateClusterRequest>) {
156+
props?: Partial<eks.CreateClusterCommandInput>,
157+
oldProps?: Partial<eks.CreateClusterCommandInput>) {
153158
return {
154159
StackId: 'fake-stack-id',
155160
RequestId: 'fake-request-id',

0 commit comments

Comments
 (0)