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

256/remove application - refactor code to collect resource information #511

Merged
merged 18 commits into from Jul 23, 2021

Conversation

jrryjcksn
Copy link
Contributor

What changed?
As I started to consider how to find all the resources to remove, I noticed that the necessary information was spread out through the code and based on AddParams. This collects all information about created resources in an AppResourceInfo structure (derived from a wego.Application) so that it will stay consistent with applications and be available to the remove code (which doesn't have AddParams).

How did you test it?
Other than the first part of remove (which is covered by unit tests), this didn't change behavior and is tested by the existing unit tests.

Release notes
No

Documentation Changes
No

@jrryjcksn jrryjcksn self-assigned this Jul 23, 2021
@jrryjcksn jrryjcksn marked this pull request as draft July 23, 2021 15:09
@jrryjcksn jrryjcksn marked this pull request as ready for review July 23, 2021 15:22
Copy link
Contributor

@jpellizzari jpellizzari left a comment

Choose a reason for hiding this comment

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

👍 Only nits and questions. As I mentioned in Slack, similar changes around SourceType here: #497

if err != nil {
return fmt.Errorf("unable to create pull request: %w", err)
}
a.logger.Println("Pull Request created: %s\n", prLink.Get().WebURL)
return nil
}

func (a *App) getAppResourceInfo(app wego.Application, clusterName string) *AppResourceInfo {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this just be a pure function with no receiver? It might signal the intent better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. An earlier iteration was referencing something from the app and I neglected to make that change once it no longer did

return result
}

func (a *AppResourceInfo) sinkKind() string {
Copy link
Contributor

Choose a reason for hiding this comment

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

What does sink mean in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Breaking each app automation into a "source" and a "sink" (input from repo, output to cluster). I'm happy to use some other terminology if you have any ideas.

Copy link
Contributor

Choose a reason for hiding this comment

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

This appears to be returning an "automation" kind, which we are referring to as a DeploymentType elsewhere. Maybe deployKind() for now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, that seems reasonable to me

TargetDirAutomationPath string
}

func dirExists(d string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

small nit: this function already exists in utils, works for both files and directories

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool. Thanks for pointing that out!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, after looking... I need to know it's a directory 🤷‍♂️

}

// findAppManifests locates all manifests associated with a specific application within a repository
func findAppManifests(application wego.Application, repoDir string) ([][]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

it seems this function is only used in the tests, show it be move it there ? 🤔

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's only used in tests right now (because I wrote the test for it first :-)). It will be used in the actual code later.

gitProvider, err := a.gitProviderFactory(params.GitProviderToken)
if err != nil {
return err
}

var secretRef string
if SourceType(params.SourceType) == SourceTypeGit {
secretRef, err = a.createAndUploadDeployKey(params, params.Url, clusterName, gitProvider)
secretRef, err = a.createAndUploadDeployKey(info, params.DryRun, info.Spec.URL, gitProvider)
Copy link
Contributor

Choose a reason for hiding this comment

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

might be a good idea to use info.Spec.URL within createAndUploadDeployKey as we are already passing in info

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're using that parameter to select either the URL or the ConfigURL and need it to decide which one

a.logger.Actionf("Cloning %s", params.Url)
if err := a.cloneRepo(params.Url, params.Branch, params.DryRun); err != nil {
a.logger.Actionf("Cloning %s", info.Spec.URL)
if err := a.cloneRepo(info.Spec.URL, info.Spec.Branch, params.DryRun); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

would it be a good idea to remove the cloned repo when the user uses --url flag ? The cloning is happening in a temporary folder but I wonder if we should delete that temporary folder right away or if it would be ok to let the system delete it later? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wow. Funny we never noticed that before. 👍

return fmt.Errorf("failed to clone application repo: %w", err)
}
}

if !params.DryRun {
if !params.AutoMerge {
if err := a.createPullRequestToRepo(params, gitProvider, ".wego", params.Url, clusterName, appHash, appSpec, appGoat); err != nil {
if err := a.createPullRequestToRepo(info, gitProvider, info.Spec.URL, appHash, appSpec, appGoat); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

we could probably skip info.Spec.URL as we are passing in info

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, we can't here. This is called in two different places; once with the URL and once with the ConfigURL. We need to know which one to use.

return "", err
}

if err := ioutil.WriteFile(filepath.Join(workloadPath1, "nginx.yaml"), []byte(workloadYaml1), 0644); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

it's a minor thing, but we don't need to specify the whole file definition to verify if the Remove is working. you could just pass the string here []byte("file1") and the same later and check it in the test.

I think that makes clear what is being actually tested and clean up the code since we don't actually need the actual definition for anything else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense; you're right, the file contents are irrelevant.

@jrryjcksn jrryjcksn merged commit 3517c79 into main Jul 23, 2021
@jrryjcksn jrryjcksn deleted the 256/remove-application branch July 23, 2021 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants