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

Commit 7146964

Browse files
author
aiordache
committed
support multi-service compose
Signed-off-by: aiordache <[email protected]>
1 parent 5f0f72e commit 7146964

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

compose/internal/env.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ func GetConfig(name string, configPaths []string) (*types.Config, string, error)
7474
}
7575

7676
func GetChartInMemory(config *types.Config, name string) (*chart.Chart, error) {
77+
for k, v := range config.Volumes {
78+
volumeName := strings.ReplaceAll(k, "_", "-")
79+
if volumeName != k {
80+
config.Volumes[volumeName] = v
81+
delete(config.Volumes, k)
82+
}
83+
}
7784
objects, err := kube.MapToKubernetesObjects(config, name)
7885
if err != nil {
7986
return nil, err

compose/internal/kube/kube.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package kube
22

33
import (
44
"fmt"
5+
"log"
56
"strings"
67
"time"
78

89
"github.com/compose-spec/compose-go/types"
910
apps "k8s.io/api/apps/v1"
1011
core "k8s.io/api/core/v1"
12+
resource "k8s.io/apimachinery/pkg/api/resource"
1113
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
1214
"k8s.io/apimachinery/pkg/runtime"
1315
"k8s.io/apimachinery/pkg/util/intstr"
@@ -17,7 +19,13 @@ func MapToKubernetesObjects(model *types.Config, name string) (map[string]runtim
1719
objects := map[string]runtime.Object{}
1820

1921
for _, service := range model.Services {
20-
objects[fmt.Sprintf("%s-service.yaml", service.Name)] = mapToService(model, service)
22+
svcObject := mapToService(model, service)
23+
if svcObject != nil {
24+
objects[fmt.Sprintf("%s-service.yaml", service.Name)] = svcObject
25+
} else {
26+
log.Println("Missing port mapping from service config.")
27+
}
28+
2129
if service.Deploy != nil && service.Deploy.Mode == "global" {
2230
daemonset, err := mapToDaemonset(service, model, name)
2331
if err != nil {
@@ -33,7 +41,8 @@ func MapToKubernetesObjects(model *types.Config, name string) (map[string]runtim
3341
}
3442
for _, vol := range service.Volumes {
3543
if vol.Type == "volume" {
36-
objects[fmt.Sprintf("%s-persistentvolumeclain.yaml", service.Name)] = mapToPVC(service, vol)
44+
vol.Source = strings.ReplaceAll(vol.Source, "_", "-")
45+
objects[fmt.Sprintf("%s-persistentvolumeclaim.yaml", vol.Source)] = mapToPVC(service, vol)
3746
}
3847
}
3948
}
@@ -51,7 +60,9 @@ func mapToService(model *types.Config, service types.ServiceConfig) *core.Servic
5160
Protocol: toProtocol(p.Protocol),
5261
})
5362
}
54-
63+
if len(ports) == 0 {
64+
return nil
65+
}
5566
return &core.Service{
5667
TypeMeta: meta.TypeMeta{
5768
Kind: "Service",
@@ -167,13 +178,27 @@ func toDeploymentStrategy(deploy *types.DeployConfig) apps.DeploymentStrategy {
167178
}
168179

169180
func mapToPVC(service types.ServiceConfig, vol types.ServiceVolumeConfig) runtime.Object {
181+
rwaccess := core.ReadWriteOnce
182+
if vol.ReadOnly {
183+
rwaccess = core.ReadOnlyMany
184+
}
170185
return &core.PersistentVolumeClaim{
186+
TypeMeta: meta.TypeMeta{
187+
Kind: "PersistentVolumeClaim",
188+
APIVersion: "v1",
189+
},
171190
ObjectMeta: meta.ObjectMeta{
172191
Name: vol.Source,
173192
Labels: map[string]string{"com.docker.compose.service": service.Name},
174193
},
175194
Spec: core.PersistentVolumeClaimSpec{
176-
VolumeName: vol.Source,
195+
VolumeName: vol.Source,
196+
AccessModes: []core.PersistentVolumeAccessMode{rwaccess},
197+
Resources: core.ResourceRequirements{
198+
Requests: core.ResourceList{
199+
core.ResourceStorage: resource.MustParse("100Mi"),
200+
},
201+
},
177202
},
178203
}
179204
}

compose/internal/kube/pod.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import (
1818

1919
func toPodTemplate(serviceConfig types.ServiceConfig, labels map[string]string, model *types.Config) (apiv1.PodTemplateSpec, error) {
2020
tpl := apiv1.PodTemplateSpec{}
21-
nodeAffinity, err := toNodeAffinity(serviceConfig.Deploy)
22-
if err != nil {
23-
return apiv1.PodTemplateSpec{}, err
24-
}
21+
//nodeAffinity, err := toNodeAffinity(serviceConfig.Deploy)
22+
//if err != nil {
23+
// return apiv1.PodTemplateSpec{}, err
24+
//}
2525
hostAliases, err := toHostAliases(serviceConfig.ExtraHosts)
2626
if err != nil {
2727
return apiv1.PodTemplateSpec{}, err
@@ -73,7 +73,7 @@ func toPodTemplate(serviceConfig types.ServiceConfig, labels map[string]string,
7373
tpl.Spec.Hostname = serviceConfig.Hostname
7474
tpl.Spec.TerminationGracePeriodSeconds = toTerminationGracePeriodSeconds(serviceConfig.StopGracePeriod)
7575
tpl.Spec.HostAliases = hostAliases
76-
tpl.Spec.Affinity = nodeAffinity
76+
//tpl.Spec.Affinity = nodeAffinity
7777
// we dont want to remove all containers and recreate them because:
7878
// an admission plugin can add sidecar containers
7979
// we for sure want to keep the main container to be additive

compose/internal/kube/volumes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func toVolumeSpecs(s types.ServiceConfig, model *types.Config) ([]volumeSpec, er
4242
source = gitVolume(m.Source)
4343
} else if m.Type == "volume" {
4444
if m.Source != "" {
45-
name = m.Source
45+
name = strings.ReplaceAll(m.Source, "_", "-")
4646
}
4747
} else {
4848
// bind mount
@@ -133,7 +133,7 @@ func toVolumes(s types.ServiceConfig, model *types.Config) ([]apiv1.Volume, erro
133133
}
134134
for _, spec := range specs {
135135
if spec.source == nil {
136-
continue
136+
spec.source = emptyVolumeInMemory()
137137
}
138138
volumes = append(volumes, apiv1.Volume{
139139
Name: spec.mount.Name,

0 commit comments

Comments
 (0)