2020-03-18 17:24:33 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include "v1_network_policy_spec.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
v1_network_policy_spec_t *v1_network_policy_spec_create(
|
|
|
|
|
list_t *egress,
|
|
|
|
|
list_t *ingress,
|
|
|
|
|
v1_label_selector_t *pod_selector,
|
|
|
|
|
list_t *policy_types
|
|
|
|
|
) {
|
|
|
|
|
v1_network_policy_spec_t *v1_network_policy_spec_local_var = malloc(sizeof(v1_network_policy_spec_t));
|
|
|
|
|
if (!v1_network_policy_spec_local_var) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
v1_network_policy_spec_local_var->egress = egress;
|
|
|
|
|
v1_network_policy_spec_local_var->ingress = ingress;
|
|
|
|
|
v1_network_policy_spec_local_var->pod_selector = pod_selector;
|
|
|
|
|
v1_network_policy_spec_local_var->policy_types = policy_types;
|
|
|
|
|
|
|
|
|
|
return v1_network_policy_spec_local_var;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void v1_network_policy_spec_free(v1_network_policy_spec_t *v1_network_policy_spec) {
|
|
|
|
|
if(NULL == v1_network_policy_spec){
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
listEntry_t *listEntry;
|
2021-01-05 15:42:34 +08:00
|
|
|
if (v1_network_policy_spec->egress) {
|
|
|
|
|
list_ForEach(listEntry, v1_network_policy_spec->egress) {
|
|
|
|
|
v1_network_policy_egress_rule_free(listEntry->data);
|
|
|
|
|
}
|
2022-03-09 10:56:53 +08:00
|
|
|
list_freeList(v1_network_policy_spec->egress);
|
2021-01-05 15:42:34 +08:00
|
|
|
v1_network_policy_spec->egress = NULL;
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
2021-01-05 15:42:34 +08:00
|
|
|
if (v1_network_policy_spec->ingress) {
|
|
|
|
|
list_ForEach(listEntry, v1_network_policy_spec->ingress) {
|
|
|
|
|
v1_network_policy_ingress_rule_free(listEntry->data);
|
|
|
|
|
}
|
2022-03-09 10:56:53 +08:00
|
|
|
list_freeList(v1_network_policy_spec->ingress);
|
2021-01-05 15:42:34 +08:00
|
|
|
v1_network_policy_spec->ingress = NULL;
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
2021-01-05 15:42:34 +08:00
|
|
|
if (v1_network_policy_spec->pod_selector) {
|
|
|
|
|
v1_label_selector_free(v1_network_policy_spec->pod_selector);
|
|
|
|
|
v1_network_policy_spec->pod_selector = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (v1_network_policy_spec->policy_types) {
|
|
|
|
|
list_ForEach(listEntry, v1_network_policy_spec->policy_types) {
|
|
|
|
|
free(listEntry->data);
|
|
|
|
|
}
|
2022-03-09 10:56:53 +08:00
|
|
|
list_freeList(v1_network_policy_spec->policy_types);
|
2021-01-05 15:42:34 +08:00
|
|
|
v1_network_policy_spec->policy_types = NULL;
|
2020-03-18 17:24:33 +08:00
|
|
|
}
|
|
|
|
|
free(v1_network_policy_spec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cJSON *v1_network_policy_spec_convertToJSON(v1_network_policy_spec_t *v1_network_policy_spec) {
|
|
|
|
|
cJSON *item = cJSON_CreateObject();
|
|
|
|
|
|
|
|
|
|
// v1_network_policy_spec->egress
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_network_policy_spec->egress) {
|
2020-03-18 17:24:33 +08:00
|
|
|
cJSON *egress = cJSON_AddArrayToObject(item, "egress");
|
|
|
|
|
if(egress == NULL) {
|
|
|
|
|
goto fail; //nonprimitive container
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listEntry_t *egressListEntry;
|
|
|
|
|
if (v1_network_policy_spec->egress) {
|
|
|
|
|
list_ForEach(egressListEntry, v1_network_policy_spec->egress) {
|
|
|
|
|
cJSON *itemLocal = v1_network_policy_egress_rule_convertToJSON(egressListEntry->data);
|
|
|
|
|
if(itemLocal == NULL) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
cJSON_AddItemToArray(egress, itemLocal);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// v1_network_policy_spec->ingress
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_network_policy_spec->ingress) {
|
2020-03-18 17:24:33 +08:00
|
|
|
cJSON *ingress = cJSON_AddArrayToObject(item, "ingress");
|
|
|
|
|
if(ingress == NULL) {
|
|
|
|
|
goto fail; //nonprimitive container
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listEntry_t *ingressListEntry;
|
|
|
|
|
if (v1_network_policy_spec->ingress) {
|
|
|
|
|
list_ForEach(ingressListEntry, v1_network_policy_spec->ingress) {
|
|
|
|
|
cJSON *itemLocal = v1_network_policy_ingress_rule_convertToJSON(ingressListEntry->data);
|
|
|
|
|
if(itemLocal == NULL) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
cJSON_AddItemToArray(ingress, itemLocal);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-18 09:29:02 +08:00
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// v1_network_policy_spec->pod_selector
|
|
|
|
|
if (!v1_network_policy_spec->pod_selector) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
cJSON *pod_selector_local_JSON = v1_label_selector_convertToJSON(v1_network_policy_spec->pod_selector);
|
|
|
|
|
if(pod_selector_local_JSON == NULL) {
|
|
|
|
|
goto fail; //model
|
|
|
|
|
}
|
|
|
|
|
cJSON_AddItemToObject(item, "podSelector", pod_selector_local_JSON);
|
|
|
|
|
if(item->child == NULL) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// v1_network_policy_spec->policy_types
|
2022-04-18 09:29:02 +08:00
|
|
|
if(v1_network_policy_spec->policy_types) {
|
2020-03-18 17:24:33 +08:00
|
|
|
cJSON *policy_types = cJSON_AddArrayToObject(item, "policyTypes");
|
|
|
|
|
if(policy_types == NULL) {
|
|
|
|
|
goto fail; //primitive container
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listEntry_t *policy_typesListEntry;
|
|
|
|
|
list_ForEach(policy_typesListEntry, v1_network_policy_spec->policy_types) {
|
|
|
|
|
if(cJSON_AddStringToObject(policy_types, "", (char*)policy_typesListEntry->data) == 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_network_policy_spec_t *v1_network_policy_spec_parseFromJSON(cJSON *v1_network_policy_specJSON){
|
|
|
|
|
|
|
|
|
|
v1_network_policy_spec_t *v1_network_policy_spec_local_var = NULL;
|
|
|
|
|
|
2022-03-29 10:12:05 +08:00
|
|
|
// define the local list for v1_network_policy_spec->egress
|
|
|
|
|
list_t *egressList = NULL;
|
|
|
|
|
|
|
|
|
|
// define the local list for v1_network_policy_spec->ingress
|
|
|
|
|
list_t *ingressList = NULL;
|
|
|
|
|
|
2021-11-05 14:13:35 +08:00
|
|
|
// define the local variable for v1_network_policy_spec->pod_selector
|
|
|
|
|
v1_label_selector_t *pod_selector_local_nonprim = NULL;
|
|
|
|
|
|
2022-03-29 10:12:05 +08:00
|
|
|
// define the local list for v1_network_policy_spec->policy_types
|
|
|
|
|
list_t *policy_typesList = NULL;
|
|
|
|
|
|
2020-03-18 17:24:33 +08:00
|
|
|
// v1_network_policy_spec->egress
|
|
|
|
|
cJSON *egress = cJSON_GetObjectItemCaseSensitive(v1_network_policy_specJSON, "egress");
|
|
|
|
|
if (egress) {
|
2022-03-29 10:12:05 +08:00
|
|
|
cJSON *egress_local_nonprimitive = NULL;
|
2020-03-18 17:24:33 +08:00
|
|
|
if(!cJSON_IsArray(egress)){
|
|
|
|
|
goto end; //nonprimitive container
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 10:56:53 +08:00
|
|
|
egressList = list_createList();
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
cJSON_ArrayForEach(egress_local_nonprimitive,egress )
|
|
|
|
|
{
|
|
|
|
|
if(!cJSON_IsObject(egress_local_nonprimitive)){
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
v1_network_policy_egress_rule_t *egressItem = v1_network_policy_egress_rule_parseFromJSON(egress_local_nonprimitive);
|
|
|
|
|
|
|
|
|
|
list_addElement(egressList, egressItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v1_network_policy_spec->ingress
|
|
|
|
|
cJSON *ingress = cJSON_GetObjectItemCaseSensitive(v1_network_policy_specJSON, "ingress");
|
|
|
|
|
if (ingress) {
|
2022-03-29 10:12:05 +08:00
|
|
|
cJSON *ingress_local_nonprimitive = NULL;
|
2020-03-18 17:24:33 +08:00
|
|
|
if(!cJSON_IsArray(ingress)){
|
|
|
|
|
goto end; //nonprimitive container
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 10:56:53 +08:00
|
|
|
ingressList = list_createList();
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
cJSON_ArrayForEach(ingress_local_nonprimitive,ingress )
|
|
|
|
|
{
|
|
|
|
|
if(!cJSON_IsObject(ingress_local_nonprimitive)){
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
v1_network_policy_ingress_rule_t *ingressItem = v1_network_policy_ingress_rule_parseFromJSON(ingress_local_nonprimitive);
|
|
|
|
|
|
|
|
|
|
list_addElement(ingressList, ingressItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v1_network_policy_spec->pod_selector
|
|
|
|
|
cJSON *pod_selector = cJSON_GetObjectItemCaseSensitive(v1_network_policy_specJSON, "podSelector");
|
|
|
|
|
if (!pod_selector) {
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pod_selector_local_nonprim = v1_label_selector_parseFromJSON(pod_selector); //nonprimitive
|
|
|
|
|
|
|
|
|
|
// v1_network_policy_spec->policy_types
|
|
|
|
|
cJSON *policy_types = cJSON_GetObjectItemCaseSensitive(v1_network_policy_specJSON, "policyTypes");
|
|
|
|
|
if (policy_types) {
|
2022-03-29 10:12:05 +08:00
|
|
|
cJSON *policy_types_local = NULL;
|
2020-03-18 17:24:33 +08:00
|
|
|
if(!cJSON_IsArray(policy_types)) {
|
|
|
|
|
goto end;//primitive container
|
|
|
|
|
}
|
2022-03-09 10:56:53 +08:00
|
|
|
policy_typesList = list_createList();
|
2020-03-18 17:24:33 +08:00
|
|
|
|
|
|
|
|
cJSON_ArrayForEach(policy_types_local, policy_types)
|
|
|
|
|
{
|
|
|
|
|
if(!cJSON_IsString(policy_types_local))
|
|
|
|
|
{
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
list_addElement(policy_typesList , strdup(policy_types_local->valuestring));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
v1_network_policy_spec_local_var = v1_network_policy_spec_create (
|
|
|
|
|
egress ? egressList : NULL,
|
|
|
|
|
ingress ? ingressList : NULL,
|
|
|
|
|
pod_selector_local_nonprim,
|
|
|
|
|
policy_types ? policy_typesList : NULL
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return v1_network_policy_spec_local_var;
|
|
|
|
|
end:
|
2022-03-29 10:12:05 +08:00
|
|
|
if (egressList) {
|
|
|
|
|
listEntry_t *listEntry = NULL;
|
|
|
|
|
list_ForEach(listEntry, egressList) {
|
|
|
|
|
v1_network_policy_egress_rule_free(listEntry->data);
|
|
|
|
|
listEntry->data = NULL;
|
|
|
|
|
}
|
|
|
|
|
list_freeList(egressList);
|
|
|
|
|
egressList = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (ingressList) {
|
|
|
|
|
listEntry_t *listEntry = NULL;
|
|
|
|
|
list_ForEach(listEntry, ingressList) {
|
|
|
|
|
v1_network_policy_ingress_rule_free(listEntry->data);
|
|
|
|
|
listEntry->data = NULL;
|
|
|
|
|
}
|
|
|
|
|
list_freeList(ingressList);
|
|
|
|
|
ingressList = NULL;
|
|
|
|
|
}
|
2021-01-11 12:15:24 +08:00
|
|
|
if (pod_selector_local_nonprim) {
|
|
|
|
|
v1_label_selector_free(pod_selector_local_nonprim);
|
|
|
|
|
pod_selector_local_nonprim = NULL;
|
|
|
|
|
}
|
2022-03-29 10:12:05 +08:00
|
|
|
if (policy_typesList) {
|
|
|
|
|
listEntry_t *listEntry = NULL;
|
|
|
|
|
list_ForEach(listEntry, policy_typesList) {
|
|
|
|
|
free(listEntry->data);
|
|
|
|
|
listEntry->data = NULL;
|
|
|
|
|
}
|
|
|
|
|
list_freeList(policy_typesList);
|
|
|
|
|
policy_typesList = NULL;
|
|
|
|
|
}
|
2020-03-18 17:24:33 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
}
|