-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
217 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import pluggy | ||
|
||
project_name = "duetector.injector" | ||
hookimpl = pluggy.HookimplMarker(project_name) |
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
Empty file.
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,58 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
from collections import namedtuple | ||
|
||
from duetector.config import Configuable | ||
|
||
|
||
class NamespaceMixin: | ||
pass | ||
|
||
|
||
class CgroupMixin: | ||
pass | ||
|
||
|
||
class Injector(Configuable): | ||
""" | ||
A base class for all Injectors. | ||
Default config scope is ``Injector.{class_name}``. | ||
subclass should override ``inject`` method and ``super`` it. | ||
User should call Injector() directly to Injector data, | ||
""" | ||
|
||
default_config = { | ||
"disabled": False, | ||
} | ||
""" | ||
Default config for ``Injector``. | ||
""" | ||
|
||
@property | ||
def config_scope(self): | ||
""" | ||
Config scope for current Injector. | ||
""" | ||
return self.__class__.__name__ | ||
|
||
@property | ||
def disabled(self): | ||
""" | ||
If current Injector is disabled. | ||
""" | ||
return self.config.disabled | ||
|
||
def inject(self, data: namedtuple) -> namedtuple: | ||
""" | ||
Implement this method to patch ``data`` | ||
""" | ||
return data | ||
|
||
def __call__(self, data: namedtuple) -> namedtuple | None: | ||
if self.disabled: | ||
return data | ||
return self.inject(data) |
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,11 @@ | ||
from duetector.extension.injector import hookimpl | ||
from duetector.injectors.base import Injector | ||
|
||
|
||
class DockerInjector(Injector): | ||
pass | ||
|
||
|
||
@hookimpl | ||
def init_injector(config=None): | ||
return DockerInjector(config=config) |
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,11 @@ | ||
from duetector.extension.injector import hookimpl | ||
from duetector.injectors.base import Injector | ||
|
||
|
||
class K8SInjector(Injector): | ||
pass | ||
|
||
|
||
@hookimpl | ||
def init_injector(config=None): | ||
return K8SInjector(config=config) |
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,4 @@ | ||
# Expose for plugin system | ||
from . import docker, k8s | ||
|
||
registers = [docker, k8s] |
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,68 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
from typing import Any | ||
|
||
import pluggy | ||
|
||
import duetector.injectors.register | ||
from duetector.extension.injector import project_name | ||
from duetector.injectors.base import Injector | ||
from duetector.log import logger | ||
from duetector.managers.base import Manager | ||
|
||
PROJECT_NAME = project_name #: Default project name for pluggy | ||
hookspec = pluggy.HookspecMarker(PROJECT_NAME) | ||
|
||
|
||
@hookspec | ||
def init_injector(config) -> Injector | None: | ||
""" | ||
Initialize Injector from config | ||
None means the Injector is not available | ||
Also the Injector can be disabled by config, Manager will discard disabled Injector | ||
""" | ||
|
||
|
||
class InjectorManager(Manager): | ||
""" | ||
Manager for all Injectors. | ||
Injectors are initialized from config, and can be ``disabled`` by config. | ||
""" | ||
|
||
config_scope = "injector" | ||
""" | ||
Config scope for ``InjectorManager``. | ||
""" | ||
|
||
def __init__(self, config: dict[str, Any] | None = None, *args, **kwargs): | ||
super().__init__(config, *args, **kwargs) | ||
|
||
self.pm = pluggy.PluginManager(PROJECT_NAME) | ||
self.pm.add_hookspecs(sys.modules[__name__]) | ||
if self.include_extension: | ||
self.pm.load_setuptools_entrypoints(PROJECT_NAME) | ||
self.register(duetector.injectors.register) | ||
|
||
def init(self, ignore_disabled=True, *args, **kwargs) -> list[Injector]: | ||
""" | ||
Initialize all Injectors from config. | ||
Args: | ||
ignore_disabled: Ignore disabled Injectors | ||
""" | ||
if self.disabled: | ||
logger.info("InjectorManager disabled.") | ||
return [] | ||
objs = [] | ||
for f in self.pm.hook.init_injector(config=self.config._config_dict): | ||
if not f: | ||
continue | ||
if f.disabled and ignore_disabled: | ||
logger.info(f"Injector {f.__class__.__name__} is disabled") | ||
continue | ||
|
||
objs.append(f) | ||
|
||
return objs |
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
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
Oops, something went wrong.