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

Add basic support for SPOT instances in GCP #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .drone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ go build \
-ldflags "-extldflags \"-static\" $COMMIT $VERSION" \
-o release/linux/amd64/drone-autoscaler \
github.com/drone/autoscaler/cmd/drone-autoscaler

1 change: 1 addition & 0 deletions cmd/drone-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func setupProvider(c config.Config) (autoscaler.Provider, error) {
google.WithZones(c.Google.Zone...),
google.WithUserDataKey(c.Google.UserDataKey),
google.WithRateLimit(c.Google.RateLimit),
google.WithProvisioningModel(c.Google.ProvisioningModel),
)
case c.DigitalOcean.Token != "":
return digitalocean.New(
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ type (
Zone []string `envconfig:"DRONE_GOOGLE_ZONE"`
UserDataKey string `envconfig:"DRONE_GOOGLE_USERDATA_KEY" default:"user-data"`
RateLimit int `envconfig:"DRONE_GOOGLE_READ_RATELIMIT" default:"25"`
ProvisioningModel string `envconfig:"DRONE_GOOGLE_PROVISIONING_MODEL" default:"STANDARD"`
}

HetznerCloud struct {
Expand Down
2 changes: 2 additions & 0 deletions config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestLoad(t *testing.T) {
"DRONE_GOOGLE_USERDATA": "#cloud-init",
"DRONE_GOOGLE_USERDATA_FILE": "/path/to/cloud/init.yml",
"DRONE_GOOGLE_READ_RATELIMIT": "20",
"DRONE_GOOGLE_PROVISIONING_MODEL": "STANDARD",
"DRONE_AMAZON_IMAGE": "ami-07f84a50d2dec2fa4",
"DRONE_AMAZON_INSTANCE": "t3.medium",
"DRONE_AMAZON_PRIVATE_IP": "true",
Expand Down Expand Up @@ -283,6 +284,7 @@ var jsonConfig = []byte(`{
"Network": "default",
"Subnetwork": "",
"Preemptible": true,
"ProvisioningModel": "STANDARD",
"Scopes": [
"devstorage.read_only",
"pubsub"
Expand Down
12 changes: 10 additions & 2 deletions drivers/google/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func (p *provider) Create(ctx context.Context, opts autoscaler.InstanceCreateOpt
}
}

auto_restart := true
on_host_maintenance := "MIGRATE"
if p.provisioningModel == "SPOT" {
auto_restart = false
on_host_maintenance = "TERMINATE"
}

in := &compute.Instance{
Name: name,
Zone: fmt.Sprintf("projects/%s/zones/%s", p.project, zone),
Expand Down Expand Up @@ -94,8 +101,9 @@ func (p *provider) Create(ctx context.Context, opts autoscaler.InstanceCreateOpt
Labels: p.labels,
Scheduling: &compute.Scheduling{
Preemptible: false,
OnHostMaintenance: "MIGRATE",
AutomaticRestart: googleapi.Bool(true),
ProvisioningModel: p.provisioningModel,
OnHostMaintenance: on_host_maintenance,
AutomaticRestart: googleapi.Bool(auto_restart),
},
DeletionProtection: false,
ServiceAccounts: []*compute.ServiceAccount{
Expand Down
2 changes: 2 additions & 0 deletions drivers/google/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ var insertInstanceMock = &compute.Instance{
Labels: map[string]string{},
Scheduling: &compute.Scheduling{
Preemptible: false,
ProvisioningModel: "STANDARD",
OnHostMaintenance: "MIGRATE",
AutomaticRestart: googleapi.Bool(true),
},
Expand Down Expand Up @@ -228,6 +229,7 @@ var insertInstanceMockB = &compute.Instance{
Labels: map[string]string{},
Scheduling: &compute.Scheduling{
Preemptible: false,
ProvisioningModel: "STANDARD",
OnHostMaintenance: "MIGRATE",
AutomaticRestart: googleapi.Bool(true),
},
Expand Down
7 changes: 7 additions & 0 deletions drivers/google/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,10 @@ func WithRateLimit(limitAmount int) Option {
p.rateLimiter = rate.NewLimiter(limit, 1)
}
}

// WithProvisioningModel returns an option to set the provisioningmodel ("STANDARD", "SPOT").
func WithProvisioningModel(provisioningmodel string) Option {
return func(p *provider) {
p.provisioningModel = provisioningmodel
}
}
5 changes: 5 additions & 0 deletions drivers/google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type provider struct {
zones []string
userdata *template.Template
userdataKey string
provisioningModel string

rateLimiter *rate.Limiter

Expand Down Expand Up @@ -98,6 +99,10 @@ func New(opts ...Option) (autoscaler.Provider, error) {
if p.serviceAccountEmail == "" {
p.serviceAccountEmail = "default"
}
// Defaults to STANDARD provisioningModel
if p.provisioningModel == "" {
p.provisioningModel = "STANDARD"
}

if p.rateLimiter == nil {
// If unspecified, set to the max read rate limit for the API 25/s
Expand Down
20 changes: 11 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4
github.com/go-chi/chi v3.3.2+incompatible
github.com/go-sql-driver/mysql v1.3.0
github.com/golang/mock v1.4.4
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.8
github.com/gophercloud/gophercloud v0.0.0-20181014043407-c8947f7d1c51
github.com/h2non/gock v1.0.7
Expand All @@ -35,14 +35,14 @@ require (
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.3
github.com/sirupsen/logrus v1.6.0
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b
golang.org/x/oauth2 v0.0.0-20220622183110-fd043fe589d2
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65
google.golang.org/api v0.30.0
google.golang.org/api v0.93.0
)

require (
cloud.google.com/go v0.65.0 // indirect
cloud.google.com/go/compute v1.7.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.7 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand All @@ -54,7 +54,9 @@ require (
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.1.0 // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/gorilla/mux v1.7.4 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
Expand All @@ -69,13 +71,13 @@ require (
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/tent/http-link-go v0.0.0-20130702225549-ac974c61c2f9 // indirect
go.opencensus.io v0.22.4 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect
google.golang.org/grpc v1.31.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220624142145-8cd45d7dbd1f // indirect
google.golang.org/grpc v1.47.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gotest.tools v2.2.0+incompatible // indirect
Expand Down
Loading