Skip to content

Commit fb9310c

Browse files
committed
use CustomLabels for composeV2 metadata and not impact service hash
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 2eeed84 commit fb9310c

File tree

4 files changed

+28
-32
lines changed

4 files changed

+28
-32
lines changed

cmd/compose/compose.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,24 +120,6 @@ func (o *projectOptions) WithServices(fn ProjectServicesFunc) func(cmd *cobra.Co
120120
return err
121121
}
122122

123-
if o.EnvFile != "" {
124-
var services types.Services
125-
for _, s := range project.Services {
126-
ef := o.EnvFile
127-
if ef != "" {
128-
if !filepath.IsAbs(ef) {
129-
ef = filepath.Join(project.WorkingDir, o.EnvFile)
130-
}
131-
if s.Labels == nil {
132-
s.Labels = make(map[string]string)
133-
}
134-
s.Labels[api.EnvironmentFileLabel] = ef
135-
services = append(services, s)
136-
}
137-
}
138-
project.Services = services
139-
}
140-
141123
return fn(ctx, project, args)
142124
})
143125
}
@@ -180,6 +162,25 @@ func (o *projectOptions) toProject(services []string, po ...cli.ProjectOptionsFn
180162
compose.Separator = "_"
181163
}
182164

165+
ef := o.EnvFile
166+
if ef != "" && !filepath.IsAbs(ef) {
167+
ef = filepath.Join(project.WorkingDir, o.EnvFile)
168+
}
169+
for i, s := range project.Services {
170+
s.CustomLabels = map[string]string{
171+
api.ProjectLabel: project.Name,
172+
api.ServiceLabel: s.Name,
173+
api.VersionLabel: api.ComposeVersion,
174+
api.WorkingDirLabel: project.WorkingDir,
175+
api.ConfigFilesLabel: strings.Join(project.ComposeFiles, ","),
176+
api.OneoffLabel: "False", // default, will be overridden by `run` command
177+
}
178+
if ef != "" {
179+
s.CustomLabels[api.EnvironmentFileLabel] = ef
180+
}
181+
project.Services[i] = s
182+
}
183+
183184
if len(services) > 0 {
184185
s, err := project.GetServices(services...)
185186
if err != nil {

pkg/compose/create.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func getImageName(service types.ServiceConfig, projectName string) string {
229229
func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project, service types.ServiceConfig,
230230
number int, inherit *moby.Container, autoRemove bool, attachStdin bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
231231

232-
labels, err := s.prepareLabels(p, service, number)
232+
labels, err := s.prepareLabels(service, number)
233233
if err != nil {
234234
return nil, nil, nil, err
235235
}
@@ -414,28 +414,23 @@ func parseSecurityOpts(p *types.Project, securityOpts []string) ([]string, error
414414
return securityOpts, nil
415415
}
416416

417-
func (s *composeService) prepareLabels(p *types.Project, service types.ServiceConfig, number int) (map[string]string, error) {
417+
func (s *composeService) prepareLabels(service types.ServiceConfig, number int) (map[string]string, error) {
418418
labels := map[string]string{}
419419
for k, v := range service.Labels {
420420
labels[k] = v
421421
}
422-
423-
labels[api.ProjectLabel] = p.Name
424-
labels[api.ServiceLabel] = service.Name
425-
labels[api.VersionLabel] = api.ComposeVersion
426-
if _, ok := service.Labels[api.OneoffLabel]; !ok {
427-
labels[api.OneoffLabel] = "False"
422+
for k, v := range service.CustomLabels {
423+
labels[k] = v
428424
}
429425

430426
hash, err := ServiceHash(service)
431427
if err != nil {
432428
return nil, err
433429
}
434-
435430
labels[api.ConfigHashLabel] = hash
436-
labels[api.WorkingDirLabel] = p.WorkingDir
437-
labels[api.ConfigFilesLabel] = strings.Join(p.ComposeFiles, ",")
431+
438432
labels[api.ContainerNumberLabel] = strconv.Itoa(number)
433+
439434
var dependencies []string
440435
for s := range service.DependsOn {
441436
dependencies = append(dependencies, s)

pkg/compose/hash.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
)
2525

2626
// ServiceHash compute configuration has for a service
27-
// TODO move this to compose-go
2827
func ServiceHash(o types.ServiceConfig) (string, error) {
2928
// remove the Build config when generating the service hash
3029
o.Build = nil

pkg/compose/run.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project,
153153
if service.Deploy != nil {
154154
service.Deploy.RestartPolicy = nil
155155
}
156-
service.Labels = service.Labels.Add(api.SlugLabel, slug)
157-
service.Labels = service.Labels.Add(api.OneoffLabel, "True")
156+
service.CustomLabels = service.CustomLabels.
157+
Add(api.SlugLabel, slug).
158+
Add(api.OneoffLabel, "True")
158159

159160
if err := s.ensureImagesExists(ctx, project, opts.QuietPull); err != nil { // all dependencies already checked, but might miss service img
160161
return "", err

0 commit comments

Comments
 (0)