@@ -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"
@@ -34,8 +35,6 @@ import (
34
35
)
35
36
36
37
const (
37
- headlessPort = 55555
38
- headlessName = "headless"
39
38
clusterIPHeadless = "None"
40
39
)
41
40
@@ -52,13 +51,13 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
52
51
}
53
52
54
53
if service .Deploy != nil && service .Deploy .Mode == "global" {
55
- daemonset , err := mapToDaemonset (project , service , project . Name )
54
+ daemonset , err := mapToDaemonset (project , service )
56
55
if err != nil {
57
56
return nil , err
58
57
}
59
58
objects [fmt .Sprintf ("%s-daemonset.yaml" , service .Name )] = daemonset
60
59
} else {
61
- deployment , err := mapToDeployment (project , service , project . Name )
60
+ deployment , err := mapToDeployment (project , service )
62
61
if err != nil {
63
62
return nil , err
64
63
}
@@ -67,7 +66,7 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
67
66
for _ , vol := range service .Volumes {
68
67
if vol .Type == "volume" {
69
68
vol .Source = strings .ReplaceAll (vol .Source , "_" , "-" )
70
- objects [fmt .Sprintf ("%s-persistentvolumeclaim.yaml" , vol .Source )] = mapToPVC (service , vol )
69
+ objects [fmt .Sprintf ("%s-persistentvolumeclaim.yaml" , vol .Source )] = mapToPVC (project , service , vol )
71
70
}
72
71
}
73
72
}
@@ -92,12 +91,6 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
92
91
}
93
92
if len (ports ) == 0 { // headless service
94
93
clusterIP = clusterIPHeadless
95
- ports = append (ports , core.ServicePort {
96
- Name : headlessName ,
97
- Port : headlessPort ,
98
- TargetPort : intstr .FromInt (headlessPort ),
99
- Protocol : core .ProtocolTCP ,
100
- })
101
94
}
102
95
return & core.Service {
103
96
TypeMeta : meta.TypeMeta {
@@ -109,27 +102,24 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
109
102
},
110
103
Spec : core.ServiceSpec {
111
104
ClusterIP : clusterIP ,
112
- Selector : map [ string ] string { "com.docker.compose.service" : service .Name } ,
105
+ Selector : selectorLabels ( project . Name , service .Name ) ,
113
106
Ports : ports ,
114
107
Type : serviceType ,
115
108
},
116
109
}
117
110
}
118
111
119
- func mapToDeployment (project * types.Project , service types.ServiceConfig , name string ) (* apps.Deployment , error ) {
120
- labels := map [string ]string {
121
- "com.docker.compose.service" : service .Name ,
122
- "com.docker.compose.project" : name ,
123
- }
124
- podTemplate , err := toPodTemplate (project , service , labels )
125
- if err != nil {
126
- return nil , err
127
- }
112
+ func mapToDeployment (project * types.Project , service types.ServiceConfig ) (* apps.Deployment , error ) {
113
+ labels := selectorLabels (project .Name , service .Name )
128
114
selector := new (meta.LabelSelector )
129
115
selector .MatchLabels = make (map [string ]string )
130
116
for key , val := range labels {
131
117
selector .MatchLabels [key ] = val
132
118
}
119
+ podTemplate , err := toPodTemplate (project , service , labels )
120
+ if err != nil {
121
+ return nil , err
122
+ }
133
123
return & apps.Deployment {
134
124
TypeMeta : meta.TypeMeta {
135
125
Kind : "Deployment" ,
@@ -148,11 +138,15 @@ func mapToDeployment(project *types.Project, service types.ServiceConfig, name s
148
138
}, nil
149
139
}
150
140
151
- func mapToDaemonset ( project * types. Project , service types. ServiceConfig , name string ) ( * apps. DaemonSet , error ) {
152
- labels := map [string ]string {
153
- "com.docker. compose.service" : service . Name ,
154
- "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 ,
155
145
}
146
+ }
147
+
148
+ func mapToDaemonset (project * types.Project , service types.ServiceConfig ) (* apps.DaemonSet , error ) {
149
+ labels := selectorLabels (project .Name , service .Name )
156
150
podTemplate , err := toPodTemplate (project , service , labels )
157
151
if err != nil {
158
152
return nil , err
@@ -195,7 +189,7 @@ func toDeploymentStrategy(deploy *types.DeployConfig) apps.DeploymentStrategy {
195
189
}
196
190
}
197
191
198
- func mapToPVC (service types.ServiceConfig , vol types.ServiceVolumeConfig ) runtime.Object {
192
+ func mapToPVC (project * types. Project , service types.ServiceConfig , vol types.ServiceVolumeConfig ) runtime.Object {
199
193
rwaccess := core .ReadWriteOnce
200
194
if vol .ReadOnly {
201
195
rwaccess = core .ReadOnlyMany
@@ -207,7 +201,7 @@ func mapToPVC(service types.ServiceConfig, vol types.ServiceVolumeConfig) runtim
207
201
},
208
202
ObjectMeta : meta.ObjectMeta {
209
203
Name : vol .Source ,
210
- Labels : map [ string ] string { "com.docker.compose.service" : service .Name } ,
204
+ Labels : selectorLabels ( project . Name , service .Name ) ,
211
205
},
212
206
Spec : core.PersistentVolumeClaimSpec {
213
207
VolumeName : vol .Source ,
0 commit comments