c icon indicating copy to clipboard operation
c copied to clipboard

CoreV1API_listNamespacedEvent doesn't work as expected

Open joyanta55 opened this issue 2 years ago • 5 comments

Hello, I was trying to use the CoreV1API_listNamespacedEvent in order to list all the events in the Kubenetes cluster. Now I am not sure if this happens to anyone, but in my case I can retrieve all the event, but the data attributes shows NULL, which is not expected. I am putting my code here and the output I got.

Source Code:

#include <config/kube_config.h>
#include <api/CoreV1API.h>
#include <stdio.h>

void list_pod(apiClient_t * apiClient)
{
    core_v1_event_list_t *event_list = NULL;
    event_list = CoreV1API_listNamespacedEvent(apiClient, "default",    /*namespace */
                                           "true",    /* pretty */
                                           0,   /* allowWatchBookmarks */
                                           NULL,    /* continue */
                                           NULL,    /* fieldSelector */
                                           NULL,    /* labelSelector */
                                           0,   /* limit */
                                           NULL,    /* resourceVersion */
                                           NULL,    /* resourceVersionMatch */
                                           0,   /* timeoutSeconds */
                                           0    /* watch */
        );
    printf("The return code of HTTP request=%ld\n", apiClient->response_code);
    if (event_list) {
        printf("Get Kind %s\n", event_list->kind);
        if (event_list->items)
        {       listEntry_t *listEntry;
                list_ForEach(listEntry, event_list->items)
                {
                        printf("Data Object %s\n",event_list->items->firstEntry->data);
                }
                printf("There are items in event\n");
        }
        else printf("There is no item\n");

    } else {
        printf("Cannot get any event.\n");
    }
}

The above code compiles correctly. When I run the program in a working k8s cluster, I got the followings.

Get Kind EventList Data Object (null) Data Object (null) Data Object (null) Data Object (null) Data Object (null)

See, all the data fields are getting null. So definitely we can't access name or other objects of the corresponding event.

Could anybody help me with this issue?

joyanta55 avatar Sep 15 '22 15:09 joyanta55

Thank you for reporting this issue ! This is a bug and we will fix it. If you want a quick fix with your own build:

diff --git a/kubernetes/model/core_v1_event.c b/kubernetes/model/core_v1_event.c
index 9912092..571fa25 100644
--- a/kubernetes/model/core_v1_event.c
+++ b/kubernetes/model/core_v1_event.c
@@ -344,7 +344,7 @@ core_v1_event_t *core_v1_event_parseFromJSON(cJSON *core_v1_eventJSON){
     // core_v1_event->event_time
     cJSON *event_time = cJSON_GetObjectItemCaseSensitive(core_v1_eventJSON, "eventTime");
     if (event_time) {
-    if(!cJSON_IsString(event_time))
+    if(!cJSON_IsString(event_time) && !cJSON_IsNull(event_time))
     {
     goto end; //DateTime
     }
@@ -463,7 +463,7 @@ core_v1_event_t *core_v1_event_parseFromJSON(cJSON *core_v1_eventJSON){
         action ? strdup(action->valuestring) : NULL,
         api_version ? strdup(api_version->valuestring) : NULL,
         count ? count->valuedouble : 0,
-        event_time ? strdup(event_time->valuestring) : NULL,
+        event_time && !cJSON_IsNull(event_time) ? strdup(event_time->valuestring) : NULL,
         first_timestamp ? strdup(first_timestamp->valuestring) : NULL,
         involved_object_local_nonprim,
         kind ? strdup(kind->valuestring) : NULL,

ityuhui avatar Sep 16 '22 06:09 ityuhui

And here is my test code:

if (event_list->items) { 
    listEntry_t *listEntry = NULL;
    core_v1_event_t *event = NULL;
    list_ForEach(listEntry, event_list->items) {
        event = listEntry->data;
        if (event) {
            if (event->type) {
                printf("Event Type: %s\n",event->type);
            }
        }
    }
}

ityuhui avatar Sep 16 '22 06:09 ityuhui

Thanks you so much @ityuhui

Just asking, is the timestamp parsing is the main issue here? Because it was working fine for the pod api.

joyanta55 avatar Sep 16 '22 13:09 joyanta55

The root cause of this issue is:

event_time (which is a field of core_v1_event_t) is a "date-time" (an OpenAPI data type), which cannot be parsed correctly by the function core_v1_event_parseFromJSON now. We will fix the parsing.

I think the type "date-time" is not used by pod API or the value of "date-time" is not "null".

ityuhui avatar Sep 16 '22 14:09 ityuhui

I see, thanks @ityuhui

joyanta55 avatar Sep 16 '22 15:09 joyanta55