44 "context"
55 "fmt"
66 "io/ioutil"
7+ "strconv"
78 "time"
89
910 "github.com/digitalocean/godo"
@@ -27,12 +28,12 @@ type dropletTemplate struct {
2728 userData string
2829}
2930
30- func (t * TargetPlugin ) scaleOut (ctx context.Context , num int64 , template * dropletTemplate , config map [string ]string ) error {
31- log := t .logger .With ("action" , "scale_out" , "tag" , template .nodeClass , "count" , num )
31+ func (t * TargetPlugin ) scaleOut (ctx context.Context , desired , diff int64 , template * dropletTemplate , config map [string ]string ) error {
32+ log := t .logger .With ("action" , "scale_out" , "tag" , template .nodeClass , "count" , diff )
3233
3334 log .Debug ("creating DigitalOcean droplets" )
3435
35- for i := int64 (0 ); i < num ; i ++ {
36+ for i := int64 (0 ); i < diff ; i ++ {
3637 createRequest := & godo.DropletCreateRequest {
3738 Name : template .nodeClass + "-" + randstr .String (6 ),
3839 Region : template .region ,
@@ -65,7 +66,7 @@ func (t *TargetPlugin) scaleOut(ctx context.Context, num int64, template *drople
6566
6667 log .Debug ("successfully created DigitalOcean droplets" )
6768
68- if err := t .ensureDropletsAreStable (ctx , template ); err != nil {
69+ if err := t .ensureDropletsAreStable (ctx , template , desired ); err != nil {
6970 return fmt .Errorf ("failed to confirm scale out DigitalOcean droplets: %v" , err )
7071 }
7172
@@ -74,9 +75,9 @@ func (t *TargetPlugin) scaleOut(ctx context.Context, num int64, template *drople
7475 return nil
7576}
7677
77- func (t * TargetPlugin ) scaleIn (ctx context.Context , num int64 , template * dropletTemplate , config map [string ]string ) error {
78+ func (t * TargetPlugin ) scaleIn (ctx context.Context , desired , diff int64 , template * dropletTemplate , config map [string ]string ) error {
7879
79- ids , err := t .clusterUtils .RunPreScaleInTasks (ctx , config , int (num ))
80+ ids , err := t .clusterUtils .RunPreScaleInTasks (ctx , config , int (diff ))
8081 if err != nil {
8182 return fmt .Errorf ("failed to perform pre-scale Nomad scale in tasks: %v" , err )
8283 }
@@ -98,9 +99,9 @@ func (t *TargetPlugin) scaleIn(ctx context.Context, num int64, template *droplet
9899 return fmt .Errorf ("failed to delete instances: %v" , err )
99100 }
100101
101- log .Debug ("successfully deleted DigitalOcean droplets " )
102+ log .Debug ("successfully started deletion process " )
102103
103- if err := t .ensureDropletsAreStable (ctx , template ); err != nil {
104+ if err := t .ensureDropletsAreStable (ctx , template , desired ); err != nil {
104105 return fmt .Errorf ("failed to confirm scale in DigitalOcean droplets: %v" , err )
105106 }
106107
@@ -114,11 +115,11 @@ func (t *TargetPlugin) scaleIn(ctx context.Context, num int64, template *droplet
114115 return nil
115116}
116117
117- func (t * TargetPlugin ) ensureDropletsAreStable (ctx context.Context , template * dropletTemplate ) error {
118+ func (t * TargetPlugin ) ensureDropletsAreStable (ctx context.Context , template * dropletTemplate , desired int64 ) error {
118119
119120 f := func (ctx context.Context ) (bool , error ) {
120- total , active , err := t .countDroplets (ctx , template )
121- if total == active || err != nil {
121+ _ , active , err := t .countDroplets (ctx , template )
122+ if desired == active || err != nil {
122123 return true , err
123124 } else {
124125 return false , fmt .Errorf ("waiting for droplets to become stable" )
@@ -130,6 +131,7 @@ func (t *TargetPlugin) ensureDropletsAreStable(ctx context.Context, template *dr
130131
131132func (t * TargetPlugin ) deleteDroplets (ctx context.Context , tag string , instanceIDs map [string ]bool ) error {
132133 // create options. initially, these will be blank
134+ var dropletsToDelete []int
133135 opt := & godo.ListOptions {}
134136 for {
135137 droplets , resp , err := t .client .Droplets .ListByTag (ctx , tag , opt )
@@ -140,15 +142,19 @@ func (t *TargetPlugin) deleteDroplets(ctx context.Context, tag string, instanceI
140142 for _ , d := range droplets {
141143 _ , ok := instanceIDs [d .Name ]
142144 if ok {
143- _ , err := t .client .Droplets .Delete (ctx , d .ID )
144- if err != nil {
145- return err
146- }
145+ go func (dropletId int ) {
146+ log := t .logger .With ("action" , "delete" , "droplet_id" , strconv .Itoa (dropletId ))
147+ err := shutdownDroplet (dropletId , t .client , log )
148+ if err != nil {
149+ log .Error ("error deleting droplet" , err )
150+ }
151+ }(d .ID )
152+ dropletsToDelete = append (dropletsToDelete , d .ID )
147153 }
148154 }
149155
150- // if we are at the last page, break out the for loop
151- if resp .Links == nil || resp .Links .IsLastPage () {
156+ // if we deleted all droplets or if we are at the last page, break out the for loop
157+ if len ( dropletsToDelete ) == len ( instanceIDs ) || resp .Links == nil || resp .Links .IsLastPage () {
152158 break
153159 }
154160
0 commit comments