Comments (1)
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)
- New install on Mac M3Max - gitlab runner fails. HOT 7
- Update AWS Session Credentials for aws create command HOT 1
- User can filter assets
- colony UI needs to be able to handle large numbers of assets HOT 1
- User is able to designate an asset as control plane HOT 1
- User is able to assign a global gateway IP to a cluster and a static IP to each individual asset HOT 2
- Add gitops & metaphor repository names as flags for create HOT 4
- Add command line options to specify k3d options to kubefirst k3d create HOT 2
- Installation on GCP stuck on "writing google specific secrets to vault secret store" HOT 13
- Argocd timeout
- Cannot install to a GitHub org that enforces commit signoff HOT 1
- kubefirst oss users should be allowed to install the oss platform without kubefirst pro HOT 1
- adjustments to the pipelines to accommodate the new github organization
- kubefirst minor release bump to launch under new org
- Broken link in "kubefirst beta google destroy" HOT 2
- Create a nice 404 page on the docs website HOT 2
- Argo's "Login With Vault" button doesn't work on K3d HOT 1
- adjust joyride to showcase clusters creation instead of existing clusters
- User is able to enter configure the k3s cluster details required to create a CivoStack cluster
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kubefirst.