Merge pull request #51914 from sergeylanzman/master

Automatic merge from submit-queue (batch tested with PRs 55392, 55491, 51914, 55831, 55836). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Move regexp.MustCompile to global variable

Move regexp.MustCompile to global variable.
regexp.MustCompile heavy function and can be run on init app
```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2017-11-17 00:18:22 -08:00 committed by GitHub
commit 79c2274566
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 20 deletions

View File

@ -39,7 +39,8 @@ import (
) )
var ( var (
envResources = ` validEnvNameRegexp = regexp.MustCompile("[^a-zA-Z0-9_]")
envResources = `
pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds), job, replicaset (rs)` pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds), job, replicaset (rs)`
envLong = templates.LongDesc(` envLong = templates.LongDesc(`
@ -172,7 +173,6 @@ func validateNoOverwrites(existing []v1.EnvVar, env []v1.EnvVar) error {
} }
func keyToEnvName(key string) string { func keyToEnvName(key string) string {
validEnvNameRegexp := regexp.MustCompile("[^a-zA-Z0-9_]")
return strings.ToUpper(validEnvNameRegexp.ReplaceAllString(key, "_")) return strings.ToUpper(validEnvNameRegexp.ReplaceAllString(key, "_"))
} }

View File

@ -44,8 +44,11 @@ const (
// Optional device. // Optional device.
nvidiaUVMToolsDevice string = "/dev/nvidia-uvm-tools" nvidiaUVMToolsDevice string = "/dev/nvidia-uvm-tools"
devDirectory = "/dev" devDirectory = "/dev"
nvidiaDeviceRE = `^nvidia[0-9]*$` )
nvidiaFullpathRE = `^/dev/nvidia[0-9]*$`
var (
nvidiaDeviceRE = regexp.MustCompile(`^nvidia[0-9]*$`)
nvidiaFullpathRE = regexp.MustCompile(`^/dev/nvidia[0-9]*$`)
) )
type activePodsLister interface { type activePodsLister interface {
@ -194,7 +197,6 @@ func (ngm *nvidiaGPUManager) updateAllocatedGPUs() {
// we want more features, features like schedule containers according to GPU family // we want more features, features like schedule containers according to GPU family
// name. // name.
func (ngm *nvidiaGPUManager) discoverGPUs() error { func (ngm *nvidiaGPUManager) discoverGPUs() error {
reg := regexp.MustCompile(nvidiaDeviceRE)
files, err := ioutil.ReadDir(devDirectory) files, err := ioutil.ReadDir(devDirectory)
if err != nil { if err != nil {
return err return err
@ -203,7 +205,7 @@ func (ngm *nvidiaGPUManager) discoverGPUs() error {
if f.IsDir() { if f.IsDir() {
continue continue
} }
if reg.MatchString(f.Name()) { if nvidiaDeviceRE.MatchString(f.Name()) {
glog.V(2).Infof("Found Nvidia GPU %q", f.Name()) glog.V(2).Infof("Found Nvidia GPU %q", f.Name())
ngm.allGPUs.Insert(path.Join(devDirectory, f.Name())) ngm.allGPUs.Insert(path.Join(devDirectory, f.Name()))
} }
@ -274,5 +276,5 @@ func (ngm *nvidiaGPUManager) gpusInUse() *podGPUs {
} }
func isValidPath(path string) bool { func isValidPath(path string) bool {
return regexp.MustCompile(nvidiaFullpathRE).MatchString(path) return nvidiaFullpathRE.MatchString(path)
} }

View File

@ -43,6 +43,8 @@ var (
"node.session.auth.password", "node.session.auth.password",
"node.session.auth.username_in", "node.session.auth.username_in",
"node.session.auth.password_in"} "node.session.auth.password_in"}
ifaceTransportNameRe = regexp.MustCompile(`iface.transport_name = (.*)\n`)
ifaceRe = regexp.MustCompile(`.+/iface-([^/]+)/.+`)
) )
func updateISCSIDiscoverydb(b iscsiDiskMounter, tp string) error { func updateISCSIDiscoverydb(b iscsiDiskMounter, tp string) error {
@ -429,9 +431,7 @@ func (util *ISCSIUtil) DetachDisk(c iscsiDiskUnmounter, mntPath string) error {
} }
func extractTransportname(ifaceOutput string) (iscsiTransport string) { func extractTransportname(ifaceOutput string) (iscsiTransport string) {
re := regexp.MustCompile(`iface.transport_name = (.*)\n`) rexOutput := ifaceTransportNameRe.FindStringSubmatch(ifaceOutput)
rexOutput := re.FindStringSubmatch(ifaceOutput)
if rexOutput == nil { if rexOutput == nil {
return "" return ""
} }
@ -460,9 +460,7 @@ func extractDeviceAndPrefix(mntPath string) (string, string, error) {
} }
func extractIface(mntPath string) (string, bool) { func extractIface(mntPath string) (string, bool) {
re := regexp.MustCompile(`.+/iface-([^/]+)/.+`) reOutput := ifaceRe.FindStringSubmatch(mntPath)
reOutput := re.FindStringSubmatch(mntPath)
if reOutput != nil { if reOutput != nil {
return reOutput[1], true return reOutput[1], true
} }

View File

@ -46,6 +46,10 @@ const (
rbdCmdErr = "executable file not found in $PATH" rbdCmdErr = "executable file not found in $PATH"
) )
var (
clientKubeLockMagicRe = regexp.MustCompile("client.* " + kubeLockMagic + ".*")
)
// search /sys/bus for rbd device that matches given pool and image // search /sys/bus for rbd device that matches given pool and image
func getDevFromImageAndPool(pool, image string) (string, bool) { func getDevFromImageAndPool(pool, image string) (string, bool) {
// /sys/bus/rbd/devices/X/name and /sys/bus/rbd/devices/X/pool // /sys/bus/rbd/devices/X/name and /sys/bus/rbd/devices/X/pool
@ -183,8 +187,7 @@ func (util *RBDUtil) rbdLock(b rbdMounter, lock bool) error {
} }
// best effort clean up orphaned locked if not used // best effort clean up orphaned locked if not used
re := regexp.MustCompile("client.* " + kubeLockMagic + ".*") locks := clientKubeLockMagicRe.FindAllStringSubmatch(output, -1)
locks := re.FindAllStringSubmatch(output, -1)
for _, v := range locks { for _, v := range locks {
if len(v) > 0 { if len(v) > 0 {
lockInfo := strings.Split(v[0], " ") lockInfo := strings.Split(v[0], " ")

View File

@ -43,7 +43,11 @@ type Parser struct {
width int width int
} }
var ErrSyntax = errors.New("invalid syntax") var (
ErrSyntax = errors.New("invalid syntax")
dictKeyRex = regexp.MustCompile(`^'([^']*)'$`)
sliceOperatorRex = regexp.MustCompile(`^(-?[\d]*)(:-?[\d]*)?(:[\d]*)?$`)
)
// Parse parsed the given text and return a node Parser. // Parse parsed the given text and return a node Parser.
// If an error is encountered, parsing stops and an empty // If an error is encountered, parsing stops and an empty
@ -283,8 +287,7 @@ Loop:
} }
// dict key // dict key
reg := regexp.MustCompile(`^'([^']*)'$`) value := dictKeyRex.FindStringSubmatch(text)
value := reg.FindStringSubmatch(text)
if value != nil { if value != nil {
parser, err := parseAction("arraydict", fmt.Sprintf(".%s", value[1])) parser, err := parseAction("arraydict", fmt.Sprintf(".%s", value[1]))
if err != nil { if err != nil {
@ -297,8 +300,7 @@ Loop:
} }
//slice operator //slice operator
reg = regexp.MustCompile(`^(-?[\d]*)(:-?[\d]*)?(:[\d]*)?$`) value = sliceOperatorRex.FindStringSubmatch(text)
value = reg.FindStringSubmatch(text)
if value == nil { if value == nil {
return fmt.Errorf("invalid array index %s", text) return fmt.Errorf("invalid array index %s", text)
} }