Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compose): Support container_name property #1632

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compose/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 3 additions & 7 deletions compose/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -198,8 +198,6 @@ func (v1 *v1Compose) refreshExistingNetworks(ctx context.Context, embeddedProjec
}
}

embeddedProject.Status.Networks = existingNetworks

return nil
}

Expand Down Expand Up @@ -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 {
Expand All @@ -255,8 +253,6 @@ func (v1 *v1Compose) refreshExistingVolumes(ctx context.Context, embeddedProject
}
}

embeddedProject.Status.Networks = existingVolumes

return nil
}

Expand Down
8 changes: 4 additions & 4 deletions internal/cli/kraft/compose/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/kraft/compose/down/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/kraft/compose/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/kraft/compose/pause/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/kraft/compose/ps/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/kraft/compose/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/kraft/compose/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/kraft/compose/unpause/unpause.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading