Skip to content

Commit 21163f0

Browse files
committed
feat(namespace): add namespace existence check in Inspect command
Signed-off-by: Park jungtae <[email protected]>
1 parent 90c4522 commit 21163f0

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

pkg/cmd/namespace/inspect.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,48 @@ package namespace
1818

1919
import (
2020
"context"
21+
"errors"
2122

2223
containerd "github.com/containerd/containerd/v2/client"
2324
"github.com/containerd/containerd/v2/pkg/namespaces"
25+
"github.com/containerd/log"
2426

2527
"github.com/containerd/nerdctl/v2/pkg/api/types"
2628
"github.com/containerd/nerdctl/v2/pkg/formatter"
2729
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
2830
)
2931

3032
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 {
33+
result := []interface{}{}
34+
35+
warns := []error{}
36+
for _, ns := range inspectedNamespaces {
3337
ctx = namespaces.WithNamespace(ctx, ns)
34-
labels, err := client.NamespaceService().Labels(ctx, ns)
38+
namespaceService := client.NamespaceService()
39+
if err := namespaceExists(ctx, namespaceService, ns); err != nil {
40+
warns = append(warns, err)
41+
continue
42+
}
43+
labels, err := namespaceService.Labels(ctx, ns)
3544
if err != nil {
3645
return err
3746
}
3847
nsInspect := native.Namespace{
3948
Name: ns,
4049
Labels: &labels,
4150
}
42-
result[index] = nsInspect
51+
result = append(result, nsInspect)
52+
}
53+
if err := formatter.FormatSlice(options.Format, options.Stdout, result); err != nil {
54+
return err
55+
}
56+
for _, warn := range warns {
57+
log.G(ctx).Warn(warn)
4358
}
44-
return formatter.FormatSlice(options.Format, options.Stdout, result)
59+
60+
if len(warns) != 0 {
61+
return errors.New("some namespaces could not be inspected")
62+
}
63+
64+
return nil
4565
}

0 commit comments

Comments
 (0)