Skip to content
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

Open
2 tasks done
jairoFernandez opened this issue Jun 10, 2024 · 1 comment
Open
2 tasks done

Validate k3d cluster name according to RFC1123 #2196

jairoFernandez opened this issue Jun 10, 2024 · 1 comment
Labels
feature Something new

Comments

@jairoFernandez
Copy link

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

1.	Test Case: Valid Cluster Name: valid-cluster1
•	Input: valid-cluster1
•	Expected Output: Validation passes.
2.	Test Case: Valid Cluster Name: node01.k8s
•	Input: node01.k8s
•	Expected Output: Validation passes.

Invalid Cluster Names

3.	Test Case: Invalid Cluster Name (Starts with Hyphen): -invalidcluster
•	Input: -invalidcluster
•	Expected Output: Validation fails with message “Cluster name cannot start or end with a hyphen.”
4.	Test Case: Invalid Cluster Name (Ends with Hyphen): invalidcluster-
•	Input: invalidcluster-
•	Expected Output: Validation fails with message “Cluster name cannot start or end with a hyphen.”
5.	Test Case: Invalid Cluster Name (Contains Underscore): invalid_cluster
•	Input: invalid_cluster
•	Expected Output: Validation fails with message “Cluster name cannot contain underscores.”
6.	Test Case: Invalid Cluster Name (Too Long Label): a repeated 64 times
•	Input: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
•	Expected Output: Validation fails with message “Each label in the cluster name must be between 1 and 63 characters.”

Why is it needed?

  1. Prevent Installation Failures:
    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.
  2. Compliance with Standards:
    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.
  3. Clearer Error Messaging:
    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.
  4. Consistency:
    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?

  • Yes

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jairoFernandez jairoFernandez added the feature Something new label Jun 10, 2024
@jairoFernandez
Copy link
Author

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
Labels
feature Something new
Projects
Status: No status
Development

No branches or pull requests

1 participant