Example #1
0
    def __init__(self):

        logger.debug('Creating KubernetesClient')

        config_file = os.getenv('KUBECONFIG', None)
        context = os.getenv('KUBECONTEXT', DEFAULT_CONTEXT)

        logger.debug('Configuration file is: ' +
                     os.getenv('KUBECONFIG', 'None'))
        logger.debug('Configuration context is: ' + context)

        list_kube_config_contexts = config.list_kube_config_contexts(
            config_file)
        print(list_kube_config_contexts)

        try:
            logger.debug('Trying to load config.load_incluster_config()')
            config.load_incluster_config()
        except Exception as e:
            s = str(e)
            logger.critical('Exception when config.load_incluster_config()',
                            exception=s)
            try:
                logger.debug('Trying to load config.load_kube_config()')
                config.load_kube_config(config_file, context)
            except Exception as e:
                s = str(e)
                logger.critical('Exception when config.load_kube_config()',
                                exception=s)

        # Clients
        self.ClientV1 = client.CoreV1Api()
        self.ExtensionsV1beta1Api = client.ExtensionsV1beta1Api()
Example #2
0
def main():
    level_str = os.getenv('LOG_LEVEL', 'WARNING').upper()
    format_str = os.getenv('LOG_FORMAT',
                           '%(asctime)s [%(levelname)s] %(message)s')
    console = logging.StreamHandler()
    console.setFormatter(RFC3339Formatter(format_str))
    LOGGER.addHandler(console)
    sleep_time = os.environ.get("SECONDS_BETWEEN_STREAMS", '30')
    sleep_time = int(sleep_time)
    try:
        logging.basicConfig(level=logging.getLevelName(level_str))
    except ValueError as err:
        LOGGER.error(err)
        sys.exit(1)

    try:
        config.load_kube_config()
    except (FileNotFoundError, ConfigException) as err:
        LOGGER.debug("Not able to use Kubeconfig: %s", err)
        try:
            config.load_incluster_config()
        except (FileNotFoundError, ConfigException) as err:
            LOGGER.error("Not able to use in-cluster config: %s", err)
            sys.exit(1)

    try:
        while True:
            hostess.Watcher(env=os.environ, config=configuration).execute()
            LOGGER.info("API closed connection, sleeping for %i seconds",
                        sleep_time)
            time.sleep(sleep_time)
    except RuntimeError as err:
        LOGGER.exception(err)
        sys.exit(1)
Example #3
0
    def __init__(self, org_name: str, course_id: str):
        """
        Helper class to launch grader notebooks within the kubernetes cluster

        Args:
          org_name: the organization name
          course_id: the course id

        Raises:
          ConfigException if the kubectl python client does not have a valid configuration set.
        """
        try:
            # try to load the cluster credentials
            # Configs can be set in Configuration class directly or using helper utility
            config.load_incluster_config()
        except ConfigException:
            # next method uses the KUBECONFIG env var by default
            config.load_kube_config()
        # Uncomment the following lines to enable debug logging
        self.apps_v1 = client.AppsV1Api()
        self.coreV1Api = client.CoreV1Api()
        self.course_id = course_id
        self.grader_name = f"grader-{self.course_id}"
        self.grader_token = token_hex(32)
        self.org_name = org_name

        # Course home directory, its parent should be the grader name
        self.course_dir = Path(
            f"{MNT_ROOT}/{self.org_name}/home/grader-{self.course_id}/{self.course_id}"
        )
        # set the exchange directory path
        self.exchange_dir = Path(EXCHANGE_MNT_ROOT, self.org_name, "exchange")
Example #4
0
 def create_secret_in_namespace_if_not_exist(self, payload, namespace):
     if self.in_cluster:
         config.load_incluster_config()
     else:
         config.load_kube_config(config_file="~/.kube/config")
     try:
         api_instance = client.CoreV1Api()
         api_instance.read_namespaced_secret(payload['metadata']['name'],
                                             namespace)
     except ApiException as e:
         if e.status == 404:
             try:
                 api_instance = client.CoreV1Api()
                 meta_data = client.V1ObjectMeta()
                 meta_data.name = payload['metadata']['name']
                 body = client.V1Secret(metadata=meta_data,
                                        data=payload['data'])
                 api_instance.create_namespaced_secret(namespace, body)
             except ApiException as create_e:
                 logger.error(
                     "Exception when calling CoreV1Api->create_namespaced_secret: %s\n"
                     % create_e)
                 sys.exit(1)
         else:
             logger.error(
                 "Exception when calling CoreV1Api->read_namespaced_secret: %s\n"
                 % e)
             sys.exit(1)
 def list_all_secrets_from_namespace(self, namespace):
     if self.in_cluster:
         config.load_incluster_config()
     else:
         config.load_kube_config(config_file="~/.kube/config")
     try:
         api_instance = client.CoreV1Api()
         api_response = api_instance.list_namespaced_secret(namespace)
         return api_response.items
     except ApiException as e:
         if e.status == 404:
             return []
         logger.error('Exception when calling CoreV1Api->list_namespaced_secret: %s\n' % e)
         sys.exit(1)
Example #6
0
 def replace_secret_in_namespace(self, payload, namespace):
     if self.in_cluster:
         config.load_incluster_config()
     else:
         config.load_kube_config(config_file="~/.kube/config")
     try:
         api_instance = client.CoreV1Api()
         meta_data = client.V1ObjectMeta()
         meta_data.name = payload['metadata']['name']
         body = client.V1Secret(metadata=meta_data, data=payload['data'])
         # don't use patch, which can't handle empty string: https://github.com/kubernetes/kubernetes/issues/37216
         api_instance.replace_namespaced_secret(payload['metadata']['name'],
                                                namespace, body)
     except ApiException as e:
         logger.error(
             "Exception when calling CoreV1Api->patch_namespaced_secret: %s\n"
             % e)
         sys.exit(1)
Example #7
0
 def create_group_if_not_exist(self, name):
     if self.in_cluster:
         config.load_incluster_config()
     else:
         config.load_kube_config(config_file="~/.kube/config")
     try:
         api_instance = client.CoreV1Api()
         api_instance.read_namespace(name)
     except ApiException as e:
         if e.status == 404:
             api_instance = client.CoreV1Api()
             meta_data = client.V1ObjectMeta()
             meta_data.name = name
             body = client.V1Namespace(metadata=meta_data)
             api_instance.create_namespace(body)
             return True
         logger.error("Failed to create namespace [{0}]".format(name))
         sys.exit(1)
     return False
Example #8
0
def load_kubernetes_config():
    """
    Loads kubernetes configuration in cluster or from file to be able to interact with kubernetes api
    :return:
    """
    config_loaded = False
    try:
        config.load_incluster_config()
        config_loaded = True
    except config.config_exception.ConfigException:
        logger.debug(
            "Unable to load in-cluster configuration; trying to load from Kube config file"
        )
        try:
            config.load_kube_config()
            config_loaded = True
        except (IOError, config.config_exception.ConfigException) as exc:
            logger.debug("Unable to load Kube config; reason={}".format(exc))

    if not config_loaded:
        logger.error("Unable to load in-cluster or Kube config")
        raise SystemExit(1)
    def __init__(self):
        config_loaded = False

        try:
            config.load_incluster_config()
            config_loaded = True
        except config.config_exception.ConfigException:
            logger.warn(
                "Unable to load in-cluster configuration; trying to load from Kube config file"
            )
            try:
                config.load_kube_config()
                config_loaded = True
            except (IOError, config.config_exception.ConfigException) as exc:
                logger.warn(
                    "Unable to load Kube config; reason={}".format(exc))

        if not config_loaded:
            logger.error("Unable to load in-cluster or Kube config")
            sys.exit(1)

        cli = client.CoreV1Api()
        cli.api_client.configuration.assert_hostname = False
        self.client = cli