github.com/cilium/cilium@v1.16.2/operator/pkg/ciliumendpointslice/utils.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package ciliumendpointslice
     5  
     6  import (
     7  	"math/rand/v2"
     8  	"strings"
     9  )
    10  
    11  // Generate random string for given length of characters.
    12  func randomName(n int) string {
    13  	b := make([]rune, n)
    14  	for i := range b {
    15  		b[i] = sequentialLetters[rand.IntN(len(sequentialLetters))]
    16  	}
    17  	return string(b)
    18  }
    19  
    20  // Generates unique random name for the CiliumEndpointSlice, the format
    21  // of a CES name is similar to pod k8s naming convention "ces-123456789-abcde".
    22  // First 3 letters indicates ces resource, followed by random letters.
    23  func uniqueCESliceName(mapping *CESToCEPMapping) string {
    24  	var sb strings.Builder
    25  	for {
    26  		rn1, rn2 := randomName(9), randomName(5)
    27  		sb.Reset()
    28  		sb.Grow(len(cesNamePrefix) + 1 + len(rn1) + 1 + len(rn2))
    29  		sb.WriteString(cesNamePrefix)
    30  		sb.WriteRune('-')
    31  		sb.WriteString(rn1)
    32  		sb.WriteRune('-')
    33  		sb.WriteString(rn2)
    34  		cesName := sb.String()
    35  		if !mapping.hasCESName(NewCESName(cesName)) {
    36  			return cesName
    37  		}
    38  	}
    39  }