mirror of
https://github.com/docker/compose.git
synced 2025-07-21 04:34:38 +02:00
use CustomLabels for composeV2 metadata and not impact service hash
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
2eeed8481d
commit
fb9310caf2
@ -120,24 +120,6 @@ func (o *projectOptions) WithServices(fn ProjectServicesFunc) func(cmd *cobra.Co
|
|||||||
return err
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
project.Services = services
|
|
||||||
}
|
|
||||||
|
|
||||||
return fn(ctx, project, args)
|
return fn(ctx, project, args)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -180,6 +162,25 @@ func (o *projectOptions) toProject(services []string, po ...cli.ProjectOptionsFn
|
|||||||
compose.Separator = "_"
|
compose.Separator = "_"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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[i] = s
|
||||||
|
}
|
||||||
|
|
||||||
if len(services) > 0 {
|
if len(services) > 0 {
|
||||||
s, err := project.GetServices(services...)
|
s, err := project.GetServices(services...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -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,
|
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) {
|
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 {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
@ -414,28 +414,23 @@ func parseSecurityOpts(p *types.Project, securityOpts []string) ([]string, error
|
|||||||
return securityOpts, nil
|
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{}
|
labels := map[string]string{}
|
||||||
for k, v := range service.Labels {
|
for k, v := range service.Labels {
|
||||||
labels[k] = v
|
labels[k] = v
|
||||||
}
|
}
|
||||||
|
for k, v := range service.CustomLabels {
|
||||||
labels[api.ProjectLabel] = p.Name
|
labels[k] = v
|
||||||
labels[api.ServiceLabel] = service.Name
|
|
||||||
labels[api.VersionLabel] = api.ComposeVersion
|
|
||||||
if _, ok := service.Labels[api.OneoffLabel]; !ok {
|
|
||||||
labels[api.OneoffLabel] = "False"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hash, err := ServiceHash(service)
|
hash, err := ServiceHash(service)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
labels[api.ConfigHashLabel] = hash
|
labels[api.ConfigHashLabel] = hash
|
||||||
labels[api.WorkingDirLabel] = p.WorkingDir
|
|
||||||
labels[api.ConfigFilesLabel] = strings.Join(p.ComposeFiles, ",")
|
|
||||||
labels[api.ContainerNumberLabel] = strconv.Itoa(number)
|
labels[api.ContainerNumberLabel] = strconv.Itoa(number)
|
||||||
|
|
||||||
var dependencies []string
|
var dependencies []string
|
||||||
for s := range service.DependsOn {
|
for s := range service.DependsOn {
|
||||||
dependencies = append(dependencies, s)
|
dependencies = append(dependencies, s)
|
||||||
|
@ -24,7 +24,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ServiceHash compute configuration has for a service
|
// ServiceHash compute configuration has for a service
|
||||||
// TODO move this to compose-go
|
|
||||||
func ServiceHash(o types.ServiceConfig) (string, error) {
|
func ServiceHash(o types.ServiceConfig) (string, error) {
|
||||||
// remove the Build config when generating the service hash
|
// remove the Build config when generating the service hash
|
||||||
o.Build = nil
|
o.Build = nil
|
||||||
|
@ -153,8 +153,9 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project,
|
|||||||
if service.Deploy != nil {
|
if service.Deploy != nil {
|
||||||
service.Deploy.RestartPolicy = nil
|
service.Deploy.RestartPolicy = nil
|
||||||
}
|
}
|
||||||
service.Labels = service.Labels.Add(api.SlugLabel, slug)
|
service.CustomLabels = service.CustomLabels.
|
||||||
service.Labels = service.Labels.Add(api.OneoffLabel, "True")
|
Add(api.SlugLabel, slug).
|
||||||
|
Add(api.OneoffLabel, "True")
|
||||||
|
|
||||||
if err := s.ensureImagesExists(ctx, project, opts.QuietPull); err != nil { // all dependencies already checked, but might miss service img
|
if err := s.ensureImagesExists(ctx, project, opts.QuietPull); err != nil { // all dependencies already checked, but might miss service img
|
||||||
return "", err
|
return "", err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user