Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace Improvements #3862

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions cmd/nerdctl/namespace/namespace_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package namespace

import (
"errors"

"github.com/spf13/cobra"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
Expand All @@ -26,16 +28,16 @@ import (
)

func newNamespacelabelUpdateCommand() *cobra.Command {
namespaceLableCommand := &cobra.Command{
namespaceLabelCommand := &cobra.Command{
Use: "update [flags] NAMESPACE",
Short: "Update labels for a namespace",
RunE: labelUpdateAction,
Args: cobra.MinimumNArgs(1),
SilenceUsage: true,
SilenceErrors: true,
}
namespaceLableCommand.Flags().StringArrayP("label", "l", nil, "Set labels for a namespace")
return namespaceLableCommand
namespaceLabelCommand.Flags().StringArrayP("label", "l", nil, "Set labels for a namespace (required)")
return namespaceLabelCommand
}

func processNamespaceUpdateCommandOption(cmd *cobra.Command) (types.NamespaceUpdateOptions, error) {
Expand All @@ -47,6 +49,11 @@ func processNamespaceUpdateCommandOption(cmd *cobra.Command) (types.NamespaceUpd
if err != nil {
return types.NamespaceUpdateOptions{}, err
}

if (len(labels) == 0) {
return types.NamespaceUpdateOptions{}, errors.New("use \"--label\" or \"-l\" to specify labels for namespace");
}

return types.NamespaceUpdateOptions{
GOptions: globalOptions,
Labels: labels,
Expand Down
22 changes: 21 additions & 1 deletion pkg/cmd/namespace/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package namespace

import "strings"
import (
"strings"

Check failure on line 20 in pkg/cmd/namespace/common.go

View workflow job for this annotation

GitHub Actions / go | linux | go-canary

File is not properly formatted (gofmt)
"unicode"
"errors"
)

func objectWithLabelArgs(args []string) map[string]string {
if len(args) >= 1 {
Expand All @@ -39,3 +43,19 @@

return labels
}

// Returns an error if name is invalid.
func validateNamespaceName(name string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Minterl Could we add test cases for this?

for _, c := range(name) {
if (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hyphen is allowed but not checked in the if statement.

Unexpected Behavior: "abc-123" (hyphen is neither allowed nor rejected explicitly, meaning it gets rejected)

c == ' ' ||
unicode.IsLower(c) ||
unicode.IsNumber(c)) {
continue
}

return errors.New("invalid namespace name - use only lowercase alphanumeric characters and hyphens")
}

return nil
}
4 changes: 4 additions & 0 deletions pkg/cmd/namespace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
)

func Create(ctx context.Context, client *containerd.Client, namespace string, options types.NamespaceCreateOptions) error {
if err := validateNamespaceName(namespace); err != nil {

Check failure on line 28 in pkg/cmd/namespace/create.go

View workflow job for this annotation

GitHub Actions / go | linux | go-canary

File is not properly formatted (gofmt)
return err
}

labelsArg := objectWithLabelArgs(options.Labels)
namespaces := client.NamespaceService()
return namespaces.Create(ctx, namespace, labelsArg)
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/namespace/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
func Inspect(ctx context.Context, client *containerd.Client, inspectedNamespaces []string, options types.NamespaceInspectOptions) error {
result := make([]interface{}, len(inspectedNamespaces))
for index, ns := range inspectedNamespaces {
if err := validateNamespaceName(ns); err != nil {

Check failure on line 33 in pkg/cmd/namespace/inspect.go

View workflow job for this annotation

GitHub Actions / go | linux | go-canary

File is not properly formatted (gofmt)
return err
}

ctx = namespaces.WithNamespace(ctx, ns)
labels, err := client.NamespaceService().Labels(ctx, ns)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/namespace/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
)

func Update(ctx context.Context, client *containerd.Client, namespace string, options types.NamespaceUpdateOptions) error {
if err := validateNamespaceName(namespace); err != nil {
return err
}

labelsArg := objectWithLabelArgs(options.Labels)
namespaces := client.NamespaceService()
for k, v := range labelsArg {
Expand Down
Loading