2020-03-18 17:24:33 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include "v1_topology_spread_constraint.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
v1_topology_spread_constraint_t *v1_topology_spread_constraint_create(
|
|
|
|
|
v1_label_selector_t *label_selector,
|
2022-09-26 01:26:15 +00:00
|
|
|
list_t *match_label_keys,
|
2020-03-18 17:24:33 +08:00
|
|
|
int max_skew,
|
2022-06-05 18:16:17 +00:00
|
|
|
int min_domains,
|
2022-09-26 01:26:15 +00:00
|
|
|
char *node_affinity_policy,
|
|
|
|
|
char *node_taints_policy,
|
2020-03-18 17:24:33 +08:00
|
|
|
char *topology_key,
|
2022-06-05 18:16:17 +00:00
|
|
|
char *when_unsatisfiable
|
2020-03-18 17:24:33 +08:00
|
|
|
) {
|
|
|
|
|
v1_topology_spread_constraint_t *v1_topology_spread_constraint_local_var = malloc(sizeof(v1_topology_spread_constraint_t));
|
|
|
|
|
if (!v1_topology_spread_constraint_local_var) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
v1_topology_spread_constraint_local_var->label_selector = label_selector;
|
2022-09-26 01:26:15 +00:00
|
|
|
v1_topology_spread_constraint_local_var->match_label_keys = match_label_keys;
|
2020-03-18 17:24:33 +08:00
|
|
|
v1_topology_spread_constraint_local_var->max_skew = max_skew;
|
2022-06-05 18:16:17 +00:00
|
|
|
v1_topology_spread_constraint_local_var->min_domains = min_domains;
|
2022-09-26 01:26:15 +00:00
|
|
|
v1_topology_spread_constraint_local_var->node_affinity_policy = node_affinity_policy;
|
|
|
|
|
v1_topology_spread_constraint_local_var->node_taints_policy = node_taints_policy;
|
2020-03-18 17:24:33 +08:00
|
|
|
v1_topology_spread_constraint_local_var->topology_key = topology_key;
|
|
|
|
|
v1_topology_spread_constraint_local_var->when_unsatisfiable = when_unsatisfiable;
|
|
|
|
|
|
|
|
|
|
return v1_topology_spread_constraint_local_var;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void v1_topology_spread_constraint_free(v1_topology_spread_constraint_t *v1_topology_spread_constraint) {
|
|
|
|
|
if(NULL == v1_topology_spread_constraint){
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
listEntry_t *listEntry;
|
2021-01-05 15:42:34 +08:00
|
|
|
if (v1_topology_spread_constraint->label_selector) {
|
|
|
|
|
v1_label_selector_free(v1_topology_spread_constraint->label_selector);
|
|
|
|
|
v1_topology_spread_constraint->label_selector = NULL;
|
|
|
|
|
}
|
2022-09-26 01:26:15 +00:00
|
|
|
if (v1_topology_spread_constraint->match_label_keys) {
|
|
|
|
|
list_ForEach(listEntry, v1_topology_spread_constraint->match_label_keys) {
|
|
|
|
|
free(listEntry->data);
|
|
|
|
|
}
|
|
|
|
|
list_freeList(v1_topology_spread_constraint->match_label_keys);
|
|
|
|
|
v1_topology_spread_constraint->match_label_keys = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (v1_topology_spread_constraint->node_affinity_policy) {
|
|
|
|
|
free(v1_topology_spread_constraint->node_affinity_policy);
|
|
|
|
|
v1_topology_spread_constraint->node_affinity_policy = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (v1_topology_spread_constraint->node_taints_policy) {
|
|
|
|
|
free(v1_topology_spread_constraint->node_taints_policy);
|
|
|
|
|
v1_topology_spread_constraint->node_taints_policy = NULL;
|
|
|
|
|
}
|
2021-01-05 15:42:34 +08:00
|
|
|
if (v1_topology_spread_constraint->topology_key) {
|
|
|
|
|
free(v1_topology_spread_constraint->topology_key);
|
|
|
|
|
v1_topology_spread_constraint->topology_key = NULL;
|
|
|
|
|
}
|
2022-06-05 18:16:17 +00:00
|
|
|
if (v1_topology_spread_constraint->when_unsatisfiable) {
|
|
|
|
|
free(v1_topology_spread_constraint->when_unsatisfiable);
|
|
|
|
|
v1_topology_spread_constraint->when_unsatisfiable = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
free(v1_topology_spread_constraint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cJSON *v1_topology_spread_constraint_convertToJSON(v1_topology_spread_constraint_t *v1_topology_spread_constraint) {
|
|
|
|
|
cJSON *item = cJSON_CreateObject();
|
|
|
|
|
|
|
|
|
|
// v1_topology_spread_constraint->label_selector
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_topology_spread_constraint->label_selector) {
|
2020-03-18 17:24:33 +08:00
|
|
|
cJSON *label_selector_local_JSON = v1_label_selector_convertToJSON(v1_topology_spread_constraint->label_selector);
|
|
|
|
|
if(label_selector_local_JSON == NULL) {
|
|
|
|
|
goto fail; //model
|
|
|
|
|
}
|
|
|
|
|
cJSON_AddItemToObject(item, "labelSelector", label_selector_local_JSON);
|
|
|
|
|
if(item->child == NULL) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
|
2022-09-26 01:26:15 +00:00
|
|
|
// v1_topology_spread_constraint->match_label_keys
|
|
|
|
|
if(v1_topology_spread_constraint->match_label_keys) {
|
|
|
|
|
cJSON *match_label_keys = cJSON_AddArrayToObject(item, "matchLabelKeys");
|
|
|
|
|
if(match_label_keys == NULL) {
|
|
|
|
|
goto fail; //primitive container
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listEntry_t *match_label_keysListEntry;
|
|
|
|
|
list_ForEach(match_label_keysListEntry, v1_topology_spread_constraint->match_label_keys) {
|
|
|
|
|
if(cJSON_AddStringToObject(match_label_keys, "", (char*)match_label_keysListEntry->data) == NULL)
|
|
|
|
|
{
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-03-18 17:24:33 +08:00
|
|
|
// v1_topology_spread_constraint->max_skew
|
|
|
|
|
if (!v1_topology_spread_constraint->max_skew) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
if(cJSON_AddNumberToObject(item, "maxSkew", v1_topology_spread_constraint->max_skew) == NULL) {
|
|
|
|
|
goto fail; //Numeric
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-06-05 18:16:17 +00:00
|
|
|
// v1_topology_spread_constraint->min_domains
|
|
|
|
|
if(v1_topology_spread_constraint->min_domains) {
|
|
|
|
|
if(cJSON_AddNumberToObject(item, "minDomains", v1_topology_spread_constraint->min_domains) == NULL) {
|
|
|
|
|
goto fail; //Numeric
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-09-26 01:26:15 +00:00
|
|
|
// v1_topology_spread_constraint->node_affinity_policy
|
|
|
|
|
if(v1_topology_spread_constraint->node_affinity_policy) {
|
|
|
|
|
if(cJSON_AddStringToObject(item, "nodeAffinityPolicy", v1_topology_spread_constraint->node_affinity_policy) == NULL) {
|
|
|
|
|
goto fail; //String
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// v1_topology_spread_constraint->node_taints_policy
|
|
|
|
|
if(v1_topology_spread_constraint->node_taints_policy) {
|
|
|
|
|
if(cJSON_AddStringToObject(item, "nodeTaintsPolicy", v1_topology_spread_constraint->node_taints_policy) == NULL) {
|
|
|
|
|
goto fail; //String
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-03-18 17:24:33 +08:00
|
|
|
// v1_topology_spread_constraint->topology_key
|
|
|
|
|
if (!v1_topology_spread_constraint->topology_key) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
if(cJSON_AddStringToObject(item, "topologyKey", v1_topology_spread_constraint->topology_key) == NULL) {
|
|
|
|
|
goto fail; //String
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// v1_topology_spread_constraint->when_unsatisfiable
|
2022-06-05 18:16:17 +00:00
|
|
|
if (!v1_topology_spread_constraint->when_unsatisfiable) {
|
2022-04-18 09:29:02 +08:00
|
|
|
goto fail;
|
|
|
|
|
}
|
2022-06-05 18:16:17 +00:00
|
|
|
if(cJSON_AddStringToObject(item, "whenUnsatisfiable", v1_topology_spread_constraint->when_unsatisfiable) == NULL) {
|
|
|
|
|
goto fail; //String
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
fail:
|
|
|
|
|
if (item) {
|
|
|
|
|
cJSON_Delete(item);
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v1_topology_spread_constraint_t *v1_topology_spread_constraint_parseFromJSON(cJSON *v1_topology_spread_constraintJSON){
|
|
|
|
|
|
|
|
|
|
v1_topology_spread_constraint_t *v1_topology_spread_constraint_local_var = NULL;
|
|
|
|
|
|
2021-11-05 14:13:35 +08:00
|
|
|
// define the local variable for v1_topology_spread_constraint->label_selector
|
|
|
|
|
v1_label_selector_t *label_selector_local_nonprim = NULL;
|
|
|
|
|
|
2022-09-26 01:26:15 +00:00
|
|
|
// define the local list for v1_topology_spread_constraint->match_label_keys
|
|
|
|
|
list_t *match_label_keysList = NULL;
|
|
|
|
|
|
2020-03-18 17:24:33 +08:00
|
|
|
// v1_topology_spread_constraint->label_selector
|
|
|
|
|
cJSON *label_selector = cJSON_GetObjectItemCaseSensitive(v1_topology_spread_constraintJSON, "labelSelector");
|
|
|
|
|
if (label_selector) {
|
|
|
|
|
label_selector_local_nonprim = v1_label_selector_parseFromJSON(label_selector); //nonprimitive
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 01:26:15 +00:00
|
|
|
// v1_topology_spread_constraint->match_label_keys
|
|
|
|
|
cJSON *match_label_keys = cJSON_GetObjectItemCaseSensitive(v1_topology_spread_constraintJSON, "matchLabelKeys");
|
|
|
|
|
if (match_label_keys) {
|
|
|
|
|
cJSON *match_label_keys_local = NULL;
|
|
|
|
|
if(!cJSON_IsArray(match_label_keys)) {
|
|
|
|
|
goto end;//primitive container
|
|
|
|
|
}
|
|
|
|
|
match_label_keysList = list_createList();
|
|
|
|
|
|
|
|
|
|
cJSON_ArrayForEach(match_label_keys_local, match_label_keys)
|
|
|
|
|
{
|
|
|
|
|
if(!cJSON_IsString(match_label_keys_local))
|
|
|
|
|
{
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
list_addElement(match_label_keysList , strdup(match_label_keys_local->valuestring));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 17:24:33 +08:00
|
|
|
// v1_topology_spread_constraint->max_skew
|
|
|
|
|
cJSON *max_skew = cJSON_GetObjectItemCaseSensitive(v1_topology_spread_constraintJSON, "maxSkew");
|
|
|
|
|
if (!max_skew) {
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!cJSON_IsNumber(max_skew))
|
|
|
|
|
{
|
|
|
|
|
goto end; //Numeric
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-05 18:16:17 +00:00
|
|
|
// v1_topology_spread_constraint->min_domains
|
|
|
|
|
cJSON *min_domains = cJSON_GetObjectItemCaseSensitive(v1_topology_spread_constraintJSON, "minDomains");
|
|
|
|
|
if (min_domains) {
|
|
|
|
|
if(!cJSON_IsNumber(min_domains))
|
|
|
|
|
{
|
|
|
|
|
goto end; //Numeric
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 01:26:15 +00:00
|
|
|
// v1_topology_spread_constraint->node_affinity_policy
|
|
|
|
|
cJSON *node_affinity_policy = cJSON_GetObjectItemCaseSensitive(v1_topology_spread_constraintJSON, "nodeAffinityPolicy");
|
|
|
|
|
if (node_affinity_policy) {
|
2022-12-29 10:58:47 +08:00
|
|
|
if(!cJSON_IsString(node_affinity_policy) && !cJSON_IsNull(node_affinity_policy))
|
2022-09-26 01:26:15 +00:00
|
|
|
{
|
|
|
|
|
goto end; //String
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v1_topology_spread_constraint->node_taints_policy
|
|
|
|
|
cJSON *node_taints_policy = cJSON_GetObjectItemCaseSensitive(v1_topology_spread_constraintJSON, "nodeTaintsPolicy");
|
|
|
|
|
if (node_taints_policy) {
|
2022-12-29 10:58:47 +08:00
|
|
|
if(!cJSON_IsString(node_taints_policy) && !cJSON_IsNull(node_taints_policy))
|
2022-09-26 01:26:15 +00:00
|
|
|
{
|
|
|
|
|
goto end; //String
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 17:24:33 +08:00
|
|
|
// v1_topology_spread_constraint->topology_key
|
|
|
|
|
cJSON *topology_key = cJSON_GetObjectItemCaseSensitive(v1_topology_spread_constraintJSON, "topologyKey");
|
|
|
|
|
if (!topology_key) {
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!cJSON_IsString(topology_key))
|
|
|
|
|
{
|
|
|
|
|
goto end; //String
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v1_topology_spread_constraint->when_unsatisfiable
|
|
|
|
|
cJSON *when_unsatisfiable = cJSON_GetObjectItemCaseSensitive(v1_topology_spread_constraintJSON, "whenUnsatisfiable");
|
|
|
|
|
if (!when_unsatisfiable) {
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!cJSON_IsString(when_unsatisfiable))
|
|
|
|
|
{
|
2022-06-05 18:16:17 +00:00
|
|
|
goto end; //String
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
v1_topology_spread_constraint_local_var = v1_topology_spread_constraint_create (
|
|
|
|
|
label_selector ? label_selector_local_nonprim : NULL,
|
2022-09-26 01:26:15 +00:00
|
|
|
match_label_keys ? match_label_keysList : NULL,
|
2020-03-18 17:24:33 +08:00
|
|
|
max_skew->valuedouble,
|
2022-06-05 18:16:17 +00:00
|
|
|
min_domains ? min_domains->valuedouble : 0,
|
2022-12-29 10:58:47 +08:00
|
|
|
node_affinity_policy && !cJSON_IsNull(node_affinity_policy) ? strdup(node_affinity_policy->valuestring) : NULL,
|
|
|
|
|
node_taints_policy && !cJSON_IsNull(node_taints_policy) ? strdup(node_taints_policy->valuestring) : NULL,
|
2020-03-18 17:24:33 +08:00
|
|
|
strdup(topology_key->valuestring),
|
2022-06-05 18:16:17 +00:00
|
|
|
strdup(when_unsatisfiable->valuestring)
|
2020-03-18 17:24:33 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return v1_topology_spread_constraint_local_var;
|
|
|
|
|
end:
|
2021-01-11 12:15:24 +08:00
|
|
|
if (label_selector_local_nonprim) {
|
|
|
|
|
v1_label_selector_free(label_selector_local_nonprim);
|
|
|
|
|
label_selector_local_nonprim = NULL;
|
|
|
|
|
}
|
2022-09-26 01:26:15 +00:00
|
|
|
if (match_label_keysList) {
|
|
|
|
|
listEntry_t *listEntry = NULL;
|
|
|
|
|
list_ForEach(listEntry, match_label_keysList) {
|
|
|
|
|
free(listEntry->data);
|
|
|
|
|
listEntry->data = NULL;
|
|
|
|
|
}
|
|
|
|
|
list_freeList(match_label_keysList);
|
|
|
|
|
match_label_keysList = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
}
|