Skip to content

Commit

Permalink
Merge pull request meshery#11098 from MUzairS15/MUzairS15/policy/name…
Browse files Browse the repository at this point in the history
…space

[Server] Policy for `Hierarchical Parent` relationship.
  • Loading branch information
MUzairS15 committed Jun 4, 2024
2 parents 6b91b45 + 767c8fa commit 805ef59
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 41 deletions.
10 changes: 9 additions & 1 deletion server/meshmodel/kubernetes/v1.25.2/v1.0.0/policies/common.rego
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,19 @@ extract_components(services, selectors) = components {
components := {component.traits.meshmap.id: component |
selector := selectors[_]
service := services[_]
selector.kind == service.type
is_relationship_feasible(selector, service.type)
component := service
}
}

is_relationship_feasible(selector, compType) {
selector.kind == "*"
}

is_relationship_feasible(selector, compType) {
selector.kind == compType
}

has_key(x, k) {
x[k]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package meshmodel_policy

import data.common.contains
import data.common.is_relationship_feasible
import data.common.has_key
import data.common.extract_components
import data.common.get_array_pos
import data.common.get_path
Expand All @@ -10,10 +12,9 @@ import future.keywords.in

hierarchical_inventory_relationship[updated_comps] {
relationship := data.relationships[_]
relationship.subType in {"Inventory"}
relationship.subType in {"Inventory", "Parent"}

selector_set := relationship.selectors[_]

from_selectors := {kind: selectors |
selectors := selector_set.allow.from[_]
kind := selectors.kind
Expand All @@ -31,11 +32,9 @@ hierarchical_inventory_relationship[updated_comps] {
service := input.services[_]
}

filtered_services := object.remove(services_map, object.keys(allowed_parent_comps))

updated_comps = {id: updated_comp |
some i, j
service := filtered_services[i]
service := services_map[i]
allowed_component := allowed_parent_comps[j]

allowed_component.traits.meshmap["meshmodel-metadata"].parentId == i
Expand All @@ -48,25 +47,66 @@ hierarchical_inventory_relationship[updated_comps] {
}

apply_patch(mutator, mutated, from_selectors, to_selectors) := mutated_design {
from_selectors[i].kind == mutator.type
mutator_paths := from_selectors[i].patch.mutatorRef
some i, j

is_relationship_feasible(from_selectors[i], mutator.type)

is_relationship_feasible(to_selectors[j], mutated.type)

mutatorObj := identifyMutator(from_selectors[i], to_selectors[j], mutator, mutated)

mutatedObj := identifyMutated(from_selectors[i], to_selectors[j], mutator, mutated)

to_selectors[j].kind == mutated.type
mutated_paths := to_selectors[j].patch.mutatedRef

patches := [patch |
some i
mutator_path := get_path(mutator_paths[i], mutator)
update_value := object.get(mutator, mutator_path, "")
some i
mutator_path := get_path(mutatorObj.path[i], mutatorObj.mutator)
update_value := object.get(mutatorObj.mutator, mutatorObj.path[i], "")
update_value != null
mutated_path := get_path(mutated_paths[i], mutated)
mutated_path := get_path(mutatedObj.path[i], mutatedObj.mutated)

patch := {
"op": "add",
"path": mutated_path,
"value": update_value,
}
]
resultantPatchesToApply := ensureParentPathsExist(patches, mutated)
mutated_design = json.patch(mutated, resultantPatchesToApply)
resultantPatchesToApply := ensureParentPathsExist(patches, mutatedObj.mutated)
mutated_design = json.patch(mutatedObj.mutated, resultantPatchesToApply)
}

identifyMutator(from_selector, to_selector, mutator, mutated) := mutatorObj {
has_key(to_selector.patch, "mutatorRef")
mutatorObj = {
"mutator": mutated,
"path": to_selector.patch.mutatorRef
}

}

identifyMutator(from_selector, to_selector, mutator, mutated) := mutatorObj {
has_key(from_selector.patch, "mutatorRef")
mutatorObj = {
"mutator": mutator,
"path": from_selector.patch.mutatorRef
}

}

identifyMutated(from_selector, to_selector, mutator, mutated) := mutatedObj {
has_key(from_selector.patch, "mutatedRef")
mutatedObj = {
"mutated": mutator,
"path": from_selector.patch.mutatedRef
}

}

identifyMutated(from_selector, to_selector, mutator, mutated) := mutatedObj {
has_key(to_selector.patch, "mutatedRef")
mutatedObj = {
"mutated": mutated,
"path": to_selector.patch.mutatedRef
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package meshmodel_policy

hierarchical_parent_relationship := [ result |
relationships := data.relationships
result := hierarchical_inventory_relationship with data.relationships as relationships
]

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,39 @@
},
"metadata": {}
},
"evaluationQuery": "heirarcal_parent_relationship",
"evaluationQuery": "hierarchical_parent_relationship",
"subType": "Parent",
"selectors": [
{
"allow": {
"from": [
{
"model": "*"
"model": "*",
"kind": "*",
"patch": {
"patchStrategy": "replace",
"mutatedRef": [
[
"namespace"
]
],
"description": "WASM filter configuration to be applied to Envoy Filter."
}
}
],
"to": [
{
"kind": "Namespace",
"model": "kubernetes"
"model": "kubernetes",
"patch": {
"patchStrategy": "replace",
"mutatorRef": [
[
"name"
]
],
"description": "WASM filter configuration to be applied to Envoy Filter."
}
},
{
"kind": "Node",
Expand Down

0 comments on commit 805ef59

Please sign in to comment.