kubernetes-client / c

Official C client library for Kubernetes
Apache License 2.0
144 stars 46 forks source link

list_pod_incluster example crashes when permission is denied #89

Closed lordgamez closed 2 years ago

lordgamez commented 2 years ago

Hi Team,

I was trying to run the list_pod_incluster in a kind kubernetes cluster from inside a pod, but it crashed with segmentation fault with the following backtrace:

#0  0x00007ffff7daf4a1 in v1_list_meta_free () from /usr/local/lib/libkubernetes.so
#1  0x00007ffff7dc6ff6 in v1_pod_list_parseFromJSON () from /usr/local/lib/libkubernetes.so
#2  0x00007ffff7eadaae in CoreV1API_listNamespacedPod () from /usr/local/lib/libkubernetes.so
#3  0x0000555555555400 in list_pod (apiClient=0x555555575fa0) at main.c:12
#4  0x0000555555555584 in main (argc=1, argv=0x7fffffffe4d8) at main.c:55

When removing the v1_list_meta_free call from the v1_pod_list_parseFromJSON function I get the output:

The return code of HTTP request=403
Cannot get any pod.

I do not have permission currently for running cluster level commands with the default service account I was using, so this looks okay, but the crash should not happen.

brendandburns commented 2 years ago

cc @ityuhui

I think this is a bug in the generated code. The function is free-ing the list metadata here:

https://github.com/kubernetes-client/c/blob/8eb6593e55d0e5d57a2dd3153c15c9645de677bc/kubernetes/model/v1_pod_list.c#L179

but any-place that goto end is called, metadata_local_nonprim hasn't been initialized yet.

We should either initialize that to NULL at the top of the generated function, or remove that free code.

I think the right thing to do is to remove the free code, since it doesn't appear to be being used.

ityuhui commented 2 years ago

OK. I'll look at this code soon.

lordgamez commented 2 years ago

Some additional information on this is that I had the same problem with CoreV1API_listNamespace call, so the problem is not specific to listing pods.

ityuhui commented 2 years ago

Thank you for reporting this issue @lordgamez

brendandburns commented 2 years ago

I believe that you need to add a section here:

https://github.com/OpenAPITools/openapi-generator/blob/bd8efe2f91761df866489c16e1c17870a8c88c5f/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache#L529

That looks like:

{{#vars}}
    {{^isPrimitiveType}}
    {{#isModel}}
    {{^isEnum}}
    {{^isFreeFormObject}}{{complexType}}{{/isFreeFormObject}}{{#isFreeFormObject}}object{{/isFreeFormObject}}_t *{{name}}_local_nonprim = NULL;
    {{/isEnum}}
    {{/isModel}}
{{/vars}}

And then remove the line here: https://github.com/OpenAPITools/openapi-generator/blob/bd8efe2f91761df866489c16e1c17870a8c88c5f/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache#L617

Hope that helps.

lordgamez commented 2 years ago

Thanks for the help! Although I don't have any experience with OpenAPI, can you point me to some documentation how to generate the API code?

ityuhui commented 2 years ago

@lordgamez

I'll fix this issue according to @brendandburns' suggestion. You don't need generate the API code by yourself unless you want to have a try. The guide can be found at https://github.com/kubernetes-client/c/issues/61#issuecomment-889596858

lordgamez commented 2 years ago

Thanks @ityuhui, sorry I thought the comment was directed at me :) Still I'll check out the guide just for fun. :)

ityuhui commented 2 years ago

The upstream openapi-generator breaks the generating for C client recently. Now I'm checking that issue https://github.com/OpenAPITools/openapi-generator/issues/10725.