Skip to content

Commit

Permalink
Implement InstanceExistsByProviderID() for cloud providers
Browse files Browse the repository at this point in the history
Fix kubernetes#51406
If cloud providers(like aws, gce etc...) implement ExternalID()
and support getting instance by ProviderID , they also implement
InstanceExistsByProviderID().
  • Loading branch information
FengyunPan committed Aug 26, 2017
1 parent 84d9778 commit 2858447
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 8 deletions.
19 changes: 18 additions & 1 deletion pkg/cloudprovider/providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,24 @@ func (c *Cloud) ExternalID(nodeName types.NodeName) (string, error) {
// InstanceExistsByProviderID returns true if the instance with the given provider id still exists and is running.
// If false is returned with no error, the instance will be immediately deleted by the cloud controller manager.
func (c *Cloud) InstanceExistsByProviderID(providerID string) (bool, error) {
return false, errors.New("unimplemented")
instanceID, err := kubernetesInstanceID(providerID).mapToAWSInstanceID()
if err != nil {
return false, err
}

request := &ec2.DescribeInstancesInput{
InstanceIds: []*string{instanceID.awsString()},
}

instances, err := c.ec2.DescribeInstances(request)
if err != nil {
return false, err
}
if len(instances) == 0 {
return false, nil
}

return true, nil
}

// InstanceID returns the cloud provider ID of the node with the specified nodeName.
Expand Down
16 changes: 14 additions & 2 deletions pkg/cloudprovider/providers/azure/azure_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package azure

import (
"errors"
"fmt"

"k8s.io/api/core/v1"
Expand Down Expand Up @@ -90,7 +89,20 @@ func (az *Cloud) ExternalID(name types.NodeName) (string, error) {
// InstanceExistsByProviderID returns true if the instance with the given provider id still exists and is running.
// If false is returned with no error, the instance will be immediately deleted by the cloud controller manager.
func (az *Cloud) InstanceExistsByProviderID(providerID string) (bool, error) {
return false, errors.New("unimplemented")
name, err := splitProviderID(providerID)
if err != nil {
return false, err
}

_, err = az.InstanceID(name)
if err != nil {
if err == cloudprovider.InstanceNotFound {
return false, nil
}
return false, err
}

return true, nil
}

func (az *Cloud) isCurrentInstance(name types.NodeName) (bool, error) {
Expand Down
16 changes: 14 additions & 2 deletions pkg/cloudprovider/providers/gce/gce_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package gce

import (
"errors"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -157,7 +156,20 @@ func (gce *GCECloud) ExternalID(nodeName types.NodeName) (string, error) {
// InstanceExistsByProviderID returns true if the instance with the given provider id still exists and is running.
// If false is returned with no error, the instance will be immediately deleted by the cloud controller manager.
func (gce *GCECloud) InstanceExistsByProviderID(providerID string) (bool, error) {
return false, errors.New("unimplemented")
project, zone, name, err := splitProviderID(providerID)
if err != nil {
return false, err
}

_, err = gce.getInstanceFromProjectInZoneByName(project, zone, name)
if err != nil {
if err == cloudprovider.InstanceNotFound {
return false, nil
}
return false, err
}

return true, nil
}

// InstanceID returns the cloud provider ID of the node with the specified NodeName.
Expand Down
15 changes: 14 additions & 1 deletion pkg/cloudprovider/providers/openstack/openstack_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,20 @@ func (i *Instances) ExternalID(name types.NodeName) (string, error) {
// InstanceExistsByProviderID returns true if the instance with the given provider id still exists and is running.
// If false is returned with no error, the instance will be immediately deleted by the cloud controller manager.
func (i *Instances) InstanceExistsByProviderID(providerID string) (bool, error) {
return false, errors.New("unimplemented")
instanceID, err := instanceIDFromProviderID(providerID)
if err != nil {
return false, err
}

_, err = servers.Get(i.compute, instanceID).Extract()
if err != nil {
if isNotFound(err) {
return false, nil
}
return false, err
}

return true, nil
}

// InstanceID returns the kubelet's cloud provider ID.
Expand Down
13 changes: 12 additions & 1 deletion pkg/cloudprovider/providers/rackspace/rackspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,18 @@ func (i *Instances) ExternalID(nodeName types.NodeName) (string, error) {
// InstanceExistsByProviderID returns true if the instance with the given provider id still exists and is running.
// If false is returned with no error, the instance will be immediately deleted by the cloud controller manager.
func (i *Instances) InstanceExistsByProviderID(providerID string) (bool, error) {
return false, errors.New("unimplemented")
instanceID, err := instanceIDFromProviderID(providerID)
if err != nil {
return false, err
}

_, err = servers.Get(i.compute, instanceID).Extract()
if err != nil {
// TODO: check whether instance is not found
return false, err
}

return true, nil
}

// InstanceID returns the cloud provider ID of the kubelet's instance.
Expand Down
11 changes: 10 additions & 1 deletion pkg/cloudprovider/providers/vsphere/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,16 @@ func (vs *VSphere) ExternalID(nodeName k8stypes.NodeName) (string, error) {
// InstanceExistsByProviderID returns true if the instance with the given provider id still exists and is running.
// If false is returned with no error, the instance will be immediately deleted by the cloud controller manager.
func (vs *VSphere) InstanceExistsByProviderID(providerID string) (bool, error) {
return false, errors.New("unimplemented")
vmName := path.Base(providerID)
_, err := vs.InstanceID(vmNameToNodeName(vmName))
if err != nil {
if err == cloudprovider.InstanceNotFound {
return false, nil
}
return false, err
}

return true, nil
}

// InstanceID returns the cloud provider ID of the node with the specified Name.
Expand Down

0 comments on commit 2858447

Please sign in to comment.