github.com/giantswarm/apiextensions/v6@v6.6.0/pkg/id/id.go (about)

     1  package id
     2  
     3  import (
     4  	"math/rand"
     5  	"regexp"
     6  	"time"
     7  )
     8  
     9  const (
    10  	// IDChars represents the character set used to generate cluster IDs.
    11  	// (does not contain 1 and l, to avoid confusion)
    12  	IDChars = "023456789abcdefghijkmnopqrstuvwxyz"
    13  	// IDLength represents the number of characters used to create a cluster ID.
    14  	IDLength = 5
    15  )
    16  
    17  var (
    18  	idRegexp    = regexp.MustCompile("^[a-z]([a-z][0-9]|[0-9][a-z])+$")
    19  	letterRunes = []rune(IDChars)
    20  )
    21  
    22  func Generate() string {
    23  	b := make([]rune, IDLength)
    24  	for {
    25  		rand.Seed(time.Now().UnixNano())
    26  		for i := range b {
    27  			b[i] = letterRunes[rand.Intn(len(letterRunes))] //nolint:gosec
    28  		}
    29  
    30  		id := string(b)
    31  		if !idRegexp.MatchString(id) {
    32  			continue
    33  		}
    34  
    35  		return id
    36  	}
    37  }