-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for resource_applied() callback in k8s glue
Add support for sending log events with k8s-provided timestamps Refactor env vars infrastructure
- Loading branch information
allegroai
committed
Nov 1, 2023
1 parent
d2384a9
commit 0131db8
Showing
11 changed files
with
354 additions
and
242 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 |
---|---|---|
@@ -1,69 +1,8 @@ | ||
import base64 | ||
from distutils.util import strtobool | ||
from typing import Union, Optional, Any, TypeVar, Callable, Tuple | ||
|
||
import six | ||
|
||
try: | ||
from typing import Text | ||
except ImportError: | ||
# windows conda-less hack | ||
Text = Any | ||
|
||
|
||
ConverterType = TypeVar("ConverterType", bound=Callable[[Any], Any]) | ||
|
||
|
||
def text_to_int(value, default=0): | ||
# type: (Any, int) -> int | ||
try: | ||
return int(value) | ||
except (ValueError, TypeError): | ||
return default | ||
|
||
|
||
def base64_to_text(value): | ||
# type: (Any) -> Text | ||
return base64.b64decode(value).decode("utf-8") | ||
|
||
|
||
def text_to_bool(value): | ||
# type: (Text) -> bool | ||
return bool(strtobool(value)) | ||
|
||
|
||
def safe_text_to_bool(value): | ||
# type: (Text) -> bool | ||
try: | ||
return text_to_bool(value) | ||
except ValueError: | ||
return bool(value) | ||
|
||
|
||
def any_to_bool(value): | ||
# type: (Optional[Union[int, float, Text]]) -> bool | ||
if isinstance(value, six.text_type): | ||
return text_to_bool(value) | ||
return bool(value) | ||
|
||
|
||
def or_(*converters, **kwargs): | ||
# type: (ConverterType, Tuple[Exception, ...]) -> ConverterType | ||
""" | ||
Wrapper that implements an "optional converter" pattern. Allows specifying a converter | ||
for which a set of exceptions is ignored (and the original value is returned) | ||
:param converters: A converter callable | ||
:param exceptions: A tuple of exception types to ignore | ||
""" | ||
# noinspection PyUnresolvedReferences | ||
exceptions = kwargs.get("exceptions", (ValueError, TypeError)) | ||
|
||
def wrapper(value): | ||
for converter in converters: | ||
try: | ||
return converter(value) | ||
except exceptions: | ||
pass | ||
return value | ||
|
||
return wrapper | ||
from clearml_agent.helper.environment.converters import ( | ||
base64_to_text, | ||
text_to_bool, | ||
text_to_int, | ||
safe_text_to_bool, | ||
any_to_bool, | ||
or_, | ||
) |
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,111 +1,6 @@ | ||
import abc | ||
from typing import Optional, Any, Tuple, Callable, Dict | ||
from clearml_agent.helper.environment import Entry, NotSet | ||
|
||
import six | ||
|
||
from .converters import any_to_bool | ||
|
||
try: | ||
from typing import Text | ||
except ImportError: | ||
# windows conda-less hack | ||
Text = Any | ||
|
||
|
||
NotSet = object() | ||
|
||
Converter = Callable[[Any], Any] | ||
|
||
|
||
@six.add_metaclass(abc.ABCMeta) | ||
class Entry(object): | ||
""" | ||
Configuration entry definition | ||
""" | ||
|
||
@classmethod | ||
def default_conversions(cls): | ||
# type: () -> Dict[Any, Converter] | ||
return { | ||
bool: any_to_bool, | ||
six.text_type: lambda s: six.text_type(s).strip(), | ||
} | ||
|
||
def __init__(self, key, *more_keys, **kwargs): | ||
# type: (Text, Text, Any) -> None | ||
""" | ||
:param key: Entry's key (at least one). | ||
:param more_keys: More alternate keys for this entry. | ||
:param type: Value type. If provided, will be used choosing a default conversion or | ||
(if none exists) for casting the environment value. | ||
:param converter: Value converter. If provided, will be used to convert the environment value. | ||
:param default: Default value. If provided, will be used as the default value on calls to get() and get_pair() | ||
in case no value is found for any key and no specific default value was provided in the call. | ||
Default value is None. | ||
:param help: Help text describing this entry | ||
""" | ||
self.keys = (key,) + more_keys | ||
self.type = kwargs.pop("type", six.text_type) | ||
self.converter = kwargs.pop("converter", None) | ||
self.default = kwargs.pop("default", None) | ||
self.help = kwargs.pop("help", None) | ||
|
||
def __str__(self): | ||
return str(self.key) | ||
|
||
@property | ||
def key(self): | ||
return self.keys[0] | ||
|
||
def convert(self, value, converter=None): | ||
# type: (Any, Converter) -> Optional[Any] | ||
converter = converter or self.converter | ||
if not converter: | ||
converter = self.default_conversions().get(self.type, self.type) | ||
return converter(value) | ||
|
||
def get_pair(self, default=NotSet, converter=None, value_cb=None): | ||
# type: (Any, Converter, Callable[[str, Any], None]) -> Optional[Tuple[Text, Any]] | ||
for key in self.keys: | ||
value = self._get(key) | ||
if value is NotSet: | ||
continue | ||
try: | ||
value = self.convert(value, converter) | ||
except Exception as ex: | ||
self.error("invalid value {key}={value}: {ex}".format(**locals())) | ||
break | ||
# noinspection PyBroadException | ||
try: | ||
if value_cb: | ||
value_cb(key, value) | ||
except Exception: | ||
pass | ||
return key, value | ||
|
||
result = self.default if default is NotSet else default | ||
return self.key, result | ||
|
||
def get(self, default=NotSet, converter=None, value_cb=None): | ||
# type: (Any, Converter, Callable[[str, Any], None]) -> Optional[Any] | ||
return self.get_pair(default=default, converter=converter, value_cb=value_cb)[1] | ||
|
||
def set(self, value): | ||
# type: (Any, Any) -> (Text, Any) | ||
# key, _ = self.get_pair(default=None, converter=None) | ||
for k in self.keys: | ||
self._set(k, str(value)) | ||
|
||
def _set(self, key, value): | ||
# type: (Text, Text) -> None | ||
pass | ||
|
||
@abc.abstractmethod | ||
def _get(self, key): | ||
# type: (Text) -> Any | ||
pass | ||
|
||
@abc.abstractmethod | ||
def error(self, message): | ||
# type: (Text) -> None | ||
pass | ||
__all__ = [ | ||
"Entry", | ||
"NotSet" | ||
] |
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,7 +1,11 @@ | ||
from clearml_agent.definitions import EnvironmentConfig | ||
from clearml_agent.helper.environment import EnvEntry | ||
|
||
ENV_START_AGENT_SCRIPT_PATH = EnvironmentConfig('CLEARML_K8S_GLUE_START_AGENT_SCRIPT_PATH') | ||
ENV_START_AGENT_SCRIPT_PATH = EnvEntry("CLEARML_K8S_GLUE_START_AGENT_SCRIPT_PATH", default="~/__start_agent__.sh") | ||
""" | ||
Script path to use when creating the bash script to run the agent inside the scheduled pod's docker container. | ||
Script will be appended to the specified file. | ||
""" | ||
|
||
ENV_DEFAULT_EXECUTION_AGENT_ARGS = EnvEntry("K8S_GLUE_DEF_EXEC_AGENT_ARGS", default="--full-monitoring --require-queue") | ||
ENV_POD_AGENT_INSTALL_ARGS = EnvEntry("K8S_GLUE_POD_AGENT_INSTALL_ARGS", default="", lstrip=False) | ||
ENV_POD_MONITOR_LOG_BATCH_SIZE = EnvEntry("K8S_GLUE_POD_MONITOR_LOG_BATCH_SIZE", default=5, converter=int) |
Oops, something went wrong.