github.com/percona/percona-xtradb-cluster-operator@v1.14.0/pkg/pxc/backup/names.go (about)

     1  package backup
     2  
     3  import (
     4  	api "github.com/percona/percona-xtradb-cluster-operator/pkg/apis/pxc/v1"
     5  )
     6  
     7  // GenName63 generates legit name for backup resources.
     8  // k8s sets the `job-name` label for the created by job pod.
     9  // So we have to be sure that job name won't be longer than 63 symbols.
    10  // Yet the job name has to have some meaningful name which won't be conflicting with other jobs' names.
    11  func GenName63(cr *api.PerconaXtraDBClusterBackup) string {
    12  	postfix := cr.Name
    13  	maxNameLen := 16
    14  	typ, ok := cr.GetLabels()["type"]
    15  
    16  	// in case it's not a cron-job we're not sure if the name fits rules
    17  	// but there is more room for names
    18  	if !ok || typ != "cron" {
    19  		maxNameLen = 29
    20  		postfix = trimNameRight(postfix, maxNameLen)
    21  	}
    22  
    23  	name := "xb-" + postfix
    24  
    25  	return name
    26  }
    27  
    28  // trimNameRight if needed cut off symbol by symbol from the name right side
    29  // until it satisfy requirements to end with an alphanumeric character and have a length no more than ln
    30  func trimNameRight(name string, ln int) string {
    31  	if len(name) <= ln {
    32  		ln = len(name)
    33  	}
    34  
    35  	for ; ln > 0; ln-- {
    36  		if name[ln-1] >= 'a' && name[ln-1] <= 'z' ||
    37  			name[ln-1] >= '0' && name[ln-1] <= '9' {
    38  			break
    39  		}
    40  	}
    41  
    42  	return name[:ln]
    43  }