Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Handler for the operator start/stop #59

Closed
nolar opened this issue May 10, 2019 · 1 comment
Closed

Handler for the operator start/stop #59

nolar opened this issue May 10, 2019 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@nolar
Copy link
Contributor

nolar commented May 10, 2019

To work properly with different Kubernetes client libraries, such as PyKube/PyKubeNG (#15), the authentication is needed.

One way to do this is on the module import level. Yet, this is wrong from the code structuring point of view.

It would be better to have the explicit event handler for the operator-wide (not just object-wide) events:

import kopf
import kubernetes
import pykube

config = None

@kopf.on.startup()
def auth_pykube(**_):
    global config
    try:
        config = pykube.KubeConfig.from_service_account()  # cluster env vars
    except FileNotFoundError:
        config = pykube.KubeConfig.from_file()  # developer's config files

@kopf.on.startup()
def auth_client(logger, **_):
    try:
        kubernetes.config.load_incluster_config()  # cluster env vars
        logger.debug("configured in cluster with service account")
    except kubernetes.config.ConfigException as e1:
        try:
            kubernetes.config.load_kube_config()  # developer's config files
            logger.debug("configured via kubeconfig file")
        except kubernetes.config.ConfigException as e2:
            raise Exception(f"Cannot authenticate neither in-cluster, nor via kubeconfig.")

@kopf.on.cleanup()
def purge_caches(**_):
    pass

It could be also used to start some background threads or async coroutines for monitoring the unrelated systems (e.g. API polling).

The handler should be called before any implicit login() or other API calls, so that nothing is implicitly configured by the framework. But after the logging is configured and registry is populated — so that the handler itself is logged, but it could adjust the logging, or the registry.

For the exit-handlers, perhaps the atexit stdlib can be used.

@nolar
Copy link
Contributor Author

nolar commented Nov 13, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant