Create two svc if loadbalancer and use udp/tcp the same time

This commit is contained in:
Hang Yan
2020-11-03 13:09:25 +08:00
committed by GitHub
parent 2dc6037cce
commit c0f485b4e1
7 changed files with 167 additions and 19 deletions
+27
View File
@@ -365,6 +365,33 @@ func (k *Kubernetes) PortsExist(service kobject.ServiceConfig) bool {
return len(service.Port) != 0
}
func (k *Kubernetes) CreateLBService(name string, service kobject.ServiceConfig, objects []runtime.Object) []*api.Service {
var svcs []*api.Service
tcpPorts, udpPorts := k.ConfigLBServicePorts(name, service)
if tcpPorts != nil {
svc := k.initSvcObject(name+"-tcp", service, tcpPorts)
svcs = append(svcs, svc)
}
if udpPorts != nil {
svc := k.initSvcObject(name+"-udp", service, udpPorts)
svcs = append(svcs, svc)
}
return svcs
}
func (k *Kubernetes) initSvcObject(name string, service kobject.ServiceConfig, ports []api.ServicePort) *api.Service {
svc := k.InitSvc(name, service)
svc.Spec.Ports = ports
svc.Spec.Type = api.ServiceType(service.ServiceType)
// Configure annotations
annotations := transformer.ConfigAnnotations(service)
svc.ObjectMeta.Annotations = annotations
return svc
}
// CreateService creates a k8s service
func (k *Kubernetes) CreateService(name string, service kobject.ServiceConfig, objects []runtime.Object) *api.Service {
svc := k.InitSvc(name, service)
+45 -5
View File
@@ -560,12 +560,45 @@ func (k *Kubernetes) ConfigPorts(name string, service kobject.ServiceConfig) []a
return ports
}
func (k *Kubernetes) ConfigLBServicePorts(name string, service kobject.ServiceConfig) ([]api.ServicePort, []api.ServicePort) {
var tcpPorts []api.ServicePort
var udpPorts []api.ServicePort
for _, port := range service.Port {
if port.HostPort == 0 {
port.HostPort = port.ContainerPort
}
var targetPort intstr.IntOrString
targetPort.IntVal = port.ContainerPort
targetPort.StrVal = strconv.Itoa(int(port.ContainerPort))
servicePort := api.ServicePort{
Name: strconv.Itoa(int(port.HostPort)),
Port: port.HostPort,
TargetPort: targetPort,
}
// If the default is already TCP, no need to include it.
if port.Protocol != api.ProtocolTCP {
servicePort.Protocol = port.Protocol
}
if port.Protocol == api.ProtocolTCP {
tcpPorts = append(tcpPorts, servicePort)
} else {
udpPorts = append(udpPorts, servicePort)
}
}
return tcpPorts, udpPorts
}
// ConfigServicePorts configure the container service ports.
func (k *Kubernetes) ConfigServicePorts(name string, service kobject.ServiceConfig) []api.ServicePort {
servicePorts := []api.ServicePort{}
seenPorts := make(map[int]struct{}, len(service.Port))
var servicePort api.ServicePort
log.Debugf("fuck ports: %+v", service.Port)
for _, port := range service.Port {
if port.HostPort == 0 {
port.HostPort = port.ContainerPort
@@ -1163,12 +1196,19 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
}
if k.PortsExist(service) {
svc := k.CreateService(name, service, objects)
objects = append(objects, svc)
if service.ExposeService != "" {
objects = append(objects, k.initIngress(name, service, svc.Spec.Ports[0].Port))
if service.ServiceType == "LoadBalancer" {
svcs := k.CreateLBService(name, service, objects)
for _, svc := range svcs {
objects = append(objects, svc)
}
} else {
svc := k.CreateService(name, service, objects)
objects = append(objects, svc)
if service.ExposeService != "" {
objects = append(objects, k.initIngress(name, service, svc.Spec.Ports[0].Port))
}
}
} else {
if service.ServiceType == "Headless" {
svc := k.CreateHeadlessService(name, service, objects)
+12 -4
View File
@@ -400,12 +400,20 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
}
if o.PortsExist(service) {
svc := o.CreateService(name, service, objects)
objects = append(objects, svc)
if service.ServiceType == "LoadBalancer" {
svcs := o.CreateLBService(name, service, objects)
for _, svc := range svcs {
objects = append(objects, svc)
}
} else {
svc := o.CreateService(name, service, objects)
objects = append(objects, svc)
if service.ExposeService != "" {
objects = append(objects, o.initRoute(name, service, svc.Spec.Ports[0].Port))
if service.ExposeService != "" {
objects = append(objects, o.initRoute(name, service, svc.Spec.Ports[0].Port))
}
}
} else if service.ServiceType == "Headless" {
svc := o.CreateHeadlessService(name, service, objects)
objects = append(objects, svc)