This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support custom hooks for resource context (#64)
* add option for custom hook --resource-context-hook * provide example hook * no need to pass default args * cache objects from Kubernetes API (pods for PVC) * test get_hook_function
- Loading branch information
Showing
8 changed files
with
132 additions
and
12 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
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,30 @@ | ||
""" | ||
Example resource context hooks for Kubernetes Janitor. | ||
Usage: --resource-context-hook=kube_janitor.example_hooks.random_dice | ||
""" | ||
import logging | ||
import random | ||
from typing import Any | ||
from typing import Dict | ||
|
||
from pykube.objects import APIObject | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
CACHE_KEY = "random_dice" | ||
|
||
|
||
def random_dice(resource: APIObject, cache: Dict[str, Any]) -> Dict[str, Any]: | ||
"""Built-in example resource context hook to set ``_context.random_dice`` to a random dice value (1-6).""" | ||
|
||
# re-use any value from the cache to have only one dice roll per janitor run | ||
dice_value = cache.get(CACHE_KEY) | ||
|
||
if dice_value is None: | ||
# roll the dice | ||
dice_value = random.randint(1, 6) | ||
logger.debug(f"The random dice value is {dice_value}!") | ||
cache[CACHE_KEY] = dice_value | ||
|
||
return {"random_dice": dice_value} |
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
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 |
---|---|---|
@@ -1,6 +1,13 @@ | ||
import kube_janitor.example_hooks | ||
from kube_janitor.cmd import get_hook_function | ||
from kube_janitor.cmd import get_parser | ||
|
||
|
||
def test_parse_args(): | ||
parser = get_parser() | ||
parser.parse_args(["--dry-run", "--rules-file=/config/rules.yaml"]) | ||
|
||
|
||
def test_get_example_hook_function(): | ||
func = get_hook_function("kube_janitor.example_hooks.random_dice") | ||
assert func == kube_janitor.example_hooks.random_dice |
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