forked from LaconicNetwork/kompose
Create two svc if loadbalancer and use udp/tcp the same time
This commit is contained in:
parent
2dc6037cce
commit
c0f485b4e1
13
d.yaml
Normal file
13
d.yaml
Normal file
@ -0,0 +1,13 @@
|
||||
version: "2"
|
||||
services:
|
||||
nginx:
|
||||
image: nginx
|
||||
build: ./foobar
|
||||
ports:
|
||||
- "6060:6060/udp"
|
||||
- "5000:5000"
|
||||
cap_add:
|
||||
- ALL
|
||||
container_name: foobar
|
||||
labels:
|
||||
kompose.service.type: loadbalancer
|
||||
@ -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)
|
||||
|
||||
@ -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) {
|
||||
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)
|
||||
|
||||
@ -400,12 +400,20 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
|
||||
}
|
||||
|
||||
if o.PortsExist(service) {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
} else if service.ServiceType == "Headless" {
|
||||
svc := o.CreateHeadlessService(name, service, objects)
|
||||
objects = append(objects, svc)
|
||||
|
||||
2
script/test/fixtures/v2/docker-compose.yaml
vendored
2
script/test/fixtures/v2/docker-compose.yaml
vendored
@ -18,6 +18,8 @@ services:
|
||||
|
||||
redis:
|
||||
image: redis:3.0
|
||||
labels:
|
||||
kompose.service.type: loadbalancer
|
||||
ports:
|
||||
- "6379/tcp"
|
||||
- "1234:1235/udp"
|
||||
|
||||
37
script/test/fixtures/v2/output-k8s.json
vendored
37
script/test/fixtures/v2/output-k8s.json
vendored
@ -44,10 +44,13 @@
|
||||
"kind": "Service",
|
||||
"apiVersion": "v1",
|
||||
"metadata": {
|
||||
"name": "redis",
|
||||
"name": "redis-tcp",
|
||||
"creationTimestamp": null,
|
||||
"labels": {
|
||||
"io.kompose.service": "redis"
|
||||
"io.kompose.service": "redis-tcp"
|
||||
},
|
||||
"annotations": {
|
||||
"kompose.service.type": "loadbalancer"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
@ -56,7 +59,32 @@
|
||||
"name": "6379",
|
||||
"port": 6379,
|
||||
"targetPort": 6379
|
||||
}
|
||||
],
|
||||
"selector": {
|
||||
"io.kompose.service": "redis-tcp"
|
||||
},
|
||||
"type": "LoadBalancer"
|
||||
},
|
||||
"status": {
|
||||
"loadBalancer": {}
|
||||
}
|
||||
},
|
||||
{
|
||||
"kind": "Service",
|
||||
"apiVersion": "v1",
|
||||
"metadata": {
|
||||
"name": "redis-udp",
|
||||
"creationTimestamp": null,
|
||||
"labels": {
|
||||
"io.kompose.service": "redis-udp"
|
||||
},
|
||||
"annotations": {
|
||||
"kompose.service.type": "loadbalancer"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"ports": [
|
||||
{
|
||||
"name": "1234",
|
||||
"protocol": "UDP",
|
||||
@ -65,8 +93,9 @@
|
||||
}
|
||||
],
|
||||
"selector": {
|
||||
"io.kompose.service": "redis"
|
||||
}
|
||||
"io.kompose.service": "redis-udp"
|
||||
},
|
||||
"type": "LoadBalancer"
|
||||
},
|
||||
"status": {
|
||||
"loadBalancer": {}
|
||||
|
||||
37
script/test/fixtures/v2/output-os.json
vendored
37
script/test/fixtures/v2/output-os.json
vendored
@ -44,10 +44,13 @@
|
||||
"kind": "Service",
|
||||
"apiVersion": "v1",
|
||||
"metadata": {
|
||||
"name": "redis",
|
||||
"name": "redis-tcp",
|
||||
"creationTimestamp": null,
|
||||
"labels": {
|
||||
"io.kompose.service": "redis"
|
||||
"io.kompose.service": "redis-tcp"
|
||||
},
|
||||
"annotations": {
|
||||
"kompose.service.type": "loadbalancer"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
@ -56,7 +59,32 @@
|
||||
"name": "6379",
|
||||
"port": 6379,
|
||||
"targetPort": 6379
|
||||
}
|
||||
],
|
||||
"selector": {
|
||||
"io.kompose.service": "redis-tcp"
|
||||
},
|
||||
"type": "LoadBalancer"
|
||||
},
|
||||
"status": {
|
||||
"loadBalancer": {}
|
||||
}
|
||||
},
|
||||
{
|
||||
"kind": "Service",
|
||||
"apiVersion": "v1",
|
||||
"metadata": {
|
||||
"name": "redis-udp",
|
||||
"creationTimestamp": null,
|
||||
"labels": {
|
||||
"io.kompose.service": "redis-udp"
|
||||
},
|
||||
"annotations": {
|
||||
"kompose.service.type": "loadbalancer"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"ports": [
|
||||
{
|
||||
"name": "1234",
|
||||
"protocol": "UDP",
|
||||
@ -65,8 +93,9 @@
|
||||
}
|
||||
],
|
||||
"selector": {
|
||||
"io.kompose.service": "redis"
|
||||
}
|
||||
"io.kompose.service": "redis-udp"
|
||||
},
|
||||
"type": "LoadBalancer"
|
||||
},
|
||||
"status": {
|
||||
"loadBalancer": {}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user