Skip to content

Commit 5e7aa0b

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

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pkg/cmd/namespace/common.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@
1616

1717
package namespace
1818

19-
import "strings"
19+
import (
20+
"context"
21+
"fmt"
22+
"slices"
23+
"strings"
24+
25+
"github.com/compose-spec/compose-go/v2/errdefs"
26+
"github.com/containerd/containerd/v2/pkg/namespaces"
27+
)
2028

2129
func objectWithLabelArgs(args []string) map[string]string {
2230
if len(args) >= 1 {
@@ -39,3 +47,16 @@ func labelArgs(labelStrings []string) map[string]string {
3947

4048
return labels
4149
}
50+
51+
// namespaceExists checks if the namespace exists
52+
func namespaceExists(ctx context.Context, store namespaces.Store, namespace string) error {
53+
nsList, err := store.List(ctx)
54+
if err != nil {
55+
return err
56+
}
57+
if slices.Contains(nsList, namespace) {
58+
return nil
59+
}
60+
61+
return fmt.Errorf("namespace %s: %w", namespace, errdefs.ErrNotFound)
62+
}

pkg/cmd/namespace/update.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import (
2727
func Update(ctx context.Context, client *containerd.Client, namespace string, options types.NamespaceUpdateOptions) error {
2828
labelsArg := objectWithLabelArgs(options.Labels)
2929
namespaces := client.NamespaceService()
30+
if err := namespaceExists(ctx, namespaces, namespace); err != nil {
31+
return err
32+
}
3033
for k, v := range labelsArg {
3134
if err := namespaces.SetLabel(ctx, namespace, k, v); err != nil {
3235
return err

0 commit comments

Comments
 (0)