Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit e159e82

Browse files
committed
Support exposing ports and cross-service communication
Signed-off-by: Guillaume Tardif <[email protected]>
1 parent 657e894 commit e159e82

File tree

3 files changed

+130
-24
lines changed

3 files changed

+130
-24
lines changed

kube/charts/kubernetes/kube.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ import (
3333
"k8s.io/apimachinery/pkg/util/intstr"
3434
)
3535

36+
const (
37+
headlessPort = 55555
38+
headlessName = "headless"
39+
clusterIPHeadless = "None"
40+
)
41+
3642
//MapToKubernetesObjects maps compose project to Kubernetes objects
3743
func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object, error) {
3844
objects := map[string]runtime.Object{}
@@ -70,7 +76,12 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
7076

7177
func mapToService(project *types.Project, service types.ServiceConfig) *core.Service {
7278
ports := []core.ServicePort{}
79+
serviceType := core.ServiceTypeClusterIP
80+
clusterIP := ""
7381
for _, p := range service.Ports {
82+
if p.Published != 0 {
83+
serviceType = core.ServiceTypeLoadBalancer
84+
}
7485
ports = append(ports,
7586
core.ServicePort{
7687
Name: fmt.Sprintf("%d-%s", p.Target, strings.ToLower(p.Protocol)),
@@ -79,8 +90,14 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
7990
Protocol: toProtocol(p.Protocol),
8091
})
8192
}
82-
if len(ports) == 0 {
83-
return nil
93+
if len(ports) == 0 { // headless service
94+
clusterIP = clusterIPHeadless
95+
ports = append(ports, core.ServicePort{
96+
Name: headlessName,
97+
Port: headlessPort,
98+
TargetPort: intstr.FromInt(headlessPort),
99+
Protocol: core.ProtocolTCP,
100+
})
84101
}
85102
return &core.Service{
86103
TypeMeta: meta.TypeMeta{
@@ -91,32 +108,14 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
91108
Name: service.Name,
92109
},
93110
Spec: core.ServiceSpec{
94-
Selector: map[string]string{"com.docker.compose.service": service.Name},
95-
Ports: ports,
96-
Type: mapServiceToServiceType(project, service),
111+
ClusterIP: clusterIP,
112+
Selector: map[string]string{"com.docker.compose.service": service.Name},
113+
Ports: ports,
114+
Type: serviceType,
97115
},
98116
}
99117
}
100118

101-
func mapServiceToServiceType(project *types.Project, service types.ServiceConfig) core.ServiceType {
102-
serviceType := core.ServiceTypeClusterIP
103-
if len(service.Networks) == 0 {
104-
// service is implicitly attached to "default" network
105-
serviceType = core.ServiceTypeLoadBalancer
106-
}
107-
for name := range service.Networks {
108-
if !project.Networks[name].Internal {
109-
serviceType = core.ServiceTypeLoadBalancer
110-
}
111-
}
112-
for _, port := range service.Ports {
113-
if port.Published != 0 {
114-
serviceType = core.ServiceTypeNodePort
115-
}
116-
}
117-
return serviceType
118-
}
119-
120119
func mapToDeployment(project *types.Project, service types.ServiceConfig, name string) (*apps.Deployment, error) {
121120
labels := map[string]string{
122121
"com.docker.compose.service": service.Name,

kube/charts/kubernetes/kube_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// +build kube
2+
3+
/*
4+
Copyright 2020 Docker Compose CLI authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package kubernetes
20+
21+
import (
22+
"testing"
23+
24+
"gotest.tools/v3/assert"
25+
26+
core "k8s.io/api/core/v1"
27+
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/apimachinery/pkg/util/intstr"
29+
)
30+
31+
func TestServiceWithExposedPort(t *testing.T) {
32+
model, err := loadYAML(`
33+
services:
34+
nginx:
35+
image: nginx
36+
ports:
37+
- "80:80"
38+
`)
39+
assert.NilError(t, err)
40+
41+
service := mapToService(model, model.Services[0])
42+
assert.DeepEqual(t, *service, core.Service{
43+
TypeMeta: meta.TypeMeta{
44+
Kind: "Service",
45+
APIVersion: "v1",
46+
},
47+
ObjectMeta: meta.ObjectMeta{
48+
Name: "nginx",
49+
},
50+
Spec: core.ServiceSpec{
51+
Selector: map[string]string{"com.docker.compose.service": "nginx"},
52+
Ports: []core.ServicePort{
53+
{
54+
Name: "80-tcp",
55+
Port: int32(80),
56+
TargetPort: intstr.FromInt(int(80)),
57+
Protocol: core.ProtocolTCP,
58+
},
59+
},
60+
Type: core.ServiceTypeLoadBalancer,
61+
}})
62+
}
63+
64+
func TestServiceWithoutExposedPort(t *testing.T) {
65+
model, err := loadYAML(`
66+
services:
67+
nginx:
68+
image: nginx
69+
`)
70+
assert.NilError(t, err)
71+
72+
service := mapToService(model, model.Services[0])
73+
assert.DeepEqual(t, *service, core.Service{
74+
TypeMeta: meta.TypeMeta{
75+
Kind: "Service",
76+
APIVersion: "v1",
77+
},
78+
ObjectMeta: meta.ObjectMeta{
79+
Name: "nginx",
80+
},
81+
Spec: core.ServiceSpec{
82+
Selector: map[string]string{"com.docker.compose.service": "nginx"},
83+
ClusterIP: "None",
84+
Ports: []core.ServicePort{
85+
{
86+
Name: headlessName,
87+
Protocol: core.ProtocolTCP,
88+
Port: headlessPort,
89+
TargetPort: intstr.IntOrString{IntVal: 55555},
90+
},
91+
},
92+
Type: core.ServiceTypeClusterIP,
93+
}})
94+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
services:
2+
db:
3+
build: aci-demo/db
4+
image: gtardif/sentences-db
5+
6+
words:
7+
build: aci-demo/words
8+
image: gtardif/sentences-api
9+
web:
10+
build: aci-demo/web
11+
image: gtardif/sentences-web
12+
ports:
13+
- "80:80"

0 commit comments

Comments
 (0)