GithubHelp home page GithubHelp logo

kubernetes-client / c Goto Github PK

View Code? Open in Web Editor NEW
137.0 11.0 44.0 7.17 MB

Official C client library for Kubernetes

License: Apache License 2.0

CMake 0.49% C 99.47% Shell 0.01% Dockerfile 0.01% Python 0.03%
client-library kubernetes

c's People

Contributors

ahmedalnuaimi avatar brendanburns avatar brendandburns avatar caesar0301 avatar danyt avatar dependabot[bot] avatar hirishh avatar homer6 avatar irozzo-1a avatar ityuhui avatar k8s-ci-robot avatar katepangliu avatar mrbobbytables avatar rexlim820220 avatar swhoro avatar ydcpp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

c's Issues

list_pod_incluster example crashes when permission is denied

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.

[WIP][Feature]Configuraion support for kubernetes-client/c

Hi @brendandburns

In order to meet the requirement of client bronze requirements , I begin to implement the configuration utility now.

My design is adding a directory "config" under the directory kubernetes

c/kubernetes/config 

and some source files

c/kubernetes/config/kube_config.h
c/kubernetes/config/kube_config.c

to hold the utility functions.

What methods should we support at least ?

  • Basic Auth (username/password)
  • X509 Client certificate
  • Bearer tokens
  • encryption/TLS

What's the appropriate name for this c client library

Hi @brendandburns

Now the development for c client library can start, first of all, could you please give a name for the c client library ?

I nominate some candidates:

  • libkubernetes.so/libkubernetes.dll
  • libk8s.so/libk8s.dll
  • libkubernetes-client.so/libkubernetes-client.dll
  • libk8sclient.so/libk8sclient.dll

How can I curl exec Pod?

I m trying to this command

=================================================================
curl -k -H "Authorization: Bearer $TOKEN" -H 'Accept: application/json' https://10.96.0.1:80/api/v1/namespaces/default/pods/My-Pod/exec

=================================================================

But I receive below Json Message
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {

},
"status": "Failure",
"message": "Upgrade request required",
"reason": "BadRequest",
"code": 400
}

I think ... Curl can't use TCP websocket.

How can I use Exec API???

// connect GET requests to portforward of Pod
CoreV1API_connectGetNamespacedPodPortforward(apiClient_t *apiClient, char * name , char * _namespace , int ports );

If I am using API wrong, please give me comment

vcpkg integration

I'm adding vcpkg integration for this library microsoft/vcpkg#20255
I tested it on Windows and it worked without any problem.
@ityuhui I remember that in a previous comment you said that the installation script would not work without adding the DESTINATION parameter. I had to patch that in vcpkg for the port to work. Can you please make sure that the installation really doesn't work without that parameter?

How to stop watching?

Hi,
What should I do if I want to stop watching?
If I set to CURLOPT_TIMEOUT_MS, applications will receive the entire object state once again whenever they call watch after timeout.
This is not WATCH but the same as calling GET repeatedly.
If I kill the watch thread with pthread_cancel(), we will have the problem of leaking resources.
We leak curl handle, curl mime, apiClient, cJSON_Parse in wu_convert_to_json_array() and so on.
How can I gracefully exit on curl_easy_perform()?
How can I stop the watch without resource leak?
Please consider about it.
Thanks in advance.

PATCH function needs to be added

When using only the PUT function, all Json properties must be written.

genericClient has PUT/GET/POST function but there is no function to PATCH in the genericClient function.

In C++, there are many keyword conflicts with variable names.

In C++, there are many keyword conflicts with variable names. Iโ€˜m going to commit a PR for this, which will make the following changes:
namespace => name_space
template => template_
operator => operator_
export => export_
Does it follow your code style?

How can I Use 'AutoscalingV1API_patchNamespacedHorizontalPodAutoscalerStatus' ?

Hi. I m trying to make hpa Update api.

I have confirmed that Below CURL message is working fine.

curl -k -g
-X PATCH
-d @-
-H "Authorization: Bearer $TOKEN"
-H 'Accept: application/json'
-H 'Content-Type: application/strategic-merge-patch+json'
https://[${MY_SERVICE_HOST}]:${MY_SERVICE_PORT}/apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers/My-Hpa?<<'EOF'
{ }
EOF

But 'AutoscalingV1API_patchNamespacedHorizontalPodAutoscalerStatus' call apiClient_invoke.
apiClient_Invoke makes Header like 'Content-Type: application/json' + ... etc But this Content-Type return Bad Request Error.

PATCH Request need to diffrent Apiclient_invoke function.

Not only this problem but also object_t parameter is not used anywhere.

in AutoscalingV1API_patchNamespacedHorizontalPodAutoscalerStatus "object_t* object parameter" is not used anywhere.

so Can't input changed part.

If the above words are wrong, please let me know the correct usage of

'AutoscalingV1API_patchNamespacedHorizontalPodAutoscalerStatus'

I don't know what is object_t?... and then how can I makes curl message (Like above curl Message) by using apiClient_invoke...

thank you.

Memory Leak in CoreV1API

Hi

I found some defect that Memory Leak in CoreV1API.c

In the "CoreV1API_deleteNamespacedPod" function, the localVarToReplace variable calls malloc, but if the __namespace variable is NULL, it moves to end and the function ends without calling free.
Other functions are also confirmed to have memory leak.

It needs to be fixed as soon as possible.

what is the version of library?

According to the README.md in kubernetes directory
The version is mentioned that
API version: v1.17.4
Package version:
Build package: org.openapitools.codegen.languages.CLibcurlClientCodegen

Is it the right API Version?
And there is no ABI version for library?

websockets in multi-thread

Could you put *g_lws_context and g_interrupted into the wsclient_t structure?

typedef struct wsclient_t {
    char *server_address;
    int server_port;
    char *path;
    char *data_to_send;
    long data_to_send_len;
    void *data_received;
    long data_received_len;
    data_callback_func data_callback_func;
    int log_mask;
    lws_sorted_usec_list_t sul; /* schedule connection retry */
    struct lws *wsi;        /* related wsi if any */
    uint16_t retry_count;   /* count of consequetive retries */
    sslConfig_t *ssl_config;
    list_t *api_keys;

    struct lws_context *context;
    bool   interrupted;

} wsclient_t;

Error when parsing int64 or double array

Reported by @clearday4

the type of supplemental_groups is as below:

        "supplementalGroups": {
          "description": "A list of groups applied to the first process run in each container, in addition to the container's primary GID.  If unspecified, no groups will be added to any container.",
          "items": {
            "format": "int64",
            "type": "integer"
          },
          "type": "array"
        },

it is an integer array.

We suggest changing it as follows.
In v1_pod_security_context.c

list_addElement(supplemental_groupsList , &supplemental_groups_local->valuedouble);

=>

double *supplemental_groups_local_values = malloc(sizeof(double));
*supplemental_groups_local_values = supplemental_groups_local->valuedouble;
list_addElement(supplemental_groupsList , supplemental_groups_local_values);

free Error()

@ityuhui

Hello,

I'm trying to Use pod_list_free(), But I have Error Message " free() : Invalid size "

so I trace function stack like below

#0  0x00007ffff69b57ff in raise () from /lib64/libc.so.6
#1  0x00007ffff699fc35 in abort () from /lib64/libc.so.6
#2  0x00007ffff69f8987 in __libc_message () from /lib64/libc.so.6
#3  0x00007ffff69ffd8c in malloc_printerr () from /lib64/libc.so.6
#4  0x00007ffff6a0169c in _int_free () from /lib64/libc.so.6
#5  0x00007ffff797bc42 in v1_pod_security_context_free (v1_pod_security_context=0x7da480)
    at kubernetes/model/v1_pod_security_context.c:51
#6  0x00007ffff797c842 in v1_pod_spec_free (v1_pod_spec=0x7dae40)
    at kubernetes/model/v1_pod_spec.c:214
#7  0x00007ffff797a0f3 in v1_pod_free (v1_pod=0x7db3f0)
    at kubernetes/model/v1_pod.c:52
#8  0x00007ffff797b779 in v1_pod_list_free (v1_pod_list=0x80f140)
    at kubernetes/model/v1_pod_list.c:38
#9  0x0000000000400893 in main (argc=1, argv=0x7fffffff55c8) at main.c:39

I don't know what the intent of this free()

free
void v1_pod_security_context_free(v1_pod_security_context_t *v1_pod_security_context)

=> if (v1_pod_security_context->supplemental_groups) {
list_ForEach(listEntry, v1_pod_security_context->supplemental_groups) {
free(listEntry->data);
}

allocate
kubernetes/model/v1_pod_security_context.c

v1_pod_security_context_parseFromJSON(cJSON *v1_pod_security_contextJSON)
=> list_addElement(supplemental_groupsList , &supplemental_groups_local->valuedouble);

So fixed not to call free function. It worked fine.

Is this right?

Running example/list_pod_incluster coredump

client-go

panic: pods is forbidden: User "system:serviceaccount:default:default" cannot list resource "pods" in API group "" at the cluster scope

goroutine 1 [running]:
main.main()
	/mnt/hgfs/d/apm/smartagent/clusteragent-some/client-go/examples/in-cluster-client-configuration/main.go:56 +0x2e5

client-c

backtrace:

Program received signal SIGABRT, Aborted.
0x00007ffff7623387 in raise () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.17-317.el7.x86_64 libgcc-4.8.5-44.el7.x86_64
(gdb) bt
#0  0x00007ffff7623387 in raise () from /lib64/libc.so.6
#1  0x00007ffff7624a78 in abort () from /lib64/libc.so.6
#2  0x00007ffff7665ed7 in __libc_message () from /lib64/libc.so.6
#3  0x00007ffff766e299 in _int_free () from /lib64/libc.so.6
#4  0x00000000004676ad in v1_list_meta_free ()
#5  0x00000000004064ee in v1_pod_list_parseFromJSON ()
#6  0x0000000000433c4b in CoreV1API_listNamespacedPod ()
#7  0x0000000000404652 in list_pod (apiClient=0xa11520) at main.c:13
#8  0x00000000004047b2 in main (argc=1, argv=0x7fffffffe598) at main.c:56

coredump:

(gdb) run
Starting program: /root/test-cli-incluster/list_pod_incluster 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
call CoreV1API_listNamespacedPod. 
*** Error in `/root/test-cli-incluster/list_pod_incluster': free(): invalid pointer: 0x00007fffffffe3f0 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81299)[0x7ffff766e299]
/root/test-cli-incluster/list_pod_incluster[0x4676ad]
/root/test-cli-incluster/list_pod_incluster[0x4064ee]
/root/test-cli-incluster/list_pod_incluster[0x433c4b]
/root/test-cli-incluster/list_pod_incluster[0x404652]
/root/test-cli-incluster/list_pod_incluster[0x4047b2]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7ffff760f555]
/root/test-cli-incluster/list_pod_incluster[0x404462]
======= Memory map: ========
00400000-007d4000 r-xp 00000000 fd:00 19037520                           /root/test-cli-incluster/list_pod_incluster
009d4000-00a06000 r--p 003d4000 fd:00 19037520                           /root/test-cli-incluster/list_pod_incluster
00a06000-00a0c000 rw-p 00406000 fd:00 19037520                           /root/test-cli-incluster/list_pod_incluster
00a0c000-00a76000 rw-p 00000000 00:00 0                                  [heap]
7ffff0000000-7ffff0021000 rw-p 00000000 00:00 0 
7ffff0021000-7ffff4000000 ---p 00000000 00:00 0 
7ffff73d7000-7ffff73ec000 r-xp 00000000 fd:00 85                         /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7ffff73ec000-7ffff75eb000 ---p 00015000 fd:00 85                         /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7ffff75eb000-7ffff75ec000 r--p 00014000 fd:00 85                         /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7ffff75ec000-7ffff75ed000 rw-p 00015000 fd:00 85                         /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7ffff75ed000-7ffff77b1000 r-xp 00000000 fd:00 76232                      /usr/lib64/libc-2.17.so
7ffff77b1000-7ffff79b0000 ---p 001c4000 fd:00 76232                      /usr/lib64/libc-2.17.so
7ffff79b0000-7ffff79b4000 r--p 001c3000 fd:00 76232                      /usr/lib64/libc-2.17.so
7ffff79b4000-7ffff79b6000 rw-p 001c7000 fd:00 76232                      /usr/lib64/libc-2.17.so
7ffff79b6000-7ffff79bb000 rw-p 00000000 00:00 0 
7ffff79bb000-7ffff79bd000 r-xp 00000000 fd:00 76238                      /usr/lib64/libdl-2.17.so
7ffff79bd000-7ffff7bbd000 ---p 00002000 fd:00 76238                      /usr/lib64/libdl-2.17.so
7ffff7bbd000-7ffff7bbe000 r--p 00002000 fd:00 76238                      /usr/lib64/libdl-2.17.so
7ffff7bbe000-7ffff7bbf000 rw-p 00003000 fd:00 76238                      /usr/lib64/libdl-2.17.so
7ffff7bbf000-7ffff7bd6000 r-xp 00000000 fd:00 77890                      /usr/lib64/libpthread-2.17.so
7ffff7bd6000-7ffff7dd5000 ---p 00017000 fd:00 77890                      /usr/lib64/libpthread-2.17.so
7ffff7dd5000-7ffff7dd6000 r--p 00016000 fd:00 77890                      /usr/lib64/libpthread-2.17.so
7ffff7dd6000-7ffff7dd7000 rw-p 00017000 fd:00 77890                      /usr/lib64/libpthread-2.17.so
7ffff7dd7000-7ffff7ddb000 rw-p 00000000 00:00 0 
7ffff7ddb000-7ffff7dfd000 r-xp 00000000 fd:00 76225                      /usr/lib64/ld-2.17.so
7ffff7fee000-7ffff7ff2000 rw-p 00000000 00:00 0 
7ffff7ff7000-7ffff7ffa000 rw-p 00000000 00:00 0 
7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0                          [vdso]
7ffff7ffc000-7ffff7ffd000 r--p 00021000 fd:00 76225                      /usr/lib64/ld-2.17.so
7ffff7ffd000-7ffff7ffe000 rw-p 00022000 fd:00 76225                      /usr/lib64/ld-2.17.so
7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0 
7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0                          [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

Considering multi-thread environment

curl_global_cleanup() and curl_global_init() can crash the CURL handle of other threads when making a API request from multi-thread respectively.
In the multi-thread environment, you need to do curl_easy_cleanup() instead of curl_global_cleanup(), and curl_global_init() also should be done only the first time.
curl_easy_init() will also execute if curl_global_init() was not done, so curl_global_init() seems not to have to be done.
please consider about it.

Fail to parse a map whose elements are not <string, string> pairs from JSON

Failed cases:

Name Date type Data type of value
v1_pod_disruption_budget_status->disrupted_pods map datetime
v1_certificate_signing_request_spec->extra map list<string>
v1_json_schema_props->definitions map model
v1_json_schema_props->pattern_properties map model
v1_subject_access_review_spec->extra map list<string>
v1_user_info->extra map list<string>
v1beta1_pod_disruption_budget_status->disrupted_pods map datetime

NULL check order

I think Below code have some

 // Path Params
    long sizeOfPathParams_name = strlen(name)+3 + strlen(_namespace)+3 + strlen("{ name }");
    if(name == NULL) {
        goto end;
    }``

I think strlen(name) has to do after name NULL Check

 // Path Params
    if(name == NULL) {
        goto end;
    }``
    long sizeOfPathParams_name = strlen(name)+3 + strlen(_namespace)+3 + strlen("{ name }");

Project roadmap

Hello @ityuhui @brendandburns. NetApp is interested in a Kubernetes client library for C. The documentation is clear that the project "is a work in progress and should be considered alpha quality software at this time". As you know, there is only one tagged version for k8s 1.17 and HEAD supports k8s 1.22 with some support for the releases in between: https://github.com/kubernetes-client/c/blob/master/docs/versioning-and-compatibility.md#compatibility

  1. Are there plans to have a tagged version/release for every minor k8s release going forward?
  2. What are some of the major gaps that make this project alpha and what's the timeline to address them?

Thanks!
Ardalan

secure_getenv() returns NULL when I do setcap to executable file.

I made executable file in the linux by using kubernetes-client/c code.
There was a problem because secure_getenv( ) returned NULL when I did setcap to the executable file.
I have to use setcap to the executable file because it's a non-root process and it needs some linux capabilities.

How can I solve the problem, if I have to use setcap to the executable file?
I checked the manual for secure_getenv() and I guess my program requires "secure execution" in the manual
Do I have to use secure_getenv() instead of simple getenv() in this situation?

About watch function

Is there any examples of watch pod lists?
please add "watch" function example how to set the resourceVersion and register the callback function.
Please give me an available reference. Thanks in advance.

running example for exec pod on minikube

Hello, I am unable to run exec_pod example after setting up minikube.

This is the error:

[2021/07/27 20:55:51:1527] E: callback_wsclient: wsclient connection error: Unable to connect
[2021/07/27 20:55:54:2247] E: callback_wsclient: wsclient connection error: Unable to connect
[2021/07/27 20:55:57:2970] E: callback_wsclient: wsclient connection error: Unable to connect
[2021/07/27 20:56:03:6007] E: callback_wsclient: wsclient connection error: Unable to connect
[2021/07/27 20:56:10:7051] E: callback_wsclient: wsclient connection error: Unable to connect
[2021/07/27 20:56:19:5365] E: callback_wsclient: wsclient connection error: Unable to connect

Data value is NULL in v1_secret_t

I called Function CoreV1API_listNamespacedSecret
but I can't see secret's data Info

There is Some Bug in "v1_secret_parserFromJSON"

// v1_secret->data
    cJSON *data = cJSON_GetObjectItemCaseSensitive(v1_secretJSON, "data");
    list_t *dataList;
    if (data) {
    cJSON *data_local_map;
    if(!cJSON_IsObject(data)) {
        goto end;//primitive map container
    }
    dataList = list_create();
    keyValuePair_t *localMapKeyPair;
    cJSON_ArrayForEach(data_local_map, data)
    {
        cJSON *localMapObject = data_local_map;
        list_addElement(dataList , localMapKeyPair);
    }
    }

I think it has to be changed like Below

cJSON *data = cJSON_GetObjectItemCaseSensitive(v1_secretJSON, "data");
    list_t *dataList;
    if (data) {
    cJSON *data_local_map;
    if(!cJSON_IsObject(data)) {
        goto end;//primitive map container
    }
    dataList = list_create();
    keyValuePair_t *localMapKeyPair;
    cJSON_ArrayForEach(data_local_map, data)
    {
        cJSON *localMapObject = data_local_map;
        if(!cJSON_IsString(localMapObject))
        {
            goto end;
        }
        localMapKeyPair = keyValuePair_create(strdup(localMapObject->string), strdup(localMapObject->valuestring));
        list_addElement(dataList , localMapKeyPair);
    }
    }

"localMapKeyPair" have No value, just Declared in scope. But Used in list_addElement(dataList, localMapKeyPair);

so data value is NULL in v1_secret_t.

MemoryLeak Occur in v1_object_meta_parseFromJSON(cJSON *v1_object_metaJSON) Function

Hi @ityuhui

I found some Memory Leak in v1_object_meta_parseFromJSON(cJSON *v1_object_metaJSON) function, By performing valgrind.

Below code Occur Memory Leak.

v1_object_meta_t *v1_object_meta_parseFromJSON(cJSON *v1_object_metaJSON){

    v1_object_meta_t *v1_object_meta_local_var = NULL;

    // v1_object_meta->annotations
    cJSON *annotations = cJSON_GetObjectItemCaseSensitive(v1_object_metaJSON, "annotations");
    list_t *annotationsList;
    if (annotations) {
    cJSON *annotations_local_map;
    if(!cJSON_IsObject(annotations)) {
        goto end;//primitive map container
    }
    annotationsList = list_create();
    keyValuePair_t *localMapKeyPair;
    cJSON_ArrayForEach(annotations_local_map, annotations)
    {
        cJSON *localMapObject = annotations_local_map;
        if(!cJSON_IsString(localMapObject))
        {
            goto end;
        }
        localMapKeyPair = keyValuePair_create(strdup(localMapObject->string),strdup(localMapObject->valuestring));
        list_addElement(annotationsList , localMapKeyPair);
    }
    }

    // v1_object_meta->cluster_name
    cJSON *cluster_name = cJSON_GetObjectItemCaseSensitive(v1_object_metaJSON, "clusterName");
    if (cluster_name) {
    if(!cJSON_IsString(cluster_name))
    {
    goto end; //String
    }
    }

    // v1_object_meta->deletion_timestamp
    cJSON *deletion_timestamp = cJSON_GetObjectItemCaseSensitive(v1_object_metaJSON, "deletionTimestamp");
    if (deletion_timestamp) {
    if(!cJSON_IsString(deletion_timestamp))
    {
    goto end; //DateTime
    }
    }

    // v1_object_meta->finalizers
    cJSON *finalizers = cJSON_GetObjectItemCaseSensitive(v1_object_metaJSON, "finalizers");
    list_t *finalizersList;
    if (finalizers) {
    cJSON *finalizers_local;
    if(!cJSON_IsArray(finalizers)) {
        goto end;//primitive container
    }
    finalizersList = list_create();

    cJSON_ArrayForEach(finalizers_local, finalizers)
    {
        if(!cJSON_IsString(finalizers_local))
        {
            goto end;
        }
        list_addElement(finalizersList , strdup(finalizers_local->valuestring));
    }
    }
...


    v1_object_meta_local_var = v1_object_meta_create (
        annotations ? annotationsList : NULL,
        cluster_name ? strdup(cluster_name->valuestring) : NULL,
        creation_timestamp ? strdup(creation_timestamp->valuestring) : NULL,
        deletion_grace_period_seconds ? deletion_grace_period_seconds->valuedouble : 0,
        deletion_timestamp ? strdup(deletion_timestamp->valuestring) : NULL,
        finalizers ? finalizersList : NULL,
        generate_name ? strdup(generate_name->valuestring) : NULL,
        generation ? generation->valuedouble : 0,
        labels ? labelsList : NULL,
        managed_fields ? managed_fieldsList : NULL,
        name ? strdup(name->valuestring) : NULL,
        _namespace ? strdup(_namespace->valuestring) : NULL,
        owner_references ? owner_referencesList : NULL,
        resource_version ? strdup(resource_version->valuestring) : NULL,
        self_link ? strdup(self_link->valuestring) : NULL,
        uid ? strdup(uid->valuestring) : NULL
        );

    return v1_object_meta_local_var;
end:
    return NULL;

}

if finalizersList is NULL, goto end and return NULL. but Before annotationsList allocated list By calling list_create() and list_addElement() with keyValuePair_t .

I think It is need to fix. like below.

end:
#ifdef CONFIG_CP_BUGFIX
    if(annotationsList)
    {
       listEntry_t *listEntry = NULL;
       list_ForEach(listEntry, annotationsList)
       {
           keyValuePair_t *localKeyValue = (keyValuePair_t*)listEntry->data;
           free(localKeyValue->key);
           free(localKeyValue->value);
           keyValuePair_free(localKeyValue);
       }
       list_free(annotationsList);
       annotationsList = NULL;
    }

    if(finalizersList)
    {
        listEntry_t *listEntry = NULL;
        list_ForEach(listEntry, finalizersList)
        {
            free(listEntry->data);
        }
        list_free(finalizersList);
        finalizersList = NULL;
    }

    if(labelsList)
    {
        listEntry_t *listEntry = NULL;

        list_ForEach(listEntry, labelsList)
        {
            keyValuePair_t *localKeyValue = (keyValuePair_t *)listEntry->data;
            free(localKeyValue->key);
            free(localKeyValue->value);
            keyValuePair_free(localKeyValue);
        }
        list_free(labelsList);
        labelsList = NULL;
    }

    if(managed_fieldsList)
    {
        listEntry_t *listEntry = NULL;
        list_ForEach(listEntry, managed_fieldsList)
        {
            v1_managed_fields_entry_t *local_managed_fields_entry = (v1_managed_fields_entry_t *)listEntry->data;
            v1_managed_fields_entry_free(local_managed_fields_entry);
            local_managed_fields_entry = NULL;
        }
        list_free(managed_fieldsList);
        managed_fieldsList = NULL;
    }
#endif
    return NULL;
}

and all lit_t* local variable need to declared at the beginning of the function. Because end point check the Null variable.

[BUG] libkubernetes frees the memory that is passed in

v1_volume_mount_create uses the address of "mount_path", "mount_propagation","name","sub_path","sub_path_expr" directly,

v1_volume_mount_t *v1_volume_mount_create(
    char *mount_path,
    char *mount_propagation,
    char *name,
    int read_only,
    char *sub_path,
    char *sub_path_expr
    ) {
    v1_volume_mount_t *v1_volume_mount_local_var = malloc(sizeof(v1_volume_mount_t));
    if (!v1_volume_mount_local_var) {
        return NULL;
    }
    v1_volume_mount_local_var->mount_path = mount_path;
    v1_volume_mount_local_var->mount_propagation = mount_propagation;
    v1_volume_mount_local_var->name = name;
    v1_volume_mount_local_var->read_only = read_only;
    v1_volume_mount_local_var->sub_path = sub_path;
    v1_volume_mount_local_var->sub_path_expr = sub_path_expr;

    return v1_volume_mount_local_var;
}

but v1_volume_mount_free will free the memory when cleaning up:

void v1_volume_mount_free(v1_volume_mount_t *v1_volume_mount) {
    if(NULL == v1_volume_mount){
        return ;
    }
    listEntry_t *listEntry;
    free(v1_volume_mount->mount_path);
    free(v1_volume_mount->mount_propagation);
    free(v1_volume_mount->name);
    free(v1_volume_mount->sub_path);
    free(v1_volume_mount->sub_path_expr);
    free(v1_volume_mount);
}

So it will introduce a coredump when

    v1_volume_mount_t *volmou = v1_volume_mount_create("/test", NULL, "test", 0, NULL, NULL);
    v1_volume_mount_free(volmou);

How can I solve this Error...?

Hi, @ityuhui
As you said I tried to update hpa using generic client.

But generic Make Path function does not include Header par.

How about add Header Parameters in generic Make Path function?

double free occurs while calling api in the multi-thread

There is a problem that double free crashes while calling CoreV1API_listNamespacedPod() continuously in the Multi-thread.
The direct cause is that the duplicated detection of v1_pod_free()'s address, it seems that cJSON is basically thread unsafe.

DaveGamble/cJSON#221
DaveGamble/cJSON#314

Looking at the above link, If you don't specify return_parse_end in cJSON_ParseWithOpts or use cJSON_GetErrorPtr (), it cause memory violation in the multi-thread.

And we need an additional review of why v1_pod_free() accesses duplicate addresses per thread.
We got the following logs using address sanitizer.

=================================================================
==1659378== ERROR: AddressSanitizer: attempting double-free on 0x600800040390:
#0 0x7ffff4e60dd9 (/usr/lib/libasan.so+0x15dd9)
#1 0x7ffff18777e8 (/pkg/lib/libkubernetes.so.xcg+0xc47e8)
0x600800040390 is located 0 bytes inside of 40-byte region [0x600800040390,0x6008000403b8)
freed by thread T146 here:
#0 0x7ffff4e60dd9 (/usr/lib/libasan.so+0x15dd9)
#1 0x7ffff18715de (/pkg/lib/libkubernetes.so.xcg+0xbe5de)
previously allocated by thread T147 here:
#0 0x7ffff4e60ef9 (/usr/lib/libasan.so+0x15ef9)
#1 0x7ffff1876100 (/pkg/lib/libkubernetes.so.xcg+0xc3100)
Thread T147 created by T0 here:
#0 0x7ffff4e55a0a (/usr/lib/libasan.so+0xaa0a)
#1 0x45db8d (/pkg/bin/nrdhcpad.xcg+0x45db8d)
#2 0x434238 (/pkg/bin/nrdhcpad.xcg+0x434238)
#3 0x43ce65 (/pkg/bin/nrdhcpad.xcg+0x43ce65)
#4 0x4742e6 (/pkg/bin/nrdhcpad.xcg+0x4742e6)
#5 0x474cf2 (/pkg/bin/nrdhcpad.xcg+0x474cf2)
#6 0x431352 (/pkg/bin/nrdhcpad.xcg+0x431352)
#7 0x4080aa (/pkg/bin/nrdhcpad.xcg+0x4080aa)
#8 0x505ce9 (/pkg/bin/nrdhcpad.xcg+0x505ce9)
#9 0x40f8e5 (/pkg/bin/nrdhcpad.xcg+0x40f8e5)
#10 0x405015 (/pkg/bin/nrdhcpad.xcg+0x405015)
#11 0x7fffef6e0554 (/usr/lib64/libc-2.17.so+0x22554)
Thread T146 created by T0 here:
#0 0x7ffff4e55a0a (/usr/lib/libasan.so+0xaa0a)
#1 0x45db8d (/pkg/bin/nrdhcpad.xcg+0x45db8d)
#2 0x434238 (/pkg/bin/nrdhcpad.xcg+0x434238)
#3 0x43ce65 (/pkg/bin/nrdhcpad.xcg+0x43ce65)
#4 0x4742e6 (/pkg/bin/nrdhcpad.xcg+0x4742e6)
#5 0x474cf2 (/pkg/bin/nrdhcpad.xcg+0x474cf2)
#6 0x431352 (/pkg/bin/nrdhcpad.xcg+0x431352)
#7 0x4080aa (/pkg/bin/nrdhcpad.xcg+0x4080aa)
#8 0x505ce9 (/pkg/bin/nrdhcpad.xcg+0x505ce9)
#9 0x40f8e5 (/pkg/bin/nrdhcpad.xcg+0x40f8e5)
#10 0x405015 (/pkg/bin/nrdhcpad.xcg+0x405015)
#11 0x7fffef6e0554 (/usr/lib64/libc-2.17.so+0x22554)
==1659378== ABORTING

==2107129== ERROR: AddressSanitizer: attempting double-free on 0x6004003171b0:
[0319|10:26:06.251|NRDHCPAD|DHCPAD|0180|INFO|8900][ERR]dal_db_connect fail(-26, Unknown error)
[2021-03-19 10:26:06.251][dhcpad_dal.c:180][ERR]dal_db_connect fail(-26, Unknown error)
#0 0x7ffff4e60dd9 (/usr/lib64/libasan.so.0+0x15dd9)
#1 0x7ffff189a26c (/usr/lib64/libkubernetes.so+0xbe26c)
0x6004003171b0 is located 0 bytes inside of 4-byte region [0x6004003171b0,0x6004003171b4)
freed by thread T5 here:
#0 0x7ffff4e60dd9 (/usr/lib64/libasan.so.0+0x15dd9)
#1 0x7ffff189a26c (/usr/lib64/libkubernetes.so+0xbe26c)
previously allocated by thread T5 here:
#0 0x7ffff4e60ef9 (/usr/lib64/libasan.so.0+0x15ef9)
#1 0x7fffef773b89 (/usr/lib64/libc-2.17.so+0x8cb89)
Thread T6 created by T0 here:
#0 0x7ffff4e55a0a (/usr/lib64/libasan.so.0+0xaa0a)
#1 0x45db8d (/pkg/bin/nrdhcpad.xcg+0x45db8d)
#2 0x434238 (/pkg/bin/nrdhcpad.xcg+0x434238)
#3 0x43ce65 (/pkg/bin/nrdhcpad.xcg+0x43ce65)
#4 0x4742e6 (/pkg/bin/nrdhcpad.xcg+0x4742e6)
#5 0x474cf2 (/pkg/bin/nrdhcpad.xcg+0x474cf2)
#6 0x431352 G(/pkg/bin/nrdhcpad.xcg+0x431352)
#7 0x4080aa (/pkg/bin/nrdhcpad.xcg+0x4080aa)
#8 0x505ce9 (/pkg/bin/nrdhcpad.xcg+0x505ce9)
#9 0x40f8e5 (/pkg/bin/nrdhcpad.xcg+0x40f8e5)
#10 0x405015 (/pkg/bin/nrdhcpad.xcg+0x405015)
#11 0x7fffef709554 (/usr/lib64/libc-2.17.so+0x22554)
Thread T5 created by T0 here:
#0 0x7ffff4e55a0a (/usr/lib64/libasan.so.0+0xaa0a)
#1 0x45db8d (/pkg/bin/nrdhcpad.xcg+0x45db8d)
#2 0x434238 (/pkg/bin/nrdhcpad.xcg+0x434238)
#3 0x43ce65 (/pkg/bin/nrdhcpad.xcg+0x43ce65)
#4 0x4742e6 (/pkg/bin/nrdhcpad.xcg+0x4742e6)
#5 0x474cf2 (/pkg/bin/nrdhcpad.xcg+0x474cf2)
#6 0x431352 (/pkg/bin/nrdhcpad.xcg+0x431352)
#7 0x4080aa (/pkg/bin/nrdhcpad.xcg+0x4080aa)
#8 0x505ce9 (/pkg/bin/nrdhcpad.xcg+0x505ce9)
#9 0x40f8e5 (/pkg/bin/nrdhcpad.xcg+0x40f8e5)
#10 0x405015 (/pkg/bin/nrdhcpad.xcg+0x405015)
#11 0x7fffef709554 (/usr/lib64/libc-2.17.so+0x22554)
==2107129== ABORTING

We would like to ask you to analyze this issue.
Thanks in advance.

[BUG][Valgrind memcheck] Memory leak in examples/create_pod

Command:

valgrind --tool=memcheck --leak-check=full ./create_pod_bin

Result:

==9334== 1,216 (40 direct, 1,176 indirect) bytes in 1 blocks are definitely lost in loss record 27 of 27
==9334== at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9334== by 0x108B96: create_a_pod (main.c:12)
==9334== by 0x108F14: main (main.c:86)

The type of target_port of v1_service_port_t is invalid.

The type of target_port in the v1_service_port_t structure is abnormal.

typedef struct v1_service_port_t {
    char *name; // string
    int node_port; //numeric
    int port; //numeric
    char *protocol; // string
    object_t *target_port; //object  <--- it is not an object type

} v1_service_port_t;

I think target_port should be an integer type like port and node_port.
In the v1_service_port.c, target port is parsed from object_parseFromJSON() so it returns null.
object_parseFromJSON() is always return NULL.
when I changed target_port to integer type and got it with cJSON_GetObjectItemCaseSensitive() as sams as port, it works properly.

please consider about it.
Thanks

how to config a created pod to a Node? Can you help me to check code?

list_t *nSelector = list_create();
char *node = strdup("ssd"); // IP address
list_addElement(nSelector, node);
// v1_node_selector_t *nodeselector = calloc(1, sizeof(v1_node_selector_t));
// nodeselector->node_selector_terms = nSelector;

v1_node_selector_t *nodeselector = v1_node_selector_create(nSelector);
list_t *nodeSelectorCreator = list_create();
list_addElement(nodeSelectorCreator, nodeselector);
podinfo->spec->node_selector = nodeSelectorCreator;
podinfo->spec->node_name = strdup("szvphicprd10133");

how to create a configmap with using map(keyvaluepairs)?

    v1_config_map_t* body = calloc(1, sizeof(v1_config_map_t));
    body->api_version = strdup("v1");
    body->metadata = calloc(1, sizeof(v1_object_meta_t));
    body->metadata->name = strdup("xh_configmap");
    keyValuePair_t *kv = calloc(2, sizeof(keyValuePair_t));
    kv[0].key = "worker1";
    kv[0].value = "1";
    kv[1].key = "worker2";
    kv[1].value = "2";
    list_t *binData = list_create();
    list_addElement(binData, kv);
    body->binary_data = binData;
    body->kind = strdup("ConfigMap");
    v1_config_map_t *retConfigMap = CoreV1API_createNamespacedConfigMap(apiClient, "default", body, NULL, NULL, NULL);
    printf("code=%ld\n", apiClient->response_code);
    printf("code=%ld\n", apiClient->response_code);
    v1_config_map_free(retConfigMap);
    v1_config_map_free(body);

the example given above is my case but return with code 422, seems it is illegal to describe configmap.
please help me, thanks a lot! @idvoretskyi @tg123 @webwurst @justaugustus @lavalamp

Memory Leak

Hi

I run valgrind with kubernetes c-client in my machine.

well ...

I found Memory Leak in all Api(v1_object_create() );

for example

 v1_node_local_var = v1_node_create (
        api_version ? strdup(api_version->valuestring) : NULL,
        kind ? strdup(kind->valuestring) : NULL,
        metadata ? metadata_local_nonprim : NULL,
        spec ? spec_local_nonprim : NULL,
        status ? status_local_nonprim : NULL
        );

    return v1_node_local_var;
end:
    return NULL;
}

v1_node_local_var = v1_node_create() but most parameters allocate dynamic memory in function.

v1_node_t *v1_node_create(
    char *api_version,
    char *kind,
    v1_object_meta_t *metadata,
    v1_node_spec_t *spec,
    v1_node_status_t *status
    ) {
    v1_node_t *v1_node_local_var = malloc(sizeof(v1_node_t));
    if (!v1_node_local_var) {
        return NULL;
    }
    v1_node_local_var->api_version = api_version;
    v1_node_local_var->kind = kind;
    v1_node_local_var->metadata = metadata;
    v1_node_local_var->spec = spec;
    v1_node_local_var->status = status;

    return v1_node_local_var;
}

if(v1_node_local_var == NULL) , Failed to malloc in v1_node_create

NULL is returned without parameter Free.

Most api are operating in this way.

I think this could be a fatal problem.

fatal error: yaml.h: No such file or directory

I try to build this library using this dockerfile

FROM dlang2/ldc-ubuntu:1.23.0 as ldc

RUN apt-get update \
    && apt-get install -y clang-9 libclang-9-dev git cmake uncrustify g++ libcurl4-openssl-dev
RUN ln -s /usr/bin/clang-9 /usr/bin/clang

RUN cd /tmp \
    && git clone https://github.com/kubernetes-client/c.git \
    && cd c/kubernetes \
    && mkdir build && cd build \
    && cmake .. \
    && make && make install

An error is thrown:

Step 4/4 : RUN cd /tmp && git clone https://github.com/kubernetes-client/c.git && cd c/kubernetes && mkdir build && cd build && cmake .. && make && make install
---> Running in 05849b54c95c
Cloning into 'c'...
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libcrypto.so (found version "1.1.1")
-- OPENSSL found
-- Using OpenSSL 1.1.1
-- Found CURL: /usr/lib/x86_64-linux-gnu/libcurl.so (found suitable version "7.58.0", minimum required is "7.58.0")
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/c/kubernetes/build
Scanning dependencies of target kubernetes
[ 0%] Building C object CMakeFiles/kubernetes.dir/config/kube_config_model.c.o
[ 0%] Building C object CMakeFiles/kubernetes.dir/config/kube_config_yaml.c.o
/tmp/c/kubernetes/config/kube_config_yaml.c:1:10: fatal error: yaml.h: No such file or directory
#include <yaml.h>
^~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/kubernetes.dir/config/kube_config_yaml.c.o] Error 1
CMakeFiles/kubernetes.dir/build.make:86: recipe for target 'CMakeFiles/kubernetes.dir/config/kube_config_yaml.c.o' failed
make[1]: *** [CMakeFiles/kubernetes.dir/all] Error 2
make: *** [all] Error 2
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/kubernetes.dir/all' failed
Makefile:129: recipe for target 'all' failed

Need to modify parameters

I think functions should receive "const char*" rather than "char*".

for example name or _namespace are unlikely to be modified.

Different return types of Delete Pod

When calling CoreV1API_deleteNamespacedPod() if there is an error, returns a pointer of v1_status_t type. but if it succeeds, it returns a pointer of v1_pod_t type.
The return type depends on the success / fail of the function call.
How can we handle this?

https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#delete-pod-v1-core
According to the above link, v1_pod_t is returned when delete pod is success.
and v1_status_t is returned when delete collection of pod is success.
There is no mention of what structure type will be returned when delete command fails.

First, you need to change the return type of CoreV1API_deleteNamespacedPod() to v1_pod_t pointer type.

[BUG][Valgrind memcheck] Memory leak in example/list_pod

Command:

valgrind --tool=memcheck --leak-check=full ./list_pod_bin

Result:

==8104== 5,965 (32 direct, 5,933 indirect) bytes in 1 blocks are definitely lost in loss record 83 of 83
==8104== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8104== by 0x4EF3FB0: v1_pod_list_create (v1_pod_list.c:14)
==8104== by 0x4EF43FF: v1_pod_list_parseFromJSON (v1_pod_list.c:158)
==8104== by 0x502C8FB: CoreV1API_listNamespacedPod (CoreV1API.c:19442)
==8104== by 0x1089D4: list_pod (main.c:11)
==8104== by 0x108B3D: main (main.c:54)

some keywords conflict occurs with zebos

If I link the libkubernetes and zebos together, some keywords conflict occurred.
list_create, list_free
could you change these keywords?

18:04:22,338 INFO  - //MW_LIB/KAS/obj/x86_K8.4/libkubernetes.a(list.c.o): In function `list_create':
18:04:22,338 INFO  - //MW_LIB/KAS/K8SCLIENT_C_3RD/kubernetes/src/list.c:26: multiple definition of `list_create'
18:04:22,338 INFO  - ../../platform/arch-x86_K/obj/lib.a(linklist.o)://NET_APP/ZEBOS/ZEBOS3RD/lib/linklist.c:21: first defined here
18:04:22,338 INFO  - //MW_LIB/KAS/obj/x86_K8.4/libkubernetes.a(list.c.o): In function `list_free':
18:04:22,338 INFO  - //MW_LIB/KAS/K8SCLIENT_C_3RD/kubernetes/src/list.c:92: multiple definition of `list_free'
18:04:22,338 INFO  - ../../platform/arch-x86_K/obj/lib.a(linklist.o)://NET_APP/ZEBOS/ZEBOS3RD/lib/linklist.c:48: first defined here
18:04:22,338 INFO  - collect2: error: ld returned 1 exit status

About metric API

Do you have a plan to support "apis/metrics.k8s.io/v1beta1" APIs for gathering resources?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.