-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Runtime Submission Context (#39)
The submission context infers submitter and submitter's system information at runtime and attaches these to the `k8s_annotations`. We dropped submission of environment information due to security concerns.
- Loading branch information
Showing
4 changed files
with
92 additions
and
5 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,81 @@ | ||
import logging | ||
import platform | ||
import subprocess | ||
from dataclasses import asdict, dataclass, field | ||
from typing import Any, Callable, TypeVar, overload | ||
|
||
T = TypeVar("T") | ||
TDefault = TypeVar("TDefault") | ||
|
||
|
||
@overload | ||
def _maybe(fn: Callable[..., T]) -> T | None: ... | ||
|
||
|
||
@overload | ||
def _maybe(fn: Callable[..., T], default_value: TDefault) -> T | TDefault: ... | ||
|
||
|
||
def _maybe( | ||
fn: Callable[..., T], default_value: TDefault | None = None | ||
) -> T | TDefault | None: | ||
try: | ||
return fn() | ||
except: # noqa E722 | ||
logging.debug( | ||
f"Failed to execute {fn.__name__}, using fallback {default_value}" | ||
) | ||
return default_value | ||
|
||
|
||
def get_git_config_info(item: str) -> str: | ||
return _maybe( | ||
lambda: subprocess.check_output( | ||
["git", "config", item], encoding="utf-8" | ||
).strip(), | ||
"Unknown", | ||
) | ||
|
||
|
||
def get_git_username() -> str: | ||
return get_git_config_info("user.name") | ||
|
||
|
||
def get_git_user_email() -> str: | ||
return get_git_config_info("user.email") | ||
|
||
|
||
def determine_platform_info() -> dict[str, Any]: | ||
skip = ["system_alias"] | ||
return { | ||
name: _maybe(fn) | ||
for name, fn in platform.__dict__.items() | ||
if callable(fn) and not name.startswith("_") and name not in skip | ||
} | ||
|
||
|
||
@dataclass | ||
class SubmitterInformation: | ||
username: str = field(default_factory=lambda: get_git_config_info("user.name")) | ||
email: str = field(default_factory=lambda: get_git_config_info("user.email")) | ||
|
||
|
||
@dataclass | ||
class SubmissionContext: | ||
submitter: SubmitterInformation = field(default_factory=SubmitterInformation) | ||
platform_info: dict[str, Any] = field(default_factory=determine_platform_info) | ||
|
||
def to_dict(self) -> dict[str, Any]: | ||
return { | ||
"submitter": asdict(self.submitter), | ||
"platform": self.platform_info, | ||
} | ||
|
||
def resolve(self) -> dict[str, str]: | ||
result = {} | ||
for key, value in self.to_dict().items(): | ||
if isinstance(value, dict): | ||
result.update({f"{key}.{k}": str(v) for k, v in value.items()}) | ||
else: | ||
result[key] = str(value) | ||
return result |
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