Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort the list result for ps,sandboxes,images #171

Merged
merged 1 commit into from
Nov 2, 2017

Conversation

yanxuean
Copy link
Contributor

@yanxuean yanxuean commented Nov 1, 2017

fix #168
Signed-off-by: yanxuean yan.xuean@zte.com.cn

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 1, 2017
@yanxuean
Copy link
Contributor Author

yanxuean commented Nov 1, 2017

test case

  1. for container
root@ubuntu:/home/cloud/test# crictl ps
DEBU[0000] ListContainerRequest: &ListContainersRequest{Filter:&ContainerFilter{Id:,State:nil,PodSandboxId:,LabelSelector:map[string]string{},},} 
DEBU[0000] ListContainerResponse: &ListContainersResponse{Containers:[&Container{Id:11053ee6e8789351163f75720c364237c6f36e1c822525075a7546d7153600fd,PodSandboxId:dba535aa7bf2ea6675a4dce63457b11cf2c2fffdc151c1489f2478f4a5d50dfd,Metadata:&ContainerMetadata{Name:busybox2,Attempt:0,},Image:&ImageSpec{Image:busybox,},ImageRef:sha256:54511612f1c4d97e93430fc3d5dc2f05dfbe8fb7e6259b7351deeca95eaf2971,State:CONTAINER_CREATED,CreatedAt:1509518392514969405,Labels:map[string]string{},Annotations:map[string]string{},} &Container{Id:11c4699514bea2acdf83e0d063a9a5b0ffab50876591c4032ba022cedda393c4,PodSandboxId:dba535aa7bf2ea6675a4dce63457b11cf2c2fffdc151c1489f2478f4a5d50dfd,Metadata:&ContainerMetadata{Name:busybox1,Attempt:0,},Image:&ImageSpec{Image:busybox,},ImageRef:sha256:54511612f1c4d97e93430fc3d5dc2f05dfbe8fb7e6259b7351deeca95eaf2971,State:CONTAINER_CREATED,CreatedAt:1509518419261782382,Labels:map[string]string{},Annotations:map[string]string{},}],} 
CONTAINER ID                                                       CREATED             STATE               NAME
11c4699514bea2acdf83e0d063a9a5b0ffab50876591c4032ba022cedda393c4   11 seconds ago      CONTAINER_CREATED   busybox1
11053ee6e8789351163f75720c364237c6f36e1c822525075a7546d7153600fd   37 seconds ago      CONTAINER_CREATED   busybox2
root@ubuntu:/home/cloud/test# 
  1. for image
root@ubuntu:/home/cloud/test# crictl images --digests
DEBU[0000] ListImagesRequest: &ListImagesRequest{Filter:&ImageFilter{Image:&ImageSpec{Image:,},},} 
DEBU[0000] ListImagesResponse: &ListImagesResponse{Images:[&Image{Id:sha256:c30178c5239f2937c21c261b0365efcda25be4921ccb95acd63beeeb78786f27,RepoTags:[docker.io/library/busybox:1.26],RepoDigests:[docker.io/library/busybox@sha256:be3c11fdba7cfe299214e46edc642e09514dbb9bbefcd0d3836c05a1e0cd0642],Size_:701277,Uid:nil,Username:,} &Image{Id:sha256:54511612f1c4d97e93430fc3d5dc2f05dfbe8fb7e6259b7351deeca95eaf2971,RepoTags:[docker.io/library/busybox:latest docker.io/library/busybox:1.27.2],RepoDigests:[docker.io/library/busybox@sha256:3e8fa85ddfef1af9ca85a5cfb714148956984e02f00bec3f7f49d3925a91e0e7],Size_:719904,Uid:nil,Username:,} &Image{Id:sha256:320aedbfaad3d719b5e6a711834d2d06e2b0a172ce6fdadf84c640b6136914ed,RepoTags:[gcr.io/google_containers/pause:3.0],RepoDigests:[],Size_:312194,Uid:nil,Username:,} &Image{Id:sha256:350b164e7ae1dcddeffadd65c76226c9b6dc5553f5179153fb0e36b78f2a5e06,RepoTags:[gcr.io/google_containers/pause:latest],RepoDigests:[gcr.io/google_containers/pause@sha256:a78c2d6208eff9b672de43f880093100050983047b7b0afe0217d3656e1b0d5f],Size_:72335,Uid:nil,Username:,} &Image{Id:sha256:0300bfa969e985f2a2d35cc516ccaff1f132f3cc8362253926a8bdcb58c7d59f,RepoTags:[gcr.io/google_containers/pause:2.0],RepoDigests:[],Size_:341337,Uid:nil,Username:,}],} 
IMAGE                            TAG                 DIGEST                                                                    IMAGE ID            SIZE
docker.io/library/busybox        latest              sha256:3e8fa85ddfef1af9ca85a5cfb714148956984e02f00bec3f7f49d3925a91e0e7   54511612f1c4d       720kB
docker.io/library/busybox        1.27.2              sha256:3e8fa85ddfef1af9ca85a5cfb714148956984e02f00bec3f7f49d3925a91e0e7   54511612f1c4d       720kB
docker.io/library/busybox        1.26                sha256:be3c11fdba7cfe299214e46edc642e09514dbb9bbefcd0d3836c05a1e0cd0642   c30178c5239f2       701kB
gcr.io/google_containers/pause   2.0                 <none>                                                                    0300bfa969e98       341kB
gcr.io/google_containers/pause   3.0                 <none>                                                                    320aedbfaad3d       312kB
gcr.io/google_containers/pause   latest              sha256:a78c2d6208eff9b672de43f880093100050983047b7b0afe0217d3656e1b0d5f   350b164e7ae1d       72.3kB
root@ubuntu:/home/cloud/test# 

  1. for sandbox
root@ubuntu:/home/cloud/test# crictl sandboxes
DEBU[0000] ListPodSandboxRequest: &ListPodSandboxRequest{Filter:&PodSandboxFilter{Id:,State:nil,LabelSelector:map[string]string{},},} 
DEBU[0000] ListPodSandboxResponse: &ListPodSandboxResponse{Items:[&PodSandbox{Id:dba535aa7bf2ea6675a4dce63457b11cf2c2fffdc151c1489f2478f4a5d50dfd,Metadata:&PodSandboxMetadata{Name:busybox-sandbox,Uid:hdishd83djaidwnduwk28bcsb,Namespace:default,Attempt:1,},State:SANDBOX_READY,CreatedAt:1509509486978741638,Labels:map[string]string{},Annotations:map[string]string{},} &PodSandbox{Id:7595b8b07d43987fefa06a2d27fbce4ced376f0517a771f015e0ba9a36f15bb7,Metadata:&PodSandboxMetadata{Name:busybox-sandbox2,Uid:hdishd83djaidwnduwk28bcsc,Namespace:default,Attempt:1,},State:SANDBOX_READY,CreatedAt:1509524324987042544,Labels:map[string]string{},Annotations:map[string]string{},}],} 
SANDBOX ID                                                         NAME                STATE
dba535aa7bf2ea6675a4dce63457b11cf2c2fffdc151c1489f2478f4a5d50dfd   busybox-sandbox     SANDBOX_READY
7595b8b07d43987fefa06a2d27fbce4ced376f0517a771f015e0ba9a36f15bb7   busybox-sandbox2    SANDBOX_READY
root@ubuntu:/home/cloud/test# crictl sandboxes
DEBU[0000] ListPodSandboxRequest: &ListPodSandboxRequest{Filter:&PodSandboxFilter{Id:,State:nil,LabelSelector:map[string]string{},},} 
DEBU[0000] ListPodSandboxResponse: &ListPodSandboxResponse{Items:[&PodSandbox{Id:7595b8b07d43987fefa06a2d27fbce4ced376f0517a771f015e0ba9a36f15bb7,Metadata:&PodSandboxMetadata{Name:busybox-sandbox2,Uid:hdishd83djaidwnduwk28bcsc,Namespace:default,Attempt:1,},State:SANDBOX_READY,CreatedAt:1509524324987042544,Labels:map[string]string{},Annotations:map[string]string{},} &PodSandbox{Id:dba535aa7bf2ea6675a4dce63457b11cf2c2fffdc151c1489f2478f4a5d50dfd,Metadata:&PodSandboxMetadata{Name:busybox-sandbox,Uid:hdishd83djaidwnduwk28bcsb,Namespace:default,Attempt:1,},State:SANDBOX_READY,CreatedAt:1509509486978741638,Labels:map[string]string{},Annotations:map[string]string{},}],} 
SANDBOX ID                                                         NAME                STATE
dba535aa7bf2ea6675a4dce63457b11cf2c2fffdc151c1489f2478f4a5d50dfd   busybox-sandbox     SANDBOX_READY
7595b8b07d43987fefa06a2d27fbce4ced376f0517a771f015e0ba9a36f15bb7   busybox-sandbox2    SANDBOX_READY
root@ubuntu:/home/cloud/test#

@@ -524,6 +536,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
if err != nil {
return err
}
sort.Sort(containerByName(r.Containers))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems docker ps sorts output via createdAt , could you also sort the output this way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course. done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not studied the docker source code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name should be containerByCreated now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -30,6 +31,20 @@ import (
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)

type sandboxBySort []*pb.PodSandbox

func (a sandboxBySort) Len() int { return len(a) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also sort sandbox by created time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to compare CreatAt first?
Is it more usable that putting all in one place that belong to one user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only compare CreatAt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense. I think you are right.

@@ -35,6 +36,20 @@ const (
truncatedImageIDLen = 13
)

type imageBySort []*pb.Image
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have created time for image. :( I think it's fine to sort by tag/digest for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why compare digest first? I think we care more about tag, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ithink digest is immutable. repotag can be modified.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@@ -524,6 +536,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
if err != nil {
return err
}
sort.Sort(containerByName(r.Containers))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name should be containerByCreated now.

@@ -105,6 +120,7 @@ var listImageCommand = cli.Command{
if err != nil {
return fmt.Errorf("listing images failed: %v", err)
}
sort.Sort(imageBySort(r.Images))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imageByRef?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -30,6 +31,20 @@ import (
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)

type sandboxBySort []*pb.PodSandbox

func (a sandboxBySort) Len() int { return len(a) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense. I think you are right.

fix kubernetes-sigs#168
Signed-off-by: yanxuean <yan.xuean@zte.com.cn>
@Random-Liu
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 2, 2017
@Random-Liu Random-Liu merged commit c88d957 into kubernetes-sigs:master Nov 2, 2017
@yanxuean yanxuean deleted the sort branch November 2, 2017 06:32
@Random-Liu Random-Liu mentioned this pull request Nov 6, 2017
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

list result should be sorted.
4 participants