keep the platform defined, in priority, via DOCKER_DEFAULT_PLATFORM or the service.plaform one if no build platforms provided

Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
This commit is contained in:
Guillaume Lours 2022-09-22 12:06:34 +02:00
parent e2a3fe9427
commit ce3700d334
1 changed files with 22 additions and 14 deletions

View File

@ -179,7 +179,7 @@ func (s *composeService) getBuildOptions(project *types.Project, images map[stri
"load": "true",
},
}}
if opt.Platforms, err = useDockerDefaultPlatform(project, service.Build.Platforms); err != nil {
if opt.Platforms, err = useDockerDefaultOrServicePlatform(project, service, true); err != nil {
opt.Platforms = []specs.Platform{}
}
opts[imageName] = opt
@ -363,23 +363,11 @@ func addSecretsConfig(project *types.Project, service types.ServiceConfig) (sess
}
func addPlatforms(project *types.Project, service types.ServiceConfig) ([]specs.Platform, error) {
plats, err := useDockerDefaultPlatform(project, service.Build.Platforms)
plats, err := useDockerDefaultOrServicePlatform(project, service, false)
if err != nil {
return nil, err
}
if service.Platform != "" && !utils.StringContains(service.Build.Platforms, service.Platform) {
if len(service.Build.Platforms) > 0 {
return nil, fmt.Errorf("service.platform should be part of the service.build.platforms: %q", service.Platform)
}
// User defined a service platform and no build platforms, so we should keep the one define on the service level
p, err := platforms.Parse(service.Platform)
if !utils.Contains(plats, p) {
plats = append(plats, p)
}
return plats, err
}
for _, buildPlatform := range service.Build.Platforms {
p, err := platforms.Parse(buildPlatform)
if err != nil {
@ -420,3 +408,23 @@ func useDockerDefaultPlatform(project *types.Project, platformList types.StringL
}
return plats, nil
}
func useDockerDefaultOrServicePlatform(project *types.Project, service types.ServiceConfig, useOnePlatform bool) ([]specs.Platform, error) {
plats, err := useDockerDefaultPlatform(project, service.Build.Platforms)
if (len(plats) > 0 && useOnePlatform) || err != nil {
return plats, err
}
if service.Platform != "" && !utils.StringContains(service.Build.Platforms, service.Platform) {
if len(service.Build.Platforms) > 0 {
return nil, fmt.Errorf("service.platform should be part of the service.build.platforms: %q", service.Platform)
}
// User defined a service platform and no build platforms, so we should keep the one define on the service level
p, err := platforms.Parse(service.Platform)
if !utils.Contains(plats, p) {
plats = append(plats, p)
}
return plats, err
}
return plats, nil
}