Git Product home page Git Product logo

Comments (1)

jairoFernandez avatar jairoFernandez commented on July 25, 2024

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
}

from kubefirst.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.