...

Source file src/edge-infra.dev/pkg/edge/api/utils/labels.go

Documentation: edge-infra.dev/pkg/edge/api/utils

     1  package utils
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  	"unicode"
     8  
     9  	"k8s.io/apimachinery/pkg/util/validation"
    10  )
    11  
    12  var (
    13  	// allowedEnvSpecialCharacters a list of valid special characters for kubernetes environment variables.
    14  	// taken from the k8s.io/apimachinery/pkg/util/validation Line 420 pkg.
    15  	allowedEnvSpecialCharacters = map[string]bool{
    16  		"_": true,
    17  		"-": true,
    18  		".": true,
    19  	}
    20  )
    21  
    22  // a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.', and must not start with a digit
    23  func ToENVName(labelKey string) string {
    24  	if IsValidEnvironmentName(labelKey) {
    25  		return labelKey
    26  	}
    27  	// if it's not a valid environment name
    28  	// we need to change it to conform to be an env name
    29  	return toENVName(labelKey)
    30  }
    31  
    32  func ToK8sName(labelKey string) string {
    33  	var k8sname strings.Builder
    34  	labelKey = strings.Trim(labelKey, " ")
    35  	for i := 0; i < len(labelKey); i++ {
    36  		// if valid short circuit and continue
    37  		if IsValidLabelValue(string(labelKey[i])) {
    38  			k8sname.WriteString(string(labelKey[i]))
    39  			continue
    40  		}
    41  		// if previous value was invalid and trimmed then do not set to _, instead convert to empty ""
    42  		if i == 0 && !IsValidLabelValue(string(labelKey[i])) || i > 0 && !IsValidLabelValue(string(labelKey[i-1])) || i == len(labelKey)-1 && !IsValidLabel(string(labelKey[i])) {
    43  			k8sname.WriteString("")
    44  			continue
    45  		}
    46  		k8sname.WriteString("-")
    47  	}
    48  	return k8sname.String()
    49  }
    50  
    51  func ValuesValidation(values []string) error {
    52  	var err error
    53  	for _, val := range values {
    54  		if validation.IsValidLabelValue(val) != nil {
    55  			err = errors.Join(err, fmt.Errorf("invalid k8s label value: %s", val))
    56  		}
    57  	}
    58  	return err
    59  }
    60  
    61  func IsValidLabelValue(label string) bool {
    62  	return validation.IsValidLabelValue(label) == nil
    63  }
    64  
    65  func IsValidLabel(label string) bool {
    66  	return validation.IsDNS1123Label(label) == nil
    67  }
    68  
    69  // IsValidEnvironmentName verifies that the specified env name is a valid kubernetes environment name.
    70  func IsValidEnvironmentName(name string) bool {
    71  	return len(validation.IsEnvVarName(name)) == 0
    72  }
    73  
    74  // toENVName converts a string value to a valid environment variable name.
    75  // a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.',
    76  // and must not start with a digit
    77  func toENVName(val string) string {
    78  	var result strings.Builder
    79  	// ensure that all white spaces are removed and trimmed.
    80  	val = strings.TrimSpace(val)
    81  	for index, valRune := range val {
    82  		switch {
    83  		case unicode.IsDigit(valRune) && index == 0:
    84  			// if the first character in the env name is a digit
    85  			// we need to ignore it
    86  			continue
    87  		case unicode.IsSpace(valRune):
    88  			// if the character is a space and is not the first character
    89  			// we convert it to an _
    90  			result.WriteString("_")
    91  		case !unicode.IsLetter(valRune) && !unicode.IsDigit(valRune) && isNotAnAllowedSpecialChar(valRune):
    92  			// if the character is not an alphabetic character
    93  			// or a digit or an allowed special character.
    94  			// we need to ignore it.
    95  			continue
    96  		default:
    97  			result.WriteString(string(valRune))
    98  		}
    99  	}
   100  	resultString := result.String()
   101  	if !IsValidEnvironmentName(resultString) && len(resultString) > 2 {
   102  		resultString = toENVName(resultString)
   103  	}
   104  	return resultString
   105  }
   106  
   107  // isNotAnAllowedSpecialChar checks the allowedEnvSpecialCharacters to see if the specified character exists.
   108  func isNotAnAllowedSpecialChar(valRune rune) bool {
   109  	_, exists := allowedEnvSpecialCharacters[string(valRune)]
   110  	return !exists
   111  }
   112  

View as plain text