Skip to content

Commit ebd7b76

Browse files
ndeloofglours
authored andcommitted
sanitize service name so they can be used as bake targets
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent ea48480 commit ebd7b76

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

pkg/compose/build_bake.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,21 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
144144
privileged bool
145145
read []string
146146
expectedImages = make(map[string]string, len(serviceToBeBuild)) // service name -> expected image
147+
targets = make(map[string]string, len(serviceToBeBuild)) // service name -> build target
147148
)
148149

150+
// produce a unique ID for service used as bake target
151+
for serviceName := range serviceToBeBuild {
152+
t := strings.ReplaceAll(serviceName, ".", "_")
153+
for {
154+
if _, ok := targets[serviceName]; !ok {
155+
targets[serviceName] = t
156+
break
157+
}
158+
t += "_"
159+
}
160+
}
161+
149162
for serviceName, service := range serviceToBeBuild {
150163
if service.Build == nil {
151164
continue
@@ -192,9 +205,10 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
192205
}
193206
}
194207

195-
cfg.Targets[serviceName] = bakeTarget{
208+
target := targets[serviceName]
209+
cfg.Targets[target] = bakeTarget{
196210
Context: build.Context,
197-
Contexts: additionalContexts(build.AdditionalContexts),
211+
Contexts: additionalContexts(build.AdditionalContexts, targets),
198212
Dockerfile: dockerFilePath(build.Context, build.Dockerfile),
199213
DockerfileInline: strings.ReplaceAll(build.DockerfileInline, "${", "$${"),
200214
Args: args,
@@ -216,7 +230,7 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
216230
Outputs: outputs,
217231
Call: call,
218232
}
219-
group.Targets = append(group.Targets, serviceName)
233+
group.Targets = append(group.Targets, target)
220234
}
221235

222236
cfg.Groups["default"] = group
@@ -340,7 +354,7 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
340354
cw := progress.ContextWriter(ctx)
341355
results := map[string]string{}
342356
for service, name := range expectedImages {
343-
built, ok := md[service] // bake target == service name
357+
built, ok := md[targets[service]]
344358
if !ok {
345359
return nil, fmt.Errorf("build result not found in Bake metadata for service %s", service)
346360
}
@@ -350,11 +364,11 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
350364
return results, nil
351365
}
352366

353-
func additionalContexts(contexts types.Mapping) map[string]string {
367+
func additionalContexts(contexts types.Mapping, targets map[string]string) map[string]string {
354368
ac := map[string]string{}
355369
for k, v := range contexts {
356370
if target, found := strings.CutPrefix(v, types.ServicePrefix); found {
357-
v = "target:" + target
371+
v = "target:" + targets[target]
358372
}
359373
ac[k] = v
360374
}

0 commit comments

Comments
 (0)