Skip to content

Commit f1efbb8

Browse files
thaJeztahndeloof
authored andcommitted
use enum-consts for State and Health
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 1d52012 commit f1efbb8

File tree

4 files changed

+55
-48
lines changed

4 files changed

+55
-48
lines changed

pkg/api/dryrunclient.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (d *DryRunClient) ContainerInspect(ctx context.Context, container string) (
130130
ID: id,
131131
Name: container,
132132
State: &containerType.State{
133-
Status: "running", // needed for --wait option
133+
Status: containerType.StateRunning, // needed for --wait option
134134
Health: &containerType.Health{
135135
Status: containerType.Healthy, // needed for healthcheck control
136136
},

pkg/compose/convergence.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -828,46 +828,46 @@ func (s *composeService) getLinks(ctx context.Context, projectName string, servi
828828

829829
func (s *composeService) isServiceHealthy(ctx context.Context, containers Containers, fallbackRunning bool) (bool, error) {
830830
for _, c := range containers {
831-
container, err := s.apiClient().ContainerInspect(ctx, c.ID)
831+
ctr, err := s.apiClient().ContainerInspect(ctx, c.ID)
832832
if err != nil {
833833
return false, err
834834
}
835-
name := container.Name[1:]
835+
name := ctr.Name[1:]
836836

837-
if container.State.Status == "exited" {
838-
return false, fmt.Errorf("container %s exited (%d)", name, container.State.ExitCode)
837+
if ctr.State.Status == containerType.StateExited {
838+
return false, fmt.Errorf("container %s exited (%d)", name, ctr.State.ExitCode)
839839
}
840840

841-
if container.Config.Healthcheck == nil && fallbackRunning {
841+
if ctr.Config.Healthcheck == nil && fallbackRunning {
842842
// Container does not define a health check, but we can fall back to "running" state
843-
return container.State != nil && container.State.Status == "running", nil
843+
return ctr.State != nil && ctr.State.Status == containerType.StateRunning, nil
844844
}
845845

846-
if container.State == nil || container.State.Health == nil {
846+
if ctr.State == nil || ctr.State.Health == nil {
847847
return false, fmt.Errorf("container %s has no healthcheck configured", name)
848848
}
849-
switch container.State.Health.Status {
849+
switch ctr.State.Health.Status {
850850
case containerType.Healthy:
851851
// Continue by checking the next container.
852852
case containerType.Unhealthy:
853853
return false, fmt.Errorf("container %s is unhealthy", name)
854854
case containerType.Starting:
855855
return false, nil
856856
default:
857-
return false, fmt.Errorf("container %s had unexpected health status %q", name, container.State.Health.Status)
857+
return false, fmt.Errorf("container %s had unexpected health status %q", name, ctr.State.Health.Status)
858858
}
859859
}
860860
return true, nil
861861
}
862862

863863
func (s *composeService) isServiceCompleted(ctx context.Context, containers Containers) (bool, int, error) {
864864
for _, c := range containers {
865-
container, err := s.apiClient().ContainerInspect(ctx, c.ID)
865+
ctr, err := s.apiClient().ContainerInspect(ctx, c.ID)
866866
if err != nil {
867867
return false, 0, err
868868
}
869-
if container.State != nil && container.State.Status == "exited" {
870-
return true, container.State.ExitCode, nil
869+
if ctr.State != nil && ctr.State.Status == containerType.StateExited {
870+
return true, ctr.State.ExitCode, nil
871871
}
872872
}
873873
return false, 0, nil

pkg/compose/ps.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"sort"
2222
"strings"
2323

24+
"github.com/docker/docker/api/types/container"
2425
"golang.org/x/sync/errgroup"
2526

2627
"github.com/docker/compose/v2/pkg/api"
@@ -42,13 +43,13 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options api
4243
}
4344
summary := make([]api.ContainerSummary, len(containers))
4445
eg, ctx := errgroup.WithContext(ctx)
45-
for i, container := range containers {
46+
for i, ctr := range containers {
4647
eg.Go(func() error {
47-
publishers := make([]api.PortPublisher, len(container.Ports))
48-
sort.Slice(container.Ports, func(i, j int) bool {
49-
return container.Ports[i].PrivatePort < container.Ports[j].PrivatePort
48+
publishers := make([]api.PortPublisher, len(ctr.Ports))
49+
sort.Slice(ctr.Ports, func(i, j int) bool {
50+
return ctr.Ports[i].PrivatePort < ctr.Ports[j].PrivatePort
5051
})
51-
for i, p := range container.Ports {
52+
for i, p := range ctr.Ports {
5253
publishers[i] = api.PortPublisher{
5354
URL: p.IP,
5455
TargetPort: int(p.PrivatePort),
@@ -57,22 +58,22 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options api
5758
}
5859
}
5960

60-
inspect, err := s.apiClient().ContainerInspect(ctx, container.ID)
61+
inspect, err := s.apiClient().ContainerInspect(ctx, ctr.ID)
6162
if err != nil {
6263
return err
6364
}
6465

6566
var (
66-
health string
67+
health container.HealthStatus
6768
exitCode int
6869
)
6970
if inspect.State != nil {
7071
switch inspect.State.Status {
71-
case "running":
72+
case container.StateRunning:
7273
if inspect.State.Health != nil {
7374
health = inspect.State.Health.Status
7475
}
75-
case "exited", "dead":
76+
case container.StateExited, container.StateDead:
7677
exitCode = inspect.State.ExitCode
7778
}
7879
}
@@ -81,7 +82,7 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options api
8182
local int
8283
mounts []string
8384
)
84-
for _, m := range container.Mounts {
85+
for _, m := range ctr.Mounts {
8586
name := m.Name
8687
if name == "" {
8788
name = m.Source
@@ -93,26 +94,26 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options api
9394
}
9495

9596
var networks []string
96-
if container.NetworkSettings != nil {
97-
for k := range container.NetworkSettings.Networks {
97+
if ctr.NetworkSettings != nil {
98+
for k := range ctr.NetworkSettings.Networks {
9899
networks = append(networks, k)
99100
}
100101
}
101102

102103
summary[i] = api.ContainerSummary{
103-
ID: container.ID,
104-
Name: getCanonicalContainerName(container),
105-
Names: container.Names,
106-
Image: container.Image,
107-
Project: container.Labels[api.ProjectLabel],
108-
Service: container.Labels[api.ServiceLabel],
109-
Command: container.Command,
110-
State: container.State,
111-
Status: container.Status,
112-
Created: container.Created,
113-
Labels: container.Labels,
114-
SizeRw: container.SizeRw,
115-
SizeRootFs: container.SizeRootFs,
104+
ID: ctr.ID,
105+
Name: getCanonicalContainerName(ctr),
106+
Names: ctr.Names,
107+
Image: ctr.Image,
108+
Project: ctr.Labels[api.ProjectLabel],
109+
Service: ctr.Labels[api.ServiceLabel],
110+
Command: ctr.Command,
111+
State: ctr.State,
112+
Status: ctr.Status,
113+
Created: ctr.Created,
114+
Labels: ctr.Labels,
115+
SizeRw: ctr.SizeRw,
116+
SizeRootFs: ctr.SizeRootFs,
116117
Mounts: mounts,
117118
LocalVolumes: local,
118119
Networks: networks,

pkg/compose/ps_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import (
2222
"testing"
2323

2424
containerType "github.com/docker/docker/api/types/container"
25+
"github.com/docker/docker/api/types/filters"
2526
"go.uber.org/mock/gomock"
2627
"gotest.tools/v3/assert"
2728

2829
compose "github.com/docker/compose/v2/pkg/api"
29-
"github.com/docker/docker/api/types/filters"
3030
)
3131

3232
func TestPs(t *testing.T) {
@@ -42,10 +42,10 @@ func TestPs(t *testing.T) {
4242
args := filters.NewArgs(projectFilter(strings.ToLower(testProject)), hasConfigHashLabel())
4343
args.Add("label", "com.docker.compose.oneoff=False")
4444
listOpts := containerType.ListOptions{Filters: args, All: false}
45-
c1, inspect1 := containerDetails("service1", "123", "running", "healthy", 0)
46-
c2, inspect2 := containerDetails("service1", "456", "running", "", 0)
45+
c1, inspect1 := containerDetails("service1", "123", containerType.StateRunning, containerType.Healthy, 0)
46+
c2, inspect2 := containerDetails("service1", "456", containerType.StateRunning, "", 0)
4747
c2.Ports = []containerType.Port{{PublicPort: 80, PrivatePort: 90, IP: "localhost"}}
48-
c3, inspect3 := containerDetails("service2", "789", "exited", "", 130)
48+
c3, inspect3 := containerDetails("service2", "789", containerType.StateExited, "", 130)
4949
api.EXPECT().ContainerList(ctx, listOpts).Return([]containerType.Summary{c1, c2, c3}, nil)
5050
api.EXPECT().ContainerInspect(anyCancellableContext(), "123").Return(inspect1, nil)
5151
api.EXPECT().ContainerInspect(anyCancellableContext(), "456").Return(inspect2, nil)
@@ -56,7 +56,9 @@ func TestPs(t *testing.T) {
5656
expected := []compose.ContainerSummary{
5757
{
5858
ID: "123", Name: "123", Names: []string{"/123"}, Image: "foo", Project: strings.ToLower(testProject), Service: "service1",
59-
State: "running", Health: "healthy", Publishers: []compose.PortPublisher{},
59+
State: containerType.StateRunning,
60+
Health: containerType.Healthy,
61+
Publishers: []compose.PortPublisher{},
6062
Labels: map[string]string{
6163
compose.ProjectLabel: strings.ToLower(testProject),
6264
compose.ConfigFilesLabel: "/src/pkg/compose/testdata/compose.yaml",
@@ -66,7 +68,8 @@ func TestPs(t *testing.T) {
6668
},
6769
{
6870
ID: "456", Name: "456", Names: []string{"/456"}, Image: "foo", Project: strings.ToLower(testProject), Service: "service1",
69-
State: "running", Health: "",
71+
State: containerType.StateRunning,
72+
Health: "",
7073
Publishers: []compose.PortPublisher{{URL: "localhost", TargetPort: 90, PublishedPort: 80}},
7174
Labels: map[string]string{
7275
compose.ProjectLabel: strings.ToLower(testProject),
@@ -77,7 +80,10 @@ func TestPs(t *testing.T) {
7780
},
7881
{
7982
ID: "789", Name: "789", Names: []string{"/789"}, Image: "foo", Project: strings.ToLower(testProject), Service: "service2",
80-
State: "exited", Health: "", ExitCode: 130, Publishers: []compose.PortPublisher{},
83+
State: containerType.StateExited,
84+
Health: "",
85+
ExitCode: 130,
86+
Publishers: []compose.PortPublisher{},
8187
Labels: map[string]string{
8288
compose.ProjectLabel: strings.ToLower(testProject),
8389
compose.ConfigFilesLabel: "/src/pkg/compose/testdata/compose.yaml",
@@ -90,8 +96,8 @@ func TestPs(t *testing.T) {
9096
assert.DeepEqual(t, containers, expected)
9197
}
9298

93-
func containerDetails(service string, id string, status string, health string, exitCode int) (containerType.Summary, containerType.InspectResponse) {
94-
container := containerType.Summary{
99+
func containerDetails(service string, id string, status containerType.ContainerState, health containerType.HealthStatus, exitCode int) (containerType.Summary, containerType.InspectResponse) {
100+
ctr := containerType.Summary{
95101
ID: id,
96102
Names: []string{"/" + id},
97103
Image: "foo",
@@ -107,5 +113,5 @@ func containerDetails(service string, id string, status string, health string, e
107113
},
108114
},
109115
}
110-
return container, inspect
116+
return ctr, inspect
111117
}

0 commit comments

Comments
 (0)