Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli): remove legacy compat code to support version reporting in v1 apps #32710

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 1 addition & 124 deletions packages/aws-cdk/lib/api/cxapp/cloud-executable.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { promises as fs } from 'fs';
import * as cxapi from '@aws-cdk/cx-api';
import { RegionInfo } from '@aws-cdk/region-info';
import * as semver from 'semver';
import { CloudAssembly } from './cloud-assembly';
import * as contextproviders from '../../context-providers';
import { debug, warning } from '../../logging';
import { debug } from '../../logging';
import { Configuration } from '../../settings';
import { ToolkitError } from '../../toolkit/error';
import { SdkProvider } from '../aws-auth';
Expand All @@ -14,13 +11,6 @@ import { SdkProvider } from '../aws-auth';
*/
export type Synthesizer = (aws: SdkProvider, config: Configuration) => Promise<cxapi.CloudAssembly>;

/**
* The Cloud Assembly schema version where the framework started to generate analytics itself
*
* See https://github.com/aws/aws-cdk/pull/10306
*/
const TEMPLATE_INCLUDES_ANALYTICS_SCHEMA_VERSION = '6.0.0';

export interface CloudExecutableProps {
/**
* Application configuration (settings and context)
Expand Down Expand Up @@ -69,8 +59,6 @@ export class CloudExecutable {
}

private async doSynthesize(): Promise<CloudAssembly> {
const trackVersions: boolean = this.props.configuration.settings.get(['versionReporting']);

// We may need to run the cloud executable multiple times in order to satisfy all missing context
// (When the executable runs, it will tell us about context it wants to use
// but it missing. We'll then look up the context and run the executable again, and
Expand Down Expand Up @@ -113,76 +101,10 @@ export class CloudExecutable {
}
}

if (trackVersions && !semver.gte(assembly.version, TEMPLATE_INCLUDES_ANALYTICS_SCHEMA_VERSION)) {
// @deprecate(v2): the framework now manages its own analytics. For
// Cloud Assemblies *older* than when we introduced this feature, have
// the CLI add it. Otherwise, do nothing.
await this.addMetadataResource(assembly);
}

return new CloudAssembly(assembly);
}
}

/**
* Modify the templates in the assembly in-place to add metadata resource declarations
*/
private async addMetadataResource(rootAssembly: cxapi.CloudAssembly) {
if (!rootAssembly.runtime) { return; }

const modules = formatModules(rootAssembly.runtime);
await processAssembly(rootAssembly);

async function processAssembly(assembly: cxapi.CloudAssembly) {
for (const stack of assembly.stacks) {
await processStack(stack);
}
for (const nested of assembly.nestedAssemblies) {
await processAssembly(nested.nestedAssembly);
}
}

async function processStack(stack: cxapi.CloudFormationStackArtifact) {
const resourcePresent = stack.environment.region === cxapi.UNKNOWN_REGION
|| RegionInfo.get(stack.environment.region).cdkMetadataResourceAvailable;
if (!resourcePresent) { return; }

if (!stack.template.Resources) {
stack.template.Resources = {};
}
if (stack.template.Resources.CDKMetadata) {
// Already added by framework, this is expected.
return;
}

stack.template.Resources.CDKMetadata = {
Type: 'AWS::CDK::Metadata',
Properties: {
Modules: modules,
},
};

if (stack.environment.region === cxapi.UNKNOWN_REGION) {
stack.template.Conditions = stack.template.Conditions || {};
const condName = 'CDKMetadataAvailable';
if (!stack.template.Conditions[condName]) {
stack.template.Conditions[condName] = _makeCdkMetadataAvailableCondition();
stack.template.Resources.CDKMetadata.Condition = condName;
} else {
warning(`The stack ${stack.id} already includes a ${condName} condition`);
}
}

// The template has changed in-memory, but the file on disk remains unchanged so far.
// The CLI *might* later on deploy the in-memory version (if it's <50kB) or use the
// on-disk version (if it's >50kB).
//
// Be sure to flush the changes we just made back to disk. The on-disk format is always
// JSON.
await fs.writeFile(stack.templateFullPath, JSON.stringify(stack.template, undefined, 2), { encoding: 'utf-8' });
}
}

private get canLookup() {
return !!(this.props.configuration.settings.get(['lookups']) ?? true);
}
Expand All @@ -202,48 +124,3 @@ function setsEqual<A>(a: Set<A>, b: Set<A>) {
}
return true;
}

function _makeCdkMetadataAvailableCondition() {
return _fnOr(RegionInfo.regions
.filter(ri => ri.cdkMetadataResourceAvailable)
.map(ri => ({ 'Fn::Equals': [{ Ref: 'AWS::Region' }, ri.name] })));
}

/**
* This takes a bunch of operands and crafts an `Fn::Or` for those. Funny thing is `Fn::Or` requires
* at least 2 operands and at most 10 operands, so we have to... do this.
*/
function _fnOr(operands: any[]): any {
if (operands.length === 0) {
throw new ToolkitError('Cannot build `Fn::Or` with zero operands!');
}
if (operands.length === 1) {
return operands[0];
}
if (operands.length <= 10) {
return { 'Fn::Or': operands };
}
return _fnOr(_inGroupsOf(operands, 10).map(group => _fnOr(group)));
}

function _inGroupsOf<T>(array: T[], maxGroup: number): T[][] {
const result = new Array<T[]>();
for (let i = 0; i < array.length; i += maxGroup) {
result.push(array.slice(i, i + maxGroup));
}
return result;
}

function formatModules(runtime: cxapi.RuntimeInfo): string {
const modules = new Array<string>();

// inject toolkit version to list of modules
// eslint-disable-next-line @typescript-eslint/no-require-imports
const toolkitVersion = require('../../../package.json').version;
modules.push(`aws-cdk=${toolkitVersion}`);

for (const key of Object.keys(runtime.libraries).sort()) {
modules.push(`${key}=${runtime.libraries[key]}`);
}
return modules.join(',');
}
Loading