Merge pull request #33 from rumpl/fix-ps

Get the real status of containers
This commit is contained in:
Djordje Lukic 2020-05-04 10:45:29 +02:00 committed by GitHub
commit 2cf6d6513b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 21 deletions

View File

@ -14,7 +14,7 @@ import (
) )
type containerService struct { type containerService struct {
cgc containerinstance.ContainerGroupsClient containerGroupsClient containerinstance.ContainerGroupsClient
ctx store.AciContext ctx store.AciContext
} }
@ -29,50 +29,57 @@ func getter() interface{} {
} }
func New(ctx context.Context) (containers.ContainerService, error) { func New(ctx context.Context) (containers.ContainerService, error) {
cc := apicontext.CurrentContext(ctx) currentContext := apicontext.CurrentContext(ctx)
contextStore, err := store.New() contextStore, err := store.New()
if err != nil { if err != nil {
return nil, err return nil, err
} }
metadata, err := contextStore.Get(cc, getter) metadata, err := contextStore.Get(currentContext, getter)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "wrong context type") return nil, errors.Wrap(err, "wrong context type")
} }
tc, _ := metadata.Metadata.Data.(store.AciContext) aciContext, _ := metadata.Metadata.Data.(store.AciContext)
auth, _ := auth.NewAuthorizerFromCLI() auth, _ := auth.NewAuthorizerFromCLI()
containerGroupsClient := containerinstance.NewContainerGroupsClient(tc.SubscriptionID) containerGroupsClient := containerinstance.NewContainerGroupsClient(aciContext.SubscriptionID)
containerGroupsClient.Authorizer = auth containerGroupsClient.Authorizer = auth
return &containerService{ return &containerService{
cgc: containerGroupsClient, containerGroupsClient: containerGroupsClient,
ctx: tc, ctx: aciContext,
}, nil }, nil
} }
func (cs *containerService) List(ctx context.Context) ([]containers.Container, error) { func (cs *containerService) List(ctx context.Context) ([]containers.Container, error) {
var cg []containerinstance.ContainerGroup var containerGroups []containerinstance.ContainerGroup
result, err := cs.cgc.ListByResourceGroup(ctx, cs.ctx.ResourceGroup) result, err := cs.containerGroupsClient.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
if err != nil { if err != nil {
return []containers.Container{}, err return []containers.Container{}, err
} }
for result.NotDone() { for result.NotDone() {
cg = append(cg, result.Values()...) containerGroups = append(containerGroups, result.Values()...)
if err := result.NextWithContext(ctx); err != nil { if err := result.NextWithContext(ctx); err != nil {
return []containers.Container{}, err return []containers.Container{}, err
} }
} }
res := []containers.Container{} res := []containers.Container{}
for _, c := range cg { for _, containerGroup := range containerGroups {
cc := *c.Containers group, err := cs.containerGroupsClient.Get(ctx, cs.ctx.ResourceGroup, *containerGroup.Name)
for _, d := range cc { if err != nil {
return []containers.Container{}, err
}
for _, container := range *group.Containers {
status := "Unknown"
if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
status = *container.InstanceView.CurrentState.State
}
res = append(res, containers.Container{ res = append(res, containers.Container{
ID: *c.Name, ID: *container.Name,
Image: *d.Image, Image: *container.Image,
// Command: strings.Join(*d.ContainerProperties.Command, " "), // TODO command can be null Status: status,
}) })
} }
} }

View File

@ -27,10 +27,10 @@ var PsCommand = cobra.Command{
} }
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(w, "NAME\tIMAGE\tCOMMAND\n") fmt.Fprintf(w, "NAME\tIMAGE\tSTATUS\tCOMMAND\n")
format := "%s\t%s\t%s\n" format := "%s\t%s\t%s\t%s\n"
for _, c := range containers { for _, c := range containers {
fmt.Fprintf(w, format, c.ID, c.Image, c.Command) fmt.Fprintf(w, format, c.ID, c.Image, c.Status, c.Command)
} }
return w.Flush() return w.Flush()
}, },