refactor: strip project name from volume names without mangling project name

This commit is contained in:
Pasha Sviderski
2025-10-30 20:54:15 +10:00
parent 65c3e0a68d
commit 4bc47af05b
3 changed files with 17 additions and 8 deletions
+11 -4
View File
@@ -3,17 +3,14 @@ package compose
import (
"context"
"fmt"
"strings"
composecli "github.com/compose-spec/compose-go/v2/cli"
"github.com/compose-spec/compose-go/v2/types"
)
// FakeProjectName is a placeholder name for the project to be able to strip it from the resource names used as prefix.
const FakeProjectName = "f-a-k-e"
func LoadProject(ctx context.Context, paths []string, opts ...composecli.ProjectOptionsFn) (*types.Project, error) {
defaultOpts := []composecli.ProjectOptionsFn{
composecli.WithName(FakeProjectName),
// First apply os.Environment, always wins.
composecli.WithOsEnv,
// Set the local .env file to be loaded by WithDotEnv. COMPOSE_DISABLE_ENV_FILE can disable it.
@@ -43,6 +40,7 @@ func LoadProject(ctx context.Context, paths []string, opts ...composecli.Project
return nil, err
}
removeProjectPrefixFromNames(project)
if project, err = transformServicesCaddyExtension(project); err != nil {
return nil, err
}
@@ -57,3 +55,12 @@ func LoadProject(ctx context.Context, paths []string, opts ...composecli.Project
return project, nil
}
// removeProjectPrefixFromNames removes the project name prefix from volume names.
func removeProjectPrefixFromNames(project *types.Project) {
prefix := project.Name + "_"
for name, vol := range project.Volumes {
vol.Name = strings.TrimPrefix(vol.Name, prefix)
project.Volumes[name] = vol
}
}
+5 -4
View File
@@ -5,7 +5,6 @@ import (
"maps"
"os"
"slices"
"strings"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/docker/api/types/container"
@@ -126,7 +125,8 @@ func resourcesFromCompose(service types.ServiceConfig) api.ContainerResources {
}
// Convert GPU device requests from compose format, appending "gpu" capability.
resources.DeviceReservations = append(resources.DeviceReservations, deviceReservationsFromCompose(service.Gpus, "gpu")...)
resources.DeviceReservations = append(resources.DeviceReservations,
deviceReservationsFromCompose(service.Gpus, "gpu")...)
// Map resources from deploy section if specified.
if service.Deploy != nil {
@@ -144,7 +144,8 @@ func resourcesFromCompose(service types.ServiceConfig) api.ContainerResources {
resources.MemoryReservation = int64(service.Deploy.Resources.Reservations.MemoryBytes)
}
// Handle arbitrary device reservations (same structure as Gpus above).
resources.DeviceReservations = append(resources.DeviceReservations, deviceReservationsFromCompose(service.Deploy.Resources.Reservations.Devices)...)
resources.DeviceReservations = append(resources.DeviceReservations,
deviceReservationsFromCompose(service.Deploy.Resources.Reservations.Devices)...)
}
}
@@ -238,7 +239,7 @@ func dockerVolumeSpecFromCompose(serviceVolume types.ServiceVolumeConfig, volume
Name: serviceVolume.Source,
Type: api.VolumeTypeVolume,
VolumeOptions: &api.VolumeOptions{
Name: strings.TrimPrefix(volume.Name, FakeProjectName+"_"),
Name: volume.Name,
},
}
+1
View File
@@ -48,6 +48,7 @@ func loadProjectFromContent(t *testing.T, content string) (*types.Project, error
return nil, err
}
removeProjectPrefixFromNames(project)
// Apply extension transformations since we're not using LoadProject.
if project, err = transformServicesCaddyExtension(project); err != nil {
return nil, err