-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validations for K8s resource fields (#2772)
This PR adds some validation methods for K8s resource fields. Fixes #1405.
- Loading branch information
Showing
6 changed files
with
302 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
func KubernetesAdminUserName(i interface{}, k string) (warnings []string, errors []error) { | ||
adminUserName := i.(string) | ||
|
||
re := regexp.MustCompile(`^[A-Za-z][-A-Za-z0-9_]*$`) | ||
if re != nil && !re.MatchString(adminUserName) { | ||
errors = append(errors, fmt.Errorf("%s must start with alphabet and/or continue with alphanumeric characters, underscores, hyphens. Got %q.", k, adminUserName)) | ||
} | ||
|
||
return warnings, errors | ||
} | ||
|
||
func KubernetesAgentPoolName(i interface{}, k string) (warnings []string, errors []error) { | ||
agentPoolName := i.(string) | ||
|
||
re := regexp.MustCompile(`^[a-z]{1}[a-z0-9]{0,11}$`) | ||
if re != nil && !re.MatchString(agentPoolName) { | ||
errors = append(errors, fmt.Errorf("%s must start with a lowercase letter, have max length of 12, and only have characters a-z0-9. Got %q.", k, agentPoolName)) | ||
} | ||
|
||
return warnings, errors | ||
} | ||
|
||
func KubernetesDNSPrefix(i interface{}, k string) (warnings []string, errors []error) { | ||
dnsPrefix := i.(string) | ||
|
||
re := regexp.MustCompile(`^[a-zA-Z][-a-zA-Z0-9]{0,43}[a-zA-Z0-9]$`) | ||
if re != nil && !re.MatchString(dnsPrefix) { | ||
errors = append(errors, fmt.Errorf("%s must contain between 2 and 45 characters. The name can contain only letters, numbers, and hyphens. The name must start with a letter and must end with an alphanumeric character.. Got %q.", k, dnsPrefix)) | ||
} | ||
|
||
return warnings, errors | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package validate | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestKubernetesAdminUserName(t *testing.T) { | ||
cases := []struct { | ||
AdminUserName string | ||
Errors int | ||
}{ | ||
{ | ||
AdminUserName: "", | ||
Errors: 1, | ||
}, | ||
{ | ||
AdminUserName: "Abc-123_abc", | ||
Errors: 0, | ||
}, | ||
{ | ||
AdminUserName: "123abc", | ||
Errors: 1, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.AdminUserName, func(t *testing.T) { | ||
_, errors := KubernetesAdminUserName(tc.AdminUserName, "test") | ||
|
||
if len(errors) != tc.Errors { | ||
t.Fatalf("Expected AdminUserName to return %d error(s) not %d", tc.Errors, len(errors)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestKubernetesAgentPoolName(t *testing.T) { | ||
cases := []struct { | ||
AgentPoolName string | ||
Errors int | ||
}{ | ||
{ | ||
AgentPoolName: "", | ||
Errors: 1, | ||
}, | ||
{ | ||
AgentPoolName: "ABC123", | ||
Errors: 1, | ||
}, | ||
{ | ||
AgentPoolName: "abc123", | ||
Errors: 0, | ||
}, | ||
{ | ||
AgentPoolName: "123abc", | ||
Errors: 1, | ||
}, | ||
{ | ||
AgentPoolName: "hi", | ||
Errors: 0, | ||
}, | ||
{ | ||
AgentPoolName: "hello", | ||
Errors: 0, | ||
}, | ||
{ | ||
AgentPoolName: "hello-world", | ||
Errors: 1, | ||
}, | ||
{ | ||
AgentPoolName: "helloworld123", | ||
Errors: 1, | ||
}, | ||
{ | ||
AgentPoolName: "hello_world", | ||
Errors: 1, | ||
}, | ||
{ | ||
AgentPoolName: "Hello-World", | ||
Errors: 1, | ||
}, | ||
{ | ||
AgentPoolName: "20202020", | ||
Errors: 1, | ||
}, | ||
{ | ||
AgentPoolName: "h20202020", | ||
Errors: 0, | ||
}, | ||
{ | ||
AgentPoolName: "ABC123!@£", | ||
Errors: 1, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.AgentPoolName, func(t *testing.T) { | ||
_, errors := KubernetesAgentPoolName(tc.AgentPoolName, "test") | ||
|
||
if len(errors) != tc.Errors { | ||
t.Fatalf("Expected AgentPoolName to return %d error(s) not %d", tc.Errors, len(errors)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestKubernetesDNSPrefix(t *testing.T) { | ||
cases := []struct { | ||
DNSPrefix string | ||
Errors int | ||
}{ | ||
{ | ||
DNSPrefix: "", | ||
Errors: 1, | ||
}, | ||
{ | ||
DNSPrefix: "a", | ||
Errors: 1, | ||
}, | ||
{ | ||
DNSPrefix: "aBc-123abc", | ||
Errors: 0, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.DNSPrefix, func(t *testing.T) { | ||
_, errors := KubernetesDNSPrefix(tc.DNSPrefix, "test") | ||
|
||
if len(errors) != tc.Errors { | ||
t.Fatalf("Expected DNSPrefix to return %d error(s) not %d", tc.Errors, len(errors)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.