-
Notifications
You must be signed in to change notification settings - Fork 4k
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
fix(cli): metadata not recorded for templates >50k #10184
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8dea0fe
fix(cli): metadata not recorded in templates >50k
rix0rrr 250fecb
Lint
rix0rrr e8d1673
Remove console statement
rix0rrr d2e03e6
Update cloud-executable.ts
rix0rrr 2df648e
Merge branch 'master' into huijbers/metadata-in-template
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { promises as fs } from 'fs'; | ||
import * as cxapi from '@aws-cdk/cx-api'; | ||
import { RegionInfo } from '@aws-cdk/region-info'; | ||
import * as contextproviders from '../../context-providers'; | ||
|
@@ -89,54 +90,76 @@ export class CloudExecutable { | |
} | ||
} | ||
|
||
if (trackVersions && assembly.runtime) { | ||
const modules = formatModules(assembly.runtime); | ||
for (const stack of assembly.stacks) { | ||
if (!stack.template.Resources) { | ||
stack.template.Resources = {}; | ||
} | ||
const resourcePresent = stack.environment.region === cxapi.UNKNOWN_REGION | ||
|| RegionInfo.get(stack.environment.region).cdkMetadataResourceAvailable; | ||
if (resourcePresent) { | ||
if (!stack.template.Resources.CDKMetadata) { | ||
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`); | ||
} | ||
} | ||
} else { | ||
warning(`The stack ${stack.id} already includes a CDKMetadata resource`); | ||
} | ||
} | ||
} | ||
if (trackVersions) { | ||
// @deprecated(v2): remove this 'if' block and all code referenced by it. | ||
// This should honestly not be done here. The framework | ||
// should (and will, shortly) synthesize this information directly into | ||
// the template. However, in order to support old framework versions | ||
// that don't synthesize this info yet, we can only remove this code | ||
// once we break backwards compatibility. | ||
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); | ||
} | ||
} | ||
|
||
function formatModules(runtime: cxapi.RuntimeInfo): string { | ||
const modules = new Array<string>(); | ||
async function processStack(stack: cxapi.CloudFormationStackArtifact) { | ||
const resourcePresent = stack.environment.region === cxapi.UNKNOWN_REGION | ||
|| RegionInfo.get(stack.environment.region).cdkMetadataResourceAvailable; | ||
if (!resourcePresent) { return; } | ||
|
||
// 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}`); | ||
if (!stack.template.Resources) { | ||
stack.template.Resources = {}; | ||
} | ||
if (stack.template.Resources.CDKMetadata) { | ||
warning(`The stack ${stack.id} already includes a CDKMetadata resource`); | ||
return; | ||
} | ||
|
||
for (const key of Object.keys(runtime.libraries).sort()) { | ||
modules.push(`${key}=${runtime.libraries[key]}`); | ||
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`); | ||
} | ||
return modules.join(','); | ||
} | ||
|
||
// 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' }); | ||
Comment on lines
+156
to
+162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mutate the synthesized template. Ewww... I suppose the root problem is that the CLI shouldn't be injecting the metadata. Is it safe to assume this will not happen when the correct fix in put in place? |
||
} | ||
} | ||
} | ||
|
@@ -185,4 +208,18 @@ function _inGroupsOf<T>(array: T[], maxGroup: number): T[][] { | |
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(','); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think we shouldn't add this notice until the change is done to the framework