compiled in a package level var

This commit is contained in:
Di Xu 2019-06-20 15:48:29 +08:00
parent 22664c2a47
commit de8c6deaf3
2 changed files with 10 additions and 13 deletions

View File

@ -28,6 +28,10 @@ import (
"k8s.io/klog"
)
var (
secretNameRe = regexp.MustCompile(`^` + regexp.QuoteMeta(api.BootstrapTokenSecretPrefix) + `([a-z0-9]{6})$`)
)
// GetData returns the string value for the given key in the specified Secret
// If there is an error or if the key doesn't exist, an empty string is returned.
func GetData(secret *v1.Secret, key string) string {
@ -61,13 +65,7 @@ func HasExpired(secret *v1.Secret, currentTime time.Time) bool {
// ParseName parses the name of the secret to extract the secret ID.
func ParseName(name string) (secretID string, ok bool) {
namePattern := `^` + regexp.QuoteMeta(api.BootstrapTokenSecretPrefix) + `([a-z0-9]{6})$`
nameRegExp, err := regexp.Compile(namePattern)
if err != nil {
klog.Errorf("error compiling bootstrap regex %q: %v", namePattern, err)
return "", false
}
r := nameRegExp.FindStringSubmatch(name)
r := secretNameRe.FindStringSubmatch(name)
if r == nil {
return "", false
}

View File

@ -23,15 +23,14 @@ import (
"k8s.io/cluster-bootstrap/token/api"
)
var (
bootstrapTokenRe = regexp.MustCompile(api.BootstrapTokenPattern)
)
// ParseToken tries and parse a valid token from a string.
// A token ID and token secret are returned in case of success, an error otherwise.
func ParseToken(s string) (tokenID, tokenSecret string, err error) {
bootstrapTokenRegexp, err := regexp.Compile(api.BootstrapTokenPattern)
if err != nil {
return "", "", fmt.Errorf("error compiling bootstrap regex %q: %v", api.BootstrapTokenPattern, err)
}
split := bootstrapTokenRegexp.FindStringSubmatch(s)
split := bootstrapTokenRe.FindStringSubmatch(s)
if len(split) != 3 {
return "", "", fmt.Errorf("token [%q] was not of form [%q]", s, api.BootstrapTokenPattern)
}