hide non running containers if no --all option

Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
Guillaume Tardif 2021-02-05 09:36:37 +01:00
parent f368036ddd
commit 15c0b883fe
2 changed files with 22 additions and 12 deletions

View File

@ -20,7 +20,6 @@ package client
import (
"context"
"encoding/json"
"fmt"
v1 "k8s.io/api/core/v1"
@ -54,14 +53,18 @@ func NewKubeClient(config genericclioptions.RESTClientGetter) (*KubeClient, erro
// GetContainers get containers for a given compose project
func (kc KubeClient) GetContainers(ctx context.Context, projectName string, all bool) ([]compose.ContainerSummary, error) {
pods, err := kc.client.CoreV1().Pods("").List(ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", compose.ProjectTag, projectName)})
fieldSelector := ""
if !all {
fieldSelector = "status.phase=Running"
}
pods, err := kc.client.CoreV1().Pods("").List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", compose.ProjectTag, projectName),
FieldSelector: fieldSelector,
})
if err != nil {
return nil, err
}
json, _ := json.MarshalIndent(pods, "", " ")
fmt.Println(string(json))
fmt.Printf("containers: %d\n", len(pods.Items))
result := []compose.ContainerSummary{}
for _, pod := range pods.Items {
result = append(result, podToContainerSummary(pod))

View File

@ -79,16 +79,23 @@ func TestComposeUp(t *testing.T) {
res.Assert(t, icmd.Expected{Out: `[{"Name":"compose-kube-demo","Status":"deployed"}]`})
})
t.Run("compose ps", func(t *testing.T) {
getServiceRegx := func(project string, service string) string {
t.Run("compose ps --all", func(t *testing.T) {
getServiceRegx := func(service string) string {
// match output with random hash / spaces like:
// myproject-db-698f4dd798-jd9gw db Running
return fmt.Sprintf("%s-%s-.*\\s+%s\\s+Pending\\s+", project, service, service)
// db-698f4dd798-jd9gw db Running
return fmt.Sprintf("%s-.*\\s+%s\\s+Pending\\s+", service, service)
}
res := c.RunDockerCmd("compose", "ps", "-p", projectName, "--all")
testify.Regexp(t, getServiceRegx("db"), res.Stdout())
testify.Regexp(t, getServiceRegx("words"), res.Stdout())
testify.Regexp(t, getServiceRegx("web"), res.Stdout())
assert.Equal(t, len(Lines(res.Stdout())), 4, res.Stdout())
})
t.Run("compose ps hides non running containers", func(t *testing.T) {
res := c.RunDockerCmd("compose", "ps", "-p", projectName)
testify.Regexp(t, getServiceRegx(projectName, "db"), res.Stdout())
testify.Regexp(t, getServiceRegx(projectName, "words"), res.Stdout())
testify.Regexp(t, getServiceRegx(projectName, "web"), res.Stdout())
assert.Equal(t, len(Lines(res.Stdout())), 1, res.Stdout())
})
t.Run("check running project", func(t *testing.T) {