Skip to content

Commit

Permalink
more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Jan 2, 2025
1 parent 2be6fd1 commit e4ee7ba
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 49 deletions.
8 changes: 4 additions & 4 deletions packages/aws-cdk/lib/api/bootstrap/bootstrap-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Bootstrapper {
public bootstrapEnvironment(
environment: cxapi.Environment,
sdkProvider: SdkProvider,
options: BootstrapEnvironmentOptions = {},
options: BootstrapEnvironmentOptions,
): Promise<SuccessfulDeployStackResult> {
switch (this.source.source) {
case 'legacy':
Expand All @@ -44,7 +44,7 @@ export class Bootstrapper {
private async legacyBootstrap(
environment: cxapi.Environment,
sdkProvider: SdkProvider,
options: BootstrapEnvironmentOptions = {},
options: BootstrapEnvironmentOptions,
): Promise<SuccessfulDeployStackResult> {
const params = options.parameters ?? {};

Expand Down Expand Up @@ -79,7 +79,7 @@ export class Bootstrapper {
private async modernBootstrap(
environment: cxapi.Environment,
sdkProvider: SdkProvider,
options: BootstrapEnvironmentOptions = {},
options: BootstrapEnvironmentOptions,
): Promise<SuccessfulDeployStackResult> {
const params = options.parameters ?? {};

Expand Down Expand Up @@ -327,7 +327,7 @@ export class Bootstrapper {
private async customBootstrap(
environment: cxapi.Environment,
sdkProvider: SdkProvider,
options: BootstrapEnvironmentOptions = {},
options: BootstrapEnvironmentOptions,
): Promise<SuccessfulDeployStackResult> {
// Look at the template, decide whether it's most likely a legacy or modern bootstrap
// template, and use the right bootstrapper for that.
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/bootstrap/bootstrap-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const DEFAULT_BOOTSTRAP_VARIANT = 'AWS CDK: Default Resources';
* Options for the bootstrapEnvironment operation(s)
*/
export interface BootstrapEnvironmentOptions {
readonly toolkitStackName?: string;
readonly toolkitStackName: string;
readonly roleArn?: StringWithoutPlaceholders;
readonly parameters?: BootstrappingParameters;
readonly force?: boolean;
Expand Down
6 changes: 2 additions & 4 deletions packages/aws-cdk/lib/api/bootstrap/deploy-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { SDK, SdkProvider } from '../aws-auth';
import { assertIsSuccessfulDeployStackResult, deployStack, SuccessfulDeployStackResult } from '../deploy-stack';
import { NoBootstrapStackEnvironmentResources } from '../environment-resources';
import { Mode } from '../plugin/mode';
import { DEFAULT_TOOLKIT_STACK_NAME, ToolkitInfo } from '../toolkit-info';
import { ToolkitInfo } from '../toolkit-info';

/**
* A class to hold state around stack bootstrapping
Expand All @@ -32,9 +32,7 @@ import { DEFAULT_TOOLKIT_STACK_NAME, ToolkitInfo } from '../toolkit-info';
* current bootstrap stack and doing something intelligent).
*/
export class BootstrapStack {
public static async lookup(sdkProvider: SdkProvider, environment: Environment, toolkitStackName?: string) {
toolkitStackName = toolkitStackName ?? DEFAULT_TOOLKIT_STACK_NAME;

public static async lookup(sdkProvider: SdkProvider, environment: Environment, toolkitStackName: string) {
const resolvedEnvironment = await sdkProvider.resolveEnvironment(environment);
const sdk = (await sdkProvider.forEnvironment(resolvedEnvironment, Mode.ForWriting)).sdk;

Expand Down
6 changes: 3 additions & 3 deletions packages/aws-cdk/lib/api/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadCurrentTemplateWithNestedStacks,
type RootTemplateWithNestedStacks,
} from './nested-stack-helpers';
import { DEFAULT_TOOLKIT_STACK_NAME } from './toolkit-info';
import { ToolkitInfo } from './toolkit-info';
import { determineAllowCrossAccountAssetPublishing } from './util/checks';
import {
CloudFormationStack,
Expand Down Expand Up @@ -319,7 +319,7 @@ export interface StackExistsOptions {
}

export interface DeploymentsProps {
sdkProvider: SdkProvider;
readonly sdkProvider: SdkProvider;
readonly toolkitStackName?: string;
readonly quiet?: boolean;
}
Expand Down Expand Up @@ -358,7 +358,7 @@ export class Deployments {
constructor(private readonly props: DeploymentsProps) {
this.assetSdkProvider = props.sdkProvider;
this.deployStackSdkProvider = props.sdkProvider;
this.envs = new EnvironmentAccess(props.sdkProvider, props.toolkitStackName ?? DEFAULT_TOOLKIT_STACK_NAME);
this.envs = new EnvironmentAccess(props.sdkProvider, ToolkitInfo.determineName(props.toolkitStackName));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as chalk from 'chalk';
import * as promptly from 'promptly';
import { debug, print } from '../../logging';
import { IECRClient, IS3Client, SDK, SdkProvider } from '../aws-auth';
import { DEFAULT_TOOLKIT_STACK_NAME, ToolkitInfo } from '../toolkit-info';
import { ToolkitInfo } from '../toolkit-info';
import { ProgressPrinter } from './progress-printer';
import { ActiveAssetCache, BackgroundStackRefresh, refreshStacks } from './stack-refresh';
import { ToolkitError } from '../../toolkit/error';
Expand Down Expand Up @@ -159,10 +159,8 @@ interface GarbageCollectorProps {

/**
* The name of the bootstrap stack to look for.
*
* @default DEFAULT_TOOLKIT_STACK_NAME
*/
readonly bootstrapStackName?: string;
readonly bootstrapStackName: string;

/**
* Confirm with the user before actual deletion happens
Expand Down Expand Up @@ -193,7 +191,7 @@ export class GarbageCollector {
this.permissionToTag = ['tag', 'full'].includes(props.action);
this.confirm = props.confirm ?? true;

this.bootstrapStackName = props.bootstrapStackName ?? DEFAULT_TOOLKIT_STACK_NAME;
this.bootstrapStackName = props.bootstrapStackName;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk/lib/api/logs/find-cloudwatch-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { SDK, SdkProvider } from '../aws-auth';
import { EnvironmentAccess } from '../environment-access';
import { EvaluateCloudFormationTemplate, LazyListStackResources } from '../evaluate-cloudformation-template';
import { Mode } from '../plugin/mode';
import { DEFAULT_TOOLKIT_STACK_NAME } from '../toolkit-info';

// resource types that have associated CloudWatch Log Groups that should _not_ be monitored
const IGNORE_LOGS_RESOURCE_TYPES = ['AWS::EC2::FlowLog', 'AWS::CloudTrail::Trail', 'AWS::CodeBuild::Project'];
Expand Down Expand Up @@ -36,13 +35,14 @@ export interface FoundLogGroupsResult {

export async function findCloudWatchLogGroups(
sdkProvider: SdkProvider,
toolkitStackName: string,
stackArtifact: CloudFormationStackArtifact,
): Promise<FoundLogGroupsResult> {
let sdk: SDK;
const resolvedEnv = await sdkProvider.resolveEnvironment(stackArtifact.environment);
// try to assume the lookup role and fallback to the default credentials
try {
sdk = (await new EnvironmentAccess(sdkProvider, DEFAULT_TOOLKIT_STACK_NAME).accessStackForLookup(stackArtifact)).sdk;
sdk = (await new EnvironmentAccess(sdkProvider, toolkitStackName).accessStackForLookup(stackArtifact)).sdk;
} catch (e: any) {
debug(`Failed to access SDK environment: ${e.message}`);
sdk = (await sdkProvider.forEnvironment(resolvedEnv, Mode.ForReading)).sdk;
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/toolkit-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const DEFAULT_TOOLKIT_STACK_NAME = 'CDKToolkit';
*
*/
export abstract class ToolkitInfo {
public static determineName(overrideName?: string) {
public static determineName(overrideName?: string): string {
return overrideName ?? DEFAULT_TOOLKIT_STACK_NAME;
}

Expand Down
33 changes: 17 additions & 16 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as chokidar from 'chokidar';
import * as fs from 'fs-extra';
import * as promptly from 'promptly';
import * as uuid from 'uuid';
import { DeploymentMethod, SuccessfulDeployStackResult } from './api';
import { DeploymentMethod, SuccessfulDeployStackResult, ToolkitInfo } from './api';
import { SdkProvider } from './api/aws-auth';
import { Bootstrapper, BootstrapEnvironmentOptions } from './api/bootstrap';
import {
Expand Down Expand Up @@ -137,11 +137,13 @@ export enum AssetBuildTime {
*/
export class CdkToolkit {
private deployments: Deployments;
private toolkitStackName: string;

constructor(private readonly props: CdkToolkitProps) {
this.toolkitStackName = ToolkitInfo.determineName(props.toolkitStackName);
this.deployments = new Deployments({
sdkProvider: props.sdkProvider,
toolkitStackName: props.toolkitStackName,
toolkitStackName: this.toolkitStackName,
});
}

Expand Down Expand Up @@ -522,7 +524,7 @@ export class CdkToolkit {
);
} finally {
if (options.cloudWatchLogMonitor) {
const foundLogGroupsResult = await findCloudWatchLogGroups(this.props.sdkProvider, stack);
const foundLogGroupsResult = await findCloudWatchLogGroups(this.props.sdkProvider, this.toolkitStackName, stack);
options.cloudWatchLogMonitor.addLogGroups(
foundLogGroupsResult.env,
foundLogGroupsResult.sdk,
Expand Down Expand Up @@ -773,7 +775,7 @@ export class CdkToolkit {
const tags = tagsForStack(stack);
await resourceImporter.importResourcesFromMap(actualImport, {
roleArn: options.roleArn,
toolkitStackName: options.toolkitStackName,
toolkitStackName: this.toolkitStackName,
tags,
deploymentMethod: options.deploymentMethod,
usePreviousParameters: true,
Expand Down Expand Up @@ -929,7 +931,7 @@ export class CdkToolkit {
*/
public async bootstrap(
userEnvironmentSpecs: string[],
options: BootstrapEnvironmentOptions,
options: BootstrapOptions,
): Promise<void> {
const bootstrapper = new Bootstrapper(options.source);
// If there is an '--app' argument and an environment looks like a glob, we
Expand All @@ -943,7 +945,10 @@ export class CdkToolkit {
await Promise.all(environments.map((environment) => limit(async () => {
success(' ⏳ Bootstrapping environment %s...', chalk.blue(environment.name));
try {
const result = await bootstrapper.bootstrapEnvironment(environment, this.props.sdkProvider, options);
const result = await bootstrapper.bootstrapEnvironment(environment, this.props.sdkProvider, {
...options,
toolkitStackName: this.toolkitStackName,
});
const message = result.noOp
? ' ✅ Environment %s bootstrapped (no changes).'
: ' ✅ Environment %s bootstrapped.';
Expand All @@ -967,7 +972,7 @@ export class CdkToolkit {
const gc = new GarbageCollector({
sdkProvider: this.props.sdkProvider,
resolvedEnvironment: environment,
bootstrapStackName: options.bootstrapStackName,
bootstrapStackName: options.bootstrapStackName ?? this.toolkitStackName,
rollbackBufferDays: options.rollbackBufferDays,
createdBufferDays: options.createdBufferDays,
action: options.action ?? 'full',
Expand Down Expand Up @@ -1294,7 +1299,7 @@ export class CdkToolkit {
// Initial Deployment
await migrateDeployment.importResourcesFromMigrate(resourcesToImport, {
roleArn: options.roleArn,
toolkitStackName: options.toolkitStackName,
toolkitStackName: this.toolkitStackName,
deploymentMethod: options.deploymentMethod,
usePreviousParameters: true,
progress: options.progress,
Expand Down Expand Up @@ -1332,6 +1337,9 @@ function printSerializedObject(obj: any, json: boolean) {
data(serializeStructure(obj, json));
}

export interface BootstrapOptions extends Omit<BootstrapEnvironmentOptions, 'toolkitStackName'> {
}

export interface DiffOptions {
/**
* Stack names to diff
Expand Down Expand Up @@ -1422,13 +1430,6 @@ interface CfnDeployOptions {
*/
selector: StackSelector;

/**
* Name of the toolkit stack to use/deploy
*
* @default CDKToolkit
*/
toolkitStackName?: string;

/**
* Role to pass to CloudFormation for deployment
*/
Expand Down Expand Up @@ -1754,7 +1755,7 @@ export interface GarbageCollectionOptions {
/**
* The stack name of the bootstrap stack.
*
* @default DEFAULT_TOOLKIT_STACK_NAME
* @default "CDKToolkit"
*/
readonly bootstrapStackName?: string;

Expand Down
4 changes: 3 additions & 1 deletion packages/aws-cdk/test/api/bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const env = {

const templateBody = toYAML(deserializeStructure(serializeStructure(legacyBootstrapTemplate({}), true)));
const changeSetName = 'cdk-deploy-change-set';
const toolkitStackName = 'CDKToolkit';

jest.mock('../../lib/api/util/checks', () => ({
determineAllowCrossAccountAssetPublishing: jest.fn().mockResolvedValue(true),
Expand Down Expand Up @@ -315,7 +316,7 @@ test('even if the bootstrap stack failed to create, can still retry bootstrappin
test('stack is not termination protected by default', async () => {
// WHEN
// Seems silly, but we process the template multiple times to get the templateBody that goes into the call
await bootstrapper.bootstrapEnvironment(env, sdk);
await bootstrapper.bootstrapEnvironment(env, sdk, { toolkitStackName });

// THEN
// There are only two ways that termination can be set: either through calling CreateStackCommand
Expand Down Expand Up @@ -344,6 +345,7 @@ test('stack is termination protected when set', async () => {
// WHEN
await bootstrapper.bootstrapEnvironment(env, sdk, {
terminationProtection: true,
toolkitStackName: 'CDKToolkit',
});

// THEN
Expand Down
Loading

0 comments on commit e4ee7ba

Please sign in to comment.