Skip to content
This repository was archived by the owner on Aug 20, 2021. It is now read-only.

Commit 8050e06

Browse files
authored
Merge pull request #45 from dweomer/list-all-pods
ps: list all containers
2 parents fa09f0a + f7d784d commit 8050e06

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

cmd/ps/ps.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
)
1313

1414
type Ps struct {
15-
A_All bool `usage:"Show all images (default hides intermediate images)"`
16-
Format string `usage:"Pretty-print images using a Go template"`
15+
A_All bool `usage:"Show all containers"`
16+
Format string `usage:"Pretty-print containers using a Go template"`
1717
NoTrunc bool `usage:"Don't truncate output"`
1818
Q_Quiet bool `usage:"Only show numeric IDs"`
1919
}

pkg/daemon/container.go

+22-8
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ package daemon
33
import (
44
"bytes"
55
"context"
6+
"encoding/json"
67
"fmt"
78
"os"
89
"path/filepath"
910
"strconv"
1011
"strings"
1112
"time"
1213

14+
"github.com/containerd/containerd/namespaces"
1315
criutil "github.com/containerd/cri/pkg/containerd/util"
14-
"github.com/rancher/k3c/pkg/defaults"
15-
16+
"github.com/containerd/cri/pkg/server"
1617
"github.com/golang/protobuf/jsonpb"
1718
"github.com/pkg/errors"
1819
"github.com/rancher/k3c/pkg/client"
1920
"github.com/rancher/k3c/pkg/daemon/volume"
21+
"github.com/rancher/k3c/pkg/defaults"
2022
"github.com/rancher/k3c/pkg/log"
2123
"github.com/rancher/k3c/pkg/remote/apis/k3c/v1alpha1"
2224
"github.com/sirupsen/logrus"
@@ -76,6 +78,7 @@ func (c *Daemon) LogContainer(ctx context.Context, containerID string, opts *v1.
7678
opts = &v1.PodLogOptions{}
7779
}
7880

81+
ctx = criutil.WithUnlisted(namespaces.WithNamespace(ctx, defaults.PublicNamespace), defaults.PrivateNamespace)
7982
_, _, containerID, err := c.GetContainer(ctx, containerID)
8083
if err != nil {
8184
return nil, err
@@ -483,10 +486,21 @@ func boolPointer(priv bool) *bool {
483486
return nil
484487
}
485488

486-
func toContainer(mappings []*pb.PortMapping, volumes []v1.Volume, data *containerData) ([]v1.Volume, v1.Container, error) {
489+
func (c *Daemon) toContainer(ctx context.Context, mappings []*pb.PortMapping, volumes []v1.Volume, data *containerData) ([]v1.Volume, v1.Container, error) {
487490
containerConfig, _, err := getContainerConfig(data.container)
488491
if err != nil {
489-
return nil, v1.Container{}, err
492+
var (
493+
req = &pb.ContainerStatusRequest{ContainerId: data.container.Id, Verbose: true}
494+
res *pb.ContainerStatusResponse
495+
)
496+
if res, err = c.crt.ContainerStatus(ctx, req); err != nil {
497+
return nil, v1.Container{}, err
498+
}
499+
var info server.ContainerInfo
500+
if err = json.Unmarshal([]byte(res.Info["info"]), &info); err != nil {
501+
return nil, v1.Container{}, err
502+
}
503+
containerConfig = info.Config
490504
}
491505

492506
var (
@@ -510,10 +524,9 @@ func toContainer(mappings []*pb.PortMapping, volumes []v1.Volume, data *containe
510524
}
511525
ports = append(ports, cp)
512526
}
513-
514-
return volumes, v1.Container{
527+
container := v1.Container{
515528
Name: data.container.Metadata.Name,
516-
Image: data.container.Image.Image,
529+
Image: data.status.Image.Image,
517530
Command: containerConfig.Command,
518531
Args: containerConfig.Args,
519532
WorkingDir: containerConfig.WorkingDir,
@@ -541,7 +554,8 @@ func toContainer(mappings []*pb.PortMapping, volumes []v1.Volume, data *containe
541554
Stdin: containerConfig.GetStdin(),
542555
StdinOnce: containerConfig.GetStdinOnce(),
543556
TTY: containerConfig.GetTty(),
544-
}, nil
557+
}
558+
return volumes, container, nil
545559
}
546560

547561
func addMount(volumes []v1.Volume, mounts []v1.VolumeMount, mount *pb.Mount) ([]v1.Volume, []v1.VolumeMount) {

pkg/daemon/pod.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
criutil "github.com/containerd/cri/pkg/containerd/util"
16+
"github.com/containerd/cri/pkg/server"
1617
"github.com/containernetworking/cni/pkg/types/current"
1718
"github.com/golang/protobuf/jsonpb"
1819
"github.com/google/uuid"
@@ -299,9 +300,9 @@ func (c *Daemon) listPods(ctx context.Context, network bool) ([]v1.Pod, error) {
299300
}
300301

301302
for _, pod := range pods {
302-
podResult, err := toPod(pod)
303+
podResult, err := c.toPod(ctx, pod)
303304
if err != nil {
304-
logrus.Errorf("failed to convert pod %s: %v", pod.ID, err)
305+
logrus.Warnf("failed to convert pod %s: %v", pod.ID, err)
305306
continue
306307
}
307308
ips := ips[pod.ID]
@@ -494,10 +495,21 @@ func toPhase(podData *podData) v1.PodPhase {
494495
return v1.PodPending
495496
}
496497

497-
func toPod(podData *podData) (v1.Pod, error) {
498+
func (c *Daemon) toPod(ctx context.Context, podData *podData) (v1.Pod, error) {
498499
podConfig, err := getPodConfig(podData.sandbox)
499500
if err != nil {
500-
return v1.Pod{}, err
501+
var (
502+
req = &pb.PodSandboxStatusRequest{PodSandboxId: podData.ID, Verbose: true}
503+
res *pb.PodSandboxStatusResponse
504+
)
505+
if res, err = c.crt.PodSandboxStatus(ctx, req); err != nil {
506+
return v1.Pod{}, err
507+
}
508+
var info server.SandboxInfo
509+
if err = json.Unmarshal([]byte(res.Info["info"]), &info); err != nil {
510+
return v1.Pod{}, err
511+
}
512+
podConfig = info.Config
501513
}
502514

503515
pod := v1.Pod{
@@ -570,7 +582,7 @@ func toPod(podData *podData) (v1.Pod, error) {
570582
pm = nil
571583
}
572584

573-
volumes, container, err := toContainer(pm, pod.Spec.Volumes, container)
585+
volumes, container, err := c.toContainer(ctx, pm, pod.Spec.Volumes, container)
574586
if err == nil {
575587
pod.Spec.Containers = append(pod.Spec.Containers, container)
576588
pod.Spec.Volumes = volumes

pkg/tables/pods.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewPods(cli *cli.Context) table.Writer {
2424
cols := [][]string{
2525
{"CONTAINER ID", "Status.containerID | id"},
2626
{"IMAGE", "Container.image | formatImage"},
27-
{"COMMAND", "Info.runtimeSpec.process.args | join \" \""},
27+
{"COMMAND", "Info.runtimeSpec.process.args | join \" \" | formatCommand"},
2828
{"CREATED", "Typed.Pod.CreationTimestamp | ago"},
2929
{"STATUS", "Typed.Status | containerStatus"},
3030
{"PORTS", "Typed.Container.Ports | formatPorts"},
@@ -34,11 +34,21 @@ func NewPods(cli *cli.Context) table.Writer {
3434
w := table.NewWriter(cols, config(cli, "CONTAINER ID"))
3535
w.AddFormatFunc("formatPorts", formatPorts)
3636
w.AddFormatFunc("formatImage", formatImage)
37+
w.AddFormatFunc("formatCommand", formatCommandFunc(cli))
3738
w.AddFormatFunc("ago", ago)
3839
w.AddFormatFunc("containerStatus", containerStatus)
3940
return w
4041
}
4142

43+
func formatCommandFunc(cli *cli.Context) table.FormatFunc {
44+
return func(command string) (string, error) {
45+
if !cli.Bool("no-trunc") && len(command) > 40 {
46+
return command[:37] + `...`, nil
47+
}
48+
return command, nil
49+
}
50+
}
51+
4252
func formatPorts(ports []v1.ContainerPort) (string, error) {
4353
var result strings.Builder
4454
for _, port := range ports {

0 commit comments

Comments
 (0)