perf: adjust port creation (#1503)

This commit is contained in:
张启航 2023-01-09 19:55:21 +08:00 committed by GitHub
parent 4de50acbc3
commit 5a16d80675
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 44 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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}

View File

@ -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
}