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

use kpt git parsing in rpkg clone #3080

Merged
merged 4 commits into from
May 4, 2022
Merged

Conversation

natasha41575
Copy link
Contributor

@natasha41575 natasha41575 commented May 2, 2022

For kpt alpha rpkg clone, make using the --ref and --directory flags optional when using git URLs, instead allow something like

kpt alpha rpkg clone https://github.com/GoogleCloudPlatform/blueprints.git/catalog/bucket@main cloned-bucket \
  --repository=deployments \
  --namespace=default

Throw errors if the ref/directory are both present in the URL and set via flags, and they don't match.

@natasha41575 natasha41575 changed the title use kpt git parsing in porch WIP: use kpt git parsing in porch May 2, 2022
@@ -154,11 +145,15 @@ func (r *runner) preRunE(cmd *cobra.Command, args []string) error {
}

case strings.Contains(source, "/"):
gitArgs, err := parse.GitParseArgs(context.Background(), args)
Copy link
Contributor

Choose a reason for hiding this comment

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

will this try to clone the repository locally (or try to contact it in any way), or is this purely syntax-level parsing?

Copy link
Contributor Author

@natasha41575 natasha41575 May 3, 2022

Choose a reason for hiding this comment

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

From what I can tell, GitParseArgs will try to contact the repository only if there is no ref/version provided in the URL. It tries to make contact in this case to determine what the default branch is.

What is the correct behavior for kpt alpha rpkg clone if no version is provided? I can extract the syntax-level parsing out of GitParseArgs and use that here instead if we want to do something different, like throw an error or leave the ref empty.

Copy link
Contributor

Choose a reason for hiding this comment

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

empty ref would be ok, or we can default to main. Porch defaults to main in other places where ref is not specified.

Error may be OK too.

I actually thought (though may be wrong) that another case where we contact the server is if the .git/ part is missing and the parser can't tell where the repository path ends and directory path begins:

domain.com/some/path/here

is the some/path repo? or some ?

regardless, the rpkg commands should not interact with the repository and only operate at the level of syntax of the URL provided, even if there are cases we can't handle and that will result in errors (IMO)

Copy link
Contributor Author

@natasha41575 natasha41575 May 3, 2022

Choose a reason for hiding this comment

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

empty ref would be ok, or we can default to main. Porch defaults to main in other places where ref is not specified.

I'll default to main to be consistent with other places.

I actually thought (though may be wrong) that another case where we contact the server is if the .git/ part is missing and the parser can't tell where the repository path ends and directory path begins

Ah you are correct, I missed that it contacts the repo while attempting to convert the URL to a URL containing .git.

Is it a requirement for kpt alpha rpkg clone that the URL contains .git for git URLs? I'm thinking about moving the logic in GitParseArgs that handles URLs with a .git suffix out to its own helper function so that we can use it here without worrying about communication with the repo.

Copy link
Contributor

Choose a reason for hiding this comment

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

Presence of the .git part of the URL is not requirement. I'd say the requirement is that the parser doesn't modify the URL.

So if customer gave us https://github.com/org/repo.git/path/to/package
we should include the .git

if they provided https://github.com/org/repo/path/to/package
we should not artificially introduce .git part into the URL.

c.Flags().StringVar(&r.directory, "directory", "/", "Directory within the repository where the upstream package is located.")
c.Flags().StringVar(&r.ref, "ref", "main", "Branch in the repository where the upstream package is located.")
c.Flags().StringVar(&r.directory, "directory", "", "Directory within the repository where the upstream package is located.")
c.Flags().StringVar(&r.ref, "ref", "", "Branch in the repository where the upstream package is located.")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed these defaults because otherwise it is impossible to tell whether the user set the flag explicitly or not. Since we are trying to infer the ref from the URL, we need to be able to throw an error if the user sets the flag and also includes the ref in the URL and they contradict each other. I set the default manually below in L182-L187 after this error check occurs.

Copy link
Contributor

Choose a reason for hiding this comment

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

good call, thank you!

(some flag libraries have a way to tell whether a flag was explicitly set, but from top of my head I don't know if Cobra is one of them; this is a good approach though)

- https://github.com/platkrm/test-blueprints.git
- --directory=basens
- --ref=basens/v1
- https://github.com/platkrm/test-blueprints.git/basens@basens/v1
Copy link
Contributor Author

@natasha41575 natasha41575 May 4, 2022

Choose a reason for hiding this comment

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

Updated this test to demonstrate the new inferring behavior. There is another similar command that I haven't modified in rpkg-edit/config.yaml, that uses the --directory and --ref flags, demonstrating that the user can still use the flags when needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

thank you! this is great!

@natasha41575 natasha41575 marked this pull request as ready for review May 4, 2022 15:45
@natasha41575 natasha41575 requested review from martinmaly and yuwenma May 4, 2022 15:45
@natasha41575 natasha41575 changed the title WIP: use kpt git parsing in porch use kpt git parsing in rpkg clone May 4, 2022
- https://github.com/platkrm/test-blueprints.git
- --directory=basens
- --ref=basens/v1
- https://github.com/platkrm/test-blueprints.git/basens@basens/v1
Copy link
Contributor

Choose a reason for hiding this comment

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

thank you! this is great!

Comment on lines +49 to +50
* kpt alpha rpkg clone http://git-repository.git/package-name@main target --repository=repo
* kpt alpha rpkg clone http://git-repository.git target --directory=package-name --ref=main --repository=repo
Copy link
Contributor

Choose a reason for hiding this comment

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

@mortent is working on rationalizing all flags and group and command names in the alpha group; one thing I noticed (and it on me) that I picked suboptimal flag names for the clone command - there are 2 repositories involved (from, to) and the flag name is not directional (only --repository) so it may be confusing to customers. That said, let's not fix it here, just something to keep in mind for the big rename Morten is working on.

c.Flags().StringVar(&r.directory, "directory", "/", "Directory within the repository where the upstream package is located.")
c.Flags().StringVar(&r.ref, "ref", "main", "Branch in the repository where the upstream package is located.")
c.Flags().StringVar(&r.directory, "directory", "", "Directory within the repository where the upstream package is located.")
c.Flags().StringVar(&r.ref, "ref", "", "Branch in the repository where the upstream package is located.")
Copy link
Contributor

Choose a reason for hiding this comment

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

good call, thank you!

(some flag libraries have a way to tell whether a flag was explicitly set, but from top of my head I don't know if Cobra is one of them; this is a good approach though)

@@ -287,3 +285,9 @@ func getDest(v, repo, subdir string) (string, error) {
}
return v, nil
}

// HasGitSuffix returns true if the provided pkgURL is a git repo containing the ".git" suffix
func HasGitSuffix(pkgURL 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.

[nit] we are actually not looking for suffix per se, but (more accurately) checking for a URL path segment that ends with '.git'.

(and, I must admit I can't come up with a better name, so feel free to leave it :)

@natasha41575
Copy link
Contributor Author

Thanks for the reviews and answering all my questions!

@natasha41575 natasha41575 merged commit 9a8998a into kptdev:main May 4, 2022
@natasha41575 natasha41575 deleted the urlparse branch May 4, 2022 17:14
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.

2 participants