-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add list_stale.py * Rename list_stale_profiles to get_stale_profiles * Add tests for delete_stale_profiles and get_stale_profiles * Resolve comments * Don't redeploy profiles-controller if it already exists * Fix linting * Remove debugging message * Fix info message
- Loading branch information
Showing
9 changed files
with
233 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Module responsible for deleting "stale" Profiles based on a PMR. | ||
In this context, a "stale" Profile is a Profile that exists in the cluster but doesn't belong | ||
in the PMR. | ||
""" | ||
|
||
import logging | ||
|
||
from profiles_management.helpers import profiles | ||
from profiles_management.helpers.k8s import client | ||
from profiles_management.list_stale import list_stale_profiles | ||
from profiles_management.pmr.classes import ProfilesManagementRepresentation | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def delete_stale_profiles(pmr: ProfilesManagementRepresentation): | ||
"""Delete all profiles that exist in the cluster but do not belong in a given PMR. | ||
Args: | ||
pmr: The ProfilesManagementRepresentation expressing what Profiles and contributors | ||
should exist in the cluster. | ||
""" | ||
stale_profiles = list_stale_profiles(pmr) | ||
log.info("Deleting all stale Profiles.") | ||
for existing_profile in stale_profiles.values(): | ||
profiles.remove_profile(existing_profile, client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Module responsible for listing "stale" Profiles based on a PMR. | ||
In this context, a "stale" Profile is a Profile that exists in the cluster but doesn't belong | ||
in the PMR. | ||
""" | ||
|
||
import logging | ||
|
||
from lightkube.generic_resource import GenericGlobalResource | ||
|
||
from profiles_management.helpers.k8s import client, get_name | ||
from profiles_management.helpers.profiles import list_profiles | ||
from profiles_management.pmr.classes import ProfilesManagementRepresentation | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def list_stale_profiles(pmr: ProfilesManagementRepresentation) -> dict[str, GenericGlobalResource]: | ||
"""Find all profiles that exist in the cluster but do not belong in a given PMR. | ||
Args: | ||
pmr: The ProfilesManagementRepresentation expressing what Profiles and contributors | ||
should exist in the cluster. | ||
Returns: | ||
The profiles that exist in the cluster but are not part of the given PMR. | ||
""" | ||
log.info("Fetching all Profiles in the cluster") | ||
existing_profiles: dict[str, GenericGlobalResource] = {} | ||
for profile in list_profiles(client): | ||
existing_profiles[get_name(profile)] = profile | ||
|
||
stale_profiles: dict[str, GenericGlobalResource] = {} | ||
for profile_name, existing_profile in existing_profiles.items(): | ||
if not pmr.has_profile(profile_name): | ||
logging.info( | ||
"Profile %s not in PMR. Adding it to the list of stale Profiles.", profile_name | ||
) | ||
stale_profiles[profile_name] = existing_profile | ||
return stale_profiles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
tests/integration/profiles_management/test_delete_stale_profiles.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import logging | ||
|
||
import pytest | ||
from lightkube import Client | ||
|
||
from profiles_management.delete_stale import delete_stale_profiles | ||
from profiles_management.helpers.profiles import list_profiles | ||
from profiles_management.pmr import classes | ||
from tests.integration.profiles_management.helpers import profiles | ||
|
||
log = logging.getLogger(__name__) | ||
client = Client(field_manager="profiles-automator-lightkube") | ||
|
||
TESTS_YAMLS_PATH = "tests/integration/profiles_management/yamls" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_delete_stale_profiles(deploy_profiles_controller, lightkube_client: Client): | ||
await deploy_profiles_controller | ||
|
||
namespace = "test" | ||
context = {"namespace": namespace} | ||
|
||
profile_path = TESTS_YAMLS_PATH + "/profile.yaml" | ||
|
||
# Load and apply all objects from files | ||
profile_contents = profiles.load_profile_from_file(profile_path, context) | ||
|
||
log.info("Creating Profile and waiting for Namespace to be created...") | ||
profiles.apply_profile(profile_contents, lightkube_client) | ||
|
||
# Create the PMR, which should not contain the above test profile | ||
pmr = classes.ProfilesManagementRepresentation() | ||
|
||
log.info( | ||
"Running delete_stale_profiles() which should delete all Profiles we created earlier." | ||
) | ||
delete_stale_profiles(pmr) | ||
|
||
# Check that the iterator returns no elements | ||
assert all(False for _ in list_profiles(client)) |
47 changes: 47 additions & 0 deletions
47
tests/integration/profiles_management/test_list_stale_profiles.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import logging | ||
|
||
import pytest | ||
from lightkube import Client | ||
from lightkube.generic_resource import GenericGlobalResource | ||
|
||
from profiles_management.helpers.k8s import get_name | ||
from profiles_management.helpers.profiles import list_profiles | ||
from profiles_management.list_stale import list_stale_profiles | ||
from profiles_management.pmr import classes | ||
from tests.integration.profiles_management.helpers import profiles | ||
|
||
log = logging.getLogger(__name__) | ||
client = Client(field_manager="profiles-automator-lightkube") | ||
|
||
TESTS_YAMLS_PATH = "tests/integration/profiles_management/yamls" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_list_stale_profiles(deploy_profiles_controller, lightkube_client: Client): | ||
await deploy_profiles_controller | ||
|
||
namespace = "test" | ||
context = {"namespace": namespace} | ||
|
||
profile_path = TESTS_YAMLS_PATH + "/profile.yaml" | ||
|
||
# Load and apply all objects from files | ||
profile_contents = profiles.load_profile_from_file(profile_path, context) | ||
|
||
log.info("Creating Profile and waiting for Namespace to be created...") | ||
profile = profiles.apply_profile(profile_contents, lightkube_client) | ||
|
||
existing_profiles: dict[str, GenericGlobalResource] = {} | ||
for profile in list_profiles(client): | ||
existing_profiles[get_name(profile)] = profile | ||
|
||
# Create the PMR, which should not contain the above test profile | ||
pmr = classes.ProfilesManagementRepresentation() | ||
|
||
log.info("Running list_stale_profiles() which should return all Profiles we created earlier.") | ||
stale_profiles = list_stale_profiles(pmr) | ||
|
||
assert existing_profiles == stale_profiles | ||
|
||
log.info("Removing test Profile and resources in it.") | ||
profiles.remove_profile(profile, lightkube_client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters