Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CoreV1API_listNamespacedEvent doesn't work as expected #144

Closed
joyanta55 opened this issue Sep 15, 2022 · 5 comments · Fixed by #157
Closed

CoreV1API_listNamespacedEvent doesn't work as expected #144

joyanta55 opened this issue Sep 15, 2022 · 5 comments · Fixed by #157

Comments

@joyanta55
Copy link
Contributor

joyanta55 commented Sep 15, 2022

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?

@ityuhui
Copy link
Member

ityuhui commented Sep 16, 2022

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
Copy link
Member

ityuhui commented Sep 16, 2022

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);
            }
        }
    }
}

@joyanta55
Copy link
Contributor Author

joyanta55 commented Sep 16, 2022

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.

@ityuhui
Copy link
Member

ityuhui commented Sep 16, 2022

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".

@joyanta55
Copy link
Contributor Author

I see, thanks @ityuhui

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants