Skip to content

Commit 39a4dab

Browse files
authored
Merge pull request #157 from ityuhui/yh-datetime-null-1107
Check cJSON_IsNull when the data type is datetime
2 parents 92aaa7f + 18c9414 commit 39a4dab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+202
-104
lines changed

examples/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ all:
1111
cd exec_pod; make
1212
cd list_secret; make
1313
cd configmap; make
14+
cd list_event; make
1415

1516
clean:
1617
cd create_pod; make clean
@@ -25,6 +26,7 @@ clean:
2526
cd exec_pod; make clean
2627
cd list_secret; make clean
2728
cd configmap; make clean
29+
cd list_event; make clean
2830

2931
test:
3032
cd create_pod; make test;
@@ -34,6 +36,7 @@ test:
3436
kubectl wait --for=delete pod/test-pod-6 -n default --timeout=120s
3537
cd list_secret; make test
3638
cd configmap; make test
39+
cd list_event; make test
3740
cd generic; make test
3841
cd multi_thread; make test;
3942
kubectl wait --for=condition=ready pod/test-pod-8 -n default --timeout=60s
@@ -48,6 +51,7 @@ memcheck:
4851
kubectl wait --for=delete pod/test-pod-6 -n default --timeout=120s
4952
cd list_secret; make memcheck
5053
cd configmap; make memcheck
54+
cd list_event; make test
5155
cd generic; make memcheck
5256
cd multi_thread; make memcheck;
5357
kubectl wait --for=condition=ready pod/test-pod-8 -n default --timeout=60s

examples/list_event/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
list_event_bin

examples/list_event/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
INCLUDE:=-I../../kubernetes/
2+
LIBS:=-L../../kubernetes/build -lyaml -lwebsockets -lkubernetes -L/usr/local/lib
3+
CFLAGS:=-g
4+
BIN:=list_event_bin
5+
6+
.PHONY : all clean test memcheck
7+
all:
8+
gcc main.c $(CFLAGS) $(INCLUDE) $(LIBS) -o $(BIN)
9+
10+
test:
11+
./$(BIN)
12+
13+
memcheck:
14+
valgrind --tool=memcheck --leak-check=full ./$(BIN)
15+
16+
clean:
17+
rm ./$(BIN)

examples/list_event/main.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <config/kube_config.h>
2+
#include <api/CoreV1API.h>
3+
#include <stdio.h>
4+
5+
#include <config/kube_config.h>
6+
#include <api/CoreV1API.h>
7+
#include <stdio.h>
8+
9+
void list_event(apiClient_t * apiClient)
10+
{
11+
core_v1_event_list_t *event_list = CoreV1API_listNamespacedEvent(apiClient, "default", /*namespace */
12+
"true", /* pretty */
13+
0, /* allowWatchBookmarks */
14+
NULL, /* continue */
15+
NULL, /* fieldSelector */
16+
NULL, /* labelSelector */
17+
0, /* limit */
18+
NULL, /* resourceVersion */
19+
NULL, /* resourceVersionMatch */
20+
0, /* timeoutSeconds */
21+
0 /* watch */
22+
);
23+
printf("The return code of HTTP request=%ld\n", apiClient->response_code);
24+
if (event_list) {
25+
if (event_list->items) {
26+
listEntry_t *listEntry = NULL;
27+
core_v1_event_t *event = NULL;
28+
list_ForEach(listEntry, event_list->items) {
29+
event = listEntry->data;
30+
if (event) {
31+
if (event->type) {
32+
printf("Event Type: %s\n", event->type);
33+
}
34+
if (event->message) {
35+
printf("Event Message: %s\n", event->message);
36+
}
37+
}
38+
}
39+
} else {
40+
fprintf(stderr, "There are no events in event list.\n");
41+
}
42+
core_v1_event_list_free(event_list);
43+
event_list = NULL;
44+
} else {
45+
fprintf(stderr, "Cannot get event list.\n");
46+
}
47+
}
48+
49+
int main()
50+
{
51+
char *basePath = NULL;
52+
sslConfig_t *sslConfig = NULL;
53+
list_t *apiKeys = NULL;
54+
int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */
55+
if (rc != 0) {
56+
fprintf(stderr, "Cannot load kubernetes configuration.\n");
57+
return -1;
58+
}
59+
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
60+
if (!apiClient) {
61+
fprintf(stderr, "Cannot create a kubernetes client.\n");
62+
return -1;
63+
}
64+
65+
list_event(apiClient);
66+
67+
apiClient_free(apiClient);
68+
apiClient = NULL;
69+
free_client_config(basePath, sslConfig, apiKeys);
70+
basePath = NULL;
71+
sslConfig = NULL;
72+
apiKeys = NULL;
73+
apiClient_unsetupGlobalEnv();
74+
75+
return 0;
76+
}

kubernetes/model/core_v1_event.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ core_v1_event_t *core_v1_event_parseFromJSON(cJSON *core_v1_eventJSON){
344344
// core_v1_event->event_time
345345
cJSON *event_time = cJSON_GetObjectItemCaseSensitive(core_v1_eventJSON, "eventTime");
346346
if (event_time) {
347-
if(!cJSON_IsString(event_time))
347+
if(!cJSON_IsString(event_time) && !cJSON_IsNull(event_time))
348348
{
349349
goto end; //DateTime
350350
}
@@ -353,7 +353,7 @@ core_v1_event_t *core_v1_event_parseFromJSON(cJSON *core_v1_eventJSON){
353353
// core_v1_event->first_timestamp
354354
cJSON *first_timestamp = cJSON_GetObjectItemCaseSensitive(core_v1_eventJSON, "firstTimestamp");
355355
if (first_timestamp) {
356-
if(!cJSON_IsString(first_timestamp))
356+
if(!cJSON_IsString(first_timestamp) && !cJSON_IsNull(first_timestamp))
357357
{
358358
goto end; //DateTime
359359
}
@@ -380,7 +380,7 @@ core_v1_event_t *core_v1_event_parseFromJSON(cJSON *core_v1_eventJSON){
380380
// core_v1_event->last_timestamp
381381
cJSON *last_timestamp = cJSON_GetObjectItemCaseSensitive(core_v1_eventJSON, "lastTimestamp");
382382
if (last_timestamp) {
383-
if(!cJSON_IsString(last_timestamp))
383+
if(!cJSON_IsString(last_timestamp) && !cJSON_IsNull(last_timestamp))
384384
{
385385
goto end; //DateTime
386386
}
@@ -463,11 +463,11 @@ core_v1_event_t *core_v1_event_parseFromJSON(cJSON *core_v1_eventJSON){
463463
action ? strdup(action->valuestring) : NULL,
464464
api_version ? strdup(api_version->valuestring) : NULL,
465465
count ? count->valuedouble : 0,
466-
event_time ? strdup(event_time->valuestring) : NULL,
467-
first_timestamp ? strdup(first_timestamp->valuestring) : NULL,
466+
event_time && !cJSON_IsNull(event_time) ? strdup(event_time->valuestring) : NULL,
467+
first_timestamp && !cJSON_IsNull(first_timestamp) ? strdup(first_timestamp->valuestring) : NULL,
468468
involved_object_local_nonprim,
469469
kind ? strdup(kind->valuestring) : NULL,
470-
last_timestamp ? strdup(last_timestamp->valuestring) : NULL,
470+
last_timestamp && !cJSON_IsNull(last_timestamp) ? strdup(last_timestamp->valuestring) : NULL,
471471
message ? strdup(message->valuestring) : NULL,
472472
metadata_local_nonprim,
473473
reason ? strdup(reason->valuestring) : NULL,

kubernetes/model/core_v1_event_series.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ core_v1_event_series_t *core_v1_event_series_parseFromJSON(cJSON *core_v1_event_
7474
// core_v1_event_series->last_observed_time
7575
cJSON *last_observed_time = cJSON_GetObjectItemCaseSensitive(core_v1_event_seriesJSON, "lastObservedTime");
7676
if (last_observed_time) {
77-
if(!cJSON_IsString(last_observed_time))
77+
if(!cJSON_IsString(last_observed_time) && !cJSON_IsNull(last_observed_time))
7878
{
7979
goto end; //DateTime
8080
}
@@ -83,7 +83,7 @@ core_v1_event_series_t *core_v1_event_series_parseFromJSON(cJSON *core_v1_event_
8383

8484
core_v1_event_series_local_var = core_v1_event_series_create (
8585
count ? count->valuedouble : 0,
86-
last_observed_time ? strdup(last_observed_time->valuestring) : NULL
86+
last_observed_time && !cJSON_IsNull(last_observed_time) ? strdup(last_observed_time->valuestring) : NULL
8787
);
8888

8989
return core_v1_event_series_local_var;

kubernetes/model/events_v1_event.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ events_v1_event_t *events_v1_event_parseFromJSON(cJSON *events_v1_eventJSON){
343343
// events_v1_event->deprecated_first_timestamp
344344
cJSON *deprecated_first_timestamp = cJSON_GetObjectItemCaseSensitive(events_v1_eventJSON, "deprecatedFirstTimestamp");
345345
if (deprecated_first_timestamp) {
346-
if(!cJSON_IsString(deprecated_first_timestamp))
346+
if(!cJSON_IsString(deprecated_first_timestamp) && !cJSON_IsNull(deprecated_first_timestamp))
347347
{
348348
goto end; //DateTime
349349
}
@@ -352,7 +352,7 @@ events_v1_event_t *events_v1_event_parseFromJSON(cJSON *events_v1_eventJSON){
352352
// events_v1_event->deprecated_last_timestamp
353353
cJSON *deprecated_last_timestamp = cJSON_GetObjectItemCaseSensitive(events_v1_eventJSON, "deprecatedLastTimestamp");
354354
if (deprecated_last_timestamp) {
355-
if(!cJSON_IsString(deprecated_last_timestamp))
355+
if(!cJSON_IsString(deprecated_last_timestamp) && !cJSON_IsNull(deprecated_last_timestamp))
356356
{
357357
goto end; //DateTime
358358
}
@@ -371,7 +371,7 @@ events_v1_event_t *events_v1_event_parseFromJSON(cJSON *events_v1_eventJSON){
371371
}
372372

373373

374-
if(!cJSON_IsString(event_time))
374+
if(!cJSON_IsString(event_time) && !cJSON_IsNull(event_time))
375375
{
376376
goto end; //DateTime
377377
}
@@ -459,8 +459,8 @@ events_v1_event_t *events_v1_event_parseFromJSON(cJSON *events_v1_eventJSON){
459459
action ? strdup(action->valuestring) : NULL,
460460
api_version ? strdup(api_version->valuestring) : NULL,
461461
deprecated_count ? deprecated_count->valuedouble : 0,
462-
deprecated_first_timestamp ? strdup(deprecated_first_timestamp->valuestring) : NULL,
463-
deprecated_last_timestamp ? strdup(deprecated_last_timestamp->valuestring) : NULL,
462+
deprecated_first_timestamp && !cJSON_IsNull(deprecated_first_timestamp) ? strdup(deprecated_first_timestamp->valuestring) : NULL,
463+
deprecated_last_timestamp && !cJSON_IsNull(deprecated_last_timestamp) ? strdup(deprecated_last_timestamp->valuestring) : NULL,
464464
deprecated_source ? deprecated_source_local_nonprim : NULL,
465465
strdup(event_time->valuestring),
466466
kind ? strdup(kind->valuestring) : NULL,

kubernetes/model/events_v1_event_series.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ events_v1_event_series_t *events_v1_event_series_parseFromJSON(cJSON *events_v1_
8383
}
8484

8585

86-
if(!cJSON_IsString(last_observed_time))
86+
if(!cJSON_IsString(last_observed_time) && !cJSON_IsNull(last_observed_time))
8787
{
8888
goto end; //DateTime
8989
}

kubernetes/model/v1_api_service_condition.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ v1_api_service_condition_t *v1_api_service_condition_parseFromJSON(cJSON *v1_api
113113
// v1_api_service_condition->last_transition_time
114114
cJSON *last_transition_time = cJSON_GetObjectItemCaseSensitive(v1_api_service_conditionJSON, "lastTransitionTime");
115115
if (last_transition_time) {
116-
if(!cJSON_IsString(last_transition_time))
116+
if(!cJSON_IsString(last_transition_time) && !cJSON_IsNull(last_transition_time))
117117
{
118118
goto end; //DateTime
119119
}
@@ -163,7 +163,7 @@ v1_api_service_condition_t *v1_api_service_condition_parseFromJSON(cJSON *v1_api
163163

164164

165165
v1_api_service_condition_local_var = v1_api_service_condition_create (
166-
last_transition_time ? strdup(last_transition_time->valuestring) : NULL,
166+
last_transition_time && !cJSON_IsNull(last_transition_time) ? strdup(last_transition_time->valuestring) : NULL,
167167
message ? strdup(message->valuestring) : NULL,
168168
reason ? strdup(reason->valuestring) : NULL,
169169
strdup(status->valuestring),

kubernetes/model/v1_certificate_signing_request_condition.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ v1_certificate_signing_request_condition_t *v1_certificate_signing_request_condi
127127
// v1_certificate_signing_request_condition->last_transition_time
128128
cJSON *last_transition_time = cJSON_GetObjectItemCaseSensitive(v1_certificate_signing_request_conditionJSON, "lastTransitionTime");
129129
if (last_transition_time) {
130-
if(!cJSON_IsString(last_transition_time))
130+
if(!cJSON_IsString(last_transition_time) && !cJSON_IsNull(last_transition_time))
131131
{
132132
goto end; //DateTime
133133
}
@@ -136,7 +136,7 @@ v1_certificate_signing_request_condition_t *v1_certificate_signing_request_condi
136136
// v1_certificate_signing_request_condition->last_update_time
137137
cJSON *last_update_time = cJSON_GetObjectItemCaseSensitive(v1_certificate_signing_request_conditionJSON, "lastUpdateTime");
138138
if (last_update_time) {
139-
if(!cJSON_IsString(last_update_time))
139+
if(!cJSON_IsString(last_update_time) && !cJSON_IsNull(last_update_time))
140140
{
141141
goto end; //DateTime
142142
}
@@ -186,8 +186,8 @@ v1_certificate_signing_request_condition_t *v1_certificate_signing_request_condi
186186

187187

188188
v1_certificate_signing_request_condition_local_var = v1_certificate_signing_request_condition_create (
189-
last_transition_time ? strdup(last_transition_time->valuestring) : NULL,
190-
last_update_time ? strdup(last_update_time->valuestring) : NULL,
189+
last_transition_time && !cJSON_IsNull(last_transition_time) ? strdup(last_transition_time->valuestring) : NULL,
190+
last_update_time && !cJSON_IsNull(last_update_time) ? strdup(last_update_time->valuestring) : NULL,
191191
message ? strdup(message->valuestring) : NULL,
192192
reason ? strdup(reason->valuestring) : NULL,
193193
strdup(status->valuestring),

0 commit comments

Comments
 (0)