@@ -25,6 +25,7 @@ import (
2525 "time"
2626
2727 "github.com/compose-spec/compose-go/types"
28+ "github.com/docker/compose-cli/api/compose"
2829 apps "k8s.io/api/apps/v1"
2930 core "k8s.io/api/core/v1"
3031 resource "k8s.io/apimachinery/pkg/api/resource"
@@ -33,6 +34,10 @@ import (
3334 "k8s.io/apimachinery/pkg/util/intstr"
3435)
3536
37+ const (
38+ clusterIPHeadless = "None"
39+ )
40+
3641//MapToKubernetesObjects maps compose project to Kubernetes objects
3742func MapToKubernetesObjects (project * types.Project ) (map [string ]runtime.Object , error ) {
3843 objects := map [string ]runtime.Object {}
@@ -46,13 +51,13 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
4651 }
4752
4853 if service .Deploy != nil && service .Deploy .Mode == "global" {
49- daemonset , err := mapToDaemonset (project , service , project . Name )
54+ daemonset , err := mapToDaemonset (project , service )
5055 if err != nil {
5156 return nil , err
5257 }
5358 objects [fmt .Sprintf ("%s-daemonset.yaml" , service .Name )] = daemonset
5459 } else {
55- deployment , err := mapToDeployment (project , service , project . Name )
60+ deployment , err := mapToDeployment (project , service )
5661 if err != nil {
5762 return nil , err
5863 }
@@ -61,7 +66,7 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
6166 for _ , vol := range service .Volumes {
6267 if vol .Type == "volume" {
6368 vol .Source = strings .ReplaceAll (vol .Source , "_" , "-" )
64- objects [fmt .Sprintf ("%s-persistentvolumeclaim.yaml" , vol .Source )] = mapToPVC (service , vol )
69+ objects [fmt .Sprintf ("%s-persistentvolumeclaim.yaml" , vol .Source )] = mapToPVC (project , service , vol )
6570 }
6671 }
6772 }
@@ -70,7 +75,12 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
7075
7176func mapToService (project * types.Project , service types.ServiceConfig ) * core.Service {
7277 ports := []core.ServicePort {}
78+ serviceType := core .ServiceTypeClusterIP
79+ clusterIP := ""
7380 for _ , p := range service .Ports {
81+ if p .Published != 0 {
82+ serviceType = core .ServiceTypeLoadBalancer
83+ }
7484 ports = append (ports ,
7585 core.ServicePort {
7686 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
7989 Protocol : toProtocol (p .Protocol ),
8090 })
8191 }
82- if len (ports ) == 0 {
83- return nil
92+ if len (ports ) == 0 { // headless service
93+ clusterIP = clusterIPHeadless
8494 }
8595 return & core.Service {
8696 TypeMeta : meta.TypeMeta {
@@ -91,46 +101,25 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
91101 Name : service .Name ,
92102 },
93103 Spec : core.ServiceSpec {
94- Selector : map [string ]string {"com.docker.compose.service" : service .Name },
95- Ports : ports ,
96- Type : mapServiceToServiceType (project , service ),
104+ ClusterIP : clusterIP ,
105+ Selector : selectorLabels (project .Name , service .Name ),
106+ Ports : ports ,
107+ Type : serviceType ,
97108 },
98109 }
99110}
100111
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-
120- func mapToDeployment (project * types.Project , service types.ServiceConfig , name string ) (* apps.Deployment , error ) {
121- labels := map [string ]string {
122- "com.docker.compose.service" : service .Name ,
123- "com.docker.compose.project" : name ,
124- }
125- podTemplate , err := toPodTemplate (project , service , labels )
126- if err != nil {
127- return nil , err
128- }
112+ func mapToDeployment (project * types.Project , service types.ServiceConfig ) (* apps.Deployment , error ) {
113+ labels := selectorLabels (project .Name , service .Name )
129114 selector := new (meta.LabelSelector )
130115 selector .MatchLabels = make (map [string ]string )
131116 for key , val := range labels {
132117 selector .MatchLabels [key ] = val
133118 }
119+ podTemplate , err := toPodTemplate (project , service , labels )
120+ if err != nil {
121+ return nil , err
122+ }
134123 return & apps.Deployment {
135124 TypeMeta : meta.TypeMeta {
136125 Kind : "Deployment" ,
@@ -149,11 +138,15 @@ func mapToDeployment(project *types.Project, service types.ServiceConfig, name s
149138 }, nil
150139}
151140
152- func mapToDaemonset ( project * types. Project , service types. ServiceConfig , name string ) ( * apps. DaemonSet , error ) {
153- labels := map [string ]string {
154- "com.docker. compose.service" : service . Name ,
155- "com.docker. compose.project" : name ,
141+ func selectorLabels ( projectName string , serviceName string ) map [ string ] string {
142+ return map [string ]string {
143+ compose .ProjectTag : projectName ,
144+ compose .ServiceTag : serviceName ,
156145 }
146+ }
147+
148+ func mapToDaemonset (project * types.Project , service types.ServiceConfig ) (* apps.DaemonSet , error ) {
149+ labels := selectorLabels (project .Name , service .Name )
157150 podTemplate , err := toPodTemplate (project , service , labels )
158151 if err != nil {
159152 return nil , err
@@ -196,7 +189,7 @@ func toDeploymentStrategy(deploy *types.DeployConfig) apps.DeploymentStrategy {
196189 }
197190}
198191
199- func mapToPVC (service types.ServiceConfig , vol types.ServiceVolumeConfig ) runtime.Object {
192+ func mapToPVC (project * types. Project , service types.ServiceConfig , vol types.ServiceVolumeConfig ) runtime.Object {
200193 rwaccess := core .ReadWriteOnce
201194 if vol .ReadOnly {
202195 rwaccess = core .ReadOnlyMany
@@ -208,7 +201,7 @@ func mapToPVC(service types.ServiceConfig, vol types.ServiceVolumeConfig) runtim
208201 },
209202 ObjectMeta : meta.ObjectMeta {
210203 Name : vol .Source ,
211- Labels : map [ string ] string { "com.docker.compose.service" : service .Name } ,
204+ Labels : selectorLabels ( project . Name , service .Name ) ,
212205 },
213206 Spec : core.PersistentVolumeClaimSpec {
214207 VolumeName : vol .Source ,
0 commit comments