From 9977e0345391e748e95e46627a374787873d8b02 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 24 Nov 2021 09:11:27 +0100 Subject: [PATCH] use CustomLabels for composeV2 metadata and not impact service hash Signed-off-by: Nicolas De Loof --- cmd/compose/compose.go | 31 ++++++++++++++++--------------- pkg/compose/create.go | 17 ++++++----------- pkg/compose/hash.go | 1 - 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index f5a74c08768..1957987b989 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -120,22 +120,23 @@ func (o *projectOptions) WithServices(fn ProjectServicesFunc) func(cmd *cobra.Co return err } - if o.EnvFile != "" { - var services types.Services - for _, s := range project.Services { - ef := o.EnvFile - if ef != "" { - if !filepath.IsAbs(ef) { - ef = filepath.Join(project.WorkingDir, o.EnvFile) - } - if s.Labels == nil { - s.Labels = make(map[string]string) - } - s.Labels[api.EnvironmentFileLabel] = ef - services = append(services, s) - } + ef := o.EnvFile + if ef != "" && !filepath.IsAbs(ef) { + ef = filepath.Join(project.WorkingDir, o.EnvFile) + } + for i, s := range project.Services { + s.CustomLabels = map[string]string{ + api.ProjectLabel: project.Name, + api.ServiceLabel: s.Name, + api.VersionLabel: api.ComposeVersion, + api.WorkingDirLabel: project.WorkingDir, + api.ConfigFilesLabel: strings.Join(project.ComposeFiles, ","), + api.OneoffLabel: "False", // default, will be overridden by `run` command + } + if ef != "" { + s.CustomLabels[api.EnvironmentFileLabel] = ef } - project.Services = services + project.Services[i] = s } return fn(ctx, project, args) diff --git a/pkg/compose/create.go b/pkg/compose/create.go index ebedcb3f7e7..d77e075db3d 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -229,7 +229,7 @@ func getImageName(service types.ServiceConfig, projectName string) string { func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project, service types.ServiceConfig, number int, inherit *moby.Container, autoRemove bool, attachStdin bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) { - labels, err := s.prepareLabels(p, service, number) + labels, err := s.prepareLabels(service, number) if err != nil { return nil, nil, nil, err } @@ -414,28 +414,23 @@ func parseSecurityOpts(p *types.Project, securityOpts []string) ([]string, error return securityOpts, nil } -func (s *composeService) prepareLabels(p *types.Project, service types.ServiceConfig, number int) (map[string]string, error) { +func (s *composeService) prepareLabels(service types.ServiceConfig, number int) (map[string]string, error) { labels := map[string]string{} for k, v := range service.Labels { labels[k] = v } - - labels[api.ProjectLabel] = p.Name - labels[api.ServiceLabel] = service.Name - labels[api.VersionLabel] = api.ComposeVersion - if _, ok := service.Labels[api.OneoffLabel]; !ok { - labels[api.OneoffLabel] = "False" + for k, v := range service.CustomLabels { + labels[k] = v } hash, err := ServiceHash(service) if err != nil { return nil, err } - labels[api.ConfigHashLabel] = hash - labels[api.WorkingDirLabel] = p.WorkingDir - labels[api.ConfigFilesLabel] = strings.Join(p.ComposeFiles, ",") + labels[api.ContainerNumberLabel] = strconv.Itoa(number) + var dependencies []string for s := range service.DependsOn { dependencies = append(dependencies, s) diff --git a/pkg/compose/hash.go b/pkg/compose/hash.go index 56f7e3e9d53..e3f2aa0e337 100644 --- a/pkg/compose/hash.go +++ b/pkg/compose/hash.go @@ -24,7 +24,6 @@ import ( ) // ServiceHash compute configuration has for a service -// TODO move this to compose-go func ServiceHash(o types.ServiceConfig) (string, error) { // remove the Build config when generating the service hash o.Build = nil