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

Add usage of list trees to include cluster files in deletion pr #1210

Merged
merged 21 commits into from
Aug 25, 2022

Conversation

ranatrk
Copy link
Contributor

@ranatrk ranatrk commented Aug 3, 2022

Fixes #747
Add usage of list trees from go-git-provider to list all files of the cluster when creating a delete pull request for cluster deletion

Depends on fluxcd/go-git-providers#153

@ranatrk ranatrk added the bug Something isn't working label Aug 4, 2022
@ranatrk ranatrk force-pushed the remove-cluster-directories branch 3 times, most recently from c69772e to 10185c8 Compare August 4, 2022 12:35
Copy link
Collaborator

@foot foot left a comment

Choose a reason for hiding this comment

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

This is really clean 💯 , looking great!

The only thing is making sure we support gitlab too. It would be nice to keep go-git-providers as the interface abstraction layer over both github/gitlab (instead of e.g. branching here based on provider type, and doing ListTree for github and GetFiles(recursive=true) for gitlab.

Would it be tricky to implement ListTrees in ggp for gitlab?

cmd/clusters-service/pkg/git/git.go Outdated Show resolved Hide resolved
cmd/clusters-service/pkg/server/clusters.go Outdated Show resolved Hide resolved
currFile := fakeGitProvider.originalFiles[i].Path
nextFile := fakeGitProvider.originalFiles[j].Path
return currFile < nextFile
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice bit of logic here ✨ !

Buttt, maybe we use the similar pattern in TestCreatePullRequest and provide the expected list of commitedFiles in each test case above.

Just so we can all test it doesn't try and delete all the files in tree.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@foot isn't this the same as using the originalFiles passed to the provider? The comparison is done between that (expected) and the actual committed files

filteredTreeEntries := make(map[string]bool)
for _, f := range p.originalFiles {
if _, ok := filteredTreeEntries[f.Path]; !ok {
filteredTreeEntries[f.Path] = true
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this in case we provide duplicates in p.originalFiles? I'm not sure if we need it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is because the add files to commit already adds the files in the management cluster belonging to the specified cluster, which are also in the original files , so if they are found they should be ignored (for the test's case)

@ranatrk
Copy link
Contributor Author

ranatrk commented Aug 10, 2022

@foot updated with adding path parameter as well as removing filtering unless no cluster path is found, and simply retrieving all the cluster paths from the tree listing instead of adding the manifest file and then checking not to add it again

@ranatrk ranatrk requested a review from foot August 10, 2022 14:26
Copy link
Collaborator

@foot foot left a comment

Choose a reason for hiding this comment

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

💯 Looking great

filesList = append(filesList, gitprovider.CommitFile{
Path: &path,
Content: nil,
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also still want to delete the getClusterManifestPath here 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is replaced with getClusterDirPath

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right!

There are actually 2 "places" we want to delete files from when cleaning up.

  1. getClusterManifestPath which is usually ./clusters/management/clusters/my-cluster.yaml, It holds the cluster definition (how many nodes it has, what version of k8s it runs), this file is loaded into the management cluster. The existing deletion behaviour only tried to delete this file.
  2. NewgetClusterDirPath, which is something like ./clusters/default/my-cluster/**, all the files that should run on the new cluster, could be kustomizations, helmrelease etc. We write is into a different location so that it doesn't get loaded into the management cluster. It should only run on my-cluster.

E.g. here is PR creating a cluster https://github.com/wkp-example-org/capd-demo-reloaded/pull/203/files

So we still wanna delete both 👍

cmd/clusters-service/pkg/server/clusters.go Outdated Show resolved Hide resolved
cmd/clusters-service/pkg/server/clusters.go Show resolved Hide resolved
cmd/clusters-service/pkg/server/clusters.go Outdated Show resolved Hide resolved
cmd/clusters-service/pkg/server/clusters_test.go Outdated Show resolved Hide resolved
cmd/clusters-service/pkg/server/clusters_test.go Outdated Show resolved Hide resolved
ranatrk and others added 9 commits August 15, 2022 13:09
Update NewFakeGitProvider calls with missing arguments
Update adding files paths to be deleted to all be from the tree listing instead of manually adding the manifest file seperately

Signed-off-by: Rana Tarek Hassan <[email protected]>
fix cluster file path in test

Co-authored-by: Simon <[email protected]>
Cleanup delete cluster pull request test in clusters_test

Remove filtering retrieved tree files in delete cluster pull request

Signed-off-by: Rana Tarek Hassan <[email protected]>
Update delete cluster pr test with updated manifest and cluster paths

Signed-off-by: Rana Tarek Hassan <[email protected]>
@ranatrk ranatrk requested a review from foot August 15, 2022 14:47
Copy link
Collaborator

@foot foot left a comment

Choose a reason for hiding this comment

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

Amazing

image

Works great! ⭐

name: "create delete pull request with namespaced cluster names including multiple files in tree",
provider: NewFakeGitProvider("https://github.com/org/repo/pull/1", nil, nil, []string{
"clusters/ns-foo/foo/kustomization.yaml",
"clusters/management/clusters/ns-foo/foo.yaml",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess everything coming back from getTrees should be deleted actually? I was thinking we should add some other files here which shouldn't be deleted.. but as ggp is handling all the filtering this is basically just the set of files that should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes these are the files that are added by fake gpp so adding other files in the list of the test will actually add them in the commit to be deleted

go.mod Outdated
@@ -288,6 +288,7 @@ replace (
// to fix a vulnerability affecting the github.com/gorilla/handlers dependency. For more info visit
// https://security.snyk.io/vuln/SNYK-GOLANG-GITHUBCOMGORILLAHANDLERS-540773. Newer versions _should_ also work.
github.com/docker/distribution => github.com/docker/distribution v0.0.0-20201106182221-03aaf6ab5111
github.com/fluxcd/go-git-providers => github.com/ranatrk/go-git-providers v0.0.0-20220809210816-77b521f7fbb7
Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets update this to a commit on main when the ggp PR is merged!

@ranatrk ranatrk merged commit 7ef05e7 into main Aug 25, 2022
@ranatrk ranatrk deleted the remove-cluster-directories branch August 25, 2022 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deletion PRs don't remove cluster directories
2 participants