def __save_port(self, tenant, service, tenant_service_ports, lb_port_info):
     port_list = []
     for port in tenant_service_ports:
         port.pop("ID")
         new_port = TenantServicesPort(**port)
         new_port.service_id = service.service_id
         new_port.tenant_id = tenant.tenant_id
         if lb_port_info:
             lb_port = lb_port_info.get(str(port["container_port"]), None)
             if lb_port is not None:
                 new_port.lb_mapping_port = lb_port
         port_list.append(new_port)
     if port_list:
         TenantServicesPort.objects.bulk_create(port_list)
Example #2
0
    def __save_port(self, tenant, service, tenant_service_ports):
        port_list = []
        for port in tenant_service_ports:
            port.pop("ID")
            new_port = TenantServicesPort(**port)
            new_port.service_id = service.service_id
            new_port.tenant_id = tenant.tenant_id
            port_list.append(new_port)
        if port_list:
            TenantServicesPort.objects.bulk_create(port_list)
            region = region_repo.get_region_by_region_name(
                service.service_region)
            # 为每一个端口状态是打开的生成默认域名
            for port in port_list:
                if port.is_outer_service:
                    if port.protocol == "http":
                        service_domains = domain_repo.get_service_domain_by_container_port(
                            service.service_id, port.container_port)
                        # 在domain表中保存数据
                        if service_domains:
                            for service_domain in service_domains:
                                service_domain.is_outer_service = True
                                service_domain.save()
                        else:
                            # 在service_domain表中保存数据
                            service_id = service.service_id
                            service_name = service.service_alias
                            container_port = port.container_port
                            domain_name = str(container_port) + "." + str(
                                service_name) + "." + str(
                                    tenant.tenant_name) + "." + str(
                                        region.httpdomain)
                            create_time = datetime.datetime.now().strftime(
                                '%Y-%m-%d %H:%M:%S')
                            protocol = "http"
                            http_rule_id = make_uuid(domain_name)
                            tenant_id = tenant.tenant_id
                            service_alias = service.service_cname
                            region_id = region.region_id
                            domain_repo.create_service_domains(
                                service_id, service_name, domain_name,
                                create_time, container_port, protocol,
                                http_rule_id, tenant_id, service_alias,
                                region_id)
                            # 给数据中心发请求添加默认域名
                            data = dict()
                            data["domain"] = domain_name
                            data["service_id"] = service.service_id
                            data["tenant_id"] = tenant.tenant_id
                            data["tenant_name"] = tenant.tenant_name
                            data["protocol"] = protocol
                            data["container_port"] = int(container_port)
                            data["http_rule_id"] = http_rule_id
                            try:
                                region_api.bind_http_domain(
                                    service.service_region, tenant.tenant_name,
                                    data)
                            except Exception as e:
                                logger.exception(e)
                                domain_repo.delete_http_domains(http_rule_id)
                                continue

                    else:
                        service_tcp_domains = tcp_domain.get_service_tcp_domains_by_service_id_and_port(
                            service.service_id, port.container_port)
                        if service_tcp_domains:
                            for service_tcp_domain in service_tcp_domains:
                                # 改变tcpdomain表中状态
                                service_tcp_domain.is_outer_service = True
                                service_tcp_domain.save()
                        else:
                            # ip+port
                            # 在service_tcp_domain表中保存数据
                            res, data = region_api.get_port(
                                region.region_name, tenant.tenant_name)
                            if int(res.status) != 200:
                                continue
                            end_point = str(region.tcpdomain) + ":" + str(
                                data["bean"])
                            service_id = service.service_id
                            service_name = service.service_alias
                            create_time = datetime.datetime.now().strftime(
                                '%Y-%m-%d %H:%M:%S')
                            container_port = port.container_port
                            protocol = port.protocol
                            service_alias = service.service_cname
                            tcp_rule_id = make_uuid(end_point)
                            tenant_id = tenant.tenant_id
                            region_id = region.region_id
                            tcp_domain.create_service_tcp_domains(
                                service_id, service_name, end_point,
                                create_time, container_port, protocol,
                                service_alias, tcp_rule_id, tenant_id,
                                region_id)
                            # 默认ip不需要传给数据中心
                            # ip = end_point.split(":")[0]
                            port = end_point.split(":")[1]
                            data = dict()
                            data["service_id"] = service.service_id
                            data["container_port"] = int(container_port)
                            # data["ip"] = ip
                            data["port"] = int(port)
                            data["tcp_rule_id"] = tcp_rule_id
                            logger.debug(
                                '--------------------------------->{0}'.format(
                                    data["port"]))
                            try:
                                # 给数据中心传送数据添加策略
                                region_api.bindTcpDomain(
                                    service.service_region, tenant.tenant_name,
                                    data)
                            except Exception as e:
                                logger.exception(e)
                                tcp_domain.delete_tcp_domain(tcp_rule_id)
                                continue
    def __save_port(self,
                    region_name,
                    tenant,
                    service,
                    tenant_service_ports,
                    governance_mode,
                    tenant_service_env_vars,
                    sync_flag=False):
        port_2_envs = dict()
        for env in tenant_service_env_vars:
            container_port = env.get("container_port")
            if not container_port:
                continue
            envs = port_2_envs.get(container_port) if port_2_envs.get(
                container_port) else []
            envs.append(env)
            port_2_envs[container_port] = envs

        port_list = []
        for port in tenant_service_ports:
            port.pop("ID")
            k8s_service_name = port.get("k8s_service_name", "")
            if k8s_service_name:
                try:
                    port_repo.get_by_k8s_service_name(tenant.tenant_id,
                                                      k8s_service_name)
                    k8s_service_name += "-" + make_uuid()[-4:]
                    # update port if k8s_service_name has changed.
                    body = port
                    body["k8s_service_name"] = k8s_service_name
                    if sync_flag:
                        port_service.update_service_port(
                            tenant, region_name, service.service_alias, body)
                except TenantServicesPort.DoesNotExist:
                    pass
            new_port = TenantServicesPort(**port)
            new_port.service_id = service.service_id
            new_port.tenant_id = tenant.tenant_id
            new_port.k8s_service_name = port.get("k8s_service_name")
            port_list.append(new_port)

            # make sure the value of X_HOST env is correct
            envs = port_2_envs.get(port["container_port"])
            if envs:
                for env in envs:
                    if not env.get("container_port"
                                   ) or not env["attr_name"].endswith("_HOST"):
                        continue
                    origin_attr_value = env["attr_value"]
                    if governance_mode == GovernanceModeEnum.BUILD_IN_SERVICE_MESH.name:
                        env["attr_value"] = "127.0.0.1"
                    else:
                        env["attr_value"] = k8s_service_name
                    # update env if attr_value has changed.
                    if origin_attr_value != env["attr_value"] and sync_flag:
                        region_api.update_service_env(
                            region_name, tenant.tenant_name,
                            service.service_alias, {
                                "env_name": env["attr_name"],
                                "env_value": env["attr_value"]
                            })

        if port_list:
            TenantServicesPort.objects.bulk_create(port_list)
            region = region_repo.get_region_by_region_name(
                service.service_region)
            for port in port_list:
                if port.is_outer_service:
                    if port.protocol == "http":
                        service_domains = domain_repo.get_service_domain_by_container_port(
                            service.service_id, port.container_port)
                        # 在domain表中保存数据
                        if service_domains:
                            for service_domain in service_domains:
                                service_domain.is_outer_service = True
                                service_domain.save()
                        else:
                            # 在service_domain表中保存数据
                            service_id = service.service_id
                            service_name = service.service_alias
                            container_port = port.container_port
                            domain_name = str(container_port) + "." + str(
                                service_name) + "." + str(
                                    tenant.tenant_name) + "." + str(
                                        region.httpdomain)
                            create_time = datetime.datetime.now().strftime(
                                '%Y-%m-%d %H:%M:%S')
                            protocol = "http"
                            http_rule_id = make_uuid(domain_name)
                            tenant_id = tenant.tenant_id
                            service_alias = service.service_cname
                            region_id = region.region_id
                            domain_repo.create_service_domains(
                                service_id, service_name, domain_name,
                                create_time, container_port, protocol,
                                http_rule_id, tenant_id, service_alias,
                                region_id)

                    else:
                        service_tcp_domains = tcp_domain.get_service_tcp_domains_by_service_id_and_port(
                            service.service_id, port.container_port)
                        if service_tcp_domains:
                            for service_tcp_domain in service_tcp_domains:
                                # 改变tcpdomain表中状态
                                service_tcp_domain.is_outer_service = True
                                service_tcp_domain.save()
                        else:
                            # 在service_tcp_domain表中保存数据
                            res, data = region_api.get_port(
                                region.region_name, tenant.tenant_name, True)
                            if int(res.status) != 200:
                                continue
                            end_point = str(region.tcpdomain) + ":" + str(
                                data["bean"])
                            service_id = service.service_id
                            service_name = service.service_alias
                            create_time = datetime.datetime.now().strftime(
                                '%Y-%m-%d %H:%M:%S')
                            container_port = port.container_port
                            protocol = port.protocol
                            service_alias = service.service_cname
                            tcp_rule_id = make_uuid(end_point)
                            tenant_id = tenant.tenant_id
                            region_id = region.region_id
                            tcp_domain.create_service_tcp_domains(
                                service_id, service_name, end_point,
                                create_time, container_port, protocol,
                                service_alias, tcp_rule_id, tenant_id,
                                region_id)