mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 03:53:33 +00:00
refactor: strip project name from volume names without mangling project name
This commit is contained in:
@@ -3,17 +3,14 @@ package compose
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
composecli "github.com/compose-spec/compose-go/v2/cli"
|
composecli "github.com/compose-spec/compose-go/v2/cli"
|
||||||
"github.com/compose-spec/compose-go/v2/types"
|
"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) {
|
func LoadProject(ctx context.Context, paths []string, opts ...composecli.ProjectOptionsFn) (*types.Project, error) {
|
||||||
defaultOpts := []composecli.ProjectOptionsFn{
|
defaultOpts := []composecli.ProjectOptionsFn{
|
||||||
composecli.WithName(FakeProjectName),
|
|
||||||
// First apply os.Environment, always wins.
|
// First apply os.Environment, always wins.
|
||||||
composecli.WithOsEnv,
|
composecli.WithOsEnv,
|
||||||
// Set the local .env file to be loaded by WithDotEnv. COMPOSE_DISABLE_ENV_FILE can disable it.
|
// 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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
removeProjectPrefixFromNames(project)
|
||||||
if project, err = transformServicesCaddyExtension(project); err != nil {
|
if project, err = transformServicesCaddyExtension(project); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -57,3 +55,12 @@ func LoadProject(ctx context.Context, paths []string, opts ...composecli.Project
|
|||||||
|
|
||||||
return project, nil
|
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,7 +5,6 @@ import (
|
|||||||
"maps"
|
"maps"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/v2/types"
|
"github.com/compose-spec/compose-go/v2/types"
|
||||||
"github.com/docker/docker/api/types/container"
|
"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.
|
// 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.
|
// Map resources from deploy section if specified.
|
||||||
if service.Deploy != nil {
|
if service.Deploy != nil {
|
||||||
@@ -144,7 +144,8 @@ func resourcesFromCompose(service types.ServiceConfig) api.ContainerResources {
|
|||||||
resources.MemoryReservation = int64(service.Deploy.Resources.Reservations.MemoryBytes)
|
resources.MemoryReservation = int64(service.Deploy.Resources.Reservations.MemoryBytes)
|
||||||
}
|
}
|
||||||
// Handle arbitrary device reservations (same structure as Gpus above).
|
// 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,
|
Name: serviceVolume.Source,
|
||||||
Type: api.VolumeTypeVolume,
|
Type: api.VolumeTypeVolume,
|
||||||
VolumeOptions: &api.VolumeOptions{
|
VolumeOptions: &api.VolumeOptions{
|
||||||
Name: strings.TrimPrefix(volume.Name, FakeProjectName+"_"),
|
Name: volume.Name,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ func loadProjectFromContent(t *testing.T, content string) (*types.Project, error
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
removeProjectPrefixFromNames(project)
|
||||||
// Apply extension transformations since we're not using LoadProject.
|
// Apply extension transformations since we're not using LoadProject.
|
||||||
if project, err = transformServicesCaddyExtension(project); err != nil {
|
if project, err = transformServicesCaddyExtension(project); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user