Skip to content

Commit a6fa574

Browse files
committed
add nerdctl namespace ls
Also enable bash completion for `nerdctl --namespace` Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent 7dcac36 commit a6fa574

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ It does not necessarily mean that the corresponding features are missing in cont
190190
- [:whale: nerdctl volume ls](#whale-nerdctl-volume-ls)
191191
- [:whale: nerdctl volume inspect](#whale-nerdctl-volume-inspect)
192192
- [:whale: nerdctl volume rm](#whale-nerdctl-volume-rm)
193+
- [Namespace management](#namespace-management)
194+
- [:nerd_face: nerdctl namespace ls](#nerd_face-nerdctl-namespace-ls)
193195
- [System](#system)
194196
- [:whale: nerdctl events](#whale-nerdctl-events)
195197
- [:whale: nerdctl info](#whale-nerdctl-info)
@@ -444,6 +446,13 @@ Display detailed information on one or more volumes
444446
### :whale: nerdctl volume rm
445447
Remove one or more volumes
446448

449+
## Namespace management
450+
451+
### :nerd_face: nerdctl namespace ls
452+
List containerd namespaces such as "default", "moby", or "k8s.io".
453+
454+
- `-q, --quiet`: Only display namespace names
455+
447456
## System
448457
### :whale: nerdctl events
449458
Get real time events from the server.

main.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package main
1919

2020
import (
21+
"fmt"
2122
"os"
2223
"strings"
2324

@@ -171,6 +172,7 @@ func newApp() *cli.App {
171172
networkCommand,
172173
volumeCommand,
173174
systemCommand,
175+
namespaceCommand,
174176
// Internal
175177
internalCommand,
176178
// login
@@ -180,6 +182,7 @@ func newApp() *cli.App {
180182
// Completion
181183
completionCommand,
182184
}
185+
app.BashComplete = appBashComplete
183186
return app
184187
}
185188

@@ -188,3 +191,37 @@ type Category = string
188191
const (
189192
CategoryManagement = Category("Management")
190193
)
194+
195+
func appBashComplete(clicontext *cli.Context) {
196+
if current, ok := isFlagCompletionContext(); ok {
197+
switch current {
198+
case "-n", "--namespace":
199+
bashCompleteNamespaceNames(clicontext)
200+
return
201+
}
202+
}
203+
defaultBashComplete(clicontext)
204+
}
205+
206+
func bashCompleteNamespaceNames(clicontext *cli.Context) {
207+
if rootlessutil.IsRootlessParent() {
208+
_ = rootlessutil.ParentMain()
209+
return
210+
}
211+
212+
client, ctx, cancel, err := newClient(clicontext)
213+
if err != nil {
214+
logrus.Warn(err)
215+
return
216+
}
217+
defer cancel()
218+
nsService := client.NamespaceService()
219+
nsList, err := nsService.List(ctx)
220+
if err != nil {
221+
logrus.Warn(err)
222+
return
223+
}
224+
for _, ns := range nsList {
225+
fmt.Fprintln(clicontext.App.Writer, ns)
226+
}
227+
}

namespace.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright (C) nerdctl authors.
3+
Copyright (C) containerd authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package main
19+
20+
import (
21+
"fmt"
22+
"text/tabwriter"
23+
24+
"github.com/urfave/cli/v2"
25+
)
26+
27+
var namespaceCommand = &cli.Command{
28+
Name: "namespace",
29+
Usage: "Manage containerd namespaces",
30+
Description: "Unrelated to Linux namespaces and Kubernetes namespaces",
31+
Category: CategoryManagement,
32+
Subcommands: []*cli.Command{
33+
namespaceLsCommand,
34+
},
35+
}
36+
37+
var namespaceLsCommand = &cli.Command{
38+
Name: "ls",
39+
Aliases: []string{"list"},
40+
Usage: "List containerd namespaces",
41+
Action: namespaceLsAction,
42+
Flags: []cli.Flag{
43+
&cli.BoolFlag{
44+
Name: "quiet",
45+
Aliases: []string{"q"},
46+
Usage: "Only display namespace names",
47+
},
48+
},
49+
}
50+
51+
func namespaceLsAction(clicontext *cli.Context) error {
52+
client, ctx, cancel, err := newClient(clicontext)
53+
if err != nil {
54+
return err
55+
}
56+
defer cancel()
57+
58+
nsService := client.NamespaceService()
59+
nsList, err := nsService.List(ctx)
60+
if err != nil {
61+
return err
62+
}
63+
if clicontext.Bool("q") {
64+
for _, ns := range nsList {
65+
fmt.Fprintln(clicontext.App.Writer, ns)
66+
}
67+
return nil
68+
}
69+
70+
w := tabwriter.NewWriter(clicontext.App.Writer, 4, 8, 4, ' ', 0)
71+
fmt.Fprintln(w, "NAME")
72+
for _, ns := range nsList {
73+
fmt.Fprintf(w, "%ss\n", ns)
74+
}
75+
return w.Flush()
76+
}

0 commit comments

Comments
 (0)