Skip to content

Commit

Permalink
Add services name to logged containers
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano committed Jul 26, 2024
1 parent eab5375 commit f5f602f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 31 deletions.
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
6 changes: 5 additions & 1 deletion internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ type NetworkDescription struct {
type ContainerDescription struct {
Config struct {
Image string
Labels map[string]string
Labels struct {
ComposeProject string `json:"com.docker.compose.project"`
ComposeService string `json:"com.docker.compose.service"`
ComposeVersion string `json:"com.docker.compose.version"`
}
}
ID string
State struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/stack/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,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
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

0 comments on commit f5f602f

Please sign in to comment.