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

[Digital Ocean] Implement Delete Instance logic for rolling update #10000

Merged
merged 4 commits into from
Oct 13, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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