Skip to content

Commit

Permalink
[Digital Ocean] Implement Delete Instance logic for rolling update (#…
Browse files Browse the repository at this point in the history
…10000)

* Add delete Instance implementation for DO

* Add warning for DeleteInstance usage

* Use reconcile option for rolling update

* Update pkg/instancegroups/instancegroups.go

Co-authored-by: Ciprian Hacman <ciprianhacman@gmail.com>

Co-authored-by: Ciprian Hacman <ciprianhacman@gmail.com>
  • Loading branch information
srikiz and hakman committed Oct 13, 2020
1 parent 6ef2de3 commit 4d251fe
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/instancegroups/instancegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ func (c *RollingUpdateCluster) drainTerminateAndWait(u *cloudinstances.CloudInst
}

func (c *RollingUpdateCluster) reconcileInstanceGroup() error {
if api.CloudProviderID(c.Cluster.Spec.CloudProvider) != api.CloudProviderOpenstack {
if api.CloudProviderID(c.Cluster.Spec.CloudProvider) != api.CloudProviderOpenstack &&
api.CloudProviderID(c.Cluster.Spec.CloudProvider) != api.CloudProviderDO {
return nil
}
rto := fi.RunTasksOptions{}
Expand Down
47 changes: 43 additions & 4 deletions pkg/resources/digitalocean/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"strconv"
"strings"
"time"

"github.com/digitalocean/godo"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -95,7 +96,6 @@ func NewCloud(region string) (*Cloud, error) {
}, nil
}

// GetCloudGroups is not implemented yet, that needs to return the instances and groups that back a kops cluster.
func (c *Cloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodes []v1.Node) (map[string]*cloudinstances.CloudInstanceGroup, error) {
return getCloudGroups(c, cluster, instancegroups, warnUnmatched, nodes)
}
Expand All @@ -106,10 +106,45 @@ func (c *Cloud) DeleteGroup(g *cloudinstances.CloudInstanceGroup) error {
return fmt.Errorf("digital ocean cloud provider does not support deleting cloud groups at this time")
}

// DeleteInstance is not implemented yet, is func needs to delete a DO instance.
func (c *Cloud) DeleteInstance(i *cloudinstances.CloudInstance) error {
klog.V(8).Info("digitalocean cloud provider DeleteInstance not implemented yet")
return fmt.Errorf("digital ocean cloud provider does not support deleting cloud instances at this time")
dropletID, err := strconv.Atoi(i.ID)
if err != nil {
return fmt.Errorf("failed to convert droplet ID to int: %s", err)
}

_, _, err = c.Client.DropletActions.Shutdown(context.TODO(), dropletID)
if err != nil {
return fmt.Errorf("error stopping instance %q: %v", dropletID, err)
}

// Wait for 5 min to stop the instance
for i := 0; i < 5; i++ {
droplet, _, err := c.Client.Droplets.Get(context.TODO(), dropletID)
if err != nil {
return fmt.Errorf("error describing instance %q: %v", dropletID, err)
}

klog.V(8).Infof("stopping DO instance %q, current Status: %q", droplet, droplet.Status)

if droplet.Status == "off" {
break
}

if i == 5 {
return fmt.Errorf("fail to stop DO instance %v in 5 mins", dropletID)
}

time.Sleep(time.Minute * 1)
}

_, err = c.Client.Droplets.Delete(context.TODO(), dropletID)
if err != nil {
return fmt.Errorf("error stopping instance %q: %v", dropletID, err)
}

klog.V(8).Infof("deleted droplet instance %q", dropletID)

return nil
}

// DetachInstance is not implemented yet. It needs to cause a cloud instance to no longer be counted against the group's size limits.
Expand Down Expand Up @@ -147,6 +182,10 @@ func (c *Cloud) Droplets() godo.DropletsService {
return c.Client.Droplets
}

func (c *Cloud) DropletActions() godo.DropletActionsService {
return c.Client.DropletActions
}

func (c *Cloud) LoadBalancers() godo.LoadBalancersService {
return c.Client.LoadBalancers
}
Expand Down

0 comments on commit 4d251fe

Please sign in to comment.