From c4d5447eab5c03098a89736d0d72d192114af74e Mon Sep 17 00:00:00 2001 From: Luca Seritan Date: Mon, 6 May 2024 14:48:10 +0300 Subject: [PATCH 1/2] feat(compose): Support container_name property The container_name property allows setting a custom name for the machines. Signed-off-by: Luca Seritan --- compose/project.go | 5 ++++- internal/cli/kraft/compose/create/create.go | 8 ++++---- internal/cli/kraft/compose/down/down.go | 4 ++-- internal/cli/kraft/compose/logs/logs.go | 2 +- internal/cli/kraft/compose/pause/pause.go | 2 +- internal/cli/kraft/compose/ps/ps.go | 2 +- internal/cli/kraft/compose/start/start.go | 2 +- internal/cli/kraft/compose/stop/stop.go | 2 +- internal/cli/kraft/compose/unpause/unpause.go | 2 +- 9 files changed, 16 insertions(+), 13 deletions(-) diff --git a/compose/project.go b/compose/project.go index 035b434e5..bca5ecb67 100644 --- a/compose/project.go +++ b/compose/project.go @@ -98,7 +98,10 @@ func (project *Project) Validate(ctx context.Context) error { } project.Project, err = project.WithServicesTransform(func(name string, service types.ServiceConfig) (types.ServiceConfig, error) { - service.Name = fmt.Sprint(project.Name, "-", name) + service.Name = name + if service.ContainerName == "" { + service.ContainerName = fmt.Sprint(project.Name, "-", name) + } if service.Platform == "" { hostPlatform, _, err := mplatform.Detect(ctx) if err != nil { diff --git a/internal/cli/kraft/compose/create/create.go b/internal/cli/kraft/compose/create/create.go index a740bbb78..ec24155f4 100644 --- a/internal/cli/kraft/compose/create/create.go +++ b/internal/cli/kraft/compose/create/create.go @@ -277,7 +277,7 @@ func (opts *CreateOptions) Run(ctx context.Context, args []string) error { for _, service := range services { alreadyCreated := false for _, machine := range machines.Items { - if service.Name != machine.Name { + if service.ContainerName != machine.Name { continue } if machine.Status.State == machineapi.MachineStateRunning || machine.Status.State == machineapi.MachineStateCreated { @@ -288,7 +288,7 @@ func (opts *CreateOptions) Run(ctx context.Context, args []string) error { Platform: machine.Spec.Platform, } - if err := rmOpts.Run(ctx, []string{service.Name}); err != nil { + if err := rmOpts.Run(ctx, []string{service.ContainerName}); err != nil { return err } @@ -317,7 +317,7 @@ func (opts *CreateOptions) Run(ctx context.Context, args []string) error { if machine, err := machineController.Get(ctx, &machineapi.Machine{ ObjectMeta: metav1.ObjectMeta{ - Name: service.Name, + Name: service.ContainerName, }, }); err == nil && machine.Status.State == machineapi.MachineStateCreated { projectMachines = append(projectMachines, machine.ObjectMeta) @@ -506,7 +506,7 @@ func createService(ctx context.Context, project *compose.Project, service types. Detach: true, Env: environ, Memory: memory, - Name: service.Name, + Name: service.ContainerName, Networks: networks, NoStart: true, Platform: plat, diff --git a/internal/cli/kraft/compose/down/down.go b/internal/cli/kraft/compose/down/down.go index 1b3749c96..939a59b48 100644 --- a/internal/cli/kraft/compose/down/down.go +++ b/internal/cli/kraft/compose/down/down.go @@ -101,7 +101,7 @@ func (opts *DownOptions) Run(ctx context.Context, args []string) error { for _, service := range project.Services { for _, machine := range machines.Items { - if service.Name == machine.Name { + if service.ContainerName == machine.Name { if err := removeService(ctx, service); err != nil { return err } @@ -139,7 +139,7 @@ func removeService(ctx context.Context, service types.ServiceConfig) error { log.G(ctx).Infof("removing service %s...", service.Name) removeOptions := machineremove.RemoveOptions{Platform: "auto"} - return removeOptions.Run(ctx, []string{service.Name}) + return removeOptions.Run(ctx, []string{service.ContainerName}) } func removeNetwork(ctx context.Context, network types.NetworkConfig) error { diff --git a/internal/cli/kraft/compose/logs/logs.go b/internal/cli/kraft/compose/logs/logs.go index 839fe2804..a579444b3 100644 --- a/internal/cli/kraft/compose/logs/logs.go +++ b/internal/cli/kraft/compose/logs/logs.go @@ -95,7 +95,7 @@ func (opts *LogsOptions) Run(ctx context.Context, args []string) error { } machine, _ := controller.Get(ctx, &machineapi.Machine{ ObjectMeta: metav1.ObjectMeta{ - Name: service.Name, + Name: service.ContainerName, }, }) if machine != nil { diff --git a/internal/cli/kraft/compose/pause/pause.go b/internal/cli/kraft/compose/pause/pause.go index dffab6061..885f6563a 100644 --- a/internal/cli/kraft/compose/pause/pause.go +++ b/internal/cli/kraft/compose/pause/pause.go @@ -94,7 +94,7 @@ func (opts *PauseOptions) Run(ctx context.Context, args []string) error { machinesToPause := []string{} for _, service := range services { for _, machine := range machines.Items { - if service.Name == machine.Name && machine.Status.State == machineapi.MachineStateRunning { + if service.ContainerName == machine.Name && machine.Status.State == machineapi.MachineStateRunning { machinesToPause = append(machinesToPause, machine.Name) } } diff --git a/internal/cli/kraft/compose/ps/ps.go b/internal/cli/kraft/compose/ps/ps.go index d7d74b09a..9eeeb93ac 100644 --- a/internal/cli/kraft/compose/ps/ps.go +++ b/internal/cli/kraft/compose/ps/ps.go @@ -114,7 +114,7 @@ func (opts *PsOptions) Run(ctx context.Context, args []string) error { for _, machine := range embeddedProject.Status.Machines { orphaned := true for _, service := range project.Services { - if service.Name == machine.Name { + if service.ContainerName == machine.Name { orphaned = false break } diff --git a/internal/cli/kraft/compose/start/start.go b/internal/cli/kraft/compose/start/start.go index 646a65c92..15aeba343 100644 --- a/internal/cli/kraft/compose/start/start.go +++ b/internal/cli/kraft/compose/start/start.go @@ -94,7 +94,7 @@ func (opts *StartOptions) Run(ctx context.Context, args []string) error { machinesToStart := []string{} for _, service := range services { for _, machine := range machines.Items { - if service.Name == machine.Name { + if service.ContainerName == machine.Name { if machine.Status.State == machineapi.MachineStateCreated || machine.Status.State == machineapi.MachineStateExited { machinesToStart = append(machinesToStart, machine.Name) } diff --git a/internal/cli/kraft/compose/stop/stop.go b/internal/cli/kraft/compose/stop/stop.go index 4b2bfabc1..507601298 100644 --- a/internal/cli/kraft/compose/stop/stop.go +++ b/internal/cli/kraft/compose/stop/stop.go @@ -94,7 +94,7 @@ func (opts *StopOptions) Run(ctx context.Context, args []string) error { machinesToStop := []string{} for _, service := range services { for _, machine := range machines.Items { - if service.Name == machine.Name && + if service.ContainerName == machine.Name && (machine.Status.State == machineapi.MachineStateRunning || machine.Status.State == machineapi.MachineStatePaused) { machinesToStop = append(machinesToStop, machine.Name) diff --git a/internal/cli/kraft/compose/unpause/unpause.go b/internal/cli/kraft/compose/unpause/unpause.go index f5f40ce2c..e2ab7faae 100644 --- a/internal/cli/kraft/compose/unpause/unpause.go +++ b/internal/cli/kraft/compose/unpause/unpause.go @@ -94,7 +94,7 @@ func (opts *UnpauseOptions) Run(ctx context.Context, args []string) error { machinesToUnpause := []string{} for _, service := range services { for _, machine := range machines.Items { - if service.Name == machine.Name { + if service.ContainerName == machine.Name { if machine.Status.State == machineapi.MachineStatePaused { machinesToUnpause = append(machinesToUnpause, machine.Name) } From a42101f1bb8cf4f1deb556e7b7375fa4a422e100 Mon Sep 17 00:00:00 2001 From: Luca Seritan Date: Mon, 6 May 2024 15:05:15 +0300 Subject: [PATCH 2/2] fix(compose): Correctly update volumes This commit corrects a typo that was updating the networks instead of the volumes in the refreshVolumes function. It also removes 2 noop assignments. Signed-off-by: Luca Seritan --- compose/v1.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/compose/v1.go b/compose/v1.go index 9f9845cbb..5cd0bdcc7 100644 --- a/compose/v1.go +++ b/compose/v1.go @@ -90,7 +90,7 @@ func (v1 *v1Compose) refreshRunningServices(ctx context.Context, embeddedProject for _, machine := range embeddedProject.Status.Machines { isService := false for _, service := range project.Services { - if service.Name == machine.Name { + if service.ContainerName == machine.Name { isService = true break } @@ -118,7 +118,7 @@ func (v1 *v1Compose) refreshRunningServices(ctx context.Context, embeddedProject } isService := false for _, service := range project.Services { - if service.Name == m.Name { + if service.ContainerName == m.Name { isService = true break } @@ -198,8 +198,6 @@ func (v1 *v1Compose) refreshExistingNetworks(ctx context.Context, embeddedProjec } } - embeddedProject.Status.Networks = existingNetworks - return nil } @@ -229,7 +227,7 @@ func (v1 *v1Compose) refreshExistingVolumes(ctx context.Context, embeddedProject } } - embeddedProject.Status.Networks = existingVolumes + embeddedProject.Status.Volumes = existingVolumes for _, volume := range project.Volumes { if volume.External { @@ -255,8 +253,6 @@ func (v1 *v1Compose) refreshExistingVolumes(ctx context.Context, embeddedProject } } - embeddedProject.Status.Networks = existingVolumes - return nil }