Add splitProviderID for azure

This commit is contained in:
realfake 2017-05-31 23:19:38 +02:00
parent 0cff839317
commit fc748662ef
2 changed files with 64 additions and 0 deletions

View File

@ -677,3 +677,54 @@ func TestDecodeInstanceInfo(t *testing.T) {
t.Error("got incorrect fault domain")
}
}
func TestSplitProviderID(t *testing.T) {
providers := []struct {
providerID string
name types.NodeName
fail bool
}{
{
providerID: CloudProviderName + ":///subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroupName/providers/Microsoft.Compute/virtualMachines/k8s-agent-AAAAAAAA-0",
name: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroupName/providers/Microsoft.Compute/virtualMachines/k8s-agent-AAAAAAAA-0",
fail: false,
},
{
providerID: CloudProviderName + ":/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroupName/providers/Microsoft.Compute/virtualMachines/k8s-agent-AAAAAAAA-0",
name: "",
fail: true,
},
{
providerID: CloudProviderName + "://",
name: "",
fail: true,
},
{
providerID: ":///subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroupName/providers/Microsoft.Compute/virtualMachines/k8s-agent-AAAAAAAA-0",
name: "",
fail: true,
},
{
providerID: "aws:///subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroupName/providers/Microsoft.Compute/virtualMachines/k8s-agent-AAAAAAAA-0",
name: "",
fail: true,
},
}
for _, test := range providers {
name, err := splitProviderID(test.providerID)
if (err != nil) != test.fail {
t.Errorf("Expected to failt=%t, with pattern %v", test.fail, test)
}
if test.fail {
continue
}
if name != test.name {
t.Errorf("Expected %v, but got %v", test.name, name)
}
}
}

View File

@ -17,7 +17,9 @@ limitations under the License.
package azure
import (
"errors"
"fmt"
"regexp"
"strings"
"k8s.io/kubernetes/pkg/api/v1"
@ -41,6 +43,8 @@ const (
securityRuleIDTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkSecurityGroups/%s/securityRules/%s"
)
var providerIDRE = regexp.MustCompile(`^` + CloudProviderName + `://(.+)$`)
// returns the full identifier of a machine
func (az *Cloud) getMachineID(machineName string) string {
return fmt.Sprintf(
@ -268,3 +272,12 @@ func (az *Cloud) getIPForMachine(nodeName types.NodeName) (string, error) {
targetIP := *ipConfig.PrivateIPAddress
return targetIP, nil
}
// splitProviderID converts a providerID to a NodeName.
func splitProviderID(providerID string) (types.NodeName, error) {
matches := providerIDRE.FindStringSubmatch(providerID)
if len(matches) != 2 {
return "", errors.New("error splitting providerID")
}
return types.NodeName(matches[1]), nil
}