Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
separate node class from droplet name/tag
Browse files Browse the repository at this point in the history
Signed-off-by: Johan Siebens <[email protected]>
  • Loading branch information
jsiebens committed May 22, 2021
1 parent 1d43494 commit 3e2a213
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
12 changes: 6 additions & 6 deletions plugin/digitalocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ type dropletTemplate struct {
size string
vpc string
snapshotID int
nodeClass string
name string
sshKeys []string
tags []string
userData string
}

func (t *TargetPlugin) scaleOut(ctx context.Context, desired, diff int64, template *dropletTemplate, config map[string]string) error {
log := t.logger.With("action", "scale_out", "tag", template.nodeClass, "count", diff)
log := t.logger.With("action", "scale_out", "tag", template.name, "count", diff)

log.Debug("creating DigitalOcean droplets")

for i := int64(0); i < diff; i++ {
createRequest := &godo.DropletCreateRequest{
Name: template.nodeClass + "-" + randstr.String(6),
Name: template.name + "-" + randstr.String(6),
Region: template.region,
Size: template.size,
VPCUUID: template.vpc,
Expand Down Expand Up @@ -91,11 +91,11 @@ func (t *TargetPlugin) scaleIn(ctx context.Context, desired, diff int64, templat

// Create a logger for this action to pre-populate useful information we
// would like on all log lines.
log := t.logger.With("action", "scale_in", "tag", template.nodeClass, "instances", ids)
log := t.logger.With("action", "scale_in", "tag", template.name, "instances", ids)

log.Debug("deleting DigitalOcean droplets")

if err := t.deleteDroplets(ctx, template.nodeClass, instanceIDs); err != nil {
if err := t.deleteDroplets(ctx, template.name, instanceIDs); err != nil {
return fmt.Errorf("failed to delete instances: %v", err)
}

Expand Down Expand Up @@ -176,7 +176,7 @@ func (t *TargetPlugin) countDroplets(ctx context.Context, template *dropletTempl

opt := &godo.ListOptions{}
for {
droplets, resp, err := t.client.Droplets.ListByTag(ctx, template.nodeClass, opt)
droplets, resp, err := t.client.Droplets.ListByTag(ctx, template.name, opt)
if err != nil {
return 0, 0, err
}
Expand Down
23 changes: 20 additions & 3 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
configKeySnapshotID = "snapshot_id"
configKeySshKeys = "ssh_keys"
configKeyUserData = "user_data"
configKeyName = "name"
configKeyTags = "tags"
)

Expand Down Expand Up @@ -131,7 +132,7 @@ func (t *TargetPlugin) Scale(action sdk.ScalingAction, config map[string]string)
case "out":
err = t.scaleOut(ctx, action.Count, diff, template, config)
default:
t.logger.Info("scaling not required", "tag", template.nodeClass,
t.logger.Info("scaling not required", "tag", template.name,
"current_count", total, "strategy_count", action.Count)
return nil
}
Expand Down Expand Up @@ -218,7 +219,9 @@ func (t *TargetPlugin) createDropletTemplate(config map[string]string) (*droplet
return nil, fmt.Errorf("required config param %s not found", sdk.TargetConfigKeyClass)
}

var tags = []string{nodeClass}
name := t.getValueWithFallback(config, configKeyName, nodeClass)

var tags = []string{name}
if len(tagsAsString) != 0 {
tags = append(tags, strings.Split(tagsAsString, ",")...)
}
Expand All @@ -233,7 +236,7 @@ func (t *TargetPlugin) createDropletTemplate(config map[string]string) (*droplet
size: size,
vpc: vpc,
snapshotID: int(snapshotID),
nodeClass: nodeClass,
name: name,
sshKeys: sshKeyFingerprints,
userData: userData,
tags: tags,
Expand Down Expand Up @@ -264,6 +267,20 @@ func (t *TargetPlugin) getValue(config map[string]string, name string) (string,
return "", false
}

func (t *TargetPlugin) getValueWithFallback(config map[string]string, name string, fallback string) string {
v, ok := config[name]
if ok {
return v
}

v, ok = t.config[name]
if ok {
return v
}

return fallback
}

func pathOrContents(poc string) (string, error) {
if len(poc) == 0 {
return poc, nil
Expand Down
44 changes: 32 additions & 12 deletions plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,54 @@ func TestTargetPlugin_calculateDirection(t *testing.T) {

func TestTargetPlugin_createDropletTemplate(t *testing.T) {
input := map[string]string{
"region" : "ny1",
"size" : "s-1vcpu-1gb",
"vpc_uuid" : "b6ac51f4-dc83-11e8-a3da-3cfdfea9f0d8",
"snapshot_id" : "123",
"node_class" : "hashistack",
"region": "ny1",
"size": "s-1vcpu-1gb",
"vpc_uuid": "b6ac51f4-dc83-11e8-a3da-3cfdfea9f0d8",
"snapshot_id": "123",
"node_class": "hashistack",
}

plugin := TargetPlugin{}
dropletTemplate, err := plugin.createDropletTemplate(input)

assert.Nil(t, err)
assert.Equal(t, []string{}, dropletTemplate.sshKeys)
assert.Equal(t, "hashistack", dropletTemplate.name)
assert.Equal(t, []string{"hashistack"}, dropletTemplate.tags)
}

func TestTargetPlugin_createDropletTemplateWithCustomName(t *testing.T) {
input := map[string]string{
"region": "ny1",
"size": "s-1vcpu-1gb",
"vpc_uuid": "b6ac51f4-dc83-11e8-a3da-3cfdfea9f0d8",
"snapshot_id": "123",
"node_class": "batch",
"name": "hashi-batch",
}

plugin := TargetPlugin{}
dropletTemplate, err := plugin.createDropletTemplate(input)

assert.Nil(t, err)
assert.Equal(t, []string{}, dropletTemplate.sshKeys)
assert.Equal(t, "hashi-batch", dropletTemplate.name)
assert.Equal(t, []string{"hashi-batch"}, dropletTemplate.tags)
}

func TestTargetPlugin_createDropletTemplateWithMultipleTags(t *testing.T) {
input := map[string]string{
"region" : "ny1",
"size" : "s-1vcpu-1gb",
"vpc_uuid" : "b6ac51f4-dc83-11e8-a3da-3cfdfea9f0d8",
"snapshot_id" : "123",
"tags" : "tag1,tag2",
"node_class" : "hashistack",
"region": "ny1",
"size": "s-1vcpu-1gb",
"vpc_uuid": "b6ac51f4-dc83-11e8-a3da-3cfdfea9f0d8",
"snapshot_id": "123",
"tags": "tag1,tag2",
"node_class": "hashistack",
}

plugin := TargetPlugin{}
dropletTemplate, err := plugin.createDropletTemplate(input)

assert.Nil(t, err)
assert.Equal(t, []string{"hashistack", "tag1", "tag2"}, dropletTemplate.tags)
}
}

0 comments on commit 3e2a213

Please sign in to comment.