kubernetes-client / c

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

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

Closed HaunHsiung closed 3 years ago

HaunHsiung commented 3 years ago
    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

tg123 commented 3 years ago

tcpdump to track http request would help

brendandburns commented 3 years ago

You need to print the response body and it should have more info.

I think you can:

printf("%s\n", apiClient->dataReceived);
k8s-triage-robot commented 3 years ago

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale

ityuhui commented 3 years ago

/remove-lifecycle stale

I will take a look.

ityuhui commented 3 years ago

I have written a sample to create/list config maps, it works well at my env.

This sample uses data other than binary_data, if you want to use binary_data, please wait the PR https://github.com/kubernetes-client/c/pull/68 to merge.

void create_configmap(apiClient_t * apiClient)
{ 
    char *api_version = strdup("v1");
    char *kind = strdup("ConfigMap");

    list_t *data = list_create();
    keyValuePair_t *kv = keyValuePair_create(strdup("worker1"), strdup("1"));
    list_addElement(data, kv);
    kv = keyValuePair_create(strdup("worker2"), strdup("2"));
    list_addElement(data, kv);

    v1_object_meta_t *meta = v1_object_meta_create(NULL,
                                                   NULL,
                                                   NULL,
                                                   0,
                                                   NULL,
                                                   NULL,
                                                   NULL,
                                                   0,
                                                   NULL,
                                                   NULL,
                                                   strdup("cm1"),
                                                   strdup("default"),
                                                   NULL,
                                                   NULL,
                                                   NULL,
                                                   NULL);

    v1_config_map_t *body = v1_config_map_create(api_version,
                                                 NULL,
                                                 data,
                                                 kind,
                                                 meta);

    v1_config_map_t *ret_config_map = CoreV1API_createNamespacedConfigMap(apiClient,
                                                                        "default",
                                                                        body,
                                                                        NULL,
                                                                        NULL,
                                                                        NULL);

    printf("%s: The return code of HTTP request=%ld\n", __func__, apiClient->response_code);

    if (201 == apiClient->response_code) {
        printf("%s: Create the config map successfully.\n", __func__);
    } else {
        fprintf(stderr, "%s: Failed to create the config map.\n", __func__);
        return;
    }

    if (ret_config_map) {
        v1_config_map_free(ret_config_map);
        ret_config_map = NULL;
    }
    if (body) {
        v1_config_map_free(body);
        body = NULL;
    }
}

void list_configmap(apiClient_t * apiClient)
{
    v1_config_map_list_t *config_map_list = CoreV1API_listNamespacedConfigMap(apiClient,
                                                                              "default",    // char *namespace
                                                                              "true",   // char *pretty
                                                                              0,    // int allowWatchBookmarks
                                                                              NULL, // char * _continue
                                                                              NULL, // char * fieldSelector
                                                                              NULL, // char * labelSelector
                                                                              0,    // int limit
                                                                              NULL, // char * resourceVersion
                                                                              0,    // int timeoutSeconds
                                                                              0 //int watch
        );

    printf("%s: The return code of HTTP request=%ld\n", __func__, apiClient->response_code);

    if (200 == apiClient->response_code) {
        printf("%s: List the config maps successfully.\n", __func__);
    } else {
        fprintf(stderr, "%s: Failed to list the config maps.\n", __func__);
        return;
    }

    if (config_map_list && config_map_list->items) {
        listEntry_t *config_map_list_entry = NULL;
        v1_config_map_t *config_map = NULL;
        list_ForEach(config_map_list_entry, config_map_list->items) {
            config_map = config_map_list_entry->data;
            printf("\tThe config map name: %s\n", config_map->metadata->name);

            listEntry_t *data_entry = NULL;
            keyValuePair_t *pair = NULL;
            list_ForEach(data_entry, config_map->data) {
                pair = data_entry->data;
                printf("\tkey=%s, value=%s\n", pair->key, (char *) pair->value);
            }
        }
        v1_config_map_list_free(config_map_list);
        config_map_list = NULL;
    } else {
        fprintf(stderr, "%s: The config map list is invalid.\n", __func__);
    }
}