github.com/containerd/nerdctl@v1.7.7/pkg/nsutil/nsutil.go (about) 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 nsutil provides utilities for namespaces. 18 package nsutil 19 20 import ( 21 "fmt" 22 "strings" 23 ) 24 25 // Ensures the provided namespace name is valid. 26 // Namespace names cannot be path-like strings or pre-defined aliases such as "..". 27 // https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#path-segment-names 28 func ValidateNamespaceName(nsName string) error { 29 if nsName == "" { 30 return fmt.Errorf("namespace name cannot be empty") 31 } 32 33 // Slash and '$' for POSIX and backslash and '%' for Windows. 34 pathSeparators := "/\\%$" 35 if strings.ContainsAny(nsName, pathSeparators) { 36 return fmt.Errorf("namespace name cannot contain any special characters (%q): %s", pathSeparators, nsName) 37 } 38 39 specialAliases := []string{".", "..", "~"} 40 for _, alias := range specialAliases { 41 if nsName == alias { 42 return fmt.Errorf("namespace name cannot be special path alias %q", alias) 43 } 44 } 45 46 return nil 47 }