From 5a16d806756079fb0239320ad6f7d5472bd185a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=90=AF=E8=88=AA?= <101104760+ZhangSetSail@users.noreply.github.com> Date: Mon, 9 Jan 2023 19:55:21 +0800 Subject: [PATCH] perf: adjust port creation (#1503) --- api/handler/application_handler.go | 2 +- api/handler/resource_import.go | 2 +- api/handler/service.go | 4 +- worker/appm/conversion/gateway.go | 66 ++++++++++++------------------ worker/appm/f/function.go | 2 +- 5 files changed, 32 insertions(+), 44 deletions(-) diff --git a/api/handler/application_handler.go b/api/handler/application_handler.go index dfc7a7552..3abf12a8c 100644 --- a/api/handler/application_handler.go +++ b/api/handler/application_handler.go @@ -414,7 +414,7 @@ func (a *ApplicationAction) checkPorts(appID string, ports []*model.AppPort) err } for _, port := range servicesPorts { // check if the port is as same as the one in request - if _, ok := key2ports[port.ServiceID+strconv.Itoa(port.ContainerPort)]; !ok { + if svc, ok := key2ports[port.ServiceID+strconv.Itoa(port.ContainerPort)]; !ok && port.ServiceID != svc.ServiceID { logrus.Errorf("kubernetes service name(%s) already exists", port.K8sServiceName) return bcode.ErrK8sServiceNameExists } diff --git a/api/handler/resource_import.go b/api/handler/resource_import.go index 80a933527..06a7c9200 100644 --- a/api/handler/resource_import.go +++ b/api/handler/resource_import.go @@ -359,7 +359,7 @@ func (c *clusterAction) createPort(ports []model.PortManagement, service *dbmode vpD.Name = port.Name vpD.Protocol = port.Protocol vpD.PortAlias = fmt.Sprintf("%v%v", strings.ToUpper(portAlias), port.Port) - vpD.K8sServiceName = fmt.Sprintf("%v-%v", service.ServiceAlias, port.Port) + vpD.K8sServiceName = service.ServiceAlias portVar = append(portVar, &vpD) } if err := db.GetManager().TenantServicesPortDao().CreateOrUpdatePortsInBatch(portVar); err != nil { diff --git a/api/handler/service.go b/api/handler/service.go index ce745d3a2..54154976b 100644 --- a/api/handler/service.go +++ b/api/handler/service.go @@ -1228,7 +1228,7 @@ func (s *ServiceAction) CreatePorts(tenantID, serviceID string, vps *api_model.S tx.Rollback() return err } - if port != nil { + if port != nil && port.ServiceID != serviceID { tx.Rollback() return bcode.ErrK8sServiceNameExists } @@ -1333,7 +1333,7 @@ func (s *ServiceAction) PortVar(action, tenantID, serviceID string, vps *api_mod tx.Rollback() return err } - if port != nil && vpD.K8sServiceName != vp.K8sServiceName { + if port != nil && vpD.K8sServiceName != vp.K8sServiceName && port.ServiceID != serviceID { tx.Rollback() return bcode.ErrK8sServiceNameExists } diff --git a/worker/appm/conversion/gateway.go b/worker/appm/conversion/gateway.go index a25611c85..0bbac1570 100644 --- a/worker/appm/conversion/gateway.go +++ b/worker/appm/conversion/gateway.go @@ -146,16 +146,12 @@ func (a *AppServiceBuild) Build() (*v1.K8sResources, error) { var services []*corev1.Service var ingresses []interface{} var secrets []*corev1.Secret + var innerService []*model.TenantServicesPort if len(ports) > 0 { for i := range ports { port := ports[i] if *port.IsInnerService { - switch a.appService.GovernanceMode { - case model.GovernanceModeKubernetesNativeService: - services = append(services, a.createKubernetesNativeService(port)) - default: - services = append(services, a.createInnerService(port)) - } + innerService = append(innerService, port) } if *port.IsOuterService { service := a.createOuterService(port) @@ -176,7 +172,9 @@ func (a *AppServiceBuild) Build() (*v1.K8sResources, error) { } } } - + if len(innerService) > 0 { + services = append(services, a.createInnerService(innerService)) + } // build stateful service if a.replicationType == model.TypeStatefulSet { services = append(services, a.createStatefulService(ports)) @@ -385,7 +383,7 @@ func (a *AppServiceBuild) BuildOnPort(p int, isOut bool) (*corev1.Service, error } if port != nil { if !isOut && *port.IsInnerService { - return a.createInnerService(port), nil + return a.createInnerService([]*model.TenantServicesPort{port}), nil } if isOut && *port.IsOuterService { return a.createOuterService(port), nil @@ -403,47 +401,37 @@ func (a *AppServiceBuild) createServiceAnnotations() map[string]string { return annotations } -func (a *AppServiceBuild) createKubernetesNativeService(port *model.TenantServicesPort) *corev1.Service { - svc := a.createInnerService(port) - svc.Name = port.K8sServiceName - if svc.Name == "" { - svc.Name = fmt.Sprintf("%s-%d", a.appService.GetK8sWorkloadName(), port.ContainerPort) - } - return svc -} - -func (a *AppServiceBuild) createInnerService(port *model.TenantServicesPort) *corev1.Service { +func (a *AppServiceBuild) createInnerService(ports []*model.TenantServicesPort) *corev1.Service { var service corev1.Service - service.Name = port.K8sServiceName - if service.Name == "" { - service.Name = fmt.Sprintf("%s-%d-%d", a.appService.GetK8sWorkloadName(), port.ID, port.ContainerPort) - } + service.Name = ports[0].K8sServiceName service.Namespace = a.appService.GetNamespace() service.Labels = a.appService.GetCommonLabels(map[string]string{ - "service_type": "inner", - "name": a.service.ServiceAlias + "Service", - "port_protocol": port.Protocol, - "service_port": strconv.Itoa(port.ContainerPort), - "version": a.service.DeployVersion, + "service_type": "inner", + "name": a.service.ServiceAlias + "Service", + "version": a.service.DeployVersion, }) if a.service.Replicas <= 1 { service.Labels["rainbond.com/tolerate-unready-endpoints"] = "true" } service.Annotations = a.createServiceAnnotations() - var servicePort corev1.ServicePort - if port.Protocol == "udp" { - servicePort.Protocol = "UDP" - } else { - servicePort.Protocol = "TCP" - } - servicePort.Name = generateSVCPortName(port.Protocol, port.ContainerPort) - servicePort.TargetPort = intstr.FromInt(port.ContainerPort) - servicePort.Port = int32(port.MappingPort) - if servicePort.Port == 0 { - servicePort.Port = int32(port.ContainerPort) + var servicePorts []corev1.ServicePort + for _, port := range ports { + var servicePort corev1.ServicePort + if port.Protocol == "udp" { + servicePort.Protocol = "UDP" + } else { + servicePort.Protocol = "TCP" + } + servicePort.Name = generateSVCPortName(port.Protocol, port.ContainerPort) + servicePort.TargetPort = intstr.FromInt(port.ContainerPort) + servicePort.Port = int32(port.MappingPort) + if servicePort.Port == 0 { + servicePort.Port = int32(port.ContainerPort) + } + servicePorts = append(servicePorts, servicePort) } spec := corev1.ServiceSpec{ - Ports: []corev1.ServicePort{servicePort}, + Ports: servicePorts, } if a.appService.ServiceKind != model.ServiceKindThirdParty { spec.Selector = map[string]string{"name": a.service.ServiceAlias} diff --git a/worker/appm/f/function.go b/worker/appm/f/function.go index 584032264..5b63a7861 100644 --- a/worker/appm/f/function.go +++ b/worker/appm/f/function.go @@ -200,7 +200,7 @@ func ensureService(new *corev1.Service, clientSet kubernetes.Interface) error { func persistUpdate(service *corev1.Service, clientSet kubernetes.Interface) error { var err error for i := 0; i < clientRetryCount; i++ { - _, err = clientSet.CoreV1().Services(service.Namespace).UpdateStatus(context.Background(), service, metav1.UpdateOptions{}) + _, err = clientSet.CoreV1().Services(service.Namespace).Update(context.Background(), service, metav1.UpdateOptions{}) if err == nil { return nil }