Skip to content

Commit 8b48fe8

Browse files
refactor(cli): remove unused v1 bootstrapping detection (#32606)
### Reason for this change Removing dead code in `determineV1BootstrapSource` which could not be called anymore since version always returns v2 these days. ### Description of changes Remove the dead code and conditional. Also slightly changes the contract of `CliToolkit.bootstrap()` to take a `BootstrapSource` as part of options, instead of a the `Bootstrapper`. ### Describe any new or updated permissions being added n/a ### Description of how you validated changes existing tests and 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* --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 189bae3 commit 8b48fe8

File tree

6 files changed

+77
-48
lines changed

6 files changed

+77
-48
lines changed

packages/aws-cdk/lib/api/bootstrap/bootstrap-environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Mode } from '../plugin';
1414
export type BootstrapSource = { source: 'legacy' } | { source: 'default' } | { source: 'custom'; templateFile: string };
1515

1616
export class Bootstrapper {
17-
constructor(private readonly source: BootstrapSource) {}
17+
constructor(private readonly source: BootstrapSource = { source: 'default' }) {}
1818

1919
public bootstrapEnvironment(
2020
environment: cxapi.Environment,

packages/aws-cdk/lib/api/bootstrap/bootstrap-props.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BootstrapSource } from './bootstrap-environment';
12
import { Tag } from '../../cdk-toolkit';
23
import { StringWithoutPlaceholders } from '../util/placeholders';
34

@@ -22,6 +23,13 @@ export interface BootstrapEnvironmentOptions {
2223
readonly parameters?: BootstrappingParameters;
2324
readonly force?: boolean;
2425

26+
/**
27+
* The source of the bootstrap stack
28+
*
29+
* @default - modern v2-style bootstrapping
30+
*/
31+
readonly source?: BootstrapSource;
32+
2533
/**
2634
* Whether to execute the changeset or only create it and leave it in review.
2735
* @default true

packages/aws-cdk/lib/cdk-toolkit.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,14 +918,13 @@ export class CdkToolkit {
918918
*
919919
* @param userEnvironmentSpecs environment names that need to have toolkit support
920920
* provisioned, as a glob filter. If none is provided, all stacks are implicitly selected.
921-
* @param bootstrapper Legacy or modern.
922921
* @param options The name, role ARN, bootstrapping parameters, etc. to be used for the CDK Toolkit stack.
923922
*/
924923
public async bootstrap(
925924
userEnvironmentSpecs: string[],
926-
bootstrapper: Bootstrapper,
927925
options: BootstrapEnvironmentOptions,
928926
): Promise<void> {
927+
const bootstrapper = new Bootstrapper(options.source);
929928
// If there is an '--app' argument and an environment looks like a glob, we
930929
// select the environments from the app. Otherwise, use what the user said.
931930

packages/aws-cdk/lib/cli.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,15 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
242242
});
243243

244244
case 'bootstrap':
245-
const source: BootstrapSource = determineBootstrapVersion(args, configuration);
246-
247-
const bootstrapper = new Bootstrapper(source);
245+
const source: BootstrapSource = determineBootstrapVersion(args);
248246

249247
if (args.showTemplate) {
248+
const bootstrapper = new Bootstrapper(source);
250249
return bootstrapper.showTemplate(args.json);
251250
}
252251

253-
return cli.bootstrap(args.ENVIRONMENTS, bootstrapper, {
252+
return cli.bootstrap(args.ENVIRONMENTS, {
253+
source,
254254
roleArn: args.roleArn,
255255
force: argv.force,
256256
toolkitStackName: toolkitStackName,
@@ -468,32 +468,8 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
468468

469469
/**
470470
* Determine which version of bootstrapping
471-
* (legacy, or "new") should be used.
472471
*/
473-
function determineBootstrapVersion(args: { template?: string }, configuration: Configuration): BootstrapSource {
474-
const isV1 = version.DISPLAY_VERSION.startsWith('1.');
475-
return isV1 ? determineV1BootstrapSource(args, configuration) : determineV2BootstrapSource(args);
476-
}
477-
478-
function determineV1BootstrapSource(args: { template?: string }, configuration: Configuration): BootstrapSource {
479-
let source: BootstrapSource;
480-
if (args.template) {
481-
print(`Using bootstrapping template from ${args.template}`);
482-
source = { source: 'custom', templateFile: args.template };
483-
} else if (process.env.CDK_NEW_BOOTSTRAP) {
484-
print('CDK_NEW_BOOTSTRAP set, using new-style bootstrapping');
485-
source = { source: 'default' };
486-
} else if (isFeatureEnabled(configuration, cxapi.NEW_STYLE_STACK_SYNTHESIS_CONTEXT)) {
487-
print(`'${cxapi.NEW_STYLE_STACK_SYNTHESIS_CONTEXT}' context set, using new-style bootstrapping`);
488-
source = { source: 'default' };
489-
} else {
490-
// in V1, the "legacy" bootstrapping is the default
491-
source = { source: 'legacy' };
492-
}
493-
return source;
494-
}
495-
496-
function determineV2BootstrapSource(args: { template?: string }): BootstrapSource {
472+
function determineBootstrapVersion(args: { template?: string }): BootstrapSource {
497473
let source: BootstrapSource;
498474
if (args.template) {
499475
print(`Using bootstrapping template from ${args.template}`);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Bootstrapper } from '../lib/api/bootstrap/bootstrap-environment';
2+
import { exec } from '../lib/cli';
3+
4+
beforeEach(() => {
5+
jest.clearAllMocks();
6+
});
7+
8+
describe('cdk bootstrap', () => {
9+
const bootstrapEnvironmentMock = jest.spyOn(Bootstrapper.prototype, 'bootstrapEnvironment');
10+
11+
test('will bootstrap the a provided environment', async () => {
12+
bootstrapEnvironmentMock.mockResolvedValueOnce({
13+
noOp: false,
14+
outputs: {},
15+
type: 'did-deploy-stack',
16+
stackArn: 'fake-arn',
17+
});
18+
19+
await exec(['bootstrap', 'aws://123456789012/us-east-1']);
20+
expect(bootstrapEnvironmentMock).toHaveBeenCalledTimes(1);
21+
expect(bootstrapEnvironmentMock).toHaveBeenCalledWith({
22+
name: 'aws://123456789012/us-east-1',
23+
account: '123456789012',
24+
region: 'us-east-1',
25+
}, expect.anything(), expect.anything());
26+
});
27+
});
28+
29+
describe('cdk bootstrap --show-template', () => {
30+
const stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { return true; });
31+
32+
test('prints the default bootstrap template', async () => {
33+
await exec(['bootstrap', '--show-template']);
34+
expect(stdoutSpy).toHaveBeenCalledWith(expect.stringContaining('BootstrapVersion'));
35+
});
36+
});

packages/aws-cdk/test/cdk-toolkit.test.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ import {
7676
mockSSMClient,
7777
restoreSdkMocksToDefault,
7878
} from './util/mock-sdk';
79-
import { Bootstrapper } from '../lib/api/bootstrap';
79+
import { Bootstrapper, type BootstrapSource } from '../lib/api/bootstrap';
8080
import { DeployStackResult, SuccessfulDeployStackResult } from '../lib/api/deploy-stack';
8181
import {
8282
Deployments,
@@ -97,8 +97,9 @@ markTesting();
9797

9898
process.env.CXAPI_DISABLE_SELECT_BY_ID = '1';
9999

100+
const defaultBootstrapSource: BootstrapSource = { source: 'default' };
101+
const bootstrapEnvironmentMock = jest.spyOn(Bootstrapper.prototype, 'bootstrapEnvironment');
100102
let cloudExecutable: MockCloudExecutable;
101-
let bootstrapper: jest.Mocked<Bootstrapper>;
102103
let stderrMock: jest.SpyInstance;
103104
beforeEach(() => {
104105
jest.resetAllMocks();
@@ -108,11 +109,12 @@ beforeEach(() => {
108109
// on() in chokidar's Watcher returns 'this'
109110
mockChokidarWatcherOn.mockReturnValue(fakeChokidarWatcher);
110111

111-
bootstrapper = instanceMockFrom(Bootstrapper);
112-
bootstrapper.bootstrapEnvironment.mockResolvedValue({
112+
bootstrapEnvironmentMock.mockResolvedValue({
113113
noOp: false,
114114
outputs: {},
115-
} as any);
115+
type: 'did-deploy-stack',
116+
stackArn: 'fake-arn',
117+
});
116118

117119
cloudExecutable = new MockCloudExecutable({
118120
stacks: [MockStack.MOCK_STACK_A, MockStack.MOCK_STACK_B],
@@ -539,17 +541,19 @@ describe('bootstrap', () => {
539541
configuration.context.set('@aws-cdk/core:bootstrapQualifier', 'abcde');
540542

541543
// WHEN
542-
await toolkit.bootstrap(['aws://56789/south-pole'], bootstrapper, {
544+
await toolkit.bootstrap(['aws://56789/south-pole'], {
545+
source: defaultBootstrapSource,
543546
parameters: {
544547
qualifier: configuration.context.get('@aws-cdk/core:bootstrapQualifier'),
545548
},
546549
});
547550

548551
// THEN
549-
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledWith(expect.anything(), expect.anything(), {
552+
expect(bootstrapEnvironmentMock).toHaveBeenCalledWith(expect.anything(), expect.anything(), {
550553
parameters: {
551554
qualifier: 'abcde',
552555
},
556+
source: defaultBootstrapSource,
553557
});
554558
});
555559
});
@@ -868,10 +872,12 @@ describe('deploy', () => {
868872
const toolkit = defaultToolkitSetup();
869873

870874
// WHEN
871-
await toolkit.bootstrap(['aws://56789/south-pole'], bootstrapper, {});
875+
await toolkit.bootstrap(['aws://56789/south-pole'], {
876+
source: defaultBootstrapSource,
877+
});
872878

873879
// THEN
874-
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledWith(
880+
expect(bootstrapEnvironmentMock).toHaveBeenCalledWith(
875881
{
876882
account: '56789',
877883
region: 'south-pole',
@@ -880,7 +886,7 @@ describe('deploy', () => {
880886
expect.anything(),
881887
expect.anything(),
882888
);
883-
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledTimes(1);
889+
expect(bootstrapEnvironmentMock).toHaveBeenCalledTimes(1);
884890
});
885891

886892
test('globby bootstrap uses whats in the stacks', async () => {
@@ -889,10 +895,12 @@ describe('deploy', () => {
889895
cloudExecutable.configuration.settings.set(['app'], 'something');
890896

891897
// WHEN
892-
await toolkit.bootstrap(['aws://*/bermuda-triangle-1'], bootstrapper, {});
898+
await toolkit.bootstrap(['aws://*/bermuda-triangle-1'], {
899+
source: defaultBootstrapSource,
900+
});
893901

894902
// THEN
895-
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledWith(
903+
expect(bootstrapEnvironmentMock).toHaveBeenCalledWith(
896904
{
897905
account: '123456789012',
898906
region: 'bermuda-triangle-1',
@@ -901,7 +909,7 @@ describe('deploy', () => {
901909
expect.anything(),
902910
expect.anything(),
903911
);
904-
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledTimes(1);
912+
expect(bootstrapEnvironmentMock).toHaveBeenCalledTimes(1);
905913
});
906914

907915
test('bootstrap can be invoked without the --app argument', async () => {
@@ -913,10 +921,12 @@ describe('deploy', () => {
913921
const toolkit = defaultToolkitSetup();
914922

915923
// WHEN
916-
await toolkit.bootstrap(['aws://123456789012/west-pole'], bootstrapper, {});
924+
await toolkit.bootstrap(['aws://123456789012/west-pole'], {
925+
source: defaultBootstrapSource,
926+
});
917927

918928
// THEN
919-
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledWith(
929+
expect(bootstrapEnvironmentMock).toHaveBeenCalledWith(
920930
{
921931
account: '123456789012',
922932
region: 'west-pole',
@@ -925,7 +935,7 @@ describe('deploy', () => {
925935
expect.anything(),
926936
expect.anything(),
927937
);
928-
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledTimes(1);
938+
expect(bootstrapEnvironmentMock).toHaveBeenCalledTimes(1);
929939

930940
expect(cloudExecutable.hasApp).toEqual(false);
931941
expect(mockSynthesize).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)