Skip to content

Commit 6617def

Browse files
authored
Merge pull request #3383 from crosbymichael/ns-delete
Add cgroup delete opt for ns deletion
2 parents d1b766a + 844c581 commit 6617def

7 files changed

Lines changed: 135 additions & 7 deletions

File tree

cmd/ctr/commands/namespaces/namespaces.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,24 @@ var removeCommand = cli.Command{
146146
Usage: "remove one or more namespaces",
147147
ArgsUsage: "<name> [<name>, ...]",
148148
Description: "remove one or more namespaces. for now, the namespace must be empty",
149+
Flags: []cli.Flag{
150+
cli.BoolFlag{
151+
Name: "cgroup,c",
152+
Usage: "delete the namespace's cgroup",
153+
},
154+
},
149155
Action: func(context *cli.Context) error {
150156
var exitErr error
151157
client, ctx, cancel, err := commands.NewClient(context)
152158
if err != nil {
153159
return err
154160
}
155161
defer cancel()
162+
163+
opts := deleteOpts(context)
156164
namespaces := client.NamespaceService()
157165
for _, target := range context.Args() {
158-
if err := namespaces.Delete(ctx, target); err != nil {
166+
if err := namespaces.Delete(ctx, target, opts...); err != nil {
159167
if !errdefs.IsNotFound(err) {
160168
if exitErr == nil {
161169
exitErr = errors.Wrapf(err, "unable to delete %v", target)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package namespaces
18+
19+
import (
20+
"github.com/containerd/containerd"
21+
"github.com/containerd/containerd/namespaces"
22+
"github.com/urfave/cli"
23+
)
24+
25+
func deleteOpts(context *cli.Context) []namespaces.DeleteOpts {
26+
var opts []namespaces.DeleteOpts
27+
if context.Bool("cgroup") {
28+
opts = append(opts, containerd.WithNamespaceCgroupDeletion)
29+
}
30+
return opts
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// +build !linux
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package namespaces
20+
21+
import (
22+
"github.com/containerd/containerd/namespaces"
23+
"github.com/urfave/cli"
24+
)
25+
26+
func deleteOpts(context *cli.Context) []namespaces.DeleteOpts {
27+
return nil
28+
}

metadata/namespaces.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,15 @@ func (s *namespaceStore) List(ctx context.Context) ([]string, error) {
129129
return namespaces, nil
130130
}
131131

132-
func (s *namespaceStore) Delete(ctx context.Context, namespace string) error {
132+
func (s *namespaceStore) Delete(ctx context.Context, namespace string, opts ...namespaces.DeleteOpts) error {
133+
i := &namespaces.DeleteInfo{
134+
Name: namespace,
135+
}
136+
for _, o := range opts {
137+
if err := o(ctx, i); err != nil {
138+
return err
139+
}
140+
}
133141
bkt := getBucket(s.tx, bucketKeyVersion)
134142
if empty, err := s.namespaceEmpty(ctx, namespace); err != nil {
135143
return err

namespaces.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,18 @@ func (r *remoteNamespaces) List(ctx context.Context) ([]string, error) {
100100
return namespaces, nil
101101
}
102102

103-
func (r *remoteNamespaces) Delete(ctx context.Context, namespace string) error {
104-
var req api.DeleteNamespaceRequest
105-
106-
req.Name = namespace
103+
func (r *remoteNamespaces) Delete(ctx context.Context, namespace string, opts ...namespaces.DeleteOpts) error {
104+
i := namespaces.DeleteInfo{
105+
Name: namespace,
106+
}
107+
for _, o := range opts {
108+
if err := o(ctx, &i); err != nil {
109+
return err
110+
}
111+
}
112+
req := api.DeleteNamespaceRequest{
113+
Name: namespace,
114+
}
107115
_, err := r.client.Delete(ctx, &req)
108116
if err != nil {
109117
return errdefs.FromGRPC(err)

namespaces/store.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,14 @@ type Store interface {
3333
List(ctx context.Context) ([]string, error)
3434

3535
// Delete removes the namespace. The namespace must be empty to be deleted.
36-
Delete(ctx context.Context, namespace string) error
36+
Delete(ctx context.Context, namespace string, opts ...DeleteOpts) error
3737
}
38+
39+
// DeleteInfo specifies information for the deletion of a namespace
40+
type DeleteInfo struct {
41+
// Name of the namespace
42+
Name string
43+
}
44+
45+
// DeleteOpts allows the caller to set options for namespace deletion
46+
type DeleteOpts func(context.Context, *DeleteInfo) error

namespaces_opts_linux.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package containerd
18+
19+
import (
20+
"context"
21+
22+
"github.com/containerd/cgroups"
23+
"github.com/containerd/containerd/namespaces"
24+
)
25+
26+
// WithNamespaceCgroupDeletion removes the cgroup directory that was created for the namespace
27+
func WithNamespaceCgroupDeletion(ctx context.Context, i *namespaces.DeleteInfo) error {
28+
cg, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(i.Name))
29+
if err != nil {
30+
if err == cgroups.ErrCgroupDeleted {
31+
return nil
32+
}
33+
return err
34+
}
35+
return cg.Delete()
36+
}

0 commit comments

Comments
 (0)