mirror of
https://github.com/docker/compose.git
synced 2025-07-15 09:44:28 +02:00
Merge pull request #1196 from gtardif/kube_expose_LoadBalancer
Support exposing ports and cross-service communication
This commit is contained in:
commit
6215445b8a
3
.github/labeler.yml
vendored
3
.github/labeler.yml
vendored
@ -7,6 +7,9 @@ ecs:
|
|||||||
local:
|
local:
|
||||||
- local/**/*
|
- local/**/*
|
||||||
|
|
||||||
|
kube:
|
||||||
|
- kube/**/*
|
||||||
|
|
||||||
cli:
|
cli:
|
||||||
- cli/**/*
|
- cli/**/*
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
|
"github.com/docker/compose-cli/api/compose"
|
||||||
apps "k8s.io/api/apps/v1"
|
apps "k8s.io/api/apps/v1"
|
||||||
core "k8s.io/api/core/v1"
|
core "k8s.io/api/core/v1"
|
||||||
resource "k8s.io/apimachinery/pkg/api/resource"
|
resource "k8s.io/apimachinery/pkg/api/resource"
|
||||||
@ -33,6 +34,10 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/util/intstr"
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
clusterIPHeadless = "None"
|
||||||
|
)
|
||||||
|
|
||||||
//MapToKubernetesObjects maps compose project to Kubernetes objects
|
//MapToKubernetesObjects maps compose project to Kubernetes objects
|
||||||
func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object, error) {
|
func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object, error) {
|
||||||
objects := map[string]runtime.Object{}
|
objects := map[string]runtime.Object{}
|
||||||
@ -46,13 +51,13 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if service.Deploy != nil && service.Deploy.Mode == "global" {
|
if service.Deploy != nil && service.Deploy.Mode == "global" {
|
||||||
daemonset, err := mapToDaemonset(project, service, project.Name)
|
daemonset, err := mapToDaemonset(project, service)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
objects[fmt.Sprintf("%s-daemonset.yaml", service.Name)] = daemonset
|
objects[fmt.Sprintf("%s-daemonset.yaml", service.Name)] = daemonset
|
||||||
} else {
|
} else {
|
||||||
deployment, err := mapToDeployment(project, service, project.Name)
|
deployment, err := mapToDeployment(project, service)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -61,7 +66,7 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
|
|||||||
for _, vol := range service.Volumes {
|
for _, vol := range service.Volumes {
|
||||||
if vol.Type == "volume" {
|
if vol.Type == "volume" {
|
||||||
vol.Source = strings.ReplaceAll(vol.Source, "_", "-")
|
vol.Source = strings.ReplaceAll(vol.Source, "_", "-")
|
||||||
objects[fmt.Sprintf("%s-persistentvolumeclaim.yaml", vol.Source)] = mapToPVC(service, vol)
|
objects[fmt.Sprintf("%s-persistentvolumeclaim.yaml", vol.Source)] = mapToPVC(project, service, vol)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,7 +75,12 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
|
|||||||
|
|
||||||
func mapToService(project *types.Project, service types.ServiceConfig) *core.Service {
|
func mapToService(project *types.Project, service types.ServiceConfig) *core.Service {
|
||||||
ports := []core.ServicePort{}
|
ports := []core.ServicePort{}
|
||||||
|
serviceType := core.ServiceTypeClusterIP
|
||||||
|
clusterIP := ""
|
||||||
for _, p := range service.Ports {
|
for _, p := range service.Ports {
|
||||||
|
if p.Published != 0 {
|
||||||
|
serviceType = core.ServiceTypeLoadBalancer
|
||||||
|
}
|
||||||
ports = append(ports,
|
ports = append(ports,
|
||||||
core.ServicePort{
|
core.ServicePort{
|
||||||
Name: fmt.Sprintf("%d-%s", p.Target, strings.ToLower(p.Protocol)),
|
Name: fmt.Sprintf("%d-%s", p.Target, strings.ToLower(p.Protocol)),
|
||||||
@ -79,8 +89,8 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
|
|||||||
Protocol: toProtocol(p.Protocol),
|
Protocol: toProtocol(p.Protocol),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if len(ports) == 0 {
|
if len(ports) == 0 { // headless service
|
||||||
return nil
|
clusterIP = clusterIPHeadless
|
||||||
}
|
}
|
||||||
return &core.Service{
|
return &core.Service{
|
||||||
TypeMeta: meta.TypeMeta{
|
TypeMeta: meta.TypeMeta{
|
||||||
@ -91,46 +101,25 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
|
|||||||
Name: service.Name,
|
Name: service.Name,
|
||||||
},
|
},
|
||||||
Spec: core.ServiceSpec{
|
Spec: core.ServiceSpec{
|
||||||
Selector: map[string]string{"com.docker.compose.service": service.Name},
|
ClusterIP: clusterIP,
|
||||||
Ports: ports,
|
Selector: selectorLabels(project.Name, service.Name),
|
||||||
Type: mapServiceToServiceType(project, service),
|
Ports: ports,
|
||||||
|
Type: serviceType,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapServiceToServiceType(project *types.Project, service types.ServiceConfig) core.ServiceType {
|
func mapToDeployment(project *types.Project, service types.ServiceConfig) (*apps.Deployment, error) {
|
||||||
serviceType := core.ServiceTypeClusterIP
|
labels := selectorLabels(project.Name, service.Name)
|
||||||
if len(service.Networks) == 0 {
|
|
||||||
// service is implicitly attached to "default" network
|
|
||||||
serviceType = core.ServiceTypeLoadBalancer
|
|
||||||
}
|
|
||||||
for name := range service.Networks {
|
|
||||||
if !project.Networks[name].Internal {
|
|
||||||
serviceType = core.ServiceTypeLoadBalancer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, port := range service.Ports {
|
|
||||||
if port.Published != 0 {
|
|
||||||
serviceType = core.ServiceTypeNodePort
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return serviceType
|
|
||||||
}
|
|
||||||
|
|
||||||
func mapToDeployment(project *types.Project, service types.ServiceConfig, name string) (*apps.Deployment, error) {
|
|
||||||
labels := map[string]string{
|
|
||||||
"com.docker.compose.service": service.Name,
|
|
||||||
"com.docker.compose.project": name,
|
|
||||||
}
|
|
||||||
podTemplate, err := toPodTemplate(project, service, labels)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
selector := new(meta.LabelSelector)
|
selector := new(meta.LabelSelector)
|
||||||
selector.MatchLabels = make(map[string]string)
|
selector.MatchLabels = make(map[string]string)
|
||||||
for key, val := range labels {
|
for key, val := range labels {
|
||||||
selector.MatchLabels[key] = val
|
selector.MatchLabels[key] = val
|
||||||
}
|
}
|
||||||
|
podTemplate, err := toPodTemplate(project, service, labels)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &apps.Deployment{
|
return &apps.Deployment{
|
||||||
TypeMeta: meta.TypeMeta{
|
TypeMeta: meta.TypeMeta{
|
||||||
Kind: "Deployment",
|
Kind: "Deployment",
|
||||||
@ -149,11 +138,15 @@ func mapToDeployment(project *types.Project, service types.ServiceConfig, name s
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapToDaemonset(project *types.Project, service types.ServiceConfig, name string) (*apps.DaemonSet, error) {
|
func selectorLabels(projectName string, serviceName string) map[string]string {
|
||||||
labels := map[string]string{
|
return map[string]string{
|
||||||
"com.docker.compose.service": service.Name,
|
compose.ProjectTag: projectName,
|
||||||
"com.docker.compose.project": name,
|
compose.ServiceTag: serviceName,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapToDaemonset(project *types.Project, service types.ServiceConfig) (*apps.DaemonSet, error) {
|
||||||
|
labels := selectorLabels(project.Name, service.Name)
|
||||||
podTemplate, err := toPodTemplate(project, service, labels)
|
podTemplate, err := toPodTemplate(project, service, labels)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -196,7 +189,7 @@ func toDeploymentStrategy(deploy *types.DeployConfig) apps.DeploymentStrategy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapToPVC(service types.ServiceConfig, vol types.ServiceVolumeConfig) runtime.Object {
|
func mapToPVC(project *types.Project, service types.ServiceConfig, vol types.ServiceVolumeConfig) runtime.Object {
|
||||||
rwaccess := core.ReadWriteOnce
|
rwaccess := core.ReadWriteOnce
|
||||||
if vol.ReadOnly {
|
if vol.ReadOnly {
|
||||||
rwaccess = core.ReadOnlyMany
|
rwaccess = core.ReadOnlyMany
|
||||||
@ -208,7 +201,7 @@ func mapToPVC(service types.ServiceConfig, vol types.ServiceVolumeConfig) runtim
|
|||||||
},
|
},
|
||||||
ObjectMeta: meta.ObjectMeta{
|
ObjectMeta: meta.ObjectMeta{
|
||||||
Name: vol.Source,
|
Name: vol.Source,
|
||||||
Labels: map[string]string{"com.docker.compose.service": service.Name},
|
Labels: selectorLabels(project.Name, service.Name),
|
||||||
},
|
},
|
||||||
Spec: core.PersistentVolumeClaimSpec{
|
Spec: core.PersistentVolumeClaimSpec{
|
||||||
VolumeName: vol.Source,
|
VolumeName: vol.Source,
|
||||||
|
87
kube/charts/kubernetes/kube_test.go
Normal file
87
kube/charts/kubernetes/kube_test.go
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
// +build kube
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright 2020 Docker Compose CLI authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kubernetes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
|
|
||||||
|
core "k8s.io/api/core/v1"
|
||||||
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestServiceWithExposedPort(t *testing.T) {
|
||||||
|
model, err := loadYAML(`
|
||||||
|
services:
|
||||||
|
nginx:
|
||||||
|
image: nginx
|
||||||
|
ports:
|
||||||
|
- "80:80"
|
||||||
|
`)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
|
||||||
|
service := mapToService(model, model.Services[0])
|
||||||
|
assert.DeepEqual(t, *service, core.Service{
|
||||||
|
TypeMeta: meta.TypeMeta{
|
||||||
|
Kind: "Service",
|
||||||
|
APIVersion: "v1",
|
||||||
|
},
|
||||||
|
ObjectMeta: meta.ObjectMeta{
|
||||||
|
Name: "nginx",
|
||||||
|
},
|
||||||
|
Spec: core.ServiceSpec{
|
||||||
|
Selector: map[string]string{"com.docker.compose.service": "nginx", "com.docker.compose.project": ""},
|
||||||
|
Ports: []core.ServicePort{
|
||||||
|
{
|
||||||
|
Name: "80-tcp",
|
||||||
|
Port: int32(80),
|
||||||
|
TargetPort: intstr.FromInt(int(80)),
|
||||||
|
Protocol: core.ProtocolTCP,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Type: core.ServiceTypeLoadBalancer,
|
||||||
|
}})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServiceWithoutExposedPort(t *testing.T) {
|
||||||
|
model, err := loadYAML(`
|
||||||
|
services:
|
||||||
|
nginx:
|
||||||
|
image: nginx
|
||||||
|
`)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
|
||||||
|
service := mapToService(model, model.Services[0])
|
||||||
|
assert.DeepEqual(t, *service, core.Service{
|
||||||
|
TypeMeta: meta.TypeMeta{
|
||||||
|
Kind: "Service",
|
||||||
|
APIVersion: "v1",
|
||||||
|
},
|
||||||
|
ObjectMeta: meta.ObjectMeta{
|
||||||
|
Name: "nginx",
|
||||||
|
},
|
||||||
|
Spec: core.ServiceSpec{
|
||||||
|
Selector: map[string]string{"com.docker.compose.service": "nginx", "com.docker.compose.project": ""},
|
||||||
|
ClusterIP: "None",
|
||||||
|
Ports: []core.ServicePort{},
|
||||||
|
Type: core.ServiceTypeClusterIP,
|
||||||
|
}})
|
||||||
|
}
|
13
kube/e2e/kube-simple-demo/demo_sentences.yaml
Normal file
13
kube/e2e/kube-simple-demo/demo_sentences.yaml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
services:
|
||||||
|
db:
|
||||||
|
build: aci-demo/db
|
||||||
|
image: gtardif/sentences-db
|
||||||
|
|
||||||
|
words:
|
||||||
|
build: aci-demo/words
|
||||||
|
image: gtardif/sentences-api
|
||||||
|
web:
|
||||||
|
build: aci-demo/web
|
||||||
|
image: gtardif/sentences-web
|
||||||
|
ports:
|
||||||
|
- "80:80"
|
@ -19,6 +19,7 @@ package compose
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/docker/compose-cli/api/compose"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,14 +27,14 @@ const (
|
|||||||
containerNumberLabel = "com.docker.compose.container-number"
|
containerNumberLabel = "com.docker.compose.container-number"
|
||||||
oneoffLabel = "com.docker.compose.oneoff"
|
oneoffLabel = "com.docker.compose.oneoff"
|
||||||
slugLabel = "com.docker.compose.slug"
|
slugLabel = "com.docker.compose.slug"
|
||||||
projectLabel = "com.docker.compose.project"
|
projectLabel = compose.ProjectTag
|
||||||
volumeLabel = "com.docker.compose.volume"
|
volumeLabel = compose.VolumeTag
|
||||||
workingDirLabel = "com.docker.compose.project.working_dir"
|
workingDirLabel = "com.docker.compose.project.working_dir"
|
||||||
configFilesLabel = "com.docker.compose.project.config_files"
|
configFilesLabel = "com.docker.compose.project.config_files"
|
||||||
serviceLabel = "com.docker.compose.service"
|
serviceLabel = compose.ServiceTag
|
||||||
versionLabel = "com.docker.compose.version"
|
versionLabel = "com.docker.compose.version"
|
||||||
configHashLabel = "com.docker.compose.config-hash"
|
configHashLabel = "com.docker.compose.config-hash"
|
||||||
networkLabel = "com.docker.compose.network"
|
networkLabel = compose.NetworkTag
|
||||||
|
|
||||||
//ComposeVersion Compose version
|
//ComposeVersion Compose version
|
||||||
ComposeVersion = "1.0-alpha"
|
ComposeVersion = "1.0-alpha"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user