java icon indicating copy to clipboard operation
java copied to clipboard

NullPointerException when ClientBuilder.kubeconfig(kubeconfigfile)

Open pawank0411 opened this issue 3 years ago • 2 comments
trafficstars

Hi Team,

I am facing an issue while building the ApiClient using config file which is placed in the path specified by the Kube Config documentation. Path : /.kube/config

Exception I am getting while accessing ClientBuilder.kubeconfig(kubeconfigfile) :

java.lang.NullPointerException: null at io.kubernetes.client.util.ClientBuilder.kubeconfig(ClientBuilder.java:288) at
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxySCglibMethodInvocation.invoke Joinpoint (CglibAopProxy.java:793) at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) at
org.springframework.aop.aspectj.MethodInvocationProceeding JoinPoint.proceed(MethodInvocationProceeding JoinPoint.java:89) at sun.reflect.GeneratedMethodAccessor177.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.aspectj.AbstractAspect...
.....

On logging the kubeconfig file kubeconfig.getServers() - null kubeconfig.getCluster() - I am getting a cluster object where the server field is populated.

Not sure why getServers() is null due to which I am getting the above exception.


KubeConfig File:

apiVersion: v1
kind: Config
clusters:
 - cluster:
	certificate-authority-data:MNSNDKDKDK....
	server: https://10.3.0.2
   name: <name>
users:
 - name: <service_account>
contexts:
 - context:
	cluster: <cluster>
	user: <service_account>
	namespace: <namespace>
   name: <name>
current-context: <context>

pawank0411 avatar Aug 16 '22 13:08 pawank0411

I'm sort of surprised that your path is /.kube/config as that seems to imply that your $HOME is the root of the file system which is a little weird, but assuming that is correct, I have some questions:

What version of the library are you using? What is the environment (container, desktop/laptop)?

Also it looks like you have tabs instead of spaces in your YAML, it's possible that the tabs are throwing off the YAML parser? Could you try replacing with standard YAML spacing (two-space indent) and see if that works?

brendandburns avatar Aug 16 '22 21:08 brendandburns

Hi @brendandburns Thank you for your quick response.

These are the versions and environment details : Library (client-java-extended) - 15.0.1 GKE - 1.21.12-gke.1700 Platform - Laptop/Windows 11

And, maybe I have added tabs in the above comment while creating the issue. But I will re check in the config file and let you know.

pawank0411 avatar Aug 17 '22 00:08 pawank0411

@brendandburns - I have rechecked the config file, it correctly formatted. It is following 2 spaces indent.

pawank0411 avatar Aug 17 '22 10:08 pawank0411

Where is your kubeconfig file located? The standard location on Windows would be $HOME\.kube\config.

Especially on a Windows system it seems really unlikely that the kubeconfig path that you mentioned above (/.kube/config) is the correct path.

Have you tried the default cluster config option: https://github.com/kubernetes-client/java/blob/master/examples/examples-release-15/src/main/java/io/kubernetes/client/examples/Example.java#L34

Does that work correctly?

brendandburns avatar Aug 17 '22 15:08 brendandburns

Hi @brendandburns - There was one space missing at the end of the file. After, adding a space the file is getting parsed and I can see the values as per my requirement.

Thank you for the help.

pawank0411 avatar Aug 18 '22 09:08 pawank0411

Going forward, I have started getting a different exception, let me share the logs. Please let me know the cause for this.

io.kubernetes.client.extended.kubectl.exception. KubectlException: io.kubernetes.client.openapi.ApiException: at 
io.kubernetes.client.extended.kubectl.Kubectl$ApiClientBuilder.refreshDiscovery (Kubectl.java:235) at 
io.kubernetes.client.extended.kubectl.KubectlGet $KubectlGetSingle.execute(KubectlGet.java:108) at
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at
org.springframework.aop.framework.CglibAopProxySCglibMethodInvocation.invokeJoinpoint (CglibAopProxy.java:793) at 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at
org.springframework.aop.framework.CglibAopProxySCglibMethodInvocation.proceed(CglibAopProxy.java:763) at
.....

The below code I am trying to execute.

V1Deployment pod = null;
try {
pod = Kubectl.get(V1Deployment.class).namespace("something").name("something-pod").execute();
} catch (KubectlException e) { e.printStackTrace(); }

As far I can understand, there is something wrong with the execute command in the above code.

pawank0411 avatar Aug 18 '22 10:08 pawank0411

Hi @brendandburns can you please provide an update on the above mentioned issue.

pawank0411 avatar Aug 19 '22 18:08 pawank0411

The client is trying to introspect the APIs on your cluster and it looks like it's getting an HTTP error. If you print more details of the ApiException specifically ((ApiException)KubectlException.getCause()).getResponseBody() you should get more details about what is going wrong.

brendandburns avatar Aug 21 '22 20:08 brendandburns

On printing ((ApiException)KubectlException.getCause()).getResponseBody()

I am getting the below logs, I am not sure why the user is anonymous provided the user in config file is a service account.

{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure", "message": "forbidden: User "\system: anonymous\" cannot get path \"/api\"", "reason":"Forbidden", "details":{},"code": 403}

pawank0411 avatar Aug 23 '22 05:08 pawank0411

Your requests are not authenticated (that's what system: anonymous means).

Looking at the kubeconfig file above, it appears that you don't have any authentication information in the kubeconfig (e.g. there's no token, no exec section or anything else.

I think that your kubeconfig file is incomplete.

I would take a step back and ensure that kubectl is working in the same environment. If kubectl is working, this client should also work.

brendandburns avatar Aug 25 '22 15:08 brendandburns

Hi @brendandburns - I have tried another approach to scale the PODs in GKE. Ultimately, the final requirement was to scale up/down the PODs using API and it is working as expected.

With this, on the custom API where I am invoking the client method, I want t he status of the V1Deployment as the response of the API. But, whenever I am calling getStatus() method of V1Deployment, I am getting the old status not the new. Basically the old replica count.

So, do we have any way by which we can watch/listen the update API ?

The reference code which I am using : https://github.com/kubernetes-client/java/blob/c00152ae859e5af8f2c57067e3d2c75a6d3edf14/examples/examples-release-15/src/main/java/io/kubernetes/client/examples/ExpandedExample.java#L212

Trying to get the updated status :

V1Deployment response=appsV1Api.replaceNamespacedDeployment(deploymentName, DEFAULT_NAME_SPACE, newDeploy, null, null, null, null);
response.getStatus();

pawank0411 avatar Aug 29 '22 09:08 pawank0411

I was able to wait till the response using Wait.poll

pawank0411 avatar Sep 03 '22 05:09 pawank0411

Thanks @brendandburns for your time.

pawank0411 avatar Sep 03 '22 05:09 pawank0411