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
Родитель c1fbf996a3
Коммит 87cee4d1e5
3 изменённых файлов: 60 добавлений и 34 удалений

Просмотреть файл

@ -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 {

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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,
},
}
}