Exemplo n.º 1
0
def _put(kind, *items):
    """Send items of given kind to kubernetes"""
    api = KubeQuery()
    for item in items:
        try:
            response = api._put([kind, item['metadata']['name']],
                                json.dumps(item),
                                ns=item['metadata']['namespace'])
            if response['kind'].lower() == 'status':
                print('Warning: {kind} {response}'.format(kind, response))
        except KeyError:
            pass
Exemplo n.º 2
0
def _replace_labels(replace_map, old_label, new_label):
    api = KubeQuery()

    for kind in ('pods', 'replicationcontrollers', 'services'):
        items = api._get([kind])['items']
        _add_label(replace_map,
                   items,
                   using_label=old_label,
                   add_label=new_label)
        _put(kind, *items)

    for kind in ('services', 'replicationcontrollers', 'pods'):
        items = api._get([kind])['items']
        _del_label(replace_map, items, del_label=old_label, if_label=new_label)
        _put(kind, *items)
Exemplo n.º 3
0
def upgrade(upd, with_testing, *args, **kwargs):
    pod_collection = PodCollection()
    for pod_dict in pod_collection.get(as_json=False):
        pod = pod_collection._get_by_id(pod_dict['id'])
        db_config = get_pod_config(pod.id)
        cluster_ip = db_config.pop('clusterIP', None)
        if cluster_ip is None:
            service_name = db_config.get('service')
            if service_name is None:
                continue
            namespace = db_config.get('namespace') or pod.id
            service = KubeQuery().get(['services', service_name], ns=namespace)
            cluster_ip = service.get('spec', {}).get('clusterIP')
            if cluster_ip is not None:
                db_config['podIP'] = cluster_ip
        replace_pod_config(pod, db_config)
Exemplo n.º 4
0
    def _create_or_update_ingress_config():
        client = configmap.ConfigMapClient(KubeQuery())
        try:
            client.get(
                constants.KUBERDOCK_INGRESS_CONFIG_MAP_NAME,
                namespace=constants.KUBERDOCK_INGRESS_CONFIG_MAP_NAMESPACE)

            client.patch(
                constants.KUBERDOCK_INGRESS_CONFIG_MAP_NAME,
                data={'server-name-hash-bucket-size': '128'},
                namespace=constants.KUBERDOCK_INGRESS_CONFIG_MAP_NAMESPACE)

        except configmap.ConfigMapNotFound:
            ingress.create_ingress_nginx_configmap()
Exemplo n.º 5
0
def _recreate_ingress_pod_if_needed():
    kd_user = User.get_internal()
    ingress_pod = Pod.filter_by(name=KUBERDOCK_INGRESS_POD_NAME,
                                owner=kd_user).first()
    if ingress_pod or BaseDomain.query.first():
        PodCollection(kd_user).delete(ingress_pod.id, force=True)
        default_backend_pod = Pod.filter_by(name=KUBERDOCK_BACKEND_POD_NAME,
                                            owner=kd_user).first()
        if not default_backend_pod:
            raise Exception(
                'Nginx ingress controller pod exists, but default backend pod '
                'is not found. Something wrong. Please contact support to get '
                'help.')
        PodCollection(kd_user).delete(default_backend_pod.id, force=True)
        c = ConfigMapClient(KubeQuery())
        try:
            c.delete(name=KUBERDOCK_INGRESS_CONFIG_MAP_NAME,
                     namespace=KUBERDOCK_INGRESS_CONFIG_MAP_NAMESPACE)
        except ConfigMapNotFound:
            pass
        sleep(30)  # TODO: Workaround. Remove it when AC-5470 will be fixed
        ingress.prepare_ip_sharing()
Exemplo n.º 6
0
def get_pods_by_drives():
    pods = Pod.query.filter(Pod.status != 'deleted')
    pods_by_drives = {}
    for pod in pods:
        k8s_pods = KubeQuery()._get(['pods'], ns=pod.id).get('items', [])
        if not k8s_pods:
            continue
        status = k8s_pods[0].get('status', {}).get('phase', '').lower()
        if status not in (POD_STATUSES.running, POD_STATUSES.pending):
            continue
        try:
            config = json.loads(pod.config)
        except (TypeError, ValueError):
            continue
        volumes = config.get('volumes', None) or []
        for volume in volumes:
            if 'rbd' in volume:
                drive_name = volume['rbd']['image']
            elif 'awsElasticBlockStore' in volume:
                drive_name = volume['awsElasticBlockStore']['image']
            else:
                continue
            pods_by_drives[drive_name] = pod
    return pods_by_drives
Exemplo n.º 7
0
def upgrade(upd, with_testing, *args, **kwargs):
    upgrade_db()

    # === 00124_update.py ===
    # Move index file of k8s2etcd service from / to /var/lib/kuberdock
    try:
        stop_service(u124_service_name)
        if os.path.isfile(u124_old) and not os.path.isfile(u124_new):
            shutil.move(u124_old, u124_new)
    finally:
        start_service(u124_service_name)

    # === 00126_update.py ===

    pod_collection = PodCollection()
    for pod_dict in pod_collection.get(as_json=False):
        pod = pod_collection._get_by_id(pod_dict['id'])
        db_config = get_pod_config(pod.id)
        cluster_ip = db_config.pop('clusterIP', None)
        if cluster_ip is None:
            service_name = db_config.get('service')
            if service_name is None:
                continue
            namespace = db_config.get('namespace') or pod.id
            service = KubeQuery().get(['services', service_name],
                                      ns=namespace)
            cluster_ip = service.get('spec', {}).get('clusterIP')
            if cluster_ip is not None:
                db_config['podIP'] = cluster_ip
        replace_pod_config(pod, db_config)

    # === 00127_update.py ===

    upd.print_log('Upgrading menu...')
    MenuItemRole.query.delete()
    MenuItem.query.delete()
    Menu.query.delete()
    generate_menu()

    # === 00130_update.py ===

    upd.print_log('Update permissions...')
    Permission.query.delete()
    Resource.query.delete()
    add_permissions()
    db.session.commit()

    # === 00135_update.py ===
    # upd.print_log('Changing session_data schema...')
    # upgrade_db(revision='220dacf65cba')


    # === 00137_update.py ===
    upd.print_log('Upgrading db...')
    # upgrade_db(revision='3c832810a33c')
    upd.print_log('Raise max kubes to 64')
    max_kubes = 'max_kubes_per_container'
    old_value = SystemSettings.get_by_name(max_kubes)
    if old_value == '10':
        SystemSettings.set_by_name(max_kubes, 64)
    upd.print_log('Update kubes')
    small = Kube.get_by_name('Small')
    standard = Kube.get_by_name('Standard')
    if small:
        small.cpu = 0.12
        small.name = 'Tiny'
        small.memory = 64
        if small.is_default and standard:
            small.is_default = False
            standard.is_default = True
        small.save()
    if standard:
        standard.cpu = 0.25
        standard.memory = 128
        standard.save()
    high = Kube.get_by_name('High memory')
    if high:
        high.cpu = 0.25
        high.memory = 256
        high.disk_space = 3
        high.save()

    # === 00138_update.py ===

    if not (CEPH or AWS):
        upgrade_localstorage_paths(upd)

    # === added later ===

    secret_key = SystemSettings.query.filter(
        SystemSettings.name == 'sso_secret_key').first()
    if not secret_key.value:
        secret_key.value = randstr(16)
    secret_key.description = (
    'Used for Single sign-on. Must be shared between '
    'Kuberdock and billing system or other 3rd party '
    'application.')
    db.session.commit()

    upd.print_log('Close all sessions...')
    close_all_sessions()