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

Execute git clean partially when drift detection for every app is done #5312

Merged
merged 3 commits into from
Nov 7, 2024

Conversation

ffjlabo
Copy link
Member

@ffjlabo ffjlabo commented Nov 6, 2024

What this PR does:

Fixed to execute git clean -f for every app dir after doing k8s drift detection.

Why we need it:

During drift detection, a Git repository is cloned and reused, and a common manifest cache is utilized for various operations like drift detection, plan preview, plan, and deploy.
If an incorrect manifest is cached, it can affect other processes. When running kustomize build --enable-helm, a charts directory is created, and the Helm chart is downloaded locally.
However, before kustomize v5.3.0, this directory is not updated if the Helm chart version in kustomization.yaml changes, causing drift detection to load outdated manifests.
These manifests are cached based on commit hash, leading to incorrect manifests being applied during Plan or Deploy if the same commit hash is used.

For the context, see #5124 (comment)

Which issue(s) this PR fixes:

Fixes #5124

Does this PR introduce a user-facing change?:

  • How are users affected by this change:
  • Is this breaking change: No
  • How to migrate (if breaking change):

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>
Copy link

codecov bot commented Nov 6, 2024

Codecov Report

Attention: Patch coverage is 23.07692% with 20 lines in your changes missing coverage. Please review.

Project coverage is 25.26%. Comparing base (6b43655) to head (f1ef1a9).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
pkg/app/piped/driftdetector/kubernetes/detector.go 0.00% 12 Missing ⚠️
pkg/git/gittest/git.mock.go 0.00% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5312      +/-   ##
==========================================
- Coverage   25.27%   25.26%   -0.01%     
==========================================
  Files         444      444              
  Lines       47516    47554      +38     
==========================================
+ Hits        12010    12016       +6     
- Misses      34563    34596      +33     
+ Partials      943      942       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Warashi
Warashi previously approved these changes Nov 7, 2024
Copy link
Contributor

@Warashi Warashi left a comment

Choose a reason for hiding this comment

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

LGTM

pkg/git/repo.go Outdated
@@ -259,6 +260,15 @@ func (r repo) Clean() error {
return os.RemoveAll(r.dir)
}

// CleanPartially deletes data in the given relative path in the repo with git clean.
func (r repo) CleanPartially(ctx context.Context, relativePath string) error {
Copy link
Member

Choose a reason for hiding this comment

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

Tbh I think partially is not suitable for the function name; how about renaming it CleanPath?

Copy link
Member Author

Choose a reason for hiding this comment

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

@khanhtc1202
Thank you, got it. Fixed in ab2b33c

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>
@@ -259,6 +260,15 @@ func (r repo) Clean() error {
return os.RemoveAll(r.dir)
}

// CleanPath deletes data in the given relative path in the repo with git clean.
func (r repo) CleanPath(ctx context.Context, relativePath string) error {
out, err := r.runGitCommand(ctx, "clean", "-f", relativePath)
Copy link
Member

Choose a reason for hiding this comment

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

Q: Since this is a relative path, do we need to check whether the given path points to an under dir or outer dir? (If it contains ../ or /, can the git clean command handle it carefully?)

Copy link
Member Author

Choose a reason for hiding this comment

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

@khanhtc1202
git clean command checks whether the path is under the repo or not.
So IMO, we don't need the check. WDYT?

~/oss/pipe-cd/pipecd [fujiwo]
% git clean -f ../tutorial                    
fatal: ../tutorial: '../tutorial' is outside repository at '$HOME/oss/pipe-cd/pipecd

Copy link
Member Author

@ffjlabo ffjlabo Nov 7, 2024

Choose a reason for hiding this comment

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

📝 I tried git clean at repo2 with both relative and absolute path.

at ~/test/repo2 
% git clean -f ../repo1/                                                         
fatal: ../repo1/: '../repo1/' is outside repository at '/$HOME/test/repo2'

at ~/test/repo2 
% git clean -f /$HOME/test/repo1                                          
fatal: /$HOME/test/repo1: '/$HOME/test/repo1' is outside repository at '/$HOME/test/repo2'
% tree .
.
├── repo1
│   └── README.md
├── repo2
       └── README.md

Copy link
Member Author

Choose a reason for hiding this comment

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

Also added the test case for them f1ef1a9

Copy link
Member

Choose a reason for hiding this comment

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

That's nice 👍

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>
@ffjlabo ffjlabo requested a review from khanhtc1202 November 7, 2024 02:52
Copy link
Member

@khanhtc1202 khanhtc1202 left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@ffjlabo ffjlabo requested a review from Warashi November 7, 2024 03:31
@ffjlabo
Copy link
Member Author

ffjlabo commented Nov 7, 2024

@Warashi Sorry, plz re-approve 🙏

Copy link
Contributor

@Warashi Warashi left a comment

Choose a reason for hiding this comment

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

LGTM

@ffjlabo ffjlabo merged commit 7475ea8 into master Nov 7, 2024
16 of 18 checks passed
@ffjlabo ffjlabo deleted the git-clean-when-drift-detection-is-done branch November 7, 2024 03:47
github-actions bot pushed a commit that referenced this pull request Nov 7, 2024
#5312)

* Execute git clean partially when drift detection for every app is done

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Rename to CleanPath

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Add test for the outside dir pattern

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

---------

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>
ffjlabo added a commit that referenced this pull request Nov 7, 2024
* Add event context (#5295)

* Add contexts to the RegisterEventRequest

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Add contexts to model.Event

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Store event context in Control Plane

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Add trailers when commiting on event watcher

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Fix for failed build

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

---------

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>

* Add docs for pipectl event register --contexts on the event watcher usage page (#5299)

* Add docs for pipectl event register --contexts on the event watcher usage page

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Fix docs

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Fix command

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

---------

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>

* Clone manifests not to modify original manifests (#5306)

Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>

* Lambda: clone manifests not to modify original manifests (#5308)

Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>

* Skip commit and push when no replacement happens in EventWatcher (#5310)

* fix: skip push if no diff exist

Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>

* modify message

Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>

* refactor: use noChange variable

Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>

---------

Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>

* Execute git clean partially when drift detection for every app is done (#5312)

* Execute git clean partially when drift detection for every app is done

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Rename to CleanPath

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Add test for the outside dir pattern

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

---------

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>

* Update RELEASE and docs to v0.49.3 (#5315)

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>

---------

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Co-authored-by: Yoshiki Fujikane <40124947+ffjlabo@users.noreply.github.com>
Co-authored-by: Tetsuya Kikuchi <97105818+t-kikuc@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

When deploying the helm chart with kustomize, sometimes the helm chart version cannot be updated
3 participants