示例#1
0
def _load_incluster_config():
    incluster_config.load_incluster_config()
    config = Configuration()

    namespace_path = join(dirname(incluster_config.SERVICE_CERT_FILENAME),
                          'namespace')
    with open(namespace_path) as fd:
        config.namespace = fd.read().strip()

    # [todo] is there a better way to do this?
    with open('/etc/resolv.conf') as fd:
        data = fd.read().strip().split('\n')
        config.dns_domain = [
            line for line in data if line.startswith('search')
        ][0].split()[3]

    Configuration.set_default(config)
    return Configuration._default
示例#2
0
def _load_kube_config(*args, **kwargs):
    try:
        kube_config.load_kube_config()
    except kubernetes.ConfigException as exc:
        if 'File does not exist' in exc.message:
            raise KubedConfigNotFound from exc
        elif 'Invalid kube-config file' in exc.message:
            raise KubedConfigInvalid from exc

    config = Configuration()

    config_path = kwargs.get(
        'config_file', expanduser(kube_config.KUBE_CONFIG_DEFAULT_LOCATION))
    with open(config_path) as fd:
        data = yaml.load(fd)

    current_ctx = data['current-context']
    for context in data['contexts']:
        if context['name'] == current_ctx:
            config.namespace = context['context']['namespace']
    config.dns_domain = os.getenv('KUBE_DOMAIN', 'cluster.local')
    Configuration.set_default(config)
    return Configuration._default