def _template_to_ports(self, component, ports): if not ports: return [], [] new_ports = [] gateway_rules = [] for port in ports: component_port = port["container_port"] k8s_service_name = port.get("k8s_service_name") if port.get( "k8s_service_name") else component.service_alias + "-" + str(component_port) try: port_service.check_k8s_service_name(component.tenant_id, k8s_service_name) except ErrK8sServiceNameExists: k8s_service_name = k8s_service_name + "-" + make_uuid()[:4] except AbortRequest: k8s_service_name = component.service_alias + "-" + str(component_port) port = TenantServicesPort( tenant_id=component.tenant_id, service_id=component.service_id, container_port=int(component_port), mapping_port=int(component_port), lb_mapping_port=0, protocol=port.get("protocol", "tcp"), port_alias=port.get("port_alias", ""), is_inner_service=port.get("is_inner_service", False), is_outer_service=port.get("is_outer_service", False), k8s_service_name=k8s_service_name, ) new_ports.append(port) gateway_rule = self._create_default_gateway_rule(component, port) if gateway_rule: gateway_rules.append(gateway_rule) return new_ports, gateway_rules
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)
def _create_component(snap): # component component = TenantServiceInfo(**snap["service_base"]) # component source component_source = ServiceSourceInfo(**snap["service_source"]) # environment envs = [TenantServiceEnvVar(**env) for env in snap["service_env_vars"]] # ports ports = [TenantServicesPort(**port) for port in snap["service_ports"]] # service_extend_method extend_info = ServiceExtendMethod(**snap["service_extend_method"]) # volumes volumes = [TenantServiceVolume(**volume) for volume in snap["service_volumes"]] # configuration files config_files = [TenantServiceConfigurationFile(**config_file) for config_file in snap["service_config_file"]] # probe probes = [ServiceProbe(**probe) for probe in snap["service_probes"]] # monitors monitors = [ServiceMonitor(**monitor) for monitor in snap["service_monitors"]] # graphs graphs = [ComponentGraph(**graph) for graph in snap["component_graphs"]] return Component( component=component, component_source=component_source, envs=envs, ports=ports, volumes=volumes, config_files=config_files, probe=probes[0] if probes else None, extend_info=extend_info, monitors=monitors, graphs=graphs, plugin_deps=[], )
def ports(self, service_ports): port_repo.delete_service_port(self.tenant.tenant_id, self.service.service_id) if service_ports: ports = [] for item in service_ports: item.pop("ID") ports.append(TenantServicesPort(**item)) port_repo.bulk_create(ports)
def _create_component(self, snap): # component component = TenantServiceInfo(**snap["service_base"]) # component source component_source = ServiceSourceInfo(**snap["service_source"]) # environment envs = [TenantServiceEnvVar(**env) for env in snap["service_env_vars"]] # ports ports = [TenantServicesPort(**port) for port in snap["service_ports"]] # service_extend_method extend_info = None if snap.get("service_extend_method"): extend_info = ServiceExtendMethod( **snap.get("service_extend_method")) # volumes volumes = [ TenantServiceVolume(**volume) for volume in snap["service_volumes"] ] # configuration files config_files = [ TenantServiceConfigurationFile(**config_file) for config_file in snap["service_config_file"] ] # probe probes = [ServiceProbe(**probe) for probe in snap["service_probes"]] # monitors monitors = [ ServiceMonitor(**monitor) for monitor in snap["service_monitors"] ] # graphs graphs = [ ComponentGraph(**graph) for graph in snap["component_graphs"] ] service_labels = [ ServiceLabels(**label) for label in snap["service_labels"] ] cpt = Component( component=component, component_source=component_source, envs=envs, ports=ports, volumes=volumes, config_files=config_files, probes=probes, extend_info=extend_info, monitors=monitors, graphs=graphs, plugin_deps=[], labels=service_labels, support_labels=self.support_labels, ) cpt.action_type = snap.get("action_type", ActionType.BUILD.value) return cpt
def _update_ports(self, ports): if ports is None: return add = ports.get("add", []) for port in add: # Optimization: do not update port data iteratively self._update_port_data(port) new_port = TenantServicesPort(**port) new_port.service_id = self.component.component_id self.ports.append(new_port) old_ports = {port.container_port: port for port in self.ports} upd = ports.get("upd", []) for port in upd: old_port = old_ports.get(port["container_port"]) old_port.protocol = port["protocol"] old_port.port_alias = port["port_alias"] if not old_port.is_inner_service: old_port.is_inner_service = port["is_inner_service"] if not old_port.is_outer_service: old_port.is_outer_service = port["is_outer_service"]
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)
def create_tenant_service_port(self, **kwargs): tenant_service_port = TenantServicesPort(**kwargs) tenant_service_port.save() return tenant_service_port
def init(self): self.sources = [ Tenants(), TenantRegionInfo(), TenantRegionResource(), ServiceInfo(), TenantServiceInfo(), TenantServiceInfoDelete(), TenantServiceLog(), TenantServiceRelation(), TenantServiceEnv(), TenantServiceAuth(), TenantServiceExtendMethod(), ServiceDomain(), ServiceDomainCertificate(), PermRelService(), PermRelTenant(), PhoneCode(), TenantServiceL7Info(), TenantServiceEnvVar(), TenantServicesPort(), TenantServiceMountRelation(), TenantServiceVolume(), TenantServiceConfigurationFile(), ServiceGroup(), ServiceGroupRelation(), ImageServiceRelation(), ComposeServiceRelation(), ServiceRule(), ServiceRuleHistory(), ServiceCreateStep(), ServiceProbe(), ConsoleConfig(), TenantEnterprise(), TenantEnterpriseToken(), TenantServiceGroup(), ServiceTcpDomain(), ThirdPartyServiceEndpoints(), ServiceWebhooks(), GatewayCustomConfiguration(), ConsoleSysConfig(), RainbondCenterApp(), RainbondCenterAppInherit(), RainbondCenterPlugin(), ServiceShareRecord(), EnterpriseUserPerm(), TenantUserRole(), TenantUserPermission(), TenantUserRolePermission(), PermGroup(), ServiceRelPerms(), AppExportRecord(), UserMessage(), AppImportRecord(), GroupAppBackupRecord(), GroupAppMigrateRecord(), GroupAppBackupImportRecord(), Applicants(), DeployRelation(), ServiceBuildSource(), TenantServiceBackup(), AppUpgradeRecord(), ServiceUpgradeRecord(), RegionConfig(), CloundBangImages(), Announcement(), ]