Skip to content
This repository was archived by the owner on Mar 22, 2018. It is now read-only.

Commit bc68861

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request #49770 from FengyunPan/fix-GetInstanceIDFromProviderID
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 #49769 **Release note**: ```release-note NONE ```
2 parents 1e63225 + 4a4d7a4 commit bc68861

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

pkg/cloudprovider/providers/openstack/openstack_instances.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package openstack
1919
import (
2020
"errors"
2121
"fmt"
22-
"net/url"
22+
"regexp"
2323

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

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

192-
return parsedID.Host, nil
190+
matches := providerIdRegexp.FindStringSubmatch(providerID)
191+
if len(matches) != 2 {
192+
return "", fmt.Errorf("ProviderID \"%s\" didn't match expected format \"openstack:///InstanceID\"", providerID)
193+
}
194+
return matches[1], nil
193195
}

pkg/cloudprovider/providers/openstack/openstack_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,17 +557,22 @@ func TestInstanceIDFromProviderID(t *testing.T) {
557557
fail bool
558558
}{
559559
{
560-
providerID: "openstack://7b9cf879-7146-417c-abfd-cb4272f0c935",
560+
providerID: ProviderName + "://" + "/" + "7b9cf879-7146-417c-abfd-cb4272f0c935",
561561
instanceID: "7b9cf879-7146-417c-abfd-cb4272f0c935",
562562
fail: false,
563563
},
564+
{
565+
providerID: "openstack://7b9cf879-7146-417c-abfd-cb4272f0c935",
566+
instanceID: "",
567+
fail: true,
568+
},
564569
{
565570
providerID: "7b9cf879-7146-417c-abfd-cb4272f0c935",
566571
instanceID: "",
567572
fail: true,
568573
},
569574
{
570-
providerID: "other-provider://7b9cf879-7146-417c-abfd-cb4272f0c935",
575+
providerID: "other-provider:///7b9cf879-7146-417c-abfd-cb4272f0c935",
571576
instanceID: "",
572577
fail: true,
573578
},

0 commit comments

Comments
 (0)