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

fix(cli): cdk destroy does not throw error for a non-existent stack #27293

Closed
wants to merge 10 commits into from

Conversation

go-to-k
Copy link
Contributor

@go-to-k go-to-k commented Sep 26, 2023

This PR for cli is to throw an error if stacks with wrong cases (=not exist) specified in cdk destroy.

Closes #27179.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team September 26, 2023 12:16
@github-actions github-actions bot added p2 admired-contributor [Pilot] contributed between 13-24 PRs to the CDK labels Sep 26, 2023
@go-to-k go-to-k marked this pull request as draft September 26, 2023 12:16
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@aws-cdk-automation aws-cdk-automation added the pr/needs-cli-test-run This PR needs CLI tests run against it. label Sep 26, 2023
@github-actions github-actions bot added bug This issue is a bug. effort/small Small work item – less than a day of effort labels Sep 26, 2023
@go-to-k go-to-k marked this pull request as ready for review September 26, 2023 12:22
@go-to-k
Copy link
Contributor Author

go-to-k commented Sep 26, 2023

Exemption Request: this should be covered with cli-integ tests. In my environment, the behavior was confirmed as expected.

@aws-cdk-automation aws-cdk-automation added the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Sep 26, 2023
@go-to-k go-to-k marked this pull request as draft September 26, 2023 12:37
@go-to-k go-to-k marked this pull request as ready for review September 27, 2023 03:15
@go-to-k go-to-k changed the title fix(cli): No exception when stack with wrong cases is deployed fix(cli): No exception when a non-existent stack is specified in cdk destroy Sep 27, 2023
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Sep 27, 2023
@SankyRed SankyRed self-assigned this Oct 11, 2023
@vinayak-kukreja vinayak-kukreja changed the title fix(cli): No exception when a non-existent stack is specified in cdk destroy fix(cli): cdk destroy does not throw error for a non-existent stack Oct 11, 2023
// No validation
const notExistPatterns = selector.patterns.filter(p => !stacks.stackArtifacts.find(s => minimatch(s.hierarchicalId, p)));
if (notExistPatterns.length > 0) {
throw new Error(`Stacks not exist: ${notExistPatterns.join(', ')}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please change the error message to be something more informative as to what does not exist and how they might be able to fix it?

Copy link
Contributor Author

@go-to-k go-to-k Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SankyRed

For example, if you specify stacks stack-A, stack-X, and stack-Y in cdk destroy, and only stack-A exists in your app.

In this code, the error message is to be following, Stacks not exist: stack-X, stack-Y. So you can know which of stacks do not exist.

how they might be able to fix it?

I think it is to difficult to know this. If you enter non-existent stacks, CDK does not know the way how to fix it unless it gets all the stacks and does a fuzzy search on all of them. Even if we did that, you probably wouldn't get the expected and accurate output.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tad more context here might be helpful, if we can provide it. Maybe something like Cannot run cdk destroy on stack(s) ${stacks.join(', ')}. ${notExistPatterns.join(', ')} are not deployed in account: ${account}, region: ${region}.

Copy link
Contributor Author

@go-to-k go-to-k Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheRealAmazonKendra

A tad more context here might be helpful, if we can provide it. Maybe something like Cannot run cdk destroy on stack(s) ${stacks.join(', ')}. ${notExistPatterns.join(', ')} are not deployed in account: ${account}, region: ${region}.

I think the format is difficult.

This notExistPattern handling and cdk destroy check the non-existence stacks by looking not the AWS account but stack instance definitions in the cdk app (that is cloud-assembly).

My understanding is that cdk destroy does not run the command by specifying an account or region, but looks at the stack list (CloudFormationStackArtifact[]) in cloud-assembly and if each stack has a region/account, it is used; otherwise, the default region/account is used. Actually there is no --region option in cdk destroy.

So we can see that the stack entered does not exist in the CDK app in the first place, but we cannot know WHICH account/region it does not exist in. (We may know that "the stack does not exist in the DEFAULT or ANY region and account", but perhaps that is not the information the user wants to know.)

Then, how about the following message?:

Cannot run cdk destroy on stack(s) ${stacks.join(', ')}. ${notExistPatterns.join(', ')} not exist.

@@ -769,7 +770,10 @@ export class CdkToolkit {
defaultBehavior: DefaultSelection.OnlySingle,
});

// No validation
const notExistPatterns = selector.patterns.filter(p => !stacks.stackArtifacts.find(s => minimatch(s.hierarchicalId, p)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add code such that we error out before it we even try deleting any stack, if at least one of the stacks provided does not exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SankyRed

In the current code, if any of the stacks are not existing, none of the stacks are deleted. And the cdk occurs an error.

Is this not what you are assuming?

// No validation
const notExistPatterns = selector.patterns.filter(p => !stacks.stackArtifacts.find(s => minimatch(s.hierarchicalId, p)));
if (notExistPatterns.length > 0) {
throw new Error(`Stacks not exist: ${notExistPatterns.join(', ')}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tad more context here might be helpful, if we can provide it. Maybe something like Cannot run cdk destroy on stack(s) ${stacks.join(', ')}. ${notExistPatterns.join(', ')} are not deployed in account: ${account}, region: ${region}.

@TheRealAmazonKendra
Copy link
Contributor

Exemption Request: this should be covered with cli-integ tests. In my environment, the behavior was confirmed as expected.

I would like to see this added as a failure case in the cli integ tests. Could you please add a test case there?

@TheRealAmazonKendra TheRealAmazonKendra removed the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Oct 12, 2023
@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Oct 12, 2023
@go-to-k
Copy link
Contributor Author

go-to-k commented Oct 12, 2023

@TheRealAmazonKendra

I added cli integ tests and changed an error message to preliminary one for now.

This is my first time doing cli integ tests, so please let me know if I am missing something.

3dbc266

@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

@aws-cdk-automation
Copy link
Collaborator

The pull request linter fails with the following errors:

❌ Fixes must contain a change to an integration test file and the resulting snapshot.
❌ CLI code has changed. A maintainer must run the code through the testing pipeline (git fetch origin pull/27293/head && git push -f origin FETCH_HEAD:test-main-pipeline), then add the 'pr-linter/cli-integ-tested' label when the pipeline succeeds.

PRs must pass status checks before we can provide a meaningful review.

If you would like to request an exemption from the status checks or clarification on feedback, please leave a comment on this PR containing Exemption Request and/or Clarification Request.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: b16eb1f
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

The pull request linter fails with the following errors:

❌ Fixes must contain a change to an integration test file and the resulting snapshot.
❌ CLI code has changed. A maintainer must run the code through the testing pipeline (git fetch origin pull/27293/head && git push -f origin FETCH_HEAD:test-main-pipeline), then add the 'pr-linter/cli-integ-tested' label when the pipeline succeeds.

PRs must pass status checks before we can provide a meaningful review.

If you would like to request an exemption from the status checks or clarification on feedback, please leave a comment on this PR containing Exemption Request and/or Clarification Request.

@go-to-k
Copy link
Contributor Author

go-to-k commented Nov 2, 2023

@TheRealAmazonKendra

Could you please review my changes and answers?

#27293 (comment)

#27293 (comment)

@aws-cdk-automation
Copy link
Collaborator

This PR has been in the CHANGES REQUESTED state for 3 weeks, and looks abandoned. To keep this PR from being closed, please continue work on it. If not, it will automatically be closed in a week.

@aws-cdk-automation
Copy link
Collaborator

This PR has been deemed to be abandoned, and will be automatically closed. Please create a new PR for these changes if you think this decision has been made in error.

@aws-cdk-automation aws-cdk-automation added the closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. label Nov 10, 2023
@aws-cdk-automation
Copy link
Collaborator

The pull request linter fails with the following errors:

❌ Fixes must contain a change to an integration test file and the resulting snapshot.
❌ CLI code has changed. A maintainer must run the code through the testing pipeline (git fetch origin pull/27293/head && git push -f origin FETCH_HEAD:test-main-pipeline), then add the 'pr-linter/cli-integ-tested' label when the pipeline succeeds.

PRs must pass status checks before we can provide a meaningful review.

If you would like to request an exemption from the status checks or clarification on feedback, please leave a comment on this PR containing Exemption Request and/or Clarification Request.

Copy link
Contributor

mergify bot commented Nov 10, 2023

⚠️ The sha of the head commit of this PR conflicts with #27921. Mergify cannot evaluate rules on this PR. ⚠️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
admired-contributor [Pilot] contributed between 13-24 PRs to the CDK bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/small Small work item – less than a day of effort p2 pr/needs-cli-test-run This PR needs CLI tests run against it.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No exception when stack with wrong cases is deployed
4 participants