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

View File

@ -27,10 +27,10 @@ var PsCommand = cobra.Command{
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(w, "NAME\tIMAGE\tCOMMAND\n")
format := "%s\t%s\t%s\n"
fmt.Fprintf(w, "NAME\tIMAGE\tSTATUS\tCOMMAND\n")
format := "%s\t%s\t%s\t%s\n"
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()
},