mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-17 15:13:08 +00:00
Merge pull request #9736 from sdminonne/bug_fix2
To add validation for service ports when defined as string
This commit is contained in:
@@ -105,6 +105,36 @@ func IsValidPortNum(port int) bool {
|
||||
return 0 < port && port < 65536
|
||||
}
|
||||
|
||||
const doubleHyphensFmt string = ".*(--).*"
|
||||
|
||||
var doubleHyphensRegexp = regexp.MustCompile("^" + doubleHyphensFmt + "$")
|
||||
|
||||
const IdentifierNoHyphensBeginEndFmt string = "[a-z0-9]([a-z0-9-]*[a-z0-9])*"
|
||||
|
||||
var identifierNoHyphensBeginEndRegexp = regexp.MustCompile("^" + IdentifierNoHyphensBeginEndFmt + "$")
|
||||
|
||||
const atLeastOneLetterFmt string = ".*[a-z].*"
|
||||
|
||||
var atLeastOneLetterRegexp = regexp.MustCompile("^" + atLeastOneLetterFmt + "$")
|
||||
|
||||
// IsValidPortName check that the argument is valid syntax. It must be non empty and no more than 15 characters long
|
||||
// It must contains at least one letter [a-z] and it must contains only [a-z0-9-].
|
||||
// Hypens ('-') cannot be leading or trailing character of the string and cannot be adjacent to other hyphens.
|
||||
// Although RFC 6335 allows upper and lower case characters but case is ignored for comparison purposes: (HTTP
|
||||
// and http denote the same service).
|
||||
func IsValidPortName(port string) bool {
|
||||
if len(port) < 1 || len(port) > 15 {
|
||||
return false
|
||||
}
|
||||
if doubleHyphensRegexp.MatchString(port) {
|
||||
return false
|
||||
}
|
||||
if identifierNoHyphensBeginEndRegexp.MatchString(port) && atLeastOneLetterRegexp.MatchString(port) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsValidIPv4 tests that the argument is a valid IPv4 address.
|
||||
func IsValidIPv4(value string) bool {
|
||||
return net.ParseIP(value) != nil && net.ParseIP(value).To4() != nil
|
||||
|
@@ -154,6 +154,22 @@ func TestIsValidPortNum(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidPortName(t *testing.T) {
|
||||
goodValues := []string{"telnet", "re-mail-ck", "pop3", "a", "a-1", "1-a", "a-1-b-2-c", "1-a-2-b-3"}
|
||||
for _, val := range goodValues {
|
||||
if !IsValidPortName(val) {
|
||||
t.Errorf("expected true for '%d'", val)
|
||||
}
|
||||
}
|
||||
|
||||
badValues := []string{"longerthan15characters", "", "12345", "1-2-3-4", "-begin", "end-", "two--hyphens", "1-2", "whois++"}
|
||||
for _, val := range badValues {
|
||||
if IsValidPortName(val) {
|
||||
t.Errorf("expected false for '%d'", val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsQualifiedName(t *testing.T) {
|
||||
successCases := []string{
|
||||
"simple",
|
||||
|
Reference in New Issue
Block a user