github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/chartutil/validate_name.go (about) 1 /* 2 Copyright The Helm 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 chartutil 18 19 import ( 20 "fmt" 21 "regexp" 22 23 "github.com/pkg/errors" 24 ) 25 26 // validName is a regular expression for resource names. 27 // 28 // According to the Kubernetes help text, the regular expression it uses is: 29 // 30 // [a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)* 31 // 32 // This follows the above regular expression (but requires a full string match, not partial). 33 // 34 // The Kubernetes documentation is here, though it is not entirely correct: 35 // https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names 36 var validName = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`) 37 38 var ( 39 // errMissingName indicates that a release (name) was not provided. 40 errMissingName = errors.New("no name provided") 41 42 // errInvalidName indicates that an invalid release name was provided 43 errInvalidName = fmt.Errorf( 44 "invalid release name, must match regex %s and the length must not be longer than 53", 45 validName.String()) 46 47 // errInvalidKubernetesName indicates that the name does not meet the Kubernetes 48 // restrictions on metadata names. 49 errInvalidKubernetesName = fmt.Errorf( 50 "invalid metadata name, must match regex %s and the length must not be longer than 253", 51 validName.String()) 52 ) 53 54 const ( 55 // According to the Kubernetes docs (https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#rfc-1035-label-names) 56 // some resource names have a max length of 63 characters while others have a max 57 // length of 253 characters. As we cannot be sure the resources used in a chart, we 58 // therefore need to limit it to 63 chars and reserve 10 chars for additional part to name 59 // of the resource. The reason is that chart maintainers can use release name as part of 60 // the resource name (and some additional chars). 61 maxReleaseNameLen = 53 62 // maxMetadataNameLen is the maximum length Kubernetes allows for any name. 63 maxMetadataNameLen = 253 64 ) 65 66 // ValidateReleaseName performs checks for an entry for a Helm release name 67 // 68 // For Helm to allow a name, it must be below a certain character count (53) and also match 69 // a regular expression. 70 // 71 // According to the Kubernetes help text, the regular expression it uses is: 72 // 73 // [a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)* 74 // 75 // This follows the above regular expression (but requires a full string match, not partial). 76 // 77 // The Kubernetes documentation is here, though it is not entirely correct: 78 // https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names 79 func ValidateReleaseName(name string) error { 80 // This case is preserved for backwards compatibility 81 if name == "" { 82 return errMissingName 83 84 } 85 if len(name) > maxReleaseNameLen || !validName.MatchString(name) { 86 return errInvalidName 87 } 88 return nil 89 } 90 91 // ValidateMetadataName validates the name field of a Kubernetes metadata object. 92 // 93 // Empty strings, strings longer than 253 chars, or strings that don't match the regexp 94 // will fail. 95 // 96 // According to the Kubernetes help text, the regular expression it uses is: 97 // 98 // [a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)* 99 // 100 // This follows the above regular expression (but requires a full string match, not partial). 101 // 102 // The Kubernetes documentation is here, though it is not entirely correct: 103 // https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names 104 // 105 // Deprecated: remove in Helm 4. Name validation now uses rules defined in 106 // pkg/lint/rules.validateMetadataNameFunc() 107 func ValidateMetadataName(name string) error { 108 if name == "" || len(name) > maxMetadataNameLen || !validName.MatchString(name) { 109 return errInvalidKubernetesName 110 } 111 return nil 112 }