public static String convertToKubernetesName ( String text , boolean allowDots ) { String lower = text . toLowerCase ( ) ; StringBuilder builder = new StringBuilder ( ) ; boolean started = false ; char lastCh = ' ' ; for ( int i = 0 , last = lower . length ( ) - 1 ; i <= last ; i ++ ) { char ch = lower . charAt ( i ) ; boolean digit = ch >= ' ' && ch <= ' ' ; // names cannot start with a digit so lets add a prefix if ( digit && builder . length ( ) == 0 ) { builder . append ( DIGIT_PREFIX ) ; } if ( ! ( ch >= ' ' && ch <= ' ' ) && ! digit ) { if ( ch == ' ' ) { ch = ' ' ; } else if ( ch != ' ' && ch != ' ' ) { ch = ' ' ; } if ( ! allowDots && ch == ' ' ) { ch = ' ' ; } if ( ! started || lastCh == ' ' || lastCh == ' ' || i == last ) { continue ; } } builder . append ( ch ) ; started = true ; lastCh = ch ; } return builder . toString ( ) ; }
Lets convert the string to btw a valid kubernetes resource name