Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
46 changes: 39 additions & 7 deletions internal/orchestrator/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"regexp"
"slices"
"strconv"
"strings"

"github.com/arduino/go-paths-helper"
Expand All @@ -39,6 +42,11 @@ type AppStatusInfo struct {
Status Status
}

type containerState struct {
Status Status
StatusMessage string
}

// parseAppStatus takes all the containers that matches the DockerAppLabel,
// and construct a map of the state of an app and all its dependencies state.
// For app that have at least 1 dependency, we calculate the overall state
Expand All @@ -51,13 +59,16 @@ type AppStatusInfo struct {
// starting: at least one starting
func parseAppStatus(containers []container.Summary) []AppStatusInfo {
apps := make([]AppStatusInfo, 0, len(containers))
appsStatusMap := make(map[string][]Status)
appsStatusMap := make(map[string][]containerState)
for _, c := range containers {
appPath, ok := c.Labels[DockerAppPathLabel]
if !ok {
continue
}
appsStatusMap[appPath] = append(appsStatusMap[appPath], StatusFromDockerState(c.State))
appsStatusMap[appPath] = append(appsStatusMap[appPath], containerState{
Status: StatusFromDockerState(c.State),
StatusMessage: c.Status,
})
}

appendResult := func(appPath *paths.Path, status Status) {
Expand All @@ -73,27 +84,31 @@ func parseAppStatus(containers []container.Summary) []AppStatusInfo {
appPath := paths.New(appPath)

// running: all running
if !slices.ContainsFunc(s, func(v Status) bool { return v != StatusRunning }) {
if !slices.ContainsFunc(s, func(v containerState) bool { return v.Status != StatusRunning }) {
appendResult(appPath, StatusRunning)
continue
}
// stopped: all stopped
if !slices.ContainsFunc(s, func(v Status) bool { return v != StatusStopped }) {
if !slices.ContainsFunc(s, func(v containerState) bool { return v.Status != StatusStopped }) {
appendResult(appPath, StatusStopped)
continue
}

// ...else we have multiple different status we calculate the status
// among the possible left: {failed, stopping, starting}
if slices.ContainsFunc(s, func(v Status) bool { return v == StatusFailed }) {
if slices.ContainsFunc(s, func(v containerState) bool { return v.Status == StatusFailed }) {
appendResult(appPath, StatusFailed)
continue
}
if slices.ContainsFunc(s, func(v Status) bool { return v == StatusStopping }) {
if slices.ContainsFunc(s, func(v containerState) bool { return v.Status == StatusStopped && checkExitCode(v) }) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said in a previous review

Why don't you update this mapping function and don't account for the status message there? If a container exited with anything that isn't 128 + 9 we could consider that a failing state, because it means that the process exited without Docker killing it.

I would like to move the parsing function checkExitCode to the StatusFromDockerState mapping function. I think it is clearer to have a single function for mapping docker container status to our app status, instead of adding a special check here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've finally change it to a check bool function, that parse and check that the code is grater than 128, so we now if it was killed or it failed

appendResult(appPath, StatusFailed)
continue
}
if slices.ContainsFunc(s, func(v containerState) bool { return v.Status == StatusStopping }) {
appendResult(appPath, StatusStopping)
continue
}
if slices.ContainsFunc(s, func(v Status) bool { return v == StatusStarting }) {
if slices.ContainsFunc(s, func(v containerState) bool { return v.Status == StatusStarting }) {
appendResult(appPath, StatusStarting)
continue
}
Expand Down Expand Up @@ -250,3 +265,20 @@ func setStatusLeds(trigger LedTrigger) error {
}
return nil
}

func checkExitCode(state containerState) bool {
var exitCodeRegex = regexp.MustCompile(`Exited \((\d+)\)`)
result := false
matches := exitCodeRegex.FindStringSubmatch(state.StatusMessage)

exitCode, err := strconv.Atoi(matches[1])
if err != nil {
slog.Error("Failed to parse exit code from status message", slog.String("statusMessage", state.StatusMessage), slog.String("error", err.Error()))
return false
}
if exitCode >= 0 && exitCode < 128 {
result = true
}

return result
}
25 changes: 20 additions & 5 deletions internal/orchestrator/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,75 @@ import (

"github.com/docker/docker/api/types/container"
"github.com/stretchr/testify/require"
"go.bug.st/f"
)

func TestParseAppStatus(t *testing.T) {
tests := []struct {
name string
containerState []container.ContainerState
statusMessage []string
want Status
}{
{
name: "everything running",
containerState: []container.ContainerState{container.StateRunning, container.StateRunning},
statusMessage: []string{"Up 5 minutes", "Up 10 minutes"},
want: StatusRunning,
},
{
name: "everything stopped",
containerState: []container.ContainerState{container.StateCreated, container.StatePaused, container.StateExited},
statusMessage: []string{"Created", "Paused", "Exited (137)"},
want: StatusStopped,
},
{
name: "failed container",
containerState: []container.ContainerState{container.StateRunning, container.StateDead},
statusMessage: []string{"Up 5 minutes", "Dead"},
want: StatusFailed,
},
{
name: "failed container takes precedence over stopping and starting",
containerState: []container.ContainerState{container.StateRunning, container.StateDead, container.StateRemoving, container.StateRestarting},
statusMessage: []string{"Up 5 minutes", "Dead", "Removing", "Restarting"},
want: StatusFailed,
},
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a test for the new check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created as status failed!
i've also added statusMessage string example inspired by some real cases

name: "stopping",
containerState: []container.ContainerState{container.StateRunning, container.StateRemoving},
statusMessage: []string{"Up 5 minutes", "Removing"},
want: StatusStopping,
},
{
name: "stopping takes precedence over starting",
containerState: []container.ContainerState{container.StateRunning, container.StateRestarting, container.StateRemoving},
statusMessage: []string{"Up 5 minutes", "Restarting", "Removing"},
want: StatusStopping,
},
{
name: "starting",
containerState: []container.ContainerState{container.StateRestarting, container.StateExited},
statusMessage: []string{"Restarting", "Exited (129)"},
want: StatusStarting,
},
{
name: "failed",
containerState: []container.ContainerState{container.StateRestarting, container.StateExited},
statusMessage: []string{"Restarting", "Exited (0)"},
want: StatusFailed,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
input := f.Map(tc.containerState, func(c container.ContainerState) container.Summary {
return container.Summary{
var input []container.Summary
for i, c := range tc.containerState {
input = append(input, container.Summary{
Labels: map[string]string{DockerAppPathLabel: "path1"},
State: c,
}
})
Status: tc.statusMessage[i],
})
}
res := parseAppStatus(input)
require.Len(t, res, 1)
require.Equal(t, tc.want, res[0].Status)
Expand Down