Returns `Node <Group_status>` when container group status is available and not container status (instead of `Unknown`)

Impacts output for ps, inspect

Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
This commit is contained in:
Guillaume Tardif 2020-08-18 10:50:56 +02:00
parent c1fbf996a3
commit 87cee4d1e5
3 changed files with 60 additions and 34 deletions

View File

@ -49,7 +49,6 @@ const (
singleContainerTag = "docker-single-container" singleContainerTag = "docker-single-container"
composeContainerTag = "docker-compose-application" composeContainerTag = "docker-compose-application"
composeContainerSeparator = "_" composeContainerSeparator = "_"
statusUnknown = "Unknown"
statusRunning = "Running" statusRunning = "Running"
) )
@ -166,38 +165,20 @@ func (cs *aciContainerService) List(ctx context.Context, all bool) ([]containers
if *container.Name == convert.ComposeDNSSidecarName { if *container.Name == convert.ComposeDNSSidecarName {
continue continue
} }
if !all && getStatus(container) != statusRunning { if !all && convert.GetStatus(container, group) != statusRunning {
continue continue
} }
containerID := *containerGroup.Name + composeContainerSeparator + *container.Name containerID := *containerGroup.Name + composeContainerSeparator + *container.Name
if _, ok := group.Tags[singleContainerTag]; ok { if _, ok := group.Tags[singleContainerTag]; ok {
containerID = *containerGroup.Name containerID = *containerGroup.Name
} }
c := getContainer(containerID, group.IPAddress, container) c := convert.ContainerGroupToContainer(containerID, group, container)
res = append(res, c) res = append(res, c)
} }
} }
return res, nil 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 { func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error {
if strings.Contains(r.ID, composeContainerSeparator) { if strings.Contains(r.ID, composeContainerSeparator) {
return errors.New(fmt.Sprintf("invalid container name. ACI container name cannot include %q", 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 { for _, container := range *cg.Containers {
status := statusUnknown status := convert.GetStatus(container, cg)
if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
status = *container.InstanceView.CurrentState.State
}
if status == statusRunning { if status == statusRunning {
return errdefs.ErrForbidden return errdefs.ErrForbidden
@ -397,7 +375,7 @@ func (cs *aciContainerService) Inspect(ctx context.Context, containerID string)
return containers.Container{}, errdefs.ErrNotFound return containers.Container{}, errdefs.ErrNotFound
} }
return convert.ContainerGroupToContainer(containerID, cg, cc) return convert.ContainerGroupToContainer(containerID, cg, cc), nil
} }
type aciComposeService struct { type aciComposeService struct {

View File

@ -386,7 +386,7 @@ func bytesToGb(b types.UnitBytes) float64 {
} }
// ContainerGroupToContainer composes a Container from an ACI container definition // 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. memLimits := 0.
if cc.Resources != nil && if cc.Resources != nil &&
cc.Resources.Limits != nil && cc.Resources.Limits != nil &&
@ -406,10 +406,7 @@ func ContainerGroupToContainer(containerID string, cg containerinstance.Containe
command = strings.Join(*cc.Command, " ") command = strings.Join(*cc.Command, " ")
} }
status := "Unknown" status := GetStatus(cc, cg)
if cc.InstanceView != nil && cc.InstanceView.CurrentState != nil {
status = *cc.InstanceView.CurrentState.State
}
platform := string(cg.OsType) platform := string(cg.OsType)
c := containers.Container{ c := containers.Container{
@ -429,5 +426,17 @@ func ContainerGroupToContainer(containerID string, cg containerinstance.Containe
RestartPolicyCondition: toContainerRestartPolicy(cg.RestartPolicy), 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
} }

View File

@ -99,8 +99,7 @@ func TestContainerGroupToContainer(t *testing.T) {
RestartPolicyCondition: "any", RestartPolicyCondition: "any",
} }
container, err := ContainerGroupToContainer("myContainerID", myContainerGroup, myContainer) container := ContainerGroupToContainer("myContainerID", myContainerGroup, myContainer)
assert.NilError(t, err)
assert.DeepEqual(t, container, expectedContainer) 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(containerinstance.OnFailure), "on-failure")
assert.Equal(t, toContainerRestartPolicy(""), "any") 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,
},
}
}