Skip to content

Commit

Permalink
Merge pull request kubernetes#49770 from FengyunPan/fix-GetInstanceID…
Browse files Browse the repository at this point in the history
…FromProviderID

Automatic merge from submit-queue (batch tested with PRs 51244, 50559, 49770, 51194, 50901)

Fix the matching rule of instance ProviderID

Url.Parse() can't parse ProviderID which contains ':///'.
This PR use regexp to match ProviderID.

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #
Fix kubernetes#49769

**Release note**:
```release-note
NONE
```
  • Loading branch information
Kubernetes Submit Queue authored Aug 25, 2017
2 parents 1e63225 + 4a4d7a4 commit bc68861
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
20 changes: 11 additions & 9 deletions pkg/cloudprovider/providers/openstack/openstack_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package openstack
import (
"errors"
"fmt"
"net/url"
"regexp"

"github.com/golang/glog"
"github.com/gophercloud/gophercloud"
Expand Down Expand Up @@ -180,14 +180,16 @@ func srvInstanceType(srv *servers.Server) (string, error) {
return "", fmt.Errorf("flavor name/id not found")
}

// instanceIDFromProviderID splits a provider's id and return instanceID.
// A providerID is build out of '${ProviderName}:///${instance-id}'which contains ':///'.
// See cloudprovider.GetInstanceProviderID and Instances.InstanceID.
func instanceIDFromProviderID(providerID string) (instanceID string, err error) {
parsedID, err := url.Parse(providerID)
if err != nil {
return "", err
}
if parsedID.Scheme != ProviderName {
return "", fmt.Errorf("unrecognized provider %q", parsedID.Scheme)
}
// If Instances.InstanceID or cloudprovider.GetInstanceProviderID is changed, the regexp should be changed too.
var providerIdRegexp = regexp.MustCompile(`^` + ProviderName + `:///([^/]+)$`)

return parsedID.Host, nil
matches := providerIdRegexp.FindStringSubmatch(providerID)
if len(matches) != 2 {
return "", fmt.Errorf("ProviderID \"%s\" didn't match expected format \"openstack:///InstanceID\"", providerID)
}
return matches[1], nil
}
9 changes: 7 additions & 2 deletions pkg/cloudprovider/providers/openstack/openstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,17 +557,22 @@ func TestInstanceIDFromProviderID(t *testing.T) {
fail bool
}{
{
providerID: "openstack://7b9cf879-7146-417c-abfd-cb4272f0c935",
providerID: ProviderName + "://" + "/" + "7b9cf879-7146-417c-abfd-cb4272f0c935",
instanceID: "7b9cf879-7146-417c-abfd-cb4272f0c935",
fail: false,
},
{
providerID: "openstack://7b9cf879-7146-417c-abfd-cb4272f0c935",
instanceID: "",
fail: true,
},
{
providerID: "7b9cf879-7146-417c-abfd-cb4272f0c935",
instanceID: "",
fail: true,
},
{
providerID: "other-provider://7b9cf879-7146-417c-abfd-cb4272f0c935",
providerID: "other-provider:///7b9cf879-7146-417c-abfd-cb4272f0c935",
instanceID: "",
fail: true,
},
Expand Down

0 comments on commit bc68861

Please sign in to comment.