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

Delete previous plans on autoplan or atlantis plan #1633

Conversation

giuli007
Copy link
Contributor

@giuli007 giuli007 commented Jun 15, 2021

This PR aims to fix #1624 (and #1122)

When using non-default workspaces, plans are stored in pr-and-workspace-specific directories.
This is often the case when people want to use the parallel plan/apply feature of Atlantis to speedup how long it takes to plan PRs that change a lot of projects at the same time

If a PR is updated after being created it might happen that some of the original plans are no longer relevant with regards
to the latest changes.

The change in this PR ensures that previous plans are always deleted when a generic plan is triggered either by autoplan
or by a "atlantis plan" command.

NB Even after this change plans are not going to be cleaned up when specific projects are planned explicitly with "atlantis plan -p/-d/-w" because it is reasonable to allow someone to progressively build a set of plans for different projects, with different "atlantis plan -p/-d/-w" comments, to then apply them all together.

I've added a couple of phrases to the documentation to clarify this.

@giuli007 giuli007 requested a review from a team as a code owner June 15, 2021 15:09
@giuli007 giuli007 force-pushed the delete_potentially_stale_previous_plans_which_fixes_1624 branch from 38cb1a9 to 6dfecab Compare June 15, 2021 19:29
Copy link
Contributor

@nishkrishnan nishkrishnan left a comment

Choose a reason for hiding this comment

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

The idea makes sense to me. I'm glad the change in functionality is documented here as well as that's bound to get some questions.

Only thing I'd mention is that the same problem exists with locks I would imagine. Ie. if you amend the change, the lock will still be present. The code for deleting the lock should automatically clean up the data. This is more generic than just deleting the plan file so I'd suggest doing that to harden this implementation.

@@ -45,6 +45,10 @@ atlantis plan -w staging
* `-w workspace` Switch to this [Terraform workspace](https://www.terraform.io/docs/state/workspaces.html) before planning. Defaults to `default`. If not using Terraform workspaces you can ignore this.
* `--verbose` Append Atlantis log to comment.

::: warning NOTE
A `terraform plan` (without flags), like autoplans, discards all plans previously created with `atlantis plan` `-p`/`-d`/`-w`
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
A `terraform plan` (without flags), like autoplans, discards all plans previously created with `atlantis plan` `-p`/`-d`/`-w`
An `atlantis plan` (without flags), like autoplans, discards all plans previously created with `atlantis plan` `-p`/`-d`/`-w`
Suggested change
A `terraform plan` (without flags), like autoplans, discards all plans previously created with `atlantis plan` `-p`/`-d`/`-w`
A `terraform plan` (without flags), like autoplans, discards all plans previously created with `atlantis plan` `-p`/`-d`/`-w`

Copy link
Contributor Author

@giuli007 giuli007 Jun 25, 2021

Choose a reason for hiding this comment

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

fixed the using-atlantis.md thanks

@nishkrishnan nishkrishnan added the waiting-on-response Waiting for a response from the user label Jun 22, 2021
@giuli007 giuli007 force-pushed the delete_potentially_stale_previous_plans_which_fixes_1624 branch 2 times, most recently from 1b0079f to 46f6821 Compare June 25, 2021 18:17
@giuli007
Copy link
Contributor Author

giuli007 commented Jun 30, 2021

Only thing I'd mention is that the same problem exists with locks I would imagine. Ie. if you amend the change, the lock will still be present. The code for deleting the lock should automatically clean up the data. This is more generic than just deleting the plan file so I'd suggest doing that to harden this implementation.

Thanks for the suggestion @nishkrishnan, I think that makes sense. I'll have a look at deleting the locks as well.

@giuli007 giuli007 force-pushed the delete_potentially_stale_previous_plans_which_fixes_1624 branch 3 times, most recently from 86a14f8 to 4e73bfe Compare July 14, 2021 16:59
@giuli007
Copy link
Contributor Author

@nishkrishnan I have added changes to delete the locks.

As you suggested I have also considered and tried using one of the already existing methods to delete the lock in a different PR #1704.
That method however ends up completely removing the working directories.
There are some situations where I think atlantis benefits from keeping the working directories around especially when using non-default workspaces (which means each project in a PR is cloned to a different directory).
In those cases an atlantis plan will cause atlantis to remove all the workspaces directories for that a PR and re-clone them unnecessarily(because the code the PR is for hasn't changed). This is less bad if only default workspaces are used as that means re-cloning it only once.
For those reasons I think the current PR is a better solution as it shouldn't cause the potential performance degradation mentioned.

I am still open to opinions and would be ok if we decide to merge #1704 instead, let me know what you think.

@srlightbody
Copy link

Was just reviewing open issues and saw this, I think it will also resolve issue #1122 that I opened a while back.

@angeloskaltsikis
Copy link

Hey, @giuli007 thanks for both of your PRs.
As I have worked on trying to solve the same issue in the past as well (PR, I would like to say that not deleting all the previous folders would work better for our use case (Using Atlantis + Terragrunt), due to utilizing the .terragrunt-cache folder for the new plans.
In the #1704 I believe the above happens every time.
If I understand correctly, by the approach of this PR we avoid this, correct?

P.S.
In case you need any extra help with the above I would be glad to jump in.

@giuli007
Copy link
Contributor Author

giuli007 commented Jul 27, 2021

Hi @srlightbody @angeloskaltsikis thanks for the feedback.

As I have worked on trying to solve the same issue in the past as well (PR, I would like to say that not deleting all the previous folders would work better for our use case (Using Atlantis + Terragrunt), due to utilizing the .terragrunt-cache folder for the new plans.
In the #1704 I believe the above happens every time.
If I understand correctly, by the approach of this PR we avoid this, correct?
Yes the current approach in this PR will not remove directories but only locks and previously created plans while #1704 removes everything for a specific PR (including your terragrunt cache if it is stored along the tf code).
This is the reason why I also prefer this approach.

Thanks for the pointers to #1399 and #1122 which are practically the same issue and a PR very similar to this one.

I see that @nishkrishnan concern was that all these fixes change the implicit functionality that atlantis currently has to stack up plans triggered by different atlantis plan commands and then apply them all together.

I guess the advantage of this PR is that it only discards previous plans in case an explicit change has been pushed to the PR (which would trigger autoplan) or an explicit generic atlantis plan is requested.
Alternatively a user can still build up a sequence of specific plans (with atlantis plan -p) and then apply them all together with atlantis apply.

My hope is that the changes to the docs in this PR will help making users aware of this behavioural change (I can make them even more explicit if people think that would be useful).

@giuli007 giuli007 requested a review from nishkrishnan July 28, 2021 17:13
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Aug 28, 2021
@github-actions github-actions bot closed this Sep 2, 2021
@giuli007
Copy link
Contributor Author

@nishkrishnan was there anything more you wanted me to address? Do the changes in this PR look like they could be merged?

@giuli007
Copy link
Contributor Author

giuli007 commented Oct 6, 2021

This PR has been automatically flagged as stale and closed because of lack of responses.
I do still think this is a useful fix (we have been using it for a few months now) and am interested on seeing it merged upstream, and I think other people on this thread agree.
@nishkrishnan or anyone of the maintainers (@chenrui333 / @lkysow / @anubhavmishra) can we reopen this PR?
I am happy to take the time to make it mergeable again and possibly address further concerns if any?

@jamengual jamengual reopened this Jan 18, 2022
@github-actions github-actions bot removed the Stale label Jan 19, 2022
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Feb 18, 2022
@github-actions github-actions bot closed this Feb 23, 2022
@zraider7
Copy link

zraider7 commented May 5, 2022

This is still an issue that we face. Is there a problem with the proposed solution @nishkrishnan ? Can we re-open this please?

@jamengual jamengual reopened this May 5, 2022
@jamengual
Copy link
Contributor

@giuli007 if you want to fix the conflicts we could discuss on emerging this and try it out on the prerelease

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Jul 30, 2022
@okgolove
Copy link

No stale please 🙈

@github-actions github-actions bot removed the Stale label Jul 31, 2022
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Aug 30, 2022
@giuli007
Copy link
Contributor Author

no stale

@github-actions github-actions bot removed the Stale label Aug 31, 2022
@giuli007 giuli007 force-pushed the delete_potentially_stale_previous_plans_which_fixes_1624 branch from 4e73bfe to 55a37bd Compare September 6, 2022 17:07
@giuli007
Copy link
Contributor Author

giuli007 commented Sep 6, 2022

I finally got round to update this.
I've rebased on top of v0.19.8 and run some local tests and verified that this still does what I wanted it to do: discard existing plans on autoplan or on atlantis plan commands, but not on specific atlantis plan -p <path> commands.

Out of excess of caution I went to look at the issues that the #1704 PR (alternative to this one) was causing when it was merged into v0.19.5-pre.20220622 and running that use-case (as I understand it: When autoplanning the second time (pushing an update to an open PR)) works without causing issues.
I wouldn't expect this PR to cause similar issues because it explicitly only deletes plans and locks, not workdirs. But it would perhaps be good if @ascandella can check it again once this goes into a pre-release.

@jamengual Let me know if you need anything else or we can merge it into a pre-release already.

giuli007 added 4 commits September 15, 2022 10:05
When using non-default workspaces, plans are stored
in pr-and-workspace-specific directories.
If a PR is subsequently updated it might happen that
some of the plans are no longer relevant with regards
to the latest changes.
This change ensures that plans are always deleted
when a generic plan is triggered either by autoplan
or by a "atlantis plan" command.
NB Plans are not cleaned up when specific projects are
planned explicitly with "atlantis plan -p/-d/-w".
autoplan or generic plan command
@giuli007 giuli007 force-pushed the delete_potentially_stale_previous_plans_which_fixes_1624 branch from 55a37bd to b3ed274 Compare September 15, 2022 17:25
@giuli007
Copy link
Contributor Author

I've rebased on top of master / v0.19.9-pre.20220912 to resolve conflicts with the changes from #2491

Any chance that this can be included in a v0.19.9 pre-release ?
cc @jamengual @chenrui333 @nishkrishnan

@@ -64,6 +67,7 @@ type PlanCommandRunner struct {
autoMerger *AutoMerger
parallelPoolSize int
pullStatusFetcher PullStatusFetcher
locker locking.Locker
Copy link
Contributor

Choose a reason for hiding this comment

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

This name does not follow the naming conversion of the struct

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jamengual I've now fixed this

@giuli007 giuli007 requested review from jamengual and nishkrishnan and removed request for nishkrishnan and jamengual September 16, 2022 15:36
Copy link
Contributor

@lilincmu lilincmu left a comment

Choose a reason for hiding this comment

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

This looks good to me! Thanks for your contribution 🎉

@jamengual
Copy link
Contributor

@giuli007 https://github.com/runatlantis/atlantis/releases/tag/v0.19.9-pre.20220923

@giuli007
Copy link
Contributor Author

Thanks @jamengual @lilincmu

krrrr38 pushed a commit to krrrr38/atlantis that referenced this pull request Dec 16, 2022
* Delete previous plans on autoplan or atlantis plan

When using non-default workspaces, plans are stored
in pr-and-workspace-specific directories.
If a PR is subsequently updated it might happen that
some of the plans are no longer relevant with regards
to the latest changes.
This change ensures that plans are always deleted
when a generic plan is triggered either by autoplan
or by a "atlantis plan" command.
NB Plans are not cleaned up when specific projects are
planned explicitly with "atlantis plan -p/-d/-w".

* Also delete locks along with plans when running
autoplan or generic plan command

* Fix Plan deletion tests after 0.19.8 rebase

* Fix Plan deletion tests after v0.19.9-pre.20220912 rebase

* Rename struct field to follow camelCase naming convention

Co-authored-by: giuli007 <[email protected]>
@nitrocode nitrocode added this to the v0.19.9 milestone Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working waiting-on-response Waiting for a response from the user
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Apply picks up stale plans
9 participants