Ejemplo n.º 1
0
    def update_lbaas_sg(self, service, sgs):
        LOG.debug('Setting SG for LBaaS VIP port')

        svc_namespace = service['metadata']['namespace']
        svc_name = service['metadata']['name']
        svc_ports = service['spec']['ports']

        lbaas_name = "%s/%s" % (svc_namespace, svc_name)

        endpoints_link = utils.get_endpoints_link(service)
        k8s = clients.get_kubernetes_client()
        endpoint = k8s.get(endpoints_link)

        lbaas = utils.get_lbaas_state(endpoint)
        if not lbaas:
            return

        lbaas_obj = lbaas.loadbalancer
        lbaas_obj.security_groups = sgs

        utils.set_lbaas_state(endpoint, lbaas)

        for port in svc_ports:
            port_protocol = port['protocol']
            lbaas_port = port['port']
            target_port = port['targetPort']
            sg_rule_name = "%s:%s:%s" % (lbaas_name, port_protocol, lbaas_port)

            self._apply_members_security_groups(lbaas_obj, lbaas_port,
                                                target_port, port_protocol,
                                                sg_rule_name, sgs)
Ejemplo n.º 2
0
    def on_present(self, endpoints):
        lbaas_spec = self._get_lbaas_spec(endpoints)
        if self._should_ignore(endpoints, lbaas_spec):
            if not (self._is_validendpoints(endpoints)):
                reason = 'Endpoints not valid'
            elif not lbaas_spec:
                reason = 'No lbaas_spec'
            elif not self._has_pods(endpoints):
                reason = 'No valid endpoints'
            elif not self._svc_handler_annotations_updated(
                    endpoints, lbaas_spec):
                reason = 'Not updated'
            LOG.debug("Ignoring Kubernetes endpoints %s for reason: %s",
                      endpoints['metadata']['name'], reason)
            return

        lbaas_state = utils.get_lbaas_state(endpoints)
        if not lbaas_state:
            lbaas_state = obj_lbaas.LBaaSState()

        if self._sync_lbaas_members(endpoints, lbaas_state, lbaas_spec):
            # Note(yboaron) For LoadBalancer services, we should allocate FIP,
            # associate it to LB VIP and update K8S service status
            if lbaas_state.service_pub_ip_info is None:
                service_pub_ip_info = (
                    self._drv_service_pub_ip.acquire_service_pub_ip_info(
                        lbaas_spec.type, lbaas_spec.lb_ip,
                        lbaas_spec.project_id,
                        lbaas_state.loadbalancer.port_id))
                if service_pub_ip_info:
                    self._drv_service_pub_ip.associate_pub_ip(
                        service_pub_ip_info, lbaas_state.loadbalancer.port_id)
                    lbaas_state.service_pub_ip_info = service_pub_ip_info
                    self._update_lb_status(
                        endpoints, lbaas_state.service_pub_ip_info.ip_addr)
            # REVISIT(ivc): since _sync_lbaas_members is responsible for
            # creating all lbaas components (i.e. load balancer, listeners,
            # pools, members), it is currently possible for it to fail (due
            # to invalid Kuryr/K8s/Neutron configuration, e.g. Members' IPs
            # not belonging to configured Neutron subnet or Service IP being
            # in use by gateway or VMs) leaving some Neutron entities without
            # properly updating annotation. Some sort of failsafe mechanism is
            # required to deal with such situations (e.g. cleanup, or skip
            # failing items, or validate configuration) to prevent annotation
            # being out of sync with the actual Neutron state.
            try:
                utils.set_lbaas_state(endpoints, lbaas_state)
            except k_exc.K8sResourceNotFound:
                # Note(yboaron) It's impossible to store neutron resources
                # in K8S object since object was deleted. In that case
                # we should rollback all neutron resources.
                LOG.debug("LoadBalancerHandler failed to store Openstack "
                          "resources in K8S object (not found)")
                self.on_deleted(endpoints, lbaas_state)
Ejemplo n.º 3
0
    def _ensure_release_lbaas(self, lb_obj, svc=None):
        attempts = 0
        deadline = 0
        retry = True
        timeout = config.CONF.kubernetes.watch_retry_timeout
        while retry:
            try:
                if attempts == 1:
                    deadline = time.time() + timeout
                if (attempts > 0
                        and utils.exponential_sleep(deadline, attempts) == 0):
                    LOG.error("Failed releasing lbaas '%s': deadline exceeded",
                              lb_obj.name)
                    return
                self._drv_lbaas.release_loadbalancer(lb_obj)
                retry = False
            except k_exc.ResourceNotReady:
                LOG.debug(
                    "Attempt (%s) of loadbalancer release %s failed."
                    " A retry will be triggered.", attempts, lb_obj.name)
                attempts += 1
                retry = True
        if svc:
            endpoints_link = utils.get_endpoints_link(svc)
            k8s = clients.get_kubernetes_client()
            try:
                endpoints = k8s.get(endpoints_link)
            except k_exc.K8sResourceNotFound:
                LOG.debug("Endpoint not Found.")
                return

            lbaas = utils.get_lbaas_state(endpoints)
            if lbaas:
                lbaas.loadbalancer = None
                lbaas.pools = []
                lbaas.listeners = []
                lbaas.members = []
                # NOTE(ltomasbo): give some extra time to ensure the Load
                # Balancer VIP is also released
                time.sleep(1)
                utils.set_lbaas_state(endpoints, lbaas)
Ejemplo n.º 4
0
    def update_lbaas_sg(self, service, sgs):
        LOG.debug('Setting SG for LBaaS VIP port')

        svc_namespace = service['metadata']['namespace']
        svc_name = service['metadata']['name']
        svc_ports = service['spec'].get('ports', [])

        lbaas_name = "%s/%s" % (svc_namespace, svc_name)

        endpoints_link = utils.get_endpoints_link(service)
        k8s = clients.get_kubernetes_client()
        endpoint = k8s.get(endpoints_link)

        lbaas = utils.get_lbaas_state(endpoint)
        if not lbaas:
            LOG.debug('Endpoint not yet annotated with lbaas state.')
            raise k_exc.ResourceNotReady(svc_name)

        lbaas_obj = lbaas.loadbalancer
        lbaas_obj.security_groups = sgs

        utils.set_lbaas_state(endpoint, lbaas)

        lsnr_ids = {(l.protocol, l.port): l.id for l in lbaas.listeners}

        for port in svc_ports:
            port_protocol = port['protocol']
            lbaas_port = port['port']
            target_port = port['targetPort']
            sg_rule_name = "%s:%s:%s" % (lbaas_name, port_protocol, lbaas_port)
            listener_id = lsnr_ids.get((port_protocol, target_port))
            if listener_id is None:
                LOG.warning("There is no listener associated to the protocol "
                            "%s and port %s. Skipping", port_protocol,
                            lbaas_port)
                continue
            self._apply_members_security_groups(lbaas_obj, lbaas_port,
                                                target_port, port_protocol,
                                                sg_rule_name, listener_id, sgs)