-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat: report feature flag configuration into Cloud Assembly #34798
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8528310
create a feature flag report
vivian12300 df23cc1
fix formatting issues
vivian12300 d3c03c4
modified tests
vivian12300 05ba6f0
Merge branch 'main' into feature-flag-report
rix0rrr 75e8c25
fix tests
vivian12300 3718188
Merge branch 'main' into feature-flag-report
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
39 changes: 39 additions & 0 deletions
39
packages/aws-cdk-lib/core/lib/private/feature-flag-report.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { ArtifactType, FeatureFlag } from '@aws-cdk/cloud-assembly-schema'; | ||
| import { IConstruct } from 'constructs'; | ||
| import { CloudAssemblyBuilder } from '../../../cx-api'; | ||
| import * as feats from '../../../cx-api/lib/features'; | ||
| import { FlagInfo } from '../../../cx-api/lib/private/flag-modeling'; | ||
|
|
||
| /** | ||
| * Creates a FeatureFlag object based on flag information given. | ||
| */ | ||
| function parseFeatureFlagInfo(flagName: string, info: FlagInfo, root: IConstruct): FeatureFlag { | ||
| const userValue = root.node.tryGetContext(flagName) ?? undefined; | ||
|
|
||
| let parsedFlag: FeatureFlag = { | ||
| userValue: userValue, | ||
| recommendedValue: info.recommendedValue, | ||
| explanation: info.summary, | ||
| }; | ||
|
|
||
| return parsedFlag; | ||
| } | ||
|
|
||
| /** | ||
| * Iterate through all feature flags, retrieve the user's context, | ||
| * and create a Feature Flag report. | ||
| */ | ||
| export function generateFeatureFlagReport(builder: CloudAssemblyBuilder, root: IConstruct): void { | ||
| const featureFlags: Record<string, FeatureFlag> = {}; | ||
| for (const [flagName, flagInfo] of Object.entries(feats.FLAGS)) { | ||
| featureFlags[flagName] = parseFeatureFlagInfo(flagName, flagInfo, root); | ||
| } | ||
|
|
||
| builder.addArtifact('aws-cdk-lib/feature-flag-report', { | ||
| type: ArtifactType.FEATURE_FLAG_REPORT, | ||
| properties: { | ||
| module: 'aws-cdk-lib', | ||
| flags: featureFlags, | ||
| }, | ||
| }); | ||
| } |
This file contains hidden or 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
51 changes: 51 additions & 0 deletions
51
packages/aws-cdk-lib/core/test/feature-flag-report.test.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { FeatureFlagReportProperties } from '@aws-cdk/cloud-assembly-schema'; | ||
| import * as cxapi from '../../cx-api'; | ||
| import { App } from '../lib'; | ||
| import { generateFeatureFlagReport } from '../lib/private/feature-flag-report'; | ||
|
|
||
| describe('generate feature flag report', () => { | ||
| test('feature flag report can be retrieved from CloudAssembly using its artifact ID', () => { | ||
| const app = new App(); | ||
| const assembly = app.synth(); | ||
| expect(assembly.manifest.artifacts?.['aws-cdk-lib/feature-flag-report']).toBeDefined(); | ||
| }); | ||
| test('report contains context values that represent the feature flags', () => { | ||
| const app = new App({ | ||
| context: { | ||
| '@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault': true, | ||
| '@aws-cdk/core:aspectStabilization': false, | ||
| }, | ||
| }); | ||
| const assembly = app.synth(); | ||
| const report = assembly.manifest.artifacts?.['aws-cdk-lib/feature-flag-report']; | ||
| expect(report).toEqual(expect.objectContaining({ | ||
| type: 'cdk:feature-flag-report', | ||
| properties: expect.objectContaining({ | ||
| module: 'aws-cdk-lib', | ||
| flags: expect.objectContaining({ | ||
| '@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault': expect.objectContaining({ | ||
| userValue: true, | ||
| }), | ||
| '@aws-cdk/core:aspectStabilization': expect.objectContaining({ | ||
| userValue: false, | ||
| }), | ||
| }), | ||
| }), | ||
| })); | ||
| }); | ||
| test('defaults userValue to undefined when not set in context', () => { | ||
| const app = new App(); | ||
| const builder = new cxapi.CloudAssemblyBuilder('/tmp/test'); | ||
| const spy = jest.spyOn(builder, 'addArtifact'); | ||
|
|
||
| generateFeatureFlagReport(builder, app); | ||
|
|
||
| const flags = (spy.mock.calls[0][1].properties as FeatureFlagReportProperties).flags; | ||
| expect(flags).toEqual(expect.objectContaining({ | ||
| '@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault': expect.objectContaining({ | ||
| userValue: undefined, | ||
| }), | ||
| })); | ||
| }); | ||
| }); | ||
|
|
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.