mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
compiled in a package level var
This commit is contained in:
parent
22664c2a47
commit
de8c6deaf3
@ -28,6 +28,10 @@ import (
|
|||||||
"k8s.io/klog"
|
"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
|
// 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.
|
// 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 {
|
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.
|
// ParseName parses the name of the secret to extract the secret ID.
|
||||||
func ParseName(name string) (secretID string, ok bool) {
|
func ParseName(name string) (secretID string, ok bool) {
|
||||||
namePattern := `^` + regexp.QuoteMeta(api.BootstrapTokenSecretPrefix) + `([a-z0-9]{6})$`
|
r := secretNameRe.FindStringSubmatch(name)
|
||||||
nameRegExp, err := regexp.Compile(namePattern)
|
|
||||||
if err != nil {
|
|
||||||
klog.Errorf("error compiling bootstrap regex %q: %v", namePattern, err)
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
r := nameRegExp.FindStringSubmatch(name)
|
|
||||||
if r == nil {
|
if r == nil {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
@ -23,15 +23,14 @@ import (
|
|||||||
"k8s.io/cluster-bootstrap/token/api"
|
"k8s.io/cluster-bootstrap/token/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
bootstrapTokenRe = regexp.MustCompile(api.BootstrapTokenPattern)
|
||||||
|
)
|
||||||
|
|
||||||
// ParseToken tries and parse a valid token from a string.
|
// 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.
|
// A token ID and token secret are returned in case of success, an error otherwise.
|
||||||
func ParseToken(s string) (tokenID, tokenSecret string, err error) {
|
func ParseToken(s string) (tokenID, tokenSecret string, err error) {
|
||||||
bootstrapTokenRegexp, err := regexp.Compile(api.BootstrapTokenPattern)
|
split := bootstrapTokenRe.FindStringSubmatch(s)
|
||||||
if err != nil {
|
|
||||||
return "", "", fmt.Errorf("error compiling bootstrap regex %q: %v", api.BootstrapTokenPattern, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
split := bootstrapTokenRegexp.FindStringSubmatch(s)
|
|
||||||
if len(split) != 3 {
|
if len(split) != 3 {
|
||||||
return "", "", fmt.Errorf("token [%q] was not of form [%q]", s, api.BootstrapTokenPattern)
|
return "", "", fmt.Errorf("token [%q] was not of form [%q]", s, api.BootstrapTokenPattern)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user