Skip to content

Fix: inconsistent value type for Labels returned by container list --format json #3058

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cmd/nerdctl/container_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,21 @@ func TestContainerListWithFormatLabel(t *testing.T) {
"--filter", "label="+labelK,
"--format", fmt.Sprintf("{{.Label %q}}", labelK)).AssertOutExactly(labelV + "\n")
}

func TestContainerListWithJsonFormatLabel(t *testing.T) {
t.Parallel()
base := testutil.NewBase(t)
tID := testutil.Identifier(t)
cID := tID
labelK := "label-key-" + tID
labelV := "label-value-" + tID

base.Cmd("run", "-d",
"--name", cID,
"--label", labelK+"="+labelV,
testutil.CommonImage, "sleep", "infinity").AssertOK()
defer base.Cmd("rm", "-f", cID).AssertOK()
base.Cmd("ps", "-a",
"--filter", "label="+labelK,
"--format", "json").AssertOutContains(fmt.Sprintf("%s=%s", labelK, labelV))
}
9 changes: 6 additions & 3 deletions pkg/cmd/container/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ type ListItem struct {
Status string
Runtime string // nerdctl extension
Size string
Labels map[string]string
Labels string
LabelsMap map[string]string `json:"-"`

// TODO: "LocalVolumes", "Mounts", "Networks", "RunningFor", "State"
}

func (x *ListItem) Label(s string) string {
return x.Labels[s]
return x.LabelsMap[s]
}

func prepareContainers(ctx context.Context, client *containerd.Client, containers []containerd.Container, options types.ContainerListOptions) ([]ListItem, error) {
Expand Down Expand Up @@ -138,7 +140,8 @@ func prepareContainers(ctx context.Context, client *containerd.Client, container
Ports: formatter.FormatPorts(info.Labels),
Status: formatter.ContainerStatus(ctx, c),
Runtime: info.Runtime.Name,
Labels: info.Labels,
Labels: formatter.FormatLabels(info.Labels),
LabelsMap: info.Labels,
}
if options.Size {
containerSize, err := getContainerSize(ctx, client, c, info)
Expand Down
Loading