Kubernetes-client: Use try with resources in kubernetes-examples

Created on 27 Aug 2020  路  14Comments  路  Source: fabric8io/kubernetes-client

We're not closing client in some of the examples after using it, we should either close it with client.close() or use KubernetesClient like this:

try (KubernetesClient client = new DefaultKubernetesClient()) {
  // Use Client for your stuff
}

Here are some places where it's not closed properly:

  • [X] [ListCustomResourceDefinitions.java](https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/ListCustomResourceDefinitions.java)
  • [X] [LoadMultipleDocumentsFromFileExample.java](https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/LoadMultipleDocumentsFromFileExample.java)
  • [X] [AdaptClient.java](https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/openshift/examples/AdaptClient.java)
  • [X] [BuildConfigExamples.java](https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/openshift/examples/BuildConfigExamples.java)
  • [X] [DeploymentConfigScale.java](https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/openshift/examples/DeploymentConfigScale.java)
  • [ ] [ImageStreamExample](https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/openshift/examples/ImageStreamExample.java)
  • [ ] [ImageStreamTagExample.java](https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/openshift/examples/ImageStreamTagExample.java)
  • [ ] [ListBuildConfigs.java](https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/openshift/examples/ListBuildConfigs.java)
  • [ ] [ListDeploymentConfigs.java](https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/openshift/examples/ListDeploymentConfigs.java)
  • [ ] [LoadExample.java](https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/openshift/examples/LoadExample.java)
  • [ ] [DeploymentConfigExamples.java](https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/openshift/examples/DeploymentConfigExamples.java)

There are some more occurrences listed in Sonar Bug report. You can have a look at it here.

help wanted low hanging fruit

All 14 comments

/assign

@Shivkumar13 : polite ping, Are you still working on this issue?

@rohanKanojia apologies for the delay, I will update my findings in a day.
Was in other tasks for a while.

@rohanKanojia Is this issue still relevant? If yes, I would like to work on it.

Yes, it's still relevant. You might want to check with @Shivkumar13 if he is still working on it or not. There are lots of quickstarts that need to be updated, maybe you both can collaborate on refactoring these :-)

@Shivkumar13 if you are working on it can you give an update on which all quickstart have you worked already and which all are still pending? so that I can start working on this issue.

@HeroicHitesh I will raise a PR by tomorrow, need some cross checks

I have the fix ready, while raising PR, seeing some old git changes also popping up in this Fix.
I will rebase the branch and will try again tomorrow.

@Shivkumar13 any updates? You can send a PR for the files you have modified and I'll do the rest. Or if you are facing any issue related to Git/GitHub, let us know, maybe we can help you out

@HeroicHitesh already raised a PR, day before yesterday

See: https://github.com/fabric8io/kubernetes-client/pull/2567

@Shivkumar13 My bad I couldn't see any linked PR so thought you are stuck in that rebase thing. Now, from the PR I can see
https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/LoadMultipleDocumentsFromFileExample.java

https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/openshift/examples/AdaptClient.java

these 2 are fixed and the remaining are pending. Do you have other fixes ready or can I start working on them?
Also, @rohanKanojia can you please make a checkbox in-front of the links in the issue description, so that it's easy to track which files are fixed and which are still pending.

@HeroicHitesh : Thanks for pointing this out, I've updated the issue description.

@Shivkumar13 My bad I couldn't see any linked PR so thought you are stuck in that rebase thing. Now, from the PR I can see
https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/LoadMultipleDocumentsFromFileExample.java

https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/openshift/examples/AdaptClient.java

these 2 are fixed and the remaining are pending. Do you have other fixes ready or can I start working on them?

@HeroicHitesh I am not having other ready at the moment, but I can work on them, in the meantime, feel free to work on those.
please do list here the ones that you will be fixing. Then I can pick others.

Also, @rohanKanojia can you please make a checkbox in-front of the links in the issue description, so that it's easy to track which files are fixed and which are still pending.

@rohanKanojia I have sent a PR for the following files:

  • ListCustomResourceDefinitions.java
  • BuildConfigExamples.java
  • LoadExample.java
  • DeploymentConfigExamples.java
    Please review and let me know what needs to be updated there.
Was this page helpful?
0 / 5 - 0 ratings