Skip to content

Commit

Permalink
[resotoworker][feat] Expose list of loaded collectors (#1288)
Browse files Browse the repository at this point in the history
* [resotoworker][fix] Require a restart when the list of collectors changes

* [resotoworker][feat] Expose list of loaded collectors on /info
  • Loading branch information
lloesche committed Nov 17, 2022
1 parent 9a8e694 commit abde862
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions resotoworker/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
resotolib==3.0.0a0
tenacity==8.1.0
CherryPy==18.8.0
setuptools>=65.5.1 # not directly required, pinned by Snyk to avoid a vulnerability
21 changes: 20 additions & 1 deletion resotoworker/resotoworker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import sys
import threading
import cherrypy # type: ignore
import time
from functools import partial
from queue import Queue
Expand Down Expand Up @@ -124,7 +125,7 @@ def send_request(request: requests.Request) -> requests.Response:
"ssl_key": tls_data.key_path,
}
web_server = WebServer(
WebApp(mountpoint=Config.resotoworker.web_path),
WorkerWebApp(mountpoint=Config.resotoworker.web_path, plugin_loader=plugin_loader),
web_host=Config.resotoworker.web_host,
web_port=Config.resotoworker.web_port,
**web_server_args,
Expand Down Expand Up @@ -295,5 +296,23 @@ def add_args(arg_parser: ArgumentParser) -> None:
)


class WorkerWebApp(WebApp):
def __init__(self, *args, plugin_loader: PluginLoader, **kwargs) -> None: # type: ignore
super().__init__(*args, **kwargs)
self.plugin_loader = plugin_loader

@cherrypy.expose # type: ignore
@cherrypy.tools.json_out() # type: ignore
@cherrypy.tools.allow(methods=["GET"]) # type: ignore
def info(self) -> Dict[str, List[str]]:
active_collectors: List[str] = [
plugin.cloud for plugin in self.plugin_loader.plugins(PluginType.COLLECTOR) # type: ignore
]
all_collectors: List[str] = [
plugin.cloud for plugin in self.plugin_loader.all_plugins(PluginType.COLLECTOR) # type: ignore
]
return {"active_collectors": active_collectors, "all_collectors": all_collectors}


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion resotoworker/resotoworker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ResotoWorkerConfig:
kind: ClassVar[str] = "resotoworker"
collector: List[str] = field(
factory=lambda: ["example"],
metadata={"description": "List of collectors to run"},
metadata={"description": "List of collectors to run", "restart_required": True},
)
graph: str = field(
default="resoto",
Expand Down
6 changes: 5 additions & 1 deletion resotoworker/resotoworker/pluginloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@ def plugins(

return self._plugins.get(plugin_type, []) # type: ignore

def all_plugins(self) -> List[Union[Type[BasePlugin], Type[BaseActionPlugin], Type[BasePostCollectPlugin]]]:
def all_plugins(
self, plugin_type: Optional[PluginType] = None
) -> List[Union[Type[BasePlugin], Type[BaseActionPlugin], Type[BasePostCollectPlugin]]]:
if not self._initialized:
self.find_plugins()
if plugin_type is not None:
return self._plugins.get(plugin_type, []) # type: ignore
return [plugin for plugins in self._plugins.values() for plugin in plugins]

def add_plugin_args(self, arg_parser: ArgumentParser) -> None:
Expand Down

0 comments on commit abde862

Please sign in to comment.