Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure for cloud-controller-manager #46940

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions pkg/cloudprovider/providers/azure/azure_instances.go
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package azure

import (
"errors"
"fmt"

"k8s.io/kubernetes/pkg/api/v1"
Expand All @@ -44,7 +43,12 @@ func (az *Cloud) NodeAddresses(name types.NodeName) ([]v1.NodeAddress, error) {
// This method will not be called from the node that is requesting this ID. i.e. metadata service
// and other local methods cannot be used here
func (az *Cloud) NodeAddressesByProviderID(providerID string) ([]v1.NodeAddress, error) {
return []v1.NodeAddress{}, errors.New("unimplemented")
name, err := splitProviderID(providerID)
if err != nil {
return nil, err
}

return az.NodeAddresses(name)
}

// ExternalID returns the cloud provider ID of the specified instance (deprecated).
Expand All @@ -68,7 +72,12 @@ func (az *Cloud) InstanceID(name types.NodeName) (string, error) {
// This method will not be called from the node that is requesting this ID. i.e. metadata service
// and other local methods cannot be used here
func (az *Cloud) InstanceTypeByProviderID(providerID string) (string, error) {
return "", errors.New("unimplemented")
name, err := splitProviderID(providerID)
if err != nil {
return "", err
}

return az.InstanceID(name)
}

// InstanceType returns the type of the specified instance.
Expand Down
51 changes: 51 additions & 0 deletions pkg/cloudprovider/providers/azure/azure_test.go
Expand Up @@ -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)
}

}
}
13 changes: 13 additions & 0 deletions pkg/cloudprovider/providers/azure/azure_util.go
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package azure

import (
"errors"
"fmt"
"regexp"
"strings"

"k8s.io/kubernetes/pkg/api/v1"
Expand All @@ -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(
Expand Down Expand Up @@ -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
}