-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate k3d cluster name according to RFC1123 #2196
Labels
feature
Something new
Comments
test: package main
import (
"fmt"
"regexp"
)
func main() {
testCases := []struct {
input string
expected string
}{
{"valid-cluster1", "Validation passes."},
{"node01.k8s", "Validation passes."},
{"-invalidcluster", "Validation fails: Cluster name cannot start or end with a hyphen."},
{"invalidcluster-", "Validation fails: Cluster name cannot start or end with a hyphen."},
{"invalid_cluster", "Validation fails: Cluster name cannot contain underscores."},
{string(make([]byte, 64)), "Validation fails: Each label in the cluster name must be between 1 and 63 characters."},
}
for _, tc := range testCases {
result := validateClusterName(tc.input)
fmt.Printf("Input: %s, Expected: %s, Result: %s\n", tc.input, tc.expected, result)
}
}
func validateClusterName(name string) string {
if len(name) == 0 || len(name) > 255 {
return "Validation fails: Cluster name must be between 1 and 255 characters."
}
labels := regexp.MustCompile(`\.`).Split(name, -1)
for _, label := range labels {
if len(label) == 0 || len(label) > 63 {
return "Validation fails: Each label in the cluster name must be between 1 and 63 characters."
}
if label[0] == '-' || label[len(label)-1] == '-' {
return "Validation fails: Cluster name cannot start or end with a hyphen."
}
if match, _ := regexp.MatchString(`^[a-z0-9-]+$`, label); !match {
return "Validation fails: Cluster name can only contain alphanumeric characters and hyphens."
}
}
return "Validation passes."
} Possible implementation: package main
import (
"fmt"
"regexp"
)
func main() {
clusterNames := []string{
"kubefirst_test1", // Invalid
"kubefirst-test1", // Valid
"valid-cluster1", // Valid
"node01.k8s", // Valid
"-invalidcluster", // Invalid
"invalidcluster-", // Invalid
"invalid_cluster", // Invalid
string(make([]byte, 64)), // Invalid (too long)
}
for _, name := range clusterNames {
if err := validateClusterName(name); err != nil {
fmt.Printf("Cluster name '%s' is invalid: %s\n", name, err)
} else {
fmt.Printf("Cluster name '%s' is valid.\n", name)
}
}
}
func validateClusterName(name string) error {
if len(name) == 0 || len(name) > 255 {
return fmt.Errorf("must be between 1 and 255 characters")
}
labels := regexp.MustCompile(`\.`).Split(name, -1)
for _, label := range labels {
if len(label) == 0 || len(label) > 63 {
return fmt.Errorf("each label must be between 1 and 63 characters")
}
if label[0] == '-' || label[len(label)-1] == '-' {
return fmt.Errorf("cannot start or end with a hyphen")
}
if match, _ := regexp.MatchString(`^[a-z0-9-]+$`, label); !match {
return fmt.Errorf("can only contain alphanumeric characters and hyphens")
}
}
return nil
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is your feature idea?
Description: Ensure that the cluster name is validated according to the rules specified in RFC 1123 before proceeding with the installation. The validation should check for allowed characters, correct length, and additional restrictions on the placement of hyphens.
Test Cases
Valid Cluster Names
Invalid Cluster Names
Why is it needed?
The current validation allows for invalid cluster names, such as those containing underscores (_), which are not permitted by RFC 1123. This results in installation errors and can be frustrating for users who may not immediately understand the cause of the failure. By validating names upfront, we can prevent these issues and ensure a smoother installation process.
RFC 1123 defines the requirements for hostnames, and adhering to these standards ensures compatibility and interoperability across different systems and services. Validating cluster names according to RFC 1123 guarantees that names are always compliant with industry standards.
Implementing this validation allows us to provide clear and informative error messages when a user attempts to use an invalid cluster name. This helps users quickly understand what went wrong and how to fix it, enhancing the overall user experience.
Enforcing RFC 1123 compliance ensures consistency in cluster names, which can be important for maintaining organized and predictable naming conventions within an organization. This consistency can also aid in troubleshooting and system administration.
Is this missing feature preventing you from using kubefirst?
Code of Conduct
The text was updated successfully, but these errors were encountered: