From 1a11a1886f47267f0be90186f42f60280449a5e1 Mon Sep 17 00:00:00 2001 From: stewart-yu Date: Mon, 13 Nov 2017 22:31:33 +0800 Subject: [PATCH] using regexp match achieve find efficiently --- pkg/cloudprovider/providers/aws/instances.go | 7 +++++-- pkg/cloudprovider/providers/aws/volumes.go | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/cloudprovider/providers/aws/instances.go b/pkg/cloudprovider/providers/aws/instances.go index 3fa778e2e62..a42834415dd 100644 --- a/pkg/cloudprovider/providers/aws/instances.go +++ b/pkg/cloudprovider/providers/aws/instances.go @@ -25,10 +25,14 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" "github.com/golang/glog" "k8s.io/api/core/v1" + "regexp" "sync" "time" ) +// awsInstanceRegMatch represents Regex Match for AWS instance. +var awsInstanceRegMatch = regexp.MustCompile("^i-[^/]*$") + // awsInstanceID represents the ID of the instance in the AWS API, e.g. i-12345678 // The "traditional" format is "i-12345678" // A new longer format is also being introduced: "i-12345678abcdef01" @@ -76,8 +80,7 @@ func (name kubernetesInstanceID) mapToAWSInstanceID() (awsInstanceID, error) { // We sanity check the resulting volume; the two known formats are // i-12345678 and i-12345678abcdef01 - // TODO: Regex match? - if awsID == "" || strings.Contains(awsID, "/") || !strings.HasPrefix(awsID, "i-") { + if awsID == "" || !awsInstanceRegMatch.MatchString(awsID) { return "", fmt.Errorf("Invalid format for AWS instance (%s)", name) } diff --git a/pkg/cloudprovider/providers/aws/volumes.go b/pkg/cloudprovider/providers/aws/volumes.go index 8ff342199d8..c67f080fec1 100644 --- a/pkg/cloudprovider/providers/aws/volumes.go +++ b/pkg/cloudprovider/providers/aws/volumes.go @@ -19,11 +19,15 @@ package aws import ( "fmt" "net/url" + "regexp" "strings" "github.com/aws/aws-sdk-go/aws" ) +// awsVolumeRegMatch represents Regex Match for AWS volume. +var awsVolumeRegMatch = regexp.MustCompile("^vol-[^/]*$") + // awsVolumeID represents the ID of the volume in the AWS API, e.g. vol-12345678 // The "traditional" format is "vol-12345678" // A new longer format is also being introduced: "vol-12345678abcdef01" @@ -75,8 +79,7 @@ func (name KubernetesVolumeID) mapToAWSVolumeID() (awsVolumeID, error) { // We sanity check the resulting volume; the two known formats are // vol-12345678 and vol-12345678abcdef01 - // TODO: Regex match? - if strings.Contains(awsID, "/") || !strings.HasPrefix(awsID, "vol-") { + if !awsVolumeRegMatch.MatchString(awsID) { return "", fmt.Errorf("Invalid format for AWS volume (%s)", name) }