mirror of
https://github.com/docker/compose.git
synced 2025-07-28 16:14:06 +02:00
support multi-service compose
Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
parent
5f0f72e27d
commit
7146964168
@ -74,6 +74,13 @@ func GetConfig(name string, configPaths []string) (*types.Config, string, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetChartInMemory(config *types.Config, name string) (*chart.Chart, error) {
|
func GetChartInMemory(config *types.Config, name string) (*chart.Chart, error) {
|
||||||
|
for k, v := range config.Volumes {
|
||||||
|
volumeName := strings.ReplaceAll(k, "_", "-")
|
||||||
|
if volumeName != k {
|
||||||
|
config.Volumes[volumeName] = v
|
||||||
|
delete(config.Volumes, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
objects, err := kube.MapToKubernetesObjects(config, name)
|
objects, err := kube.MapToKubernetesObjects(config, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -2,12 +2,14 @@ package kube
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
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"
|
||||||
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/intstr"
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
@ -17,7 +19,13 @@ func MapToKubernetesObjects(model *types.Config, name string) (map[string]runtim
|
|||||||
objects := map[string]runtime.Object{}
|
objects := map[string]runtime.Object{}
|
||||||
|
|
||||||
for _, service := range model.Services {
|
for _, service := range model.Services {
|
||||||
objects[fmt.Sprintf("%s-service.yaml", service.Name)] = mapToService(model, service)
|
svcObject := mapToService(model, service)
|
||||||
|
if svcObject != nil {
|
||||||
|
objects[fmt.Sprintf("%s-service.yaml", service.Name)] = svcObject
|
||||||
|
} else {
|
||||||
|
log.Println("Missing port mapping from service config.")
|
||||||
|
}
|
||||||
|
|
||||||
if service.Deploy != nil && service.Deploy.Mode == "global" {
|
if service.Deploy != nil && service.Deploy.Mode == "global" {
|
||||||
daemonset, err := mapToDaemonset(service, model, name)
|
daemonset, err := mapToDaemonset(service, model, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -33,7 +41,8 @@ func MapToKubernetesObjects(model *types.Config, name string) (map[string]runtim
|
|||||||
}
|
}
|
||||||
for _, vol := range service.Volumes {
|
for _, vol := range service.Volumes {
|
||||||
if vol.Type == "volume" {
|
if vol.Type == "volume" {
|
||||||
objects[fmt.Sprintf("%s-persistentvolumeclain.yaml", service.Name)] = mapToPVC(service, vol)
|
vol.Source = strings.ReplaceAll(vol.Source, "_", "-")
|
||||||
|
objects[fmt.Sprintf("%s-persistentvolumeclaim.yaml", vol.Source)] = mapToPVC(service, vol)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,7 +60,9 @@ func mapToService(model *types.Config, service types.ServiceConfig) *core.Servic
|
|||||||
Protocol: toProtocol(p.Protocol),
|
Protocol: toProtocol(p.Protocol),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
if len(ports) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return &core.Service{
|
return &core.Service{
|
||||||
TypeMeta: meta.TypeMeta{
|
TypeMeta: meta.TypeMeta{
|
||||||
Kind: "Service",
|
Kind: "Service",
|
||||||
@ -167,13 +178,27 @@ func toDeploymentStrategy(deploy *types.DeployConfig) apps.DeploymentStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func mapToPVC(service types.ServiceConfig, vol types.ServiceVolumeConfig) runtime.Object {
|
func mapToPVC(service types.ServiceConfig, vol types.ServiceVolumeConfig) runtime.Object {
|
||||||
|
rwaccess := core.ReadWriteOnce
|
||||||
|
if vol.ReadOnly {
|
||||||
|
rwaccess = core.ReadOnlyMany
|
||||||
|
}
|
||||||
return &core.PersistentVolumeClaim{
|
return &core.PersistentVolumeClaim{
|
||||||
|
TypeMeta: meta.TypeMeta{
|
||||||
|
Kind: "PersistentVolumeClaim",
|
||||||
|
APIVersion: "v1",
|
||||||
|
},
|
||||||
ObjectMeta: meta.ObjectMeta{
|
ObjectMeta: meta.ObjectMeta{
|
||||||
Name: vol.Source,
|
Name: vol.Source,
|
||||||
Labels: map[string]string{"com.docker.compose.service": service.Name},
|
Labels: map[string]string{"com.docker.compose.service": service.Name},
|
||||||
},
|
},
|
||||||
Spec: core.PersistentVolumeClaimSpec{
|
Spec: core.PersistentVolumeClaimSpec{
|
||||||
VolumeName: vol.Source,
|
VolumeName: vol.Source,
|
||||||
|
AccessModes: []core.PersistentVolumeAccessMode{rwaccess},
|
||||||
|
Resources: core.ResourceRequirements{
|
||||||
|
Requests: core.ResourceList{
|
||||||
|
core.ResourceStorage: resource.MustParse("100Mi"),
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,10 +18,10 @@ import (
|
|||||||
|
|
||||||
func toPodTemplate(serviceConfig types.ServiceConfig, labels map[string]string, model *types.Config) (apiv1.PodTemplateSpec, error) {
|
func toPodTemplate(serviceConfig types.ServiceConfig, labels map[string]string, model *types.Config) (apiv1.PodTemplateSpec, error) {
|
||||||
tpl := apiv1.PodTemplateSpec{}
|
tpl := apiv1.PodTemplateSpec{}
|
||||||
nodeAffinity, err := toNodeAffinity(serviceConfig.Deploy)
|
//nodeAffinity, err := toNodeAffinity(serviceConfig.Deploy)
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
return apiv1.PodTemplateSpec{}, err
|
// return apiv1.PodTemplateSpec{}, err
|
||||||
}
|
//}
|
||||||
hostAliases, err := toHostAliases(serviceConfig.ExtraHosts)
|
hostAliases, err := toHostAliases(serviceConfig.ExtraHosts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return apiv1.PodTemplateSpec{}, err
|
return apiv1.PodTemplateSpec{}, err
|
||||||
@ -73,7 +73,7 @@ func toPodTemplate(serviceConfig types.ServiceConfig, labels map[string]string,
|
|||||||
tpl.Spec.Hostname = serviceConfig.Hostname
|
tpl.Spec.Hostname = serviceConfig.Hostname
|
||||||
tpl.Spec.TerminationGracePeriodSeconds = toTerminationGracePeriodSeconds(serviceConfig.StopGracePeriod)
|
tpl.Spec.TerminationGracePeriodSeconds = toTerminationGracePeriodSeconds(serviceConfig.StopGracePeriod)
|
||||||
tpl.Spec.HostAliases = hostAliases
|
tpl.Spec.HostAliases = hostAliases
|
||||||
tpl.Spec.Affinity = nodeAffinity
|
//tpl.Spec.Affinity = nodeAffinity
|
||||||
// we dont want to remove all containers and recreate them because:
|
// we dont want to remove all containers and recreate them because:
|
||||||
// an admission plugin can add sidecar containers
|
// an admission plugin can add sidecar containers
|
||||||
// we for sure want to keep the main container to be additive
|
// we for sure want to keep the main container to be additive
|
||||||
|
@ -42,7 +42,7 @@ func toVolumeSpecs(s types.ServiceConfig, model *types.Config) ([]volumeSpec, er
|
|||||||
source = gitVolume(m.Source)
|
source = gitVolume(m.Source)
|
||||||
} else if m.Type == "volume" {
|
} else if m.Type == "volume" {
|
||||||
if m.Source != "" {
|
if m.Source != "" {
|
||||||
name = m.Source
|
name = strings.ReplaceAll(m.Source, "_", "-")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// bind mount
|
// bind mount
|
||||||
@ -133,7 +133,7 @@ func toVolumes(s types.ServiceConfig, model *types.Config) ([]apiv1.Volume, erro
|
|||||||
}
|
}
|
||||||
for _, spec := range specs {
|
for _, spec := range specs {
|
||||||
if spec.source == nil {
|
if spec.source == nil {
|
||||||
continue
|
spec.source = emptyVolumeInMemory()
|
||||||
}
|
}
|
||||||
volumes = append(volumes, apiv1.Volume{
|
volumes = append(volumes, apiv1.Volume{
|
||||||
Name: spec.mount.Name,
|
Name: spec.mount.Name,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user