X Tutup
Skip to content

Tweak call for CoreV1 group properly#1880

Merged
k8s-ci-robot merged 1 commit intokubernetes-client:masterfrom
dddddai:master
Sep 14, 2021
Merged

Tweak call for CoreV1 group properly#1880
k8s-ci-robot merged 1 commit intokubernetes-client:masterfrom
dddddai:master

Conversation

@dddddai
Copy link
Copy Markdown
Contributor

@dddddai dddddai commented Sep 10, 2021

Fix #1838

If basePath contains multiple segments (like using java client with rancher clusters), it would tweak the call incorrectly and break the request url, see #1838 (comment)

So it should calculate the segment indexes to be modified instead of hardcoding them to 0 and 1.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 10, 2021
HttpUrl tweakedUrl = url.newBuilder().removePathSegment(1).setPathSegment(0, "api").build();
String basePath = customObjectsApi.getApiClient().getBasePath();
// count segments in the basePath
int offset = 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think this will work if there is a trailing / in basePath (e.g. https://foo.bar/) and I think it probably should, can you add a test case for that and validate

Copy link
Copy Markdown
Contributor Author

@dddddai dddddai Sep 13, 2021

Choose a reason for hiding this comment

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

Thanks for your review, actually the trailing / is removed in ClientBuilder.build, so basePath would not have a trailing / (unless the user create a ApiClient directly without using ClientBuilder.build)

if (basePath != null) {
if (basePath.endsWith("/")) {
basePath = basePath.substring(0, basePath.length() - 1);
}
client.setBasePath(basePath);
}

}

@Test
public void testApplyServiceInRancher() throws KubectlException, IOException {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@dddddai this repo will stay vendor-neutral so i dont think it's a good idea to put such a test that verifying the behavior for rancher.

a better way to do this is to have an additional setter e.g. setAPIPrefix for the class GenericKubernetesApi so that users can tweak api path freely.

Copy link
Copy Markdown
Contributor Author

@dddddai dddddai Sep 13, 2021

Choose a reason for hiding this comment

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

@yue9944882 Thanks for your review

this repo will stay vendor-neutral so i dont think it's a good idea to put such a test that verifying the behavior for rancher.

Actually this test just makes sure tweakCallForCoreV1Group work fine with api prefix, so I guess the function name should be like testApplyServiceWithApiPrefix?

a better way to do this is to have an additional setter e.g. setAPIPrefix for the class GenericKubernetesApi so that users can tweak api path freely.

But users could not see GenericKubernetesApi when using Kubectl, for example:

V1Service service =
    Kubectl.apply(V1Service.class)
        .apiClient(new ClientBuilder().setBasePath("http://localhost:" + 8384 + prefix).build())
        .resource(
            new V1Service()
                .kind("Service")
                .apiVersion("v1")
                .metadata(
                    new V1ObjectMeta()
                        .namespace("foo")
                        .name("bar")
                        .labels(Collections.singletonMap("app", "bar")))
                .spec(spec))
        .execute();

In this case, users can only set api prefix on ApiClient, shall I add the setter in ClientBuilder and ApiClient?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@dddddai IIUC this pull doesnt need to involve changes in the KubectlApply related sources. making GenericKuberentesApi able to respect the prefix from the ApiClient should be sufficient. so we can remove this test, are we?

Copy link
Copy Markdown
Member

@yue9944882 yue9944882 left a comment

Choose a reason for hiding this comment

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

thanks, this pull will provide the extensibility for switching between multiple clusters. plz rename the test to get rid of the name like rancher

}

@Test
public void testApplyServiceInRancher() throws KubectlException, IOException {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@dddddai IIUC this pull doesnt need to involve changes in the KubectlApply related sources. making GenericKuberentesApi able to respect the prefix from the ApiClient should be sufficient. so we can remove this test, are we?

"",
"v1",
"pods",
new ClientBuilder().setBasePath("http://localhost:" + 8181 + prefix).build());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

on the other hand, i think the usage of setBasePath here is error-proning b/c the setter is doing an in-place modification upon the ApiClient instance. there will be thread-safety issue if multiple generic api modifying the basepath concurrently. ideally, what i expected is a function to provide an immutable shallow copy of ApiClient before we tweak the prefix, such as:

ApiClient shallowCopied = apiClient.newBuilder().setBasePath("<path>" + prefix).build()

we can do this in a follow-up anyway

Copy link
Copy Markdown
Contributor Author

@dddddai dddddai Sep 13, 2021

Choose a reason for hiding this comment

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

i think the usage of setBasePath here is error-proning b/c the setter is doing an in-place modification upon the ApiClient instance. there will be thread-safety issue if multiple generic api modifying the basepath concurrently

Sorry I didn't get your point, setBasePath sets the basepath of ClientBuilder for creating a new ApiClient object, each cluster has its own ApiClient.
And there is no method for GenericKuberentesApi to modify the basepath. Please correct me if I'm wrong.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

oh oversight, i took ClientBuilder as ApiClient.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 13, 2021
@dddddai
Copy link
Copy Markdown
Contributor Author

dddddai commented Sep 13, 2021

@yue9944882 Thanks for elaborating, I have modified the tests as you addressed, PTAL

Copy link
Copy Markdown
Member

@yue9944882 yue9944882 left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

thanks!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 14, 2021
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dddddai, yue9944882

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

The pull request process is described here

Details 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 Sep 14, 2021
@k8s-ci-robot k8s-ci-robot merged commit 8b5c359 into kubernetes-client:master Sep 14, 2021
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/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KubectlApply doesn't work with some Rancher clusters.

4 participants

X Tutup