2020-03-18 17:24:33 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include "v1_replication_controller_spec.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
v1_replication_controller_spec_t *v1_replication_controller_spec_create(
|
|
|
|
|
int min_ready_seconds,
|
|
|
|
|
int replicas,
|
|
|
|
|
list_t* selector,
|
2021-01-05 15:42:34 +08:00
|
|
|
v1_pod_template_spec_t *_template
|
2020-03-18 17:24:33 +08:00
|
|
|
) {
|
|
|
|
|
v1_replication_controller_spec_t *v1_replication_controller_spec_local_var = malloc(sizeof(v1_replication_controller_spec_t));
|
|
|
|
|
if (!v1_replication_controller_spec_local_var) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
v1_replication_controller_spec_local_var->min_ready_seconds = min_ready_seconds;
|
|
|
|
|
v1_replication_controller_spec_local_var->replicas = replicas;
|
|
|
|
|
v1_replication_controller_spec_local_var->selector = selector;
|
2021-01-05 15:42:34 +08:00
|
|
|
v1_replication_controller_spec_local_var->_template = _template;
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
return v1_replication_controller_spec_local_var;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void v1_replication_controller_spec_free(v1_replication_controller_spec_t *v1_replication_controller_spec) {
|
|
|
|
|
if(NULL == v1_replication_controller_spec){
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
listEntry_t *listEntry;
|
2021-01-05 15:42:34 +08:00
|
|
|
if (v1_replication_controller_spec->selector) {
|
|
|
|
|
list_ForEach(listEntry, v1_replication_controller_spec->selector) {
|
|
|
|
|
keyValuePair_t *localKeyValue = (keyValuePair_t*) listEntry->data;
|
|
|
|
|
free (localKeyValue->key);
|
|
|
|
|
free (localKeyValue->value);
|
|
|
|
|
keyValuePair_free(localKeyValue);
|
|
|
|
|
}
|
2022-03-09 10:56:53 +08:00
|
|
|
list_freeList(v1_replication_controller_spec->selector);
|
2021-01-05 15:42:34 +08:00
|
|
|
v1_replication_controller_spec->selector = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (v1_replication_controller_spec->_template) {
|
|
|
|
|
v1_pod_template_spec_free(v1_replication_controller_spec->_template);
|
|
|
|
|
v1_replication_controller_spec->_template = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
free(v1_replication_controller_spec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cJSON *v1_replication_controller_spec_convertToJSON(v1_replication_controller_spec_t *v1_replication_controller_spec) {
|
|
|
|
|
cJSON *item = cJSON_CreateObject();
|
|
|
|
|
|
|
|
|
|
// v1_replication_controller_spec->min_ready_seconds
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_replication_controller_spec->min_ready_seconds) {
|
2020-03-18 17:24:33 +08:00
|
|
|
if(cJSON_AddNumberToObject(item, "minReadySeconds", v1_replication_controller_spec->min_ready_seconds) == NULL) {
|
|
|
|
|
goto fail; //Numeric
|
|
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// v1_replication_controller_spec->replicas
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_replication_controller_spec->replicas) {
|
2020-03-18 17:24:33 +08:00
|
|
|
if(cJSON_AddNumberToObject(item, "replicas", v1_replication_controller_spec->replicas) == NULL) {
|
|
|
|
|
goto fail; //Numeric
|
|
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// v1_replication_controller_spec->selector
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_replication_controller_spec->selector) {
|
2020-03-18 17:24:33 +08:00
|
|
|
cJSON *selector = cJSON_AddObjectToObject(item, "selector");
|
|
|
|
|
if(selector == NULL) {
|
|
|
|
|
goto fail; //primitive map container
|
|
|
|
|
}
|
|
|
|
|
cJSON *localMapObject = selector;
|
|
|
|
|
listEntry_t *selectorListEntry;
|
|
|
|
|
if (v1_replication_controller_spec->selector) {
|
|
|
|
|
list_ForEach(selectorListEntry, v1_replication_controller_spec->selector) {
|
|
|
|
|
keyValuePair_t *localKeyValue = (keyValuePair_t*)selectorListEntry->data;
|
|
|
|
|
if(cJSON_AddStringToObject(localMapObject, localKeyValue->key, (char*)localKeyValue->value) == NULL)
|
|
|
|
|
{
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
|
2021-01-05 15:42:34 +08:00
|
|
|
// v1_replication_controller_spec->_template
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_replication_controller_spec->_template) {
|
2021-01-05 15:42:34 +08:00
|
|
|
cJSON *_template_local_JSON = v1_pod_template_spec_convertToJSON(v1_replication_controller_spec->_template);
|
|
|
|
|
if(_template_local_JSON == NULL) {
|
2020-03-18 17:24:33 +08:00
|
|
|
goto fail; //model
|
|
|
|
|
}
|
2021-01-05 15:42:34 +08:00
|
|
|
cJSON_AddItemToObject(item, "template", _template_local_JSON);
|
2020-03-18 17:24:33 +08:00
|
|
|
if(item->child == NULL) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
fail:
|
|
|
|
|
if (item) {
|
|
|
|
|
cJSON_Delete(item);
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v1_replication_controller_spec_t *v1_replication_controller_spec_parseFromJSON(cJSON *v1_replication_controller_specJSON){
|
|
|
|
|
|
|
|
|
|
v1_replication_controller_spec_t *v1_replication_controller_spec_local_var = NULL;
|
|
|
|
|
|
2022-03-29 10:12:05 +08:00
|
|
|
// define the local map for v1_replication_controller_spec->selector
|
|
|
|
|
list_t *selectorList = NULL;
|
|
|
|
|
|
2021-11-05 14:13:35 +08:00
|
|
|
// define the local variable for v1_replication_controller_spec->_template
|
|
|
|
|
v1_pod_template_spec_t *_template_local_nonprim = NULL;
|
|
|
|
|
|
2020-03-18 17:24:33 +08:00
|
|
|
// v1_replication_controller_spec->min_ready_seconds
|
|
|
|
|
cJSON *min_ready_seconds = cJSON_GetObjectItemCaseSensitive(v1_replication_controller_specJSON, "minReadySeconds");
|
|
|
|
|
if (min_ready_seconds) {
|
|
|
|
|
if(!cJSON_IsNumber(min_ready_seconds))
|
|
|
|
|
{
|
|
|
|
|
goto end; //Numeric
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v1_replication_controller_spec->replicas
|
|
|
|
|
cJSON *replicas = cJSON_GetObjectItemCaseSensitive(v1_replication_controller_specJSON, "replicas");
|
|
|
|
|
if (replicas) {
|
|
|
|
|
if(!cJSON_IsNumber(replicas))
|
|
|
|
|
{
|
|
|
|
|
goto end; //Numeric
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v1_replication_controller_spec->selector
|
|
|
|
|
cJSON *selector = cJSON_GetObjectItemCaseSensitive(v1_replication_controller_specJSON, "selector");
|
|
|
|
|
if (selector) {
|
2022-03-29 10:12:05 +08:00
|
|
|
cJSON *selector_local_map = NULL;
|
2022-09-09 10:18:51 +08:00
|
|
|
if(!cJSON_IsObject(selector) && !cJSON_IsNull(selector))
|
|
|
|
|
{
|
2020-03-18 17:24:33 +08:00
|
|
|
goto end;//primitive map container
|
|
|
|
|
}
|
2022-09-09 10:18:51 +08:00
|
|
|
if(cJSON_IsObject(selector))
|
2020-03-18 17:24:33 +08:00
|
|
|
{
|
2022-09-09 10:18:51 +08:00
|
|
|
selectorList = list_createList();
|
|
|
|
|
keyValuePair_t *localMapKeyPair;
|
|
|
|
|
cJSON_ArrayForEach(selector_local_map, selector)
|
2020-03-18 17:24:33 +08:00
|
|
|
{
|
2022-09-09 10:18:51 +08:00
|
|
|
cJSON *localMapObject = selector_local_map;
|
|
|
|
|
if(!cJSON_IsString(localMapObject))
|
|
|
|
|
{
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
localMapKeyPair = keyValuePair_create(strdup(localMapObject->string),strdup(localMapObject->valuestring));
|
|
|
|
|
list_addElement(selectorList , localMapKeyPair);
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 15:42:34 +08:00
|
|
|
// v1_replication_controller_spec->_template
|
|
|
|
|
cJSON *_template = cJSON_GetObjectItemCaseSensitive(v1_replication_controller_specJSON, "template");
|
|
|
|
|
if (_template) {
|
|
|
|
|
_template_local_nonprim = v1_pod_template_spec_parseFromJSON(_template); //nonprimitive
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
v1_replication_controller_spec_local_var = v1_replication_controller_spec_create (
|
|
|
|
|
min_ready_seconds ? min_ready_seconds->valuedouble : 0,
|
|
|
|
|
replicas ? replicas->valuedouble : 0,
|
|
|
|
|
selector ? selectorList : NULL,
|
2021-01-05 15:42:34 +08:00
|
|
|
_template ? _template_local_nonprim : NULL
|
2020-03-18 17:24:33 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return v1_replication_controller_spec_local_var;
|
|
|
|
|
end:
|
2022-03-29 10:12:05 +08:00
|
|
|
if (selectorList) {
|
|
|
|
|
listEntry_t *listEntry = NULL;
|
|
|
|
|
list_ForEach(listEntry, selectorList) {
|
|
|
|
|
keyValuePair_t *localKeyValue = (keyValuePair_t*) listEntry->data;
|
|
|
|
|
free(localKeyValue->key);
|
|
|
|
|
localKeyValue->key = NULL;
|
|
|
|
|
free(localKeyValue->value);
|
|
|
|
|
localKeyValue->value = NULL;
|
|
|
|
|
keyValuePair_free(localKeyValue);
|
|
|
|
|
localKeyValue = NULL;
|
|
|
|
|
}
|
|
|
|
|
list_freeList(selectorList);
|
|
|
|
|
selectorList = NULL;
|
|
|
|
|
}
|
2021-01-11 12:15:24 +08:00
|
|
|
if (_template_local_nonprim) {
|
|
|
|
|
v1_pod_template_spec_free(_template_local_nonprim);
|
|
|
|
|
_template_local_nonprim = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
}
|