Skip to content

Commit

Permalink
feat(compose): Support depends_on property
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
LucaSeri committed May 9, 2024
1 parent b9d9eb7 commit 2e1e57a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 6 deletions.
4 changes: 3 additions & 1 deletion internal/cli/kraft/compose/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/kraft/compose/down/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/kraft/compose/pause/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/kraft/compose/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/kraft/compose/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/kraft/compose/unpause/unpause.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2e1e57a

Please sign in to comment.