diff --git a/pkg/cloudprovider/cloud.go b/pkg/cloudprovider/cloud.go index 8124758cf95..247cb15ce78 100644 --- a/pkg/cloudprovider/cloud.go +++ b/pkg/cloudprovider/cloud.go @@ -69,7 +69,6 @@ func GetLoadBalancerName(service *v1.Service) string { } // GetInstanceProviderID builds a ProviderID for a node in a cloud. -// TODO: The name and placement of this method suggests that it is a canonical definition of ProviderID. This doesn't seem to be true. e.g. The AWS implementation uses undecorated InstanceID as the ProviderID. func GetInstanceProviderID(cloud Interface, nodeName types.NodeName) (string, error) { instances, ok := cloud.Instances() if !ok { diff --git a/pkg/cloudprovider/providers/aws/aws.go b/pkg/cloudprovider/providers/aws/aws.go index 62e6392ca3a..ce0c1d5f9e4 100644 --- a/pkg/cloudprovider/providers/aws/aws.go +++ b/pkg/cloudprovider/providers/aws/aws.go @@ -1037,8 +1037,11 @@ func (c *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 (c *Cloud) NodeAddressesByProviderID(providerID string) ([]v1.NodeAddress, error) { - // In AWS, we're using the instanceID as the providerID. - instanceID := providerID + instanceID, error := instanceIDFromProviderID(providerID) + + if error != nil { + return nil, error + } addresses, error := c.describeAddressesByInstanceID(instanceID) @@ -1111,8 +1114,11 @@ func (c *Cloud) InstanceID(nodeName 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 (c *Cloud) InstanceTypeByProviderID(providerID string) (string, error) { - // In AWS, we're using the instanceID as the providerID. - instanceID := providerID + instanceID, error := instanceIDFromProviderID(providerID) + + if error != nil { + return "", error + } instance, error := c.describeInstanceByInstanceID(instanceID) @@ -3573,6 +3579,17 @@ func convertAwsAddress(address *ec2.Address) ([]v1.NodeAddress, error) { return nodeAddresses, nil } +var providerIDRegexp = regexp.MustCompile(`^aws://([^/]+)$`) + +func instanceIDFromProviderID(providerID string) (instanceID string, err error) { + matches := providerIDRegexp.FindStringSubmatch(providerID) + if len(matches) != 1 { + return "", fmt.Errorf("ProviderID \"%s\" didn't match expected format \"aws://InstanceID\"", providerID) + } + + return matches[1], nil +} + func setNodeDisk( nodeDiskMap map[types.NodeName]map[KubernetesVolumeID]bool, volumeID KubernetesVolumeID,