Implement `compose ls` for ACI

Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
aiordache 2020-09-04 18:11:02 +02:00
parent 0dd6416742
commit 799b799f30
4 changed files with 69 additions and 32 deletions

View File

@ -134,32 +134,14 @@ type aciContainerService struct {
} }
func (cs *aciContainerService) List(ctx context.Context, all bool) ([]containers.Container, error) { func (cs *aciContainerService) List(ctx context.Context, all bool) ([]containers.Container, error) {
groupsClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID) containerGroups, err := getContainerGroups(ctx, cs.ctx.SubscriptionID, cs.ctx.ResourceGroup)
if err != nil {
return nil, err
}
var containerGroups []containerinstance.ContainerGroup
result, err := groupsClient.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
if err != nil { if err != nil {
return []containers.Container{}, err return []containers.Container{}, err
} }
for result.NotDone() {
containerGroups = append(containerGroups, result.Values()...)
if err := result.NextWithContext(ctx); err != nil {
return []containers.Container{}, err
}
}
var res []containers.Container var res []containers.Container
for _, containerGroup := range containerGroups { for _, group := range containerGroups {
group, err := groupsClient.Get(ctx, cs.ctx.ResourceGroup, *containerGroup.Name)
if err != nil {
return []containers.Container{}, err
}
if group.Containers == nil || len(*group.Containers) < 1 { if group.Containers == nil || len(*group.Containers) < 1 {
return []containers.Container{}, fmt.Errorf("no containers found in ACI container group %s", *containerGroup.Name) return []containers.Container{}, fmt.Errorf("no containers found in ACI container group %s", *group.Name)
} }
for _, container := range *group.Containers { for _, container := range *group.Containers {
@ -173,6 +155,26 @@ func (cs *aciContainerService) List(ctx context.Context, all bool) ([]containers
return res, nil return res, nil
} }
func getContainerGroups(ctx context.Context, subscriptionID string, resourceGroup string) ([]containerinstance.ContainerGroup, error) {
groupsClient, err := login.NewContainerGroupsClient(subscriptionID)
if err != nil {
return nil, err
}
var containerGroups []containerinstance.ContainerGroup
result, err := groupsClient.ListByResourceGroup(ctx, resourceGroup)
if err != nil {
return []containerinstance.ContainerGroup{}, err
}
for result.NotDone() {
containerGroups = append(containerGroups, result.Values()...)
if err := result.NextWithContext(ctx); err != nil {
return []containerinstance.ContainerGroup{}, err
}
}
return containerGroups, nil
}
func getContainerID(group containerinstance.ContainerGroup, container containerinstance.Container) string { func getContainerID(group containerinstance.ContainerGroup, container containerinstance.Container) string {
containerID := *group.Name + composeContainerSeparator + *container.Name containerID := *group.Name + composeContainerSeparator + *container.Name
if _, ok := group.Tags[singleContainerTag]; ok { if _, ok := group.Tags[singleContainerTag]; ok {
@ -446,8 +448,33 @@ func (cs *aciComposeService) Ps(ctx context.Context, project string) ([]compose.
} }
return res, nil return res, nil
} }
func (cs *aciComposeService) List(ctx context.Context, project string) ([]compose.Stack, error) { func (cs *aciComposeService) List(ctx context.Context, project string) ([]compose.Stack, error) {
return nil, errdefs.ErrNotImplemented containerGroups, err := getContainerGroups(ctx, cs.ctx.SubscriptionID, cs.ctx.ResourceGroup)
if err != nil {
return []compose.Stack{}, err
}
stacks := []compose.Stack{}
for _, group := range containerGroups {
if _, found := group.Tags[composeContainerTag]; !found {
continue
}
state := compose.RUNNING
for _, container := range *group.ContainerGroupProperties.Containers {
containerState := convert.GetStatus(container, group)
if containerState != compose.RUNNING {
state = containerState
break
}
}
stacks = append(stacks, compose.Stack{
ID: *group.ID,
Name: *group.Name,
Status: state,
})
}
return stacks, nil
} }
func (cs *aciComposeService) Logs(ctx context.Context, project string, w io.Writer) error { func (cs *aciComposeService) Logs(ctx context.Context, project string, w io.Writer) error {

View File

@ -451,7 +451,16 @@ func ContainerGroupToContainer(containerID string, cg containerinstance.Containe
// GetStatus returns status for the specified container // GetStatus returns status for the specified container
func GetStatus(container containerinstance.Container, group containerinstance.ContainerGroup) string { func GetStatus(container containerinstance.Container, group containerinstance.ContainerGroup) string {
status := "Unknown" status := compose.UNKNOWN
if group.ContainerGroupProperties != nil && group.ContainerGroupProperties.ProvisioningState != nil {
status = *group.ContainerGroupProperties.ProvisioningState
switch status {
case "Succeeded":
status = compose.RUNNING
case "Failed":
status = compose.FAILED
}
}
if group.InstanceView != nil && group.InstanceView.State != nil { if group.InstanceView != nil && group.InstanceView.State != nil {
status = "Node " + *group.InstanceView.State status = "Node " + *group.InstanceView.State
} }

View File

@ -57,23 +57,24 @@ type ServiceStatus struct {
Publishers []PortPublisher Publishers []PortPublisher
} }
// State of a compose stack
type State string
const ( const (
// STARTING indicates that stack is being deployed // STARTING indicates that stack is being deployed
STARTING State = "starting" STARTING string = "Starting"
// RUNNING indicates that stack is deployed and services are running // RUNNING indicates that stack is deployed and services are running
RUNNING State = "running" RUNNING string = "Running"
// UPDATING indicates that some stack resources are being recreated // UPDATING indicates that some stack resources are being recreated
UPDATING State = "updating" UPDATING string = "Updating"
// REMOVING indicates that stack is being deleted // REMOVING indicates that stack is being deleted
REMOVING State = "removing" REMOVING string = "Removing"
// UNKNOWN indicates unknown stack state
UNKNOWN string = "Unknown"
// FAILED indicates that stack deployment failed
FAILED string = "Failed"
) )
// Stack holds the name and state of a compose application/stack // Stack holds the name and state of a compose application/stack
type Stack struct { type Stack struct {
ID string ID string
Name string Name string
Status State Status string
} }

View File

@ -325,7 +325,7 @@ func (s sdk) ListStacks(ctx context.Context, name string) ([]compose.Stack, erro
Name: aws.StringValue(stack.StackName), Name: aws.StringValue(stack.StackName),
Status: status, Status: status,
}) })
continue break
} }
} }
} }