From 87cee4d1e590c278ad50c739ffd8c1ba49ad4a5c Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Tue, 18 Aug 2020 10:50:56 +0200 Subject: [PATCH] Returns `Node ` when container group status is available and not container status (instead of `Unknown`) Impacts output for ps, inspect Signed-off-by: Guillaume Tardif --- aci/backend.go | 30 ++++---------------------- aci/convert/convert.go | 21 ++++++++++++------ aci/convert/convert_test.go | 43 +++++++++++++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 34 deletions(-) diff --git a/aci/backend.go b/aci/backend.go index 71679a070..0a192ef82 100644 --- a/aci/backend.go +++ b/aci/backend.go @@ -49,7 +49,6 @@ const ( singleContainerTag = "docker-single-container" composeContainerTag = "docker-compose-application" composeContainerSeparator = "_" - statusUnknown = "Unknown" statusRunning = "Running" ) @@ -166,38 +165,20 @@ func (cs *aciContainerService) List(ctx context.Context, all bool) ([]containers if *container.Name == convert.ComposeDNSSidecarName { continue } - if !all && getStatus(container) != statusRunning { + if !all && convert.GetStatus(container, group) != statusRunning { continue } containerID := *containerGroup.Name + composeContainerSeparator + *container.Name if _, ok := group.Tags[singleContainerTag]; ok { containerID = *containerGroup.Name } - c := getContainer(containerID, group.IPAddress, container) + c := convert.ContainerGroupToContainer(containerID, group, container) res = append(res, c) } } return res, nil } -func getContainer(containerID string, ipAddress *containerinstance.IPAddress, container containerinstance.Container) containers.Container { - status := getStatus(container) - return containers.Container{ - ID: containerID, - Image: *container.Image, - Status: status, - Ports: convert.ToPorts(ipAddress, *container.Ports), - } -} - -func getStatus(container containerinstance.Container) string { - status := statusUnknown - if container.InstanceView != nil && container.InstanceView.CurrentState != nil { - status = *container.InstanceView.CurrentState.State - } - return status -} - func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error { if strings.Contains(r.ID, composeContainerSeparator) { return errors.New(fmt.Sprintf("invalid container name. ACI container name cannot include %q", composeContainerSeparator)) @@ -350,10 +331,7 @@ func (cs *aciContainerService) Delete(ctx context.Context, containerID string, r } for _, container := range *cg.Containers { - status := statusUnknown - if container.InstanceView != nil && container.InstanceView.CurrentState != nil { - status = *container.InstanceView.CurrentState.State - } + status := convert.GetStatus(container, cg) if status == statusRunning { return errdefs.ErrForbidden @@ -397,7 +375,7 @@ func (cs *aciContainerService) Inspect(ctx context.Context, containerID string) return containers.Container{}, errdefs.ErrNotFound } - return convert.ContainerGroupToContainer(containerID, cg, cc) + return convert.ContainerGroupToContainer(containerID, cg, cc), nil } type aciComposeService struct { diff --git a/aci/convert/convert.go b/aci/convert/convert.go index ae8528c22..01de9b5f4 100644 --- a/aci/convert/convert.go +++ b/aci/convert/convert.go @@ -386,7 +386,7 @@ func bytesToGb(b types.UnitBytes) float64 { } // ContainerGroupToContainer composes a Container from an ACI container definition -func ContainerGroupToContainer(containerID string, cg containerinstance.ContainerGroup, cc containerinstance.Container) (containers.Container, error) { +func ContainerGroupToContainer(containerID string, cg containerinstance.ContainerGroup, cc containerinstance.Container) containers.Container { memLimits := 0. if cc.Resources != nil && cc.Resources.Limits != nil && @@ -406,10 +406,7 @@ func ContainerGroupToContainer(containerID string, cg containerinstance.Containe command = strings.Join(*cc.Command, " ") } - status := "Unknown" - if cc.InstanceView != nil && cc.InstanceView.CurrentState != nil { - status = *cc.InstanceView.CurrentState.State - } + status := GetStatus(cc, cg) platform := string(cg.OsType) c := containers.Container{ @@ -429,5 +426,17 @@ func ContainerGroupToContainer(containerID string, cg containerinstance.Containe RestartPolicyCondition: toContainerRestartPolicy(cg.RestartPolicy), } - return c, nil + return c +} + +// GetStatus returns status for the specified container +func GetStatus(container containerinstance.Container, group containerinstance.ContainerGroup) string { + status := "Unknown" + if group.InstanceView != nil && group.InstanceView.State != nil { + status = "Node " + *group.InstanceView.State + } + if container.InstanceView != nil && container.InstanceView.CurrentState != nil { + status = *container.InstanceView.CurrentState.State + } + return status } diff --git a/aci/convert/convert_test.go b/aci/convert/convert_test.go index 60c6b1650..7394efdd8 100644 --- a/aci/convert/convert_test.go +++ b/aci/convert/convert_test.go @@ -99,8 +99,7 @@ func TestContainerGroupToContainer(t *testing.T) { RestartPolicyCondition: "any", } - container, err := ContainerGroupToContainer("myContainerID", myContainerGroup, myContainer) - assert.NilError(t, err) + container := ContainerGroupToContainer("myContainerID", myContainerGroup, myContainer) assert.DeepEqual(t, container, expectedContainer) } @@ -408,3 +407,43 @@ func TestConvertToDockerRestartPolicyCondition(t *testing.T) { assert.Equal(t, toContainerRestartPolicy(containerinstance.OnFailure), "on-failure") assert.Equal(t, toContainerRestartPolicy(""), "any") } + +func TestConvertContainerGroupStatus(t *testing.T) { + assert.Equal(t, "Running", GetStatus(container(to.StringPtr("Running")), group(to.StringPtr("Started")))) + assert.Equal(t, "Terminated", GetStatus(container(to.StringPtr("Terminated")), group(to.StringPtr("Stopped")))) + assert.Equal(t, "Node Stopped", GetStatus(container(nil), group(to.StringPtr("Stopped")))) + assert.Equal(t, "Node Started", GetStatus(container(nil), group(to.StringPtr("Started")))) + + assert.Equal(t, "Running", GetStatus(container(to.StringPtr("Running")), group(nil))) + assert.Equal(t, "Unknown", GetStatus(container(nil), group(nil))) +} + +func container(status *string) containerinstance.Container { + var state *containerinstance.ContainerState = nil + if status != nil { + state = &containerinstance.ContainerState{ + State: status, + } + } + return containerinstance.Container{ + ContainerProperties: &containerinstance.ContainerProperties{ + InstanceView: &containerinstance.ContainerPropertiesInstanceView{ + CurrentState: state, + }, + }, + } +} + +func group(status *string) containerinstance.ContainerGroup { + var view *containerinstance.ContainerGroupPropertiesInstanceView = nil + if status != nil { + view = &containerinstance.ContainerGroupPropertiesInstanceView{ + State: status, + } + } + return containerinstance.ContainerGroup{ + ContainerGroupProperties: &containerinstance.ContainerGroupProperties{ + InstanceView: view, + }, + } +}