github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/validation/path/name.go (about) 1 /* 2 Copyright 2015 The Kubernetes 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 path 18 19 import ( 20 "fmt" 21 "strings" 22 ) 23 24 // NameMayNotBe specifies strings that cannot be used as names specified as path segments (like the REST API or etcd store) 25 var NameMayNotBe = []string{".", ".."} 26 27 // NameMayNotContain specifies substrings that cannot be used in names specified as path segments (like the REST API or etcd store) 28 var NameMayNotContain = []string{"/", "%"} 29 30 // IsValidPathSegmentName validates the name can be safely encoded as a path segment 31 func IsValidPathSegmentName(name string) []string { 32 for _, illegalName := range NameMayNotBe { 33 if name == illegalName { 34 return []string{fmt.Sprintf(`may not be '%s'`, illegalName)} 35 } 36 } 37 38 var errors []string 39 for _, illegalContent := range NameMayNotContain { 40 if strings.Contains(name, illegalContent) { 41 errors = append(errors, fmt.Sprintf(`may not contain '%s'`, illegalContent)) 42 } 43 } 44 45 return errors 46 } 47 48 // IsValidPathSegmentPrefix validates the name can be used as a prefix for a name which will be encoded as a path segment 49 // It does not check for exact matches with disallowed names, since an arbitrary suffix might make the name valid 50 func IsValidPathSegmentPrefix(name string) []string { 51 var errors []string 52 for _, illegalContent := range NameMayNotContain { 53 if strings.Contains(name, illegalContent) { 54 errors = append(errors, fmt.Sprintf(`may not contain '%s'`, illegalContent)) 55 } 56 } 57 58 return errors 59 } 60 61 // ValidatePathSegmentName validates the name can be safely encoded as a path segment 62 func ValidatePathSegmentName(name string, prefix bool) []string { 63 if prefix { 64 return IsValidPathSegmentPrefix(name) 65 } 66 67 return IsValidPathSegmentName(name) 68 }