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

Add services name to containers logged while waiting for healthy #1990

Merged
merged 2 commits into from
Jul 30, 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
45 changes: 18 additions & 27 deletions internal/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,35 +369,26 @@ func (p *Project) WaitForHealthy(ctx context.Context, opts CommandOptions) error
return err
}

for _, containerDescription := range descriptions {

for _, d := range descriptions {
switch {
// No healthcheck defined for service
if containerDescription.State.Status == "running" && containerDescription.State.Health == nil {
logger.Debugf("Container %s status: %s (no health status)", containerDescription.ID, containerDescription.State.Status)
continue
}

// Service is up and running and it's healthy
if containerDescription.State.Status == "running" && containerDescription.State.Health.Status == "healthy" {
logger.Debugf("Container %s status: %s (health: %s)", containerDescription.ID, containerDescription.State.Status, containerDescription.State.Health.Status)
continue
}

// Container started and finished with exit code 0
if containerDescription.State.Status == "exited" && containerDescription.State.ExitCode == 0 {
logger.Debugf("Container %s status: %s (exit code: %d)", containerDescription.ID, containerDescription.State.Status, containerDescription.State.ExitCode)
continue
}

// Container exited with code > 0
if containerDescription.State.Status == "exited" && containerDescription.State.ExitCode > 0 {
logger.Debugf("Container %s status: %s (exit code: %d)", containerDescription.ID, containerDescription.State.Status, containerDescription.State.ExitCode)
return fmt.Errorf("container (ID: %s) exited with code %d", containerDescription.ID, containerDescription.State.ExitCode)
}

case d.State.Status == "running" && d.State.Health == nil:
logger.Debugf("Container %s (%s) status: %s (no health status)", d.Config.Labels.ComposeService, d.ID, d.State.Status)
// Service is up and running and it's healthy
case d.State.Status == "running" && d.State.Health.Status == "healthy":
logger.Debugf("Container %s (%s) status: %s (health: %s)", d.Config.Labels.ComposeService, d.ID, d.State.Status, d.State.Health.Status)
// Container started and finished with exit code 0
case d.State.Status == "exited" && d.State.ExitCode == 0:
logger.Debugf("Container %s (%s) status: %s (exit code: %d)", d.Config.Labels.ComposeService, d.ID, d.State.Status, d.State.ExitCode)
// Container exited with code > 0
case d.State.Status == "exited" && d.State.ExitCode > 0:
logger.Debugf("Container %s (%s) status: %s (exit code: %d)", d.Config.Labels.ComposeService, d.ID, d.State.Status, d.State.ExitCode)
return fmt.Errorf("container (ID: %s) exited with code %d", d.ID, d.State.ExitCode)
// Any different status is considered unhealthy
logger.Debugf("Container %s status: unhealthy", containerDescription.ID)
healthy = false
default:
logger.Debugf("Container %s (%s) status: unhealthy", d.Config.Labels.ComposeService, d.ID)
healthy = false
}
}

// end loop before timeout if healthy
Expand Down
9 changes: 8 additions & 1 deletion internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type NetworkDescription struct {
type ContainerDescription struct {
Config struct {
Image string
Labels map[string]string
Labels ConfigLabels
}
ID string
State struct {
Expand All @@ -44,6 +44,13 @@ type ContainerDescription struct {
}
}

// ConfigLabels are the labels included in the config in container descriptions.
type ConfigLabels struct {
ComposeProject string `json:"com.docker.compose.project"`
ComposeService string `json:"com.docker.compose.service"`
ComposeVersion string `json:"com.docker.compose.version"`
}

// String function dumps string representation of the container description.
func (c *ContainerDescription) String() string {
b, err := json.Marshal(c)
Expand Down
4 changes: 1 addition & 3 deletions internal/stack/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ type ServiceStatus struct {
const readyServicesSuffix = "is_ready"

const (
// serviceLabelDockerCompose is the label with the service name created by docker-compose
serviceLabelDockerCompose = "com.docker.compose.service"
// projectLabelDockerCompose is the label with the project name created by docker-compose
projectLabelDockerCompose = "com.docker.compose.project"
)
Expand Down Expand Up @@ -213,7 +211,7 @@ func dockerComposeStatus(ctx context.Context, options Options) ([]ServiceStatus,

func newServiceStatus(description *docker.ContainerDescription) (*ServiceStatus, error) {
service := ServiceStatus{
Name: description.Config.Labels[serviceLabelDockerCompose],
Name: description.Config.Labels.ComposeService,
Status: description.State.Status,
Version: getVersionFromDockerImage(description.Config.Image),
}
Expand Down
12 changes: 6 additions & 6 deletions internal/stack/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func TestNewServiceStatus(t *testing.T) {
description: docker.ContainerDescription{
Config: struct {
Image string
Labels map[string]string
Labels docker.ConfigLabels
}{
Image: "docker.test:1.42.0",
Labels: map[string]string{"com.docker.compose.service": "myservice", "foo": "bar"},
Labels: docker.ConfigLabels{ComposeService: "myservice"},
},
ID: "123456789ab",
State: struct {
Expand Down Expand Up @@ -85,10 +85,10 @@ func TestNewServiceStatus(t *testing.T) {
description: docker.ContainerDescription{
Config: struct {
Image string
Labels map[string]string
Labels docker.ConfigLabels
}{
Image: "docker.test:1.42.0",
Labels: map[string]string{"com.docker.compose.service": "myservice", "foo": "bar"},
Labels: docker.ConfigLabels{ComposeService: "myservice"},
},
ID: "123456789ab",
State: struct {
Expand Down Expand Up @@ -119,10 +119,10 @@ func TestNewServiceStatus(t *testing.T) {
description: docker.ContainerDescription{
Config: struct {
Image string
Labels map[string]string
Labels docker.ConfigLabels
}{
Image: "docker.test:1.42.0",
Labels: map[string]string{"com.docker.compose.service": "myservice", "foo": "bar"},
Labels: docker.ConfigLabels{ComposeService: "myservice"},
},
ID: "123456789ab",
State: struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/stack/serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func (sp *serverlessProvider) localAgentStatus() ([]ServiceStatus, error) {
func localServiceNames(project string) ([]string, error) {
services := []string{}
serviceFunc := func(description docker.ContainerDescription) error {
services = append(services, description.Config.Labels[serviceLabelDockerCompose])
services = append(services, description.Config.Labels.ComposeService)
return nil
}

Expand Down Expand Up @@ -518,7 +518,7 @@ func runOnLocalServices(project string, serviceFunc func(docker.ContainerDescrip
}

for _, containerDescription := range containerDescriptions {
serviceName := containerDescription.Config.Labels[serviceLabelDockerCompose]
serviceName := containerDescription.Config.Labels.ComposeService
if strings.HasSuffix(serviceName, readyServicesSuffix) {
continue
}
Expand Down