@@ -25,6 +25,7 @@ import (
25
25
"time"
26
26
27
27
"github.com/compose-spec/compose-go/types"
28
+ "github.com/docker/compose-cli/api/compose"
28
29
apps "k8s.io/api/apps/v1"
29
30
core "k8s.io/api/core/v1"
30
31
resource "k8s.io/apimachinery/pkg/api/resource"
@@ -33,6 +34,10 @@ import (
33
34
"k8s.io/apimachinery/pkg/util/intstr"
34
35
)
35
36
37
+ const (
38
+ clusterIPHeadless = "None"
39
+ )
40
+
36
41
//MapToKubernetesObjects maps compose project to Kubernetes objects
37
42
func MapToKubernetesObjects (project * types.Project ) (map [string ]runtime.Object , error ) {
38
43
objects := map [string ]runtime.Object {}
@@ -46,13 +51,13 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
46
51
}
47
52
48
53
if service .Deploy != nil && service .Deploy .Mode == "global" {
49
- daemonset , err := mapToDaemonset (project , service , project . Name )
54
+ daemonset , err := mapToDaemonset (project , service )
50
55
if err != nil {
51
56
return nil , err
52
57
}
53
58
objects [fmt .Sprintf ("%s-daemonset.yaml" , service .Name )] = daemonset
54
59
} else {
55
- deployment , err := mapToDeployment (project , service , project . Name )
60
+ deployment , err := mapToDeployment (project , service )
56
61
if err != nil {
57
62
return nil , err
58
63
}
@@ -61,7 +66,7 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
61
66
for _ , vol := range service .Volumes {
62
67
if vol .Type == "volume" {
63
68
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 )
65
70
}
66
71
}
67
72
}
@@ -70,7 +75,12 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
70
75
71
76
func mapToService (project * types.Project , service types.ServiceConfig ) * core.Service {
72
77
ports := []core.ServicePort {}
78
+ serviceType := core .ServiceTypeClusterIP
79
+ clusterIP := ""
73
80
for _ , p := range service .Ports {
81
+ if p .Published != 0 {
82
+ serviceType = core .ServiceTypeLoadBalancer
83
+ }
74
84
ports = append (ports ,
75
85
core.ServicePort {
76
86
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
79
89
Protocol : toProtocol (p .Protocol ),
80
90
})
81
91
}
82
- if len (ports ) == 0 {
83
- return nil
92
+ if len (ports ) == 0 { // headless service
93
+ clusterIP = clusterIPHeadless
84
94
}
85
95
return & core.Service {
86
96
TypeMeta : meta.TypeMeta {
@@ -91,46 +101,25 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
91
101
Name : service .Name ,
92
102
},
93
103
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 ,
97
108
},
98
109
}
99
110
}
100
111
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 )
129
114
selector := new (meta.LabelSelector )
130
115
selector .MatchLabels = make (map [string ]string )
131
116
for key , val := range labels {
132
117
selector .MatchLabels [key ] = val
133
118
}
119
+ podTemplate , err := toPodTemplate (project , service , labels )
120
+ if err != nil {
121
+ return nil , err
122
+ }
134
123
return & apps.Deployment {
135
124
TypeMeta : meta.TypeMeta {
136
125
Kind : "Deployment" ,
@@ -149,11 +138,15 @@ func mapToDeployment(project *types.Project, service types.ServiceConfig, name s
149
138
}, nil
150
139
}
151
140
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 ,
156
145
}
146
+ }
147
+
148
+ func mapToDaemonset (project * types.Project , service types.ServiceConfig ) (* apps.DaemonSet , error ) {
149
+ labels := selectorLabels (project .Name , service .Name )
157
150
podTemplate , err := toPodTemplate (project , service , labels )
158
151
if err != nil {
159
152
return nil , err
@@ -196,7 +189,7 @@ func toDeploymentStrategy(deploy *types.DeployConfig) apps.DeploymentStrategy {
196
189
}
197
190
}
198
191
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 {
200
193
rwaccess := core .ReadWriteOnce
201
194
if vol .ReadOnly {
202
195
rwaccess = core .ReadOnlyMany
@@ -208,7 +201,7 @@ func mapToPVC(service types.ServiceConfig, vol types.ServiceVolumeConfig) runtim
208
201
},
209
202
ObjectMeta : meta.ObjectMeta {
210
203
Name : vol .Source ,
211
- Labels : map [ string ] string { "com.docker.compose.service" : service .Name } ,
204
+ Labels : selectorLabels ( project . Name , service .Name ) ,
212
205
},
213
206
Spec : core.PersistentVolumeClaimSpec {
214
207
VolumeName : vol .Source ,
0 commit comments