class KubernetesApiMock: """Mocks the bits of the Kubernetes API that we use. This simulates both the CoreV1Api and the CustomObjectsApi (but only portions of each). """ def __init__(self) -> None: self.objects: List[Dict[str, Any]] = [] self.custom: List[Dict[str, Any]] = [] self.api_client = ApiClient() async def create_object(self, kind: str, body: Any) -> bool: body_as_dict = self.api_client.sanitize_for_serialization(body) self.objects.append(body_as_dict) return True async def create_namespaced_custom_object( self, group: str, version: str, namespace: str, plural: str, body: Dict[str, Any], ) -> None: crd_info = CRDParser.from_crd_body(body) assert crd_info.group == group assert crd_info.version == version assert crd_info.plural == plural assert body["metadata"]["namespace"] == namespace self.custom.append(body) def shared_client_mock(self, typ: str) -> Any: return self.api_client if typ == "ApiClient" else self
def make_pod(pod_name: str = 'browser-xtc9s') -> V1Pod: resp = mock.Mock() with open(os.path.join(os.path.dirname(__file__), 'pod_fixture.json')) as data: resp.data = data.read().replace('{pod_name}', pod_name) return ApiClient().deserialize(resp, 'V1Pod')
async def new_client_from_config(config_file=None, context=None, persist_config=True): """Loads configuration the same as load_kube_config but returns an ApiClient to be used with any API object. This will allow the caller to concurrently talk with multiple clusters.""" client_config = type.__call__(Configuration) await load_kube_config(config_file=config_file, context=context, client_configuration=client_config, persist_config=persist_config) return ApiClient(configuration=client_config)
async def kubernetes_controller(settings: Optional[str]) -> None: if settings: config_dependency.set_settings_path(settings) config = await config_dependency() logger = structlog.get_logger(config.safir.logger_name) logger.debug("Starting") async with ComponentFactory.standalone() as factory: await initialize_kubernetes() async with ApiClient() as api_client: kubernetes_service = factory.create_kubernetes_service(api_client) logger.debug("Updating all service tokens") await kubernetes_service.update_service_tokens() logger.debug("Starting Kubernetes watcher") queue = await kubernetes_service.start_watcher() logger.debug("Starting continuous processing") await kubernetes_service.update_service_tokens_from_queue(queue)
async def update_service_tokens(settings: Optional[str]) -> None: """Update service tokens stored in Kubernetes secrets.""" if settings: config_dependency.set_settings_path(settings) config = await config_dependency() logger = structlog.get_logger(config.safir.logger_name) async with ComponentFactory.standalone() as factory: await initialize_kubernetes() async with ApiClient() as api_client: kubernetes_service = factory.create_kubernetes_service(api_client) try: logger.debug("Updating all service tokens") await kubernetes_service.update_service_tokens() except KubernetesError as e: msg = "Failed to update service token secrets" logger.error(msg, error=str(e)) sys.exit(1)
def __init__(self) -> None: self.objects: List[Dict[str, Any]] = [] self.custom: List[Dict[str, Any]] = [] self.api_client = ApiClient()