Skip to content

Commit 198649a

Browse files
committed
fix(namespace): add namespace existence check in Inspect command
Signed-off-by: Park jungtae <[email protected]>
1 parent 5e7aa0b commit 198649a

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

pkg/cmd/namespace/inspect.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,39 @@ import (
2121

2222
containerd "github.com/containerd/containerd/v2/client"
2323
"github.com/containerd/containerd/v2/pkg/namespaces"
24+
"github.com/containerd/log"
2425

2526
"github.com/containerd/nerdctl/v2/pkg/api/types"
2627
"github.com/containerd/nerdctl/v2/pkg/formatter"
2728
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
2829
)
2930

3031
func Inspect(ctx context.Context, client *containerd.Client, inspectedNamespaces []string, options types.NamespaceInspectOptions) error {
31-
result := make([]interface{}, len(inspectedNamespaces))
32-
for index, ns := range inspectedNamespaces {
32+
result := []interface{}{}
33+
warns := []error{}
34+
35+
for _, ns := range inspectedNamespaces {
3336
ctx = namespaces.WithNamespace(ctx, ns)
34-
labels, err := client.NamespaceService().Labels(ctx, ns)
37+
namespaceService := client.NamespaceService()
38+
if err := namespaceExists(ctx, namespaceService, ns); err != nil {
39+
warns = append(warns, err)
40+
continue
41+
}
42+
labels, err := namespaceService.Labels(ctx, ns)
3543
if err != nil {
3644
return err
3745
}
3846
nsInspect := native.Namespace{
3947
Name: ns,
4048
Labels: &labels,
4149
}
42-
result[index] = nsInspect
50+
result = append(result, nsInspect)
51+
}
52+
if err := formatter.FormatSlice(options.Format, options.Stdout, result); err != nil {
53+
return err
54+
}
55+
for _, warn := range warns {
56+
log.G(ctx).Warn(warn)
4357
}
44-
return formatter.FormatSlice(options.Format, options.Stdout, result)
58+
return nil
4559
}

0 commit comments

Comments
 (0)