<commit_before>#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

"""
VNC Ingress management for kubernetes
"""

import uuid

from config_db import *
from loadbalancer import *
from vnc_api.vnc_api import *

from cfgm_common import importutils

class VncIngress(object):
    def __init__(self, args=None, vnc_lib=None, \
                 label_cache=None, logger=None, kube=None):
        self._args = args
        self._kube = kube
        self._vnc_lib = vnc_lib
        self._vn_obj = None
        self._fip_pool_obj = None
        self.service_lb_mgr = importutils.import_object(
            'kube_manager.vnc.loadbalancer.ServiceLbManager', vnc_lib, logger)
        self.service_ll_mgr = importutils.import_object(
            'kube_manager.vnc.loadbalancer.ServiceLbListenerManager', vnc_lib, logger)
        self.service_lb_pool_mgr = importutils.import_object(
            'kube_manager.vnc.loadbalancer.ServiceLbPoolManager', vnc_lib, logger)
        self.service_lb_member_mgr = importutils.import_object(
            'kube_manager.vnc.loadbalancer.ServiceLbMemberManager', vnc_lib, logger)

    def _get_project(self, namespace):
        proj_fq_name = ['default-domain', namespace]
        try:
            proj_obj = self._vnc_lib.project_read(fq_name=proj_fq_name)
        except NoIdError:
            return None
        return proj_obj

    def _get_network(self):
        if self._vn_obj:
            return self._vn_obj
        vn_fq_name = ['default-domain', 'default', 'cluster-network']
        try:
            vn_obj = self._vnc_lib.virtual_network_read(fq_name=vn_fq_name)
        except NoIdError:
            return None
        self._vn_obj = vn_obj
        return vn_obj

    def _get_public_fip_pool(self):
        if self._fip_pool_obj:
            return self._fip_pool_obj
        fip_pool_fq_name = ['default-domain', 'default', '__public__', '__fip_pool_public__']
        try:
            fip_pool_obj = self._vnc_lib.floating_ip_pool_read(fq_name=fip_pool_fq_name)
        except NoIdError:
            return None
        self._fip_pool_obj = fip_pool_obj
        return fip_pool_obj

    def _get_floating_ip(self, name, proj_obj, vmi_obj=None):
        fip_refs = vmi_obj.get_floating_ip_back_refs()
        for ref in fip_refs or []:
            fip = FloatingIpKM.get(ref['uuid'])
            if fip:
                return fip
            else:
                break
        fip_pool = self._get_public_fip_pool()
        if fip_pool is None:
            return None
        fip_obj = FloatingIp(name + "-public-ip", fip_pool)
        fip_obj.set_project(proj_obj)
        if vmi_obj:
            fip_obj.set_virtual_machine_interface(vmi_obj)
        self._vnc_lib.floating_ip_create(fip_obj)
        fip = FloatingIpKM.locate(fip_obj.uuid)
        return fip

    def _allocate_floating_ip(self, lb_obj, name, proj_obj):
        vmi_id = lb_obj.virtual_machine_interface_refs[0]['uuid']
        vmi_obj = self._vnc_lib.virtual_machine_interface_read(id=vmi_id)
        if vmi_obj is None:
            return None
        fip = self._get_floating_ip(name, proj_obj, vmi_obj)
        return fip

    def _deallocate_floating_ip(self, lb):
        vmi_id = lb.virtual_machine_interfaces.pop()
        vmi = VirtualMachineInterfaceKM.get(vmi_id)
        if vmi is None:
            return
        fip_list = vmi.floating_ips.copy()
        for fip_id in fip_list or []:
            fip_obj = self._vnc_lib.floating_ip_read(id=fip_id)
            fip_obj.set_virtual_machine_interface_list([])
            self._vnc_lib.floating_ip_update(fip_obj)
            self._vnc_lib.floating_ip_delete(id=fip_obj.uuid)
            FloatingIpKM.delete(fip_obj.uuid)

    def _vnc_create_member(self, pool, address, port, annotations):
        pool_obj = self.service_lb_pool_mgr.read(pool.uuid)
        member_obj = self.service_lb_member_mgr.create(pool_obj,
                          address, port, annotations)
        return member_obj

    def _vnc_create_pool(self, namespace, ll, port, lb_algorithm, annotations):
        proj_obj = self._get_project(namespace)
        ll_obj = self.service_ll_mgr.read(ll.uuid)
        pool_obj = self.service_lb_pool_mgr.create(ll_obj, proj_obj, \
                                            port, lb_algorithm, annotations)
        return pool_obj

    def _vnc_create_listeners(self, namespace, lb, port):
        proj_obj = self._get_project(namespace)
        lb_obj = self.service_lb_mgr.read(lb.uuid)
        ll_obj = self.service_ll_mgr.create(lb_obj, proj_obj, port)
        return ll_obj

    def _vnc_create_lb(self, uid, name, namespace):
        lb_provider = 'opencontrail'
        proj_obj = self._get_project(namespace)
        vn_obj = self._get_network()
        if proj_obj is None or vn_obj is None:
            return None

        vip_address = None
        lb_obj = self.service_lb_mgr.create(lb_provider, vn_obj, uid,
                      name, proj_obj, vip_address)
        if lb_obj:
            self._allocate_floating_ip(lb_obj, name, proj_obj)

        return lb_obj

    def _vnc_delete_member(self, member_id):<commit_msg>support to publish ingress vips to kube-api<commit_after>#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

"""
VNC Ingress management for kubernetes
"""

import uuid

from config_db import *
from loadbalancer import *
from vnc_api.vnc_api import *

from cfgm_common import importutils

class VncIngress(object):
    def __init__(self, args=None, vnc_lib=None, \
                 label_cache=None, logger=None, kube=None):
        self._args = args
        self._kube = kube
        self._vnc_lib = vnc_lib
        self._vn_obj = None
        self._fip_pool_obj = None
        self.service_lb_mgr = importutils.import_object(
            'kube_manager.vnc.loadbalancer.ServiceLbManager', vnc_lib, logger)
        self.service_ll_mgr = importutils.import_object(
            'kube_manager.vnc.loadbalancer.ServiceLbListenerManager', vnc_lib, logger)
        self.service_lb_pool_mgr = importutils.import_object(
            'kube_manager.vnc.loadbalancer.ServiceLbPoolManager', vnc_lib, logger)
        self.service_lb_member_mgr = importutils.import_object(
            'kube_manager.vnc.loadbalancer.ServiceLbMemberManager', vnc_lib, logger)

    def _get_project(self, namespace):
        proj_fq_name = ['default-domain', namespace]
        try:
            proj_obj = self._vnc_lib.project_read(fq_name=proj_fq_name)
        except NoIdError:
            return None
        return proj_obj

    def _get_network(self):
        if self._vn_obj:
            return self._vn_obj
        vn_fq_name = ['default-domain', 'default', 'cluster-network']
        try:
            vn_obj = self._vnc_lib.virtual_network_read(fq_name=vn_fq_name)
        except NoIdError:
            return None
        self._vn_obj = vn_obj
        return vn_obj

    def _get_public_fip_pool(self):
        if self._fip_pool_obj:
            return self._fip_pool_obj
        fip_pool_fq_name = ['default-domain', 'default', '__public__', '__fip_pool_public__']
        try:
            fip_pool_obj = self._vnc_lib.floating_ip_pool_read(fq_name=fip_pool_fq_name)
        except NoIdError:
            return None
        self._fip_pool_obj = fip_pool_obj
        return fip_pool_obj

    def _get_floating_ip(self, name, proj_obj, vmi_obj=None):
        fip_refs = vmi_obj.get_floating_ip_back_refs()
        for ref in fip_refs or []:
            fip = FloatingIpKM.get(ref['uuid'])
            if fip:
                return fip
            else:
                break
        fip_pool = self._get_public_fip_pool()
        if fip_pool is None:
            return None
        fip_obj = FloatingIp(name + "-public-ip", fip_pool)
        fip_obj.set_project(proj_obj)
        if vmi_obj:
            fip_obj.set_virtual_machine_interface(vmi_obj)
        self._vnc_lib.floating_ip_create(fip_obj)
        fip = FloatingIpKM.locate(fip_obj.uuid)
        return fip

    def _allocate_floating_ip(self, lb_obj, name, proj_obj):
        vmi_id = lb_obj.virtual_machine_interface_refs[0]['uuid']
        vmi_obj = self._vnc_lib.virtual_machine_interface_read(id=vmi_id)
        if vmi_obj is None:
            return None
        fip = self._get_floating_ip(name, proj_obj, vmi_obj)
        return fip

    def _deallocate_floating_ip(self, lb):
        vmi_id = lb.virtual_machine_interfaces.pop()
        vmi = VirtualMachineInterfaceKM.get(vmi_id)
        if vmi is None:
            return
        fip_list = vmi.floating_ips.copy()
        for fip_id in fip_list or []:
            fip_obj = self._vnc_lib.floating_ip_read(id=fip_id)
            fip_obj.set_virtual_machine_interface_list([])
            self._vnc_lib.floating_ip_update(fip_obj)
            self._vnc_lib.floating_ip_delete(id=fip_obj.uuid)
            FloatingIpKM.delete(fip_obj.uuid)

    def _vnc_create_member(self, pool, address, port, annotations):
        pool_obj = self.service_lb_pool_mgr.read(pool.uuid)
        member_obj = self.service_lb_member_mgr.create(pool_obj,
                          address, port, annotations)
        return member_obj

    def _vnc_create_pool(self, namespace, ll, port, lb_algorithm, annotations):
        proj_obj = self._get_project(namespace)
        ll_obj = self.service_ll_mgr.read(ll.uuid)
        pool_obj = self.service_lb_pool_mgr.create(ll_obj, proj_obj, \
                                            port, lb_algorithm, annotations)
        return pool_obj

    def _vnc_create_listeners(self, namespace, lb, port):
        proj_obj = self._get_project(namespace)
        lb_obj = self.service_lb_mgr.read(lb.uuid)
        ll_obj = self.service_ll_mgr.create(lb_obj, proj_obj, port)
        return ll_obj

    def _vnc_create_lb(self, uid, name, namespace):
        lb_provider = 'opencontrail'
        proj_obj = self._get_project(namespace)
        vn_obj = self._get_network()
        if proj_obj is None or vn_obj is None:
            return None

        vip_address = None
        lb_obj = self.service_lb_mgr.create(lb_provider, vn_obj, uid,
                      name, proj_obj, vip_address)
        if lb_obj:
            vip_info = {}
            vip_info['clusterIP'] = lb_obj._loadbalancer_properties.vip_address
            fip_obj = self._allocate_floating_ip(lb_obj, name, proj_obj)
            if fip_obj:
                vip_info['externalIP'] = fip_obj.address
            patch = {'metadata': {'annotations': vip_info}}
            self._kube.patch_resource("ingresses", name, \
                                      patch, namespace, beta=True)

        return lb_obj

    def _vnc_delete_member(self, member_id):
{'fraction_non_alphanumeric': 0.0812704343764596, 'fraction_numerical': 0.0009341429238673517, 'mean_word_length': 2.429212043561819, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6269637', 'n_tokens_mistral': 3689, 'n_tokens_neox': 3585, 'n_words': 628}