diff --git a/pkg/kubectl/cmd/set/set_env.go b/pkg/kubectl/cmd/set/set_env.go index 303ccc009d3..7f96dea5f5d 100644 --- a/pkg/kubectl/cmd/set/set_env.go +++ b/pkg/kubectl/cmd/set/set_env.go @@ -39,7 +39,8 @@ import ( ) var ( - envResources = ` + validEnvNameRegexp = regexp.MustCompile("[^a-zA-Z0-9_]") + envResources = ` pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds), job, replicaset (rs)` envLong = templates.LongDesc(` @@ -172,7 +173,6 @@ func validateNoOverwrites(existing []v1.EnvVar, env []v1.EnvVar) error { } func keyToEnvName(key string) string { - validEnvNameRegexp := regexp.MustCompile("[^a-zA-Z0-9_]") return strings.ToUpper(validEnvNameRegexp.ReplaceAllString(key, "_")) } diff --git a/pkg/kubelet/gpu/nvidia/nvidia_gpu_manager.go b/pkg/kubelet/gpu/nvidia/nvidia_gpu_manager.go index 911434f6b6b..37c30d14b55 100644 --- a/pkg/kubelet/gpu/nvidia/nvidia_gpu_manager.go +++ b/pkg/kubelet/gpu/nvidia/nvidia_gpu_manager.go @@ -44,8 +44,11 @@ const ( // Optional device. nvidiaUVMToolsDevice string = "/dev/nvidia-uvm-tools" 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 { @@ -194,7 +197,6 @@ func (ngm *nvidiaGPUManager) updateAllocatedGPUs() { // we want more features, features like schedule containers according to GPU family // name. func (ngm *nvidiaGPUManager) discoverGPUs() error { - reg := regexp.MustCompile(nvidiaDeviceRE) files, err := ioutil.ReadDir(devDirectory) if err != nil { return err @@ -203,7 +205,7 @@ func (ngm *nvidiaGPUManager) discoverGPUs() error { if f.IsDir() { continue } - if reg.MatchString(f.Name()) { + if nvidiaDeviceRE.MatchString(f.Name()) { glog.V(2).Infof("Found Nvidia GPU %q", f.Name()) ngm.allGPUs.Insert(path.Join(devDirectory, f.Name())) } @@ -274,5 +276,5 @@ func (ngm *nvidiaGPUManager) gpusInUse() *podGPUs { } func isValidPath(path string) bool { - return regexp.MustCompile(nvidiaFullpathRE).MatchString(path) + return nvidiaFullpathRE.MatchString(path) } diff --git a/pkg/volume/iscsi/iscsi_util.go b/pkg/volume/iscsi/iscsi_util.go index 46f9d8fe313..a72dace7735 100644 --- a/pkg/volume/iscsi/iscsi_util.go +++ b/pkg/volume/iscsi/iscsi_util.go @@ -43,6 +43,8 @@ var ( "node.session.auth.password", "node.session.auth.username_in", "node.session.auth.password_in"} + ifaceTransportNameRe = regexp.MustCompile(`iface.transport_name = (.*)\n`) + ifaceRe = regexp.MustCompile(`.+/iface-([^/]+)/.+`) ) 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) { - re := regexp.MustCompile(`iface.transport_name = (.*)\n`) - - rexOutput := re.FindStringSubmatch(ifaceOutput) + rexOutput := ifaceTransportNameRe.FindStringSubmatch(ifaceOutput) if rexOutput == nil { return "" } @@ -460,9 +460,7 @@ func extractDeviceAndPrefix(mntPath string) (string, string, error) { } func extractIface(mntPath string) (string, bool) { - re := regexp.MustCompile(`.+/iface-([^/]+)/.+`) - - reOutput := re.FindStringSubmatch(mntPath) + reOutput := ifaceRe.FindStringSubmatch(mntPath) if reOutput != nil { return reOutput[1], true } diff --git a/pkg/volume/rbd/rbd_util.go b/pkg/volume/rbd/rbd_util.go index 7f9fdfca415..85f4c425bf8 100644 --- a/pkg/volume/rbd/rbd_util.go +++ b/pkg/volume/rbd/rbd_util.go @@ -46,6 +46,10 @@ const ( 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 func getDevFromImageAndPool(pool, image string) (string, bool) { // /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 - re := regexp.MustCompile("client.* " + kubeLockMagic + ".*") - locks := re.FindAllStringSubmatch(output, -1) + locks := clientKubeLockMagicRe.FindAllStringSubmatch(output, -1) for _, v := range locks { if len(v) > 0 { lockInfo := strings.Split(v[0], " ") diff --git a/staging/src/k8s.io/client-go/util/jsonpath/parser.go b/staging/src/k8s.io/client-go/util/jsonpath/parser.go index 2f02de1a315..ef0f9213a4f 100644 --- a/staging/src/k8s.io/client-go/util/jsonpath/parser.go +++ b/staging/src/k8s.io/client-go/util/jsonpath/parser.go @@ -43,7 +43,11 @@ type Parser struct { 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. // If an error is encountered, parsing stops and an empty @@ -283,8 +287,7 @@ Loop: } // dict key - reg := regexp.MustCompile(`^'([^']*)'$`) - value := reg.FindStringSubmatch(text) + value := dictKeyRex.FindStringSubmatch(text) if value != nil { parser, err := parseAction("arraydict", fmt.Sprintf(".%s", value[1])) if err != nil { @@ -297,8 +300,7 @@ Loop: } //slice operator - reg = regexp.MustCompile(`^(-?[\d]*)(:-?[\d]*)?(:[\d]*)?$`) - value = reg.FindStringSubmatch(text) + value = sliceOperatorRex.FindStringSubmatch(text) if value == nil { return fmt.Errorf("invalid array index %s", text) }