Skip to content

Simplify handling of cluster URIs from kubeconfig #8499

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

Merged
merged 2 commits into from
Oct 8, 2022

Conversation

vasiliy-ul
Copy link
Contributor

What this PR does / why we need it:

Handling of cluster URIs with additional path elements added in the commit fba65ed can be simplified with the use of Request.AbsPath

// AbsPath overwrites an existing path with the segments provided. Trailing slashes are preserved
// when a single segment is passed.
func (r *Request) AbsPath(segments ...string) *Request {
if r.err != nil {
return r
}
r.pathPrefix = path.Join(r.c.base.Path, path.Join(segments...))
if len(segments) == 1 && (len(r.c.base.Path) > 1 || len(segments[0]) > 1) && strings.HasSuffix(segments[0], "/") {
// preserve any trailing slashes for legacy behavior
r.pathPrefix += "/"
}
return r
}

which literally does the same as the introduced adaptUriForHostPath function:

func (v *vmis) adaptUriForHostPath(uri string) string {
u, err := url.Parse(v.config.Host)
if err != nil {
return uri
}
return path.Join(u.Path, uri)
}

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

This is a follow-up of the discussion #8129 (comment)

Release note:

NONE

@kubevirt-bot kubevirt-bot added release-note-none Denotes a PR that doesn't merit a release note. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. size/L labels Sep 21, 2022
@xpivarc
Copy link
Member

xpivarc commented Sep 21, 2022

Thanks for following up @vasiliy-ul !
/cc

@vasiliy-ul
Copy link
Contributor Author

Thanks for following up @vasiliy-ul ! /cc

👌

... Still, let's see if the CI tests pass. I also found a couple more places where complex cluster URIs were not handled. Fixed those as well.

@vasiliy-ul
Copy link
Contributor Author

@mlhnono68, FYI. Maybe you could also take a look here? Unfortunately, I do not have a Rancher cluster to test e2e. Though I checked the resulting request URIs with different kubeconfig's and the path is preserved.

@vasiliy-ul
Copy link
Contributor Author

/retest-required

@xpivarc
Copy link
Member

xpivarc commented Sep 21, 2022

/approve
I think we could simulate this with functional test. It might be tricky as we would need to spawn proxy(the easier part) and modify kubeconfig for the one test.

@kubevirt-bot kubevirt-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 21, 2022
@@ -65,7 +65,7 @@ var _ = Describe("Kubevirt Version Client", func() {
ghttp.RespondWithJSONEncoded(http.StatusOK, groupInfo),
),
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/apis/"+groupInfo.PreferredVersion.GroupVersion+"/version"),
ghttp.VerifyRequest("GET", "/apis"+groupInfo.PreferredVersion.GroupVersion+"/version"),
Copy link
Member

Choose a reason for hiding this comment

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

How come we don't need this "/"?

Copy link
Contributor Author

@vasiliy-ul vasiliy-ul Sep 21, 2022

Choose a reason for hiding this comment

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

Yeah, looks a bit weird. It appears that we use /apis//apis/subresources.kubevirt.io/v1alpha3/version uri (with //):

// Now, query the preferred version
uri = fmt.Sprintf("%s/apis/%s/version", u.Path, group.PreferredVersion.GroupVersion)
var serverInfo version.Info
result = v.restClient.Get().RequestURI(uri).Do(context.Background())

Here group.PreferredVersion.GroupVersion already contains a leading / (potentially we can fix also the format if it makes sense).

Previously, it was passed to the RequestURI method, which under the hood calls url.Parse(uri). This one preserves the path as-is with //. Hence, we needed this extra / in the test.

With this patch, the uri is now passed to AbsPath, which does path.Join(..., uri). It looks like this join sanitizes the resulting path and the output has only a single /.

Simple reproducer: https://go.dev/play/p/IU80SS_Gj5v

https://pkg.go.dev/path#Join:

Join joins any number of path elements into a single path, separating them with slashes. Empty elements are ignored. The result is Cleaned. However, if the argument list is empty or all its elements are empty, Join returns an empty string.

@vasiliy-ul
Copy link
Contributor Author

I think we could simulate this with functional test. It might be tricky as we would need to spawn proxy(the easier part) and modify kubeconfig for the one test.

I was thinking more in the direction of unit-testing. Even if we manage to setup a proxy in CI and to use a custom config, still calling all the APIs e2e will require a significant amount of effort and resources.

@xpivarc
Copy link
Member

xpivarc commented Sep 23, 2022

I think we could simulate this with functional test. It might be tricky as we would need to spawn proxy(the easier part) and modify kubeconfig for the one test.

I was thinking more in the direction of unit-testing. Even if we manage to setup a proxy in CI and to use a custom config, still calling all the APIs e2e will require a significant amount of effort and resources.

Agree. I was thinking about discovery & dry-run but I think unit-tests are enough too.

@vasiliy-ul
Copy link
Contributor Author

Added unit tests in a separate commit. Basically, I used the existing tests for regular URIs and transformed them to tables parametrized with an additional path element.

@vasiliy-ul
Copy link
Contributor Author

/test pull-kubevirt-e2e-k8s-1.23-sig-compute-nonroot

@vasiliy-ul vasiliy-ul force-pushed the simplify-uri-handling branch from f007747 to 88a42cb Compare September 27, 2022 07:31
@vasiliy-ul
Copy link
Contributor Author

Added more unit-tests. No functional changes.

Handling of cluster URIs with additional path elements added in the
commit fba65ed can be simplified with the use of Request.AbsPath
which literally does the same as the introduced adaptUriForHostPath
function.

Signed-off-by: Vasiliy Ulyanov <vulyanov@suse.de>
E.g. https://rancher-url/k8s/clusters/c-m-someid

Signed-off-by: Vasiliy Ulyanov <vulyanov@suse.de>
@vasiliy-ul vasiliy-ul force-pushed the simplify-uri-handling branch from 88a42cb to e9c7c65 Compare September 27, 2022 08:05
Copy link
Member

@xpivarc xpivarc left a comment

Choose a reason for hiding this comment

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

/approve
@rmohr Can you take a look, please?


result := v.restClient.Get().RequestURI(uri).Do(context.Background())
uri := ApiGroupName
result := v.restClient.Get().AbsPath(uri).Do(context.Background())
Copy link
Member

Choose a reason for hiding this comment

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

nit: You can inline the uri

@kubevirt-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: xpivarc

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

@xpivarc
Copy link
Member

xpivarc commented Oct 7, 2022

/cc @fossedihelm Would you be able to have a look?

@fossedihelm
Copy link
Contributor

@xpivarc sure

@fossedihelm
Copy link
Contributor

Thanks!
/lgtm
/retest

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Oct 7, 2022
@vasiliy-ul
Copy link
Contributor Author

/retest-required

1 similar comment
@vasiliy-ul
Copy link
Contributor Author

/retest-required

@kubevirt-commenter-bot
Copy link

/retest-required
This bot automatically retries required jobs that failed/flaked on approved PRs.
Silence the bot with an /lgtm cancel or /hold comment for consistent failures.

@kubevirt-bot
Copy link
Contributor

kubevirt-bot commented Oct 7, 2022

@vasiliy-ul: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubevirt-e2e-k8s-1.23-sig-storage-nonroot e9c7c65 link true /test pull-kubevirt-e2e-k8s-1.23-sig-storage-nonroot

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@kubevirt-commenter-bot
Copy link

/retest-required
This bot automatically retries required jobs that failed/flaked on approved PRs.
Silence the bot with an /lgtm cancel or /hold comment for consistent failures.

@kubevirt-bot kubevirt-bot merged commit 85575d0 into kubevirt:main Oct 8, 2022
@vasiliy-ul vasiliy-ul deleted the simplify-uri-handling branch October 9, 2022 12:34
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. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. lgtm Indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/XL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants