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

#2241 fix bug: when running with a fat jar, need to read entries with openConnection #2952

Merged
merged 5 commits into from
Jan 8, 2024

Conversation

wkclz
Copy link
Contributor

@wkclz wkclz commented Dec 17, 2023

When running in IDE or a thin jar, it is ok.
When running with a fat jar in server, new JarFile can not open and read the entries.

… openConnection

when running in IDE or a thin jar, it is ok,
when running with a fat jar in server, new JarFile can not open and read the entries.
Copy link

linux-foundation-easycla bot commented Dec 17, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Dec 17, 2023
@k8s-ci-robot
Copy link
Contributor

Welcome @wkclz!

It looks like this is your first PR to kubernetes-client/java 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/java has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Dec 17, 2023
@brendandburns
Copy link
Contributor

Hrm, do you have a pointer to documentation about why this works this way? I definitely don't want this library to be opening URLs over the network, so is there a way to restrict this to file based entries in the fat jar?

@brendandburns
Copy link
Contributor

Also, you need to sign the CLA.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Dec 22, 2023
@wkclz
Copy link
Contributor Author

wkclz commented Dec 23, 2023

Running Yaml.load("yaml String"), in "io.kubernetes.client.util.Yaml:553"
When I run it in IDE, it can get Class by Kind, But in a fat jar, the class is null, and then, I got "Unknown apiVersionKind " + apiVersion + "/" + kind + " is it registered?"
I found several issues about this problem: issues: #1659, #2655, #2241, #2377 ...

Make a break point to trace the code, ModelMapper.initModelMap() is in the static, it will scan and cache classes to classesByGVK or preBuiltClassesByGVK from "io.kubernetes.client.openapi.models".
The problem is in here: io.kubernetes.client.util.ModelMapper.processJarPackage:
When the project is a fat jar, the packageURL is like this: "nested:/home/apps.jar/!BOOT-INF/lib/client-java-api-19.0.0.jar!/io/kubernetes/client/openapi/models", so it would get an IOException.

If try to get the jar like this: ((JarURLConnection) packageURL.openConnection()).getJarFile(), it will be OK. I have try it by overwrite this class.
This URL is only the jar url in current project, not over the network, so it is safe.
If the URL is not a JarURL, it would an ClassCastException.

In this issue #2241, I output the stack of the problem.

CLA is ok now.

jarFileName = jarFileName.substring(5, jarFileName.indexOf("!"));
logger.info("Loading classes from jar {}", jarFileName);
try (JarFile jf = new JarFile(jarFileName)) {
logger.info("Loading classes from jar {}", packageURL.getFile());
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add an explicit check for packageURL.startsWith("jar:") here?

@brendandburns
Copy link
Contributor

Ok, this approach works for me, I asked for a single explicit check for the jar: prefix otherwise looks good.

Is there a way that we can add a unit test for this?

In a fat jar, packageURL start with "nested:", 
not a fat jar, it start with "jar:".
@wkclz
Copy link
Contributor Author

wkclz commented Dec 28, 2023

Running unit test with Yaml.load("yaml string"); can only test for a packageURL start with "jar:".
You may create a project with springboot, and package it with the plugin spring-boot-maven-plugin, and then, you will get a packageURL start with "nested:", e.g.: nested:/home/apps.jar/!BOOT-INF/lib/client-java-api-19.0.0.jar!/io/kubernetes/client/openapi/models

packageURL is not jar or nested, it would be return
@brendandburns
Copy link
Contributor

/lgtm
/approve

I'm fine to merge this w/o a test given the complexity of creating one.

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brendandburns, wkclz

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 Dec 31, 2023
re import URLDecoder
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 1, 2024
@brendandburns
Copy link
Contributor

Looks like this isn't working right in our e2e tests, it can't find the class for nodes any more.

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Jan 3, 2024
@wkclz
Copy link
Contributor Author

wkclz commented Jan 3, 2024

fixed, and tested e2e tests

@yue9944882
Copy link
Member

Fixes: #1659

jf = new JarFile(jarFileName);
}
if (jf == null) {
logger.error("Loading classes from jar with error packageURL: {}", jarFileName);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
logger.error("Loading classes from jar with error packageURL: {}", jarFileName);
logger.error("Loading classes from jar with error packageURL: {}", packageURL);

nit: looks like we might want to dump the packageURL 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.

the same with packageURL

@brendandburns
Copy link
Contributor

@yue9944882 I'll wait for your lgtm on this one.

Copy link
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

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 8, 2024
@k8s-ci-robot k8s-ci-robot merged commit 65b3f8f into kubernetes-client:master Jan 8, 2024
18 checks passed
ryanjbaxter pushed a commit to ryanjbaxter/java that referenced this pull request Dec 6, 2024
In a fat jar, packageURL start with "nested:", 
not a fat jar, it start with "jar:".

(cherry picked from commit ec7e7b9)
ryanjbaxter pushed a commit to ryanjbaxter/java that referenced this pull request Dec 6, 2024
…eturn

packageURL is not jar or nested, it would be return

(cherry picked from commit 88ac227)
ryanjbaxter pushed a commit to ryanjbaxter/java that referenced this pull request Dec 6, 2024
re import URLDecoder

(cherry picked from commit de71d98)
k8s-ci-robot added a commit that referenced this pull request Dec 8, 2024
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/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants