From de8c6deaf38e63d1d28ec4e8e1683b416b8918ec Mon Sep 17 00:00:00 2001 From: Di Xu Date: Thu, 20 Jun 2019 15:48:29 +0800 Subject: [PATCH] compiled in a package level var --- .../k8s.io/cluster-bootstrap/util/secrets/secrets.go | 12 +++++------- .../k8s.io/cluster-bootstrap/util/tokens/tokens.go | 11 +++++------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/staging/src/k8s.io/cluster-bootstrap/util/secrets/secrets.go b/staging/src/k8s.io/cluster-bootstrap/util/secrets/secrets.go index 07b4a6e9740..c2a87b7398a 100644 --- a/staging/src/k8s.io/cluster-bootstrap/util/secrets/secrets.go +++ b/staging/src/k8s.io/cluster-bootstrap/util/secrets/secrets.go @@ -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 } diff --git a/staging/src/k8s.io/cluster-bootstrap/util/tokens/tokens.go b/staging/src/k8s.io/cluster-bootstrap/util/tokens/tokens.go index ad8fb3b4732..bedeaceb382 100644 --- a/staging/src/k8s.io/cluster-bootstrap/util/tokens/tokens.go +++ b/staging/src/k8s.io/cluster-bootstrap/util/tokens/tokens.go @@ -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) }