Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/nerdctl/namespace/namespace_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func updateCommand() *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
}
cmd.Flags().StringArrayP("label", "l", nil, "Set labels for a namespace")
cmd.Flags().StringArrayP("label", "l", nil, "Set labels for a namespace (required)")
cmd.MarkFlagRequired("label")
return cmd
}

Expand Down
24 changes: 23 additions & 1 deletion pkg/cmd/namespace/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@

package namespace

import "strings"
import (
"context"
"fmt"
"slices"
"strings"

"github.com/compose-spec/compose-go/v2/errdefs"

"github.com/containerd/containerd/v2/pkg/namespaces"
)

func objectWithLabelArgs(args []string) map[string]string {
if len(args) >= 1 {
Expand All @@ -39,3 +48,16 @@ func labelArgs(labelStrings []string) map[string]string {

return labels
}

// namespaceExists checks if the namespace exists
func namespaceExists(ctx context.Context, store namespaces.Store, namespace string) error {
nsList, err := store.List(ctx)
if err != nil {
return err
}
if slices.Contains(nsList, namespace) {
return nil
}

return fmt.Errorf("namespace %s: %w", namespace, errdefs.ErrNotFound)
}
24 changes: 19 additions & 5 deletions pkg/cmd/namespace/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,39 @@ import (

containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/log"

"github.com/containerd/nerdctl/v2/pkg/api/types"
"github.com/containerd/nerdctl/v2/pkg/formatter"
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
)

func Inspect(ctx context.Context, client *containerd.Client, inspectedNamespaces []string, options types.NamespaceInspectOptions) error {
result := make([]interface{}, len(inspectedNamespaces))
for index, ns := range inspectedNamespaces {
result := []interface{}{}
warns := []error{}

for _, ns := range inspectedNamespaces {
ctx = namespaces.WithNamespace(ctx, ns)
labels, err := client.NamespaceService().Labels(ctx, ns)
namespaceService := client.NamespaceService()
if err := namespaceExists(ctx, namespaceService, ns); err != nil {
warns = append(warns, err)
continue
}
labels, err := namespaceService.Labels(ctx, ns)
if err != nil {
return err
}
nsInspect := native.Namespace{
Name: ns,
Labels: &labels,
}
result[index] = nsInspect
result = append(result, nsInspect)
}
if err := formatter.FormatSlice(options.Format, options.Stdout, result); err != nil {
return err
}
for _, warn := range warns {
log.G(ctx).Warn(warn)
}
return formatter.FormatSlice(options.Format, options.Stdout, result)
return nil
}
3 changes: 3 additions & 0 deletions pkg/cmd/namespace/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
func Update(ctx context.Context, client *containerd.Client, namespace string, options types.NamespaceUpdateOptions) error {
labelsArg := objectWithLabelArgs(options.Labels)
namespaces := client.NamespaceService()
if err := namespaceExists(ctx, namespaces, namespace); err != nil {
return err
}
for k, v := range labelsArg {
if err := namespaces.SetLabel(ctx, namespace, k, v); err != nil {
return err
Expand Down
Loading