From 2e1e57a130e7a62c71c18470d6f99ef761dd37a3 Mon Sep 17 00:00:00 2001 From: Luca Seritan Date: Tue, 7 May 2024 13:40:27 +0300 Subject: [PATCH] feat(compose): Support depends_on property The depends_on property allows specifying dependencies between services. The services will always be started in the order of the dependencies and always be stopped in the opposite order. At the moment we cannot do healthchecks in krafkit, so the condition of the dependency is ignored. Signed-off-by: Luca Seritan --- internal/cli/kraft/compose/create/create.go | 4 +++- internal/cli/kraft/compose/down/down.go | 3 ++- internal/cli/kraft/compose/pause/pause.go | 3 ++- internal/cli/kraft/compose/start/start.go | 3 ++- internal/cli/kraft/compose/stop/stop.go | 3 ++- internal/cli/kraft/compose/unpause/unpause.go | 3 ++- 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/internal/cli/kraft/compose/create/create.go b/internal/cli/kraft/compose/create/create.go index ec24155f4..b24154783 100644 --- a/internal/cli/kraft/compose/create/create.go +++ b/internal/cli/kraft/compose/create/create.go @@ -274,7 +274,9 @@ func (opts *CreateOptions) Run(ctx context.Context, args []string) error { return err } - for _, service := range services { + orderedServices := project.ServicesOrderedByDependencies(ctx, services, true) + for _, service := range orderedServices { + log.G(ctx).Debugf("creating service %s...", service.Name) alreadyCreated := false for _, machine := range machines.Items { if service.ContainerName != machine.Name { diff --git a/internal/cli/kraft/compose/down/down.go b/internal/cli/kraft/compose/down/down.go index 939a59b48..edaaae41d 100644 --- a/internal/cli/kraft/compose/down/down.go +++ b/internal/cli/kraft/compose/down/down.go @@ -99,7 +99,8 @@ func (opts *DownOptions) Run(ctx context.Context, args []string) error { return err } - for _, service := range project.Services { + orderedServices := project.ServicesReversedByDependencies(ctx, project.Services, false) + for _, service := range orderedServices { for _, machine := range machines.Items { if service.ContainerName == machine.Name { if err := removeService(ctx, service); err != nil { diff --git a/internal/cli/kraft/compose/pause/pause.go b/internal/cli/kraft/compose/pause/pause.go index 885f6563a..42d885c02 100644 --- a/internal/cli/kraft/compose/pause/pause.go +++ b/internal/cli/kraft/compose/pause/pause.go @@ -91,8 +91,9 @@ func (opts *PauseOptions) Run(ctx context.Context, args []string) error { return err } + orderedServices := project.ServicesReversedByDependencies(ctx, services, false) machinesToPause := []string{} - for _, service := range services { + for _, service := range orderedServices { for _, machine := range machines.Items { if service.ContainerName == machine.Name && machine.Status.State == machineapi.MachineStateRunning { machinesToPause = append(machinesToPause, machine.Name) diff --git a/internal/cli/kraft/compose/start/start.go b/internal/cli/kraft/compose/start/start.go index 15aeba343..da16432f5 100644 --- a/internal/cli/kraft/compose/start/start.go +++ b/internal/cli/kraft/compose/start/start.go @@ -91,8 +91,9 @@ func (opts *StartOptions) Run(ctx context.Context, args []string) error { return err } + orderedServices := project.ServicesOrderedByDependencies(ctx, services, true) machinesToStart := []string{} - for _, service := range services { + for _, service := range orderedServices { for _, machine := range machines.Items { if service.ContainerName == machine.Name { if machine.Status.State == machineapi.MachineStateCreated || machine.Status.State == machineapi.MachineStateExited { diff --git a/internal/cli/kraft/compose/stop/stop.go b/internal/cli/kraft/compose/stop/stop.go index 507601298..97ece8131 100644 --- a/internal/cli/kraft/compose/stop/stop.go +++ b/internal/cli/kraft/compose/stop/stop.go @@ -91,8 +91,9 @@ func (opts *StopOptions) Run(ctx context.Context, args []string) error { return err } + orderedServices := project.ServicesReversedByDependencies(ctx, services, false) machinesToStop := []string{} - for _, service := range services { + for _, service := range orderedServices { for _, machine := range machines.Items { if service.ContainerName == machine.Name && (machine.Status.State == machineapi.MachineStateRunning || diff --git a/internal/cli/kraft/compose/unpause/unpause.go b/internal/cli/kraft/compose/unpause/unpause.go index e2ab7faae..511ba9189 100644 --- a/internal/cli/kraft/compose/unpause/unpause.go +++ b/internal/cli/kraft/compose/unpause/unpause.go @@ -91,8 +91,9 @@ func (opts *UnpauseOptions) Run(ctx context.Context, args []string) error { return err } + orderedServices := project.ServicesOrderedByDependencies(ctx, services, true) machinesToUnpause := []string{} - for _, service := range services { + for _, service := range orderedServices { for _, machine := range machines.Items { if service.ContainerName == machine.Name { if machine.Status.State == machineapi.MachineStatePaused {