Skip to content

Commit 946b748

Browse files
authored
chore(cli): remove legacy compat code to support version reporting in v1 apps (#32710)
### Reason for this change In the first versions of AWS CDK v1 and older, version metadata was injected by the CLI not by the framework. This changed with the introduction of Cloud Assembly Schema v6.0.0 in AWS CDK v1.65.0 (see [changelog](https://github.com/aws/aws-cdk/releases/tag/v1.65.0)). The CLI kept compatibility code around to ensure the metadata is injected. This code was introduced via #19010 on Feb 17, 2022. However this code was intended for v1 and kept around because v1 and v2 were initially developed on the same code base. In the meantime, AWS CDK v1 is EOS and we do not need to support v1 features from v2 anymore. This change will mean that we will stop reporting metadata for users that use the CLI v2 `>= 2.174.0` (the release removing the compat code) to deploy a Cloud Assembly developed with framework v1 `< 1.65.0`. Otherwise these users are not functionally effected by the change. We therefore decide to now remove the unused compat code. ### Description of changes Remove the unused legacy code and adjusted test cases. ### Describe any new or updated permissions being added n/a ### Description of how you validated changes Run unit test and CLI integ tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 0486b9c commit 946b748

File tree

2 files changed

+1
-183
lines changed

2 files changed

+1
-183
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import { promises as fs } from 'fs';
21
import * as cxapi from '@aws-cdk/cx-api';
3-
import { RegionInfo } from '@aws-cdk/region-info';
4-
import * as semver from 'semver';
52
import { CloudAssembly } from './cloud-assembly';
63
import * as contextproviders from '../../context-providers';
7-
import { debug, warning } from '../../logging';
4+
import { debug } from '../../logging';
85
import { Configuration } from '../../settings';
96
import { ToolkitError } from '../../toolkit/error';
107
import { SdkProvider } from '../aws-auth';
@@ -14,13 +11,6 @@ import { SdkProvider } from '../aws-auth';
1411
*/
1512
export type Synthesizer = (aws: SdkProvider, config: Configuration) => Promise<cxapi.CloudAssembly>;
1613

17-
/**
18-
* The Cloud Assembly schema version where the framework started to generate analytics itself
19-
*
20-
* See https://github.com/aws/aws-cdk/pull/10306
21-
*/
22-
const TEMPLATE_INCLUDES_ANALYTICS_SCHEMA_VERSION = '6.0.0';
23-
2414
export interface CloudExecutableProps {
2515
/**
2616
* Application configuration (settings and context)
@@ -69,8 +59,6 @@ export class CloudExecutable {
6959
}
7060

7161
private async doSynthesize(): Promise<CloudAssembly> {
72-
const trackVersions: boolean = this.props.configuration.settings.get(['versionReporting']);
73-
7462
// We may need to run the cloud executable multiple times in order to satisfy all missing context
7563
// (When the executable runs, it will tell us about context it wants to use
7664
// but it missing. We'll then look up the context and run the executable again, and
@@ -113,76 +101,10 @@ export class CloudExecutable {
113101
}
114102
}
115103

116-
if (trackVersions && !semver.gte(assembly.version, TEMPLATE_INCLUDES_ANALYTICS_SCHEMA_VERSION)) {
117-
// @deprecate(v2): the framework now manages its own analytics. For
118-
// Cloud Assemblies *older* than when we introduced this feature, have
119-
// the CLI add it. Otherwise, do nothing.
120-
await this.addMetadataResource(assembly);
121-
}
122-
123104
return new CloudAssembly(assembly);
124105
}
125106
}
126107

127-
/**
128-
* Modify the templates in the assembly in-place to add metadata resource declarations
129-
*/
130-
private async addMetadataResource(rootAssembly: cxapi.CloudAssembly) {
131-
if (!rootAssembly.runtime) { return; }
132-
133-
const modules = formatModules(rootAssembly.runtime);
134-
await processAssembly(rootAssembly);
135-
136-
async function processAssembly(assembly: cxapi.CloudAssembly) {
137-
for (const stack of assembly.stacks) {
138-
await processStack(stack);
139-
}
140-
for (const nested of assembly.nestedAssemblies) {
141-
await processAssembly(nested.nestedAssembly);
142-
}
143-
}
144-
145-
async function processStack(stack: cxapi.CloudFormationStackArtifact) {
146-
const resourcePresent = stack.environment.region === cxapi.UNKNOWN_REGION
147-
|| RegionInfo.get(stack.environment.region).cdkMetadataResourceAvailable;
148-
if (!resourcePresent) { return; }
149-
150-
if (!stack.template.Resources) {
151-
stack.template.Resources = {};
152-
}
153-
if (stack.template.Resources.CDKMetadata) {
154-
// Already added by framework, this is expected.
155-
return;
156-
}
157-
158-
stack.template.Resources.CDKMetadata = {
159-
Type: 'AWS::CDK::Metadata',
160-
Properties: {
161-
Modules: modules,
162-
},
163-
};
164-
165-
if (stack.environment.region === cxapi.UNKNOWN_REGION) {
166-
stack.template.Conditions = stack.template.Conditions || {};
167-
const condName = 'CDKMetadataAvailable';
168-
if (!stack.template.Conditions[condName]) {
169-
stack.template.Conditions[condName] = _makeCdkMetadataAvailableCondition();
170-
stack.template.Resources.CDKMetadata.Condition = condName;
171-
} else {
172-
warning(`The stack ${stack.id} already includes a ${condName} condition`);
173-
}
174-
}
175-
176-
// The template has changed in-memory, but the file on disk remains unchanged so far.
177-
// The CLI *might* later on deploy the in-memory version (if it's <50kB) or use the
178-
// on-disk version (if it's >50kB).
179-
//
180-
// Be sure to flush the changes we just made back to disk. The on-disk format is always
181-
// JSON.
182-
await fs.writeFile(stack.templateFullPath, JSON.stringify(stack.template, undefined, 2), { encoding: 'utf-8' });
183-
}
184-
}
185-
186108
private get canLookup() {
187109
return !!(this.props.configuration.settings.get(['lookups']) ?? true);
188110
}
@@ -202,48 +124,3 @@ function setsEqual<A>(a: Set<A>, b: Set<A>) {
202124
}
203125
return true;
204126
}
205-
206-
function _makeCdkMetadataAvailableCondition() {
207-
return _fnOr(RegionInfo.regions
208-
.filter(ri => ri.cdkMetadataResourceAvailable)
209-
.map(ri => ({ 'Fn::Equals': [{ Ref: 'AWS::Region' }, ri.name] })));
210-
}
211-
212-
/**
213-
* This takes a bunch of operands and crafts an `Fn::Or` for those. Funny thing is `Fn::Or` requires
214-
* at least 2 operands and at most 10 operands, so we have to... do this.
215-
*/
216-
function _fnOr(operands: any[]): any {
217-
if (operands.length === 0) {
218-
throw new ToolkitError('Cannot build `Fn::Or` with zero operands!');
219-
}
220-
if (operands.length === 1) {
221-
return operands[0];
222-
}
223-
if (operands.length <= 10) {
224-
return { 'Fn::Or': operands };
225-
}
226-
return _fnOr(_inGroupsOf(operands, 10).map(group => _fnOr(group)));
227-
}
228-
229-
function _inGroupsOf<T>(array: T[], maxGroup: number): T[][] {
230-
const result = new Array<T[]>();
231-
for (let i = 0; i < array.length; i += maxGroup) {
232-
result.push(array.slice(i, i + maxGroup));
233-
}
234-
return result;
235-
}
236-
237-
function formatModules(runtime: cxapi.RuntimeInfo): string {
238-
const modules = new Array<string>();
239-
240-
// inject toolkit version to list of modules
241-
// eslint-disable-next-line @typescript-eslint/no-require-imports
242-
const toolkitVersion = require('../../../package.json').version;
243-
modules.push(`aws-cdk=${toolkitVersion}`);
244-
245-
for (const key of Object.keys(runtime.libraries).sort()) {
246-
modules.push(`${key}=${runtime.libraries[key]}`);
247-
}
248-
return modules.join(',');
249-
}

packages/aws-cdk/test/api/cloud-executable.test.ts

-59
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,10 @@
11
/* eslint-disable import/order */
22
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
3-
import * as cxapi from '@aws-cdk/cx-api';
43
import { DefaultSelection } from '../../lib/api/cxapp/cloud-assembly';
54
import { registerContextProvider } from '../../lib/context-providers';
65
import { MockCloudExecutable } from '../util';
76

8-
// Apps on this version of the cxschema don't emit their own metadata resources
9-
// yet, so rely on the CLI to add the Metadata resource in.
10-
const SCHEMA_VERSION_THAT_DOESNT_INCLUDE_METADATA_ITSELF = '2.0.0';
11-
127
describe('AWS::CDK::Metadata', () => {
13-
test('is generated for relocatable stacks from old frameworks', async () => {
14-
const cx = await testCloudExecutable({
15-
env: `aws://${cxapi.UNKNOWN_ACCOUNT}/${cxapi.UNKNOWN_REGION}`,
16-
versionReporting: true,
17-
schemaVersion: SCHEMA_VERSION_THAT_DOESNT_INCLUDE_METADATA_ITSELF,
18-
});
19-
const cxasm = await cx.synthesize();
20-
21-
const result = cxasm.stackById('withouterrors').firstStack;
22-
const metadata = result.template.Resources && result.template.Resources.CDKMetadata;
23-
expect(metadata).toEqual({
24-
Type: 'AWS::CDK::Metadata',
25-
Properties: {
26-
// eslint-disable-next-line @typescript-eslint/no-require-imports
27-
Modules: `${require('../../package.json').name}=${require('../../package.json').version}`,
28-
},
29-
Condition: 'CDKMetadataAvailable',
30-
});
31-
32-
expect(result.template.Conditions?.CDKMetadataAvailable).toBeDefined();
33-
});
34-
35-
test('is generated for stacks in supported regions from old frameworks', async () => {
36-
const cx = await testCloudExecutable({
37-
env: 'aws://012345678912/us-east-1',
38-
versionReporting: true,
39-
schemaVersion: SCHEMA_VERSION_THAT_DOESNT_INCLUDE_METADATA_ITSELF,
40-
});
41-
const cxasm = await cx.synthesize();
42-
43-
const result = cxasm.stackById('withouterrors').firstStack;
44-
const metadata = result.template.Resources && result.template.Resources.CDKMetadata;
45-
expect(metadata).toEqual({
46-
Type: 'AWS::CDK::Metadata',
47-
Properties: {
48-
// eslint-disable-next-line @typescript-eslint/no-require-imports
49-
Modules: `${require('../../package.json').name}=${require('../../package.json').version}`,
50-
},
51-
});
52-
});
53-
54-
test('is not generated for stacks in unsupported regions from old frameworks', async () => {
55-
const cx = await testCloudExecutable({
56-
env: 'aws://012345678912/bermuda-triangle-1337',
57-
versionReporting: true,
58-
schemaVersion: SCHEMA_VERSION_THAT_DOESNT_INCLUDE_METADATA_ITSELF,
59-
});
60-
const cxasm = await cx.synthesize();
61-
62-
const result = cxasm.stackById('withouterrors').firstStack;
63-
const metadata = result.template.Resources && result.template.Resources.CDKMetadata;
64-
expect(metadata).toBeUndefined();
65-
});
66-
678
test('is not generated for new frameworks', async () => {
689
const cx = await testCloudExecutable({
6910
env: 'aws://012345678912/us-east-1',

0 commit comments

Comments
 (0)