github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/validation/validation.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors All rights reserved. 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 validation 18 19 import ( 20 "net" 21 "regexp" 22 "strings" 23 ) 24 25 const qnameCharFmt string = "[A-Za-z0-9]" 26 const qnameExtCharFmt string = "[-A-Za-z0-9_.]" 27 const QualifiedNameFmt string = "(" + qnameCharFmt + qnameExtCharFmt + "*)?" + qnameCharFmt 28 const QualifiedNameMaxLength int = 63 29 30 var qualifiedNameRegexp = regexp.MustCompile("^" + QualifiedNameFmt + "$") 31 32 func IsQualifiedName(value string) bool { 33 parts := strings.Split(value, "/") 34 var name string 35 switch len(parts) { 36 case 1: 37 name = parts[0] 38 case 2: 39 var prefix string 40 prefix, name = parts[0], parts[1] 41 if prefix == "" || !IsDNS1123Subdomain(prefix) { 42 return false 43 } 44 default: 45 return false 46 } 47 48 return name != "" && len(name) <= QualifiedNameMaxLength && qualifiedNameRegexp.MatchString(name) 49 } 50 51 const LabelValueFmt string = "(" + QualifiedNameFmt + ")?" 52 const LabelValueMaxLength int = 63 53 54 var labelValueRegexp = regexp.MustCompile("^" + LabelValueFmt + "$") 55 56 func IsValidLabelValue(value string) bool { 57 return (len(value) <= LabelValueMaxLength && labelValueRegexp.MatchString(value)) 58 } 59 60 const DNS1123LabelFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?" 61 const DNS1123LabelMaxLength int = 63 62 63 var dns1123LabelRegexp = regexp.MustCompile("^" + DNS1123LabelFmt + "$") 64 65 // IsDNS1123Label tests for a string that conforms to the definition of a label in 66 // DNS (RFC 1123). 67 func IsDNS1123Label(value string) bool { 68 return len(value) <= DNS1123LabelMaxLength && dns1123LabelRegexp.MatchString(value) 69 } 70 71 const DNS1123SubdomainFmt string = DNS1123LabelFmt + "(\\." + DNS1123LabelFmt + ")*" 72 const DNS1123SubdomainMaxLength int = 253 73 74 var dns1123SubdomainRegexp = regexp.MustCompile("^" + DNS1123SubdomainFmt + "$") 75 76 // IsDNS1123Subdomain tests for a string that conforms to the definition of a 77 // subdomain in DNS (RFC 1123). 78 func IsDNS1123Subdomain(value string) bool { 79 return len(value) <= DNS1123SubdomainMaxLength && dns1123SubdomainRegexp.MatchString(value) 80 } 81 82 const DNS952LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?" 83 const DNS952LabelMaxLength int = 24 84 85 var dns952LabelRegexp = regexp.MustCompile("^" + DNS952LabelFmt + "$") 86 87 // IsDNS952Label tests for a string that conforms to the definition of a label in 88 // DNS (RFC 952). 89 func IsDNS952Label(value string) bool { 90 return len(value) <= DNS952LabelMaxLength && dns952LabelRegexp.MatchString(value) 91 } 92 93 const CIdentifierFmt string = "[A-Za-z_][A-Za-z0-9_]*" 94 95 var cIdentifierRegexp = regexp.MustCompile("^" + CIdentifierFmt + "$") 96 97 // IsCIdentifier tests for a string that conforms the definition of an identifier 98 // in C. This checks the format, but not the length. 99 func IsCIdentifier(value string) bool { 100 return cIdentifierRegexp.MatchString(value) 101 } 102 103 // IsValidPortNum tests that the argument is a valid, non-zero port number. 104 func IsValidPortNum(port int) bool { 105 return 0 < port && port < 65536 106 } 107 108 const doubleHyphensFmt string = ".*(--).*" 109 110 var doubleHyphensRegexp = regexp.MustCompile("^" + doubleHyphensFmt + "$") 111 112 const IdentifierNoHyphensBeginEndFmt string = "[a-z0-9]([a-z0-9-]*[a-z0-9])*" 113 114 var identifierNoHyphensBeginEndRegexp = regexp.MustCompile("^" + IdentifierNoHyphensBeginEndFmt + "$") 115 116 const atLeastOneLetterFmt string = ".*[a-z].*" 117 118 var atLeastOneLetterRegexp = regexp.MustCompile("^" + atLeastOneLetterFmt + "$") 119 120 // IsValidPortName check that the argument is valid syntax. It must be non empty and no more than 15 characters long 121 // It must contains at least one letter [a-z] and it must contains only [a-z0-9-]. 122 // Hypens ('-') cannot be leading or trailing character of the string and cannot be adjacent to other hyphens. 123 // Although RFC 6335 allows upper and lower case characters but case is ignored for comparison purposes: (HTTP 124 // and http denote the same service). 125 func IsValidPortName(port string) bool { 126 if len(port) < 1 || len(port) > 15 { 127 return false 128 } 129 if doubleHyphensRegexp.MatchString(port) { 130 return false 131 } 132 if identifierNoHyphensBeginEndRegexp.MatchString(port) && atLeastOneLetterRegexp.MatchString(port) { 133 return true 134 } 135 return false 136 } 137 138 // IsValidIPv4 tests that the argument is a valid IPv4 address. 139 func IsValidIPv4(value string) bool { 140 return net.ParseIP(value) != nil && net.ParseIP(value).To4() != nil 141 } 142 143 const percentFmt string = "[0-9]+%" 144 145 var percentRegexp = regexp.MustCompile("^" + percentFmt + "$") 146 147 func IsValidPercent(percent string) bool { 148 return percentRegexp.MatchString(percent) 149 }