Skip to content

Commit

Permalink
issue-5702 - Remove duplicate mappings in group if more mappings have…
Browse files Browse the repository at this point in the history
… same cluster_key

Signed-off-by: Sekar Saravanan <[email protected]>
  • Loading branch information
sekar-saravanan committed Jul 8, 2024
1 parent 9b66738 commit 173d98f
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion python/ambassador/ir/irbasemappinggroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from .irbasemapping import IRBaseMapping
from .irresource import IRResource

from itertools import groupby

if TYPE_CHECKING:
from .ir import IR # pragma: no cover

Expand Down Expand Up @@ -72,7 +74,18 @@ def normalize_weights_in_mappings(self) -> bool:
normalized_mappings = []

current_weight = 0
for mapping in self.mappings:
groupby_mappings = groupby(sorted(self.mappings, key=lambda x:x["cluster_key"]), key=lambda x:x["cluster_key"])
for cluster_key, mapping_iterator in groupby_mappings:

mappings = list(mapping_iterator)

if len(mappings) > 1:
self.logger.error(
f"More than 1 mappings have same cluster_key {cluster_key} in group {self.group_id}. Removing the duplicate mappings with least weight"
)

mapping = max(mappings, key=lambda x:x.get("weight", 100))

if "weight" in mapping:
if mapping.weight > 100:
self.post_error(f"Mapping {mapping.name} has invalid weight {mapping.weight}")
Expand Down

0 comments on commit 173d98f

Please sign in to comment.