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

🐛 Allow using the --from flag to get a template from a github release #7453

Merged
merged 1 commit into from
Jan 18, 2023

Conversation

nunnatsa
Copy link
Contributor

@nunnatsa nunnatsa commented Oct 25, 2022

Currently, clusterctl generate cluster's --from flag only supports yaml files from github code (blob). Trying to get a template asset from a release tag is failing.

This PR adds the option to use the --form also for release assets.

Fixes #7455

Signed-off-by: Nahshon Unna-Tsameret nunnatsa@redhat.com

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Oct 25, 2022
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 25, 2022
@nunnatsa nunnatsa changed the title Allow using the --from to get template from a github release 🐛 Allow using the --from to get template from a github release Oct 25, 2022
@nunnatsa nunnatsa force-pushed the fix-from-url branch 4 times, most recently from b90b1fa to 4a7aae4 Compare October 25, 2022 14:41
@nunnatsa nunnatsa changed the title 🐛 Allow using the --from to get template from a github release 🐛 Allow using the --from flag to get a template from a github release Oct 25, 2022
@@ -181,35 +182,77 @@ func (t *templateClient) getGitHubFileContent(rURL *url.URL) ([]byte, error) {

// Extract all the info from url split.
owner := urlSplit[0]
repository := urlSplit[1]
Copy link
Contributor

Choose a reason for hiding this comment

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

just curious, what is the reason for renaming the variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The linter says: "Variable 'repository' collides with imported package name". Same for the client var below.

image

return getGithubFileContentFromCode(ghClient, rURL.Path, owner, repo, path, branch)

case "releases": // get a github release asset
tag := urlSplit[4]
Copy link
Contributor

Choose a reason for hiding this comment

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

does it make sense to extract it above with all other variables related to the URL?

Copy link
Contributor Author

@nunnatsa nunnatsa Oct 26, 2022

Choose a reason for hiding this comment

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

Not sure. I tend not to do that, because the meaning of the 4th and 5th path components is different between these two cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree with not extracting them early. Those values hold different meaning under blob and releases cases.

func getGithubAssetFromRelease(ghClient *github.Client, path string, owner string, repo string, tag string, assetName string) ([]byte, error) {
release, _, err := ghClient.Repositories.GetReleaseByTag(ctx, owner, repo, tag)
if err != nil {
return nil, handleGithubErr(err, "failed to get release '%s' from %s%s repository", tag, owner, repo)
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
return nil, handleGithubErr(err, "failed to get release '%s' from %s%s repository", tag, owner, repo)
return nil, handleGithubErr(err, "failed to get release '%s' from %s/%s repository", tag, owner, repo)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}
return content, nil
}

func getGithubAssetFromRelease(ghClient *github.Client, path string, owner string, repo string, tag string, assetName string) ([]byte, error) {
release, _, err := ghClient.Repositories.GetReleaseByTag(ctx, owner, repo, tag)
Copy link
Contributor

Choose a reason for hiding this comment

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

should we check for a non-nil release?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@nunnatsa
Copy link
Contributor Author

@alexander-demicev - could you please review again, after the fixes?

return getGithubFileContentFromCode(ghClient, rURL.Path, owner, repo, path, branch)

case "releases": // get a github release asset
tag := urlSplit[4]
Copy link
Contributor

Choose a reason for hiding this comment

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

Agree with not extracting them early. Those values hold different meaning under blob and releases cases.

var rc io.ReadCloser
for _, asset := range release.Assets {
if asset.GetName() == assetName {
rc, _, err = ghClient.Repositories.DownloadReleaseAsset(ctx, owner, repo, asset.GetID(), ghClient.Client())
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you will also have to handle the redirects when working with release assets. Similar to how we handle the redirects here:
https://github.com/nunnatsa/cluster-api/blob/fd24c247c3ae9b32fed236f544088486ec6331cf/cmd/clusterctl/client/repository/repository_github.go#L331-L344

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 believe you will also have to handle the redirects when working with release assets. Similar to how we handle the redirects here: https://github.com/nunnatsa/cluster-api/blob/fd24c247c3ae9b32fed236f544088486ec6331cf/cmd/clusterctl/client/repository/repository_github.go#L331-L344

Thanks @ykakarap. I'm not sure it's needed. According to the DownloadReleaseAsset documentation:

followRedirectsClient can be passed to download the asset from a redirected location. Passing http.DefaultClient is recommended unless special circumstances exist, but it's possible to pass any http.Client. If nil is passed the redirectURL will be returned instead.

I added unit test to check the redirect scenario. Also, This code is working with the real github, that actually responses with redirect. If you think it's a must, I'll add it, but it's not needed, I think.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seem to work for redirected assets as well. @fabriziopandini Do you know why we are redirecting here: https://github.com/nunnatsa/cluster-api/blob/fd24c247c3ae9b32fed236f544088486ec6331cf/cmd/clusterctl/client/repository/repository_github.go#L331-L344?

Do you know of a case that is not covered by the standard DownloadReleaseAsset function?

Copy link
Member

@fabriziopandini fabriziopandini Jan 2, 2023

Choose a reason for hiding this comment

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

@ykakarap we implemented this because this is one of the use case allowed by the return parameters of the GitHub API, but AFAIK we did not investigate why this parameter exists / in which case they support.

Unless we collect evidence this is unnecessary, I strongly suggest to handle redirect also 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.

Thanks @fabriziopandini. According to the DownloadReleaseAsset documentation I copied above, the redirect is only returned if the followRedirectsClient parameter is empty. This is, I guess, if you want to implement the redirect logic. But if you pass the client, the function will do it for you. This is how I understand it.

Copy link
Member

Choose a reason for hiding this comment

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

kk, I double checked the DownloadReleaseAsset implementation and it seems that this is not necessary if we are passing a followRedirectsClient.
In a separated PR we can also eventually clean up also the bit of code @ykakarap was referencing to

Copy link
Contributor

Choose a reason for hiding this comment

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

Created a follow-up issue to track the clean up work. #7942

@sbueringer
Copy link
Member

@ykakarap Can you please double check if this PR is in conflict with #7371 or if they are othogonal? (feature-wise)

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 1, 2022
@nunnatsa
Copy link
Contributor Author

nunnatsa commented Nov 1, 2022

... if this PR is in conflict with #7371 or if they are othogonal? (feature-wise)

Thanks @sbueringer - I didn't know #7371, but I just read it. As far I can see, not only this PR is not conflicts with 7371, but it is even more important with it, because with 7371, blob github link will work, non-github link will work, but github release asset links will fail - and so #7455 is even more meaningful bug.

@ykakarap
Copy link
Contributor

ykakarap commented Nov 2, 2022

@ykakarap Can you please double check if this PR is in conflict with #7371 or if they are othogonal? (feature-wise)

They are not in conflict with each other. They are complimentary. I agree with @nunnatsa that having both 7371 and this PR would be beneficial as they would cover a broader set of cases.

@sbueringer
Copy link
Member

Sounds good! Thx for checking

@CecileRobertMichon
Copy link
Contributor

/cc @Jont828

Copy link
Contributor

@ykakarap ykakarap left a comment

Choose a reason for hiding this comment

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

Overall LGTM.

This will be a nice addition :)

@@ -181,35 +182,81 @@ func (t *templateClient) getGitHubFileContent(rURL *url.URL) ([]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.

I cannot select the correct line here but the error in l.179 should be adjusted as now we support a blob as well as a release asset.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @ykakarap - fixed.

var rc io.ReadCloser
for _, asset := range release.Assets {
if asset.GetName() == assetName {
rc, _, err = ghClient.Repositories.DownloadReleaseAsset(ctx, owner, repo, asset.GetID(), ghClient.Client())
Copy link
Contributor

Choose a reason for hiding this comment

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

Seem to work for redirected assets as well. @fabriziopandini Do you know why we are redirecting here: https://github.com/nunnatsa/cluster-api/blob/fd24c247c3ae9b32fed236f544088486ec6331cf/cmd/clusterctl/client/repository/repository_github.go#L331-L344?

Do you know of a case that is not covered by the standard DownloadReleaseAsset function?

@nunnatsa
Copy link
Contributor Author

Hi @fabriziopandini. What do you think about the redirect question from @ykakarap, here? #7453 (comment)

@fabriziopandini
Copy link
Member

Hi @fabriziopandini. What do you think about the redirect question from @ykakarap, here? #7453 (comment)

Sorry for the delay, answered in the thread above

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 2, 2023
Currently, `clusterctl generate cluster`'s `--from` flag only support
yaml files from github code. Trying to get template from a rlease tag is
failing.

This PR adds the option to use the `--form` also for release assets.

Signed-off-by: Nahshon Unna-Tsameret <nunnatsa@redhat.com>
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 2, 2023
@nunnatsa
Copy link
Contributor Author

nunnatsa commented Jan 2, 2023

rebased (fix conflicts)

Copy link
Contributor

@Jont828 Jont828 left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution, overall this make sense to me. I'll try to test this out locally when I get the chance, were you able to get this working by running a copy of clusterctl built off of this code?

@nunnatsa
Copy link
Contributor Author

were you able to get this working by running a copy of clusterctl built off of this code?

yes

@fabriziopandini
Copy link
Member

/lgtm
ready to approve as soon as we get a final feedback from @Jont828 and @ykakarap

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 11, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 018505cf99c94f0d5cc398e6b24e2b18ab452b3b

@ykakarap
Copy link
Contributor

Did a final pass and this is good to go.

/lgtm

Great work! Thank you for adding the release asset support. 🚀

@sbueringer
Copy link
Member

/approve
Given lgtms above

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sbueringer

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 18, 2023
@k8s-ci-robot k8s-ci-robot merged commit 686961a into kubernetes-sigs:main Jan 18, 2023
@k8s-ci-robot k8s-ci-robot added this to the v1.4 milestone Jan 18, 2023
@nunnatsa nunnatsa deleted the fix-from-url branch January 18, 2023 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't use the --from flag to generate a cluster, from a github release asset
8 participants