Skip to content

Commit

Permalink
Remove duplicate clusters/users/contexts when using multiple configs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson authored Apr 8, 2024
1 parent 87063fc commit a843c65
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
20 changes: 17 additions & 3 deletions kr8s/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import anyio
import yaml

from kr8s._data_utils import dict_list_pack, list_dict_unpack

# TODO Implement set
# TODO Implement unset
# TODO Implement set cluster
Expand Down Expand Up @@ -116,15 +118,27 @@ def preferences(self) -> Dict:

@property
def clusters(self) -> List[Dict]:
return [cluster for config in self._configs for cluster in config.clusters]
clusters = [cluster for config in self._configs for cluster in config.clusters]
# Unpack and repack to remove duplicates
clusters = list_dict_unpack(clusters, "name", "cluster")
clusters = dict_list_pack(clusters, "name", "cluster")
return clusters

@property
def users(self) -> List[Dict]:
return [user for config in self._configs for user in config.users]
users = [user for config in self._configs for user in config.users]
# Unpack and repack to remove duplicates
users = list_dict_unpack(users, "name", "user")
users = dict_list_pack(users, "name", "user")
return users

@property
def contexts(self) -> List[Dict]:
return [context for config in self._configs for context in config.contexts]
contexts = [context for config in self._configs for context in config.contexts]
# Unpack and repack to remove duplicates
contexts = list_dict_unpack(contexts, "name", "context")
contexts = dict_list_pack(contexts, "name", "context")
return contexts

@property
def extensions(self) -> List[Dict]:
Expand Down
22 changes: 22 additions & 0 deletions kr8s/_data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ def list_dict_unpack(
return {i[key]: i[value] for i in input_list}


def dict_list_pack(
input_dict: Dict, key: str = "key", value: str = "value"
) -> List[Dict]:
"""Convert a dictionary to a list of dictionaries.
Parameters
----------
input_dict : Dict
The dictionary to convert to a list of dictionaries.
key : str, optional
The key to use for the input dictionary's keys. Defaults to "key".
value : str, optional
The key to use for the input dictionary's values. Defaults to "value".
Returns
-------
List[Dict]
A list of dictionaries with the keys and values from the input dictionary.
"""
return [{key: k, value: v} for k, v in input_dict.items()]


def dot_to_nested_dict(dot_notated_key: str, value: Any) -> Dict:
"""Convert a dot notated key to a nested dictionary.
Expand Down
26 changes: 26 additions & 0 deletions kr8s/tests/test_data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from kr8s._data_utils import (
dict_list_pack,
dict_to_selector,
dot_to_nested_dict,
list_dict_unpack,
Expand All @@ -15,6 +16,31 @@ def test_list_dict_unpack():
assert list_dict_unpack(data) == {"hello": "world"}


def test_dict_list_pack():
data = {"hello": "world"}
assert dict_list_pack(data) == [{"key": "hello", "value": "world"}]


def test_pack_unpack():
data = [{"key": "hello", "value": "world"}]
assert dict_list_pack(list_dict_unpack(data)) == data

data = {"hello": "world"}
assert list_dict_unpack(dict_list_pack(data)) == data


def test_unpack_pack_deduplicate():
data = [
{"key": "hello", "value": "world"},
{"key": "hello", "value": "there"},
]
unpacked = list_dict_unpack(data)
assert unpacked["hello"] == "there"
repacked = dict_list_pack(unpacked)
assert len(repacked) == 1
assert repacked == [{"key": "hello", "value": "there"}]


def test_dot_to_nested_dict():
assert dot_to_nested_dict("hello", "world") == {"hello": "world"}
assert dot_to_nested_dict("hello.world", "value") == {"hello": {"world": "value"}}
Expand Down

0 comments on commit a843c65

Please sign in to comment.