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

Configure Service Account name for GCP instances #68

Merged
merged 4 commits into from
Jul 14, 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
1 change: 1 addition & 0 deletions cmd/drone-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func setupProvider(c config.Config) (autoscaler.Provider, error) {
google.WithNetwork(c.Google.Network),
google.WithSubnetwork(c.Google.Subnetwork),
google.WithPrivateIP(c.Google.PrivateIP),
google.WithServiceAccountEmail(c.Google.ServiceAccountEmail),
google.WithProject(c.Google.Project),
google.WithTags(c.Google.Tags...),
google.WithUserData(c.Google.UserData),
Expand Down
29 changes: 15 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,21 @@ type (
}

Google struct {
MachineType string `envconfig:"DRONE_GOOGLE_MACHINE_TYPE"`
MachineImage string `envconfig:"DRONE_GOOGLE_MACHINE_IMAGE"`
Network string `envconfig:"DRONE_GOOGLE_NETWORK"`
Subnetwork string `envconfig:"DRONE_GOOGLE_SUBNETWORK"`
Labels map[string]string `envconfig:"DRONE_GOOGLE_LABELS"`
Scopes []string `envconfig:"DRONE_GOOGLE_SCOPES"`
DiskSize int64 `envconfig:"DRONE_GOOGLE_DISK_SIZE"`
DiskType string `envconfig:"DRONE_GOOGLE_DISK_TYPE"`
Project string `envconfig:"DRONE_GOOGLE_PROJECT"`
PrivateIP bool `split_words:"true"`
Tags []string `envconfig:"DRONE_GOOGLE_TAGS"`
UserData string `envconfig:"DRONE_GOOGLE_USERDATA"`
UserDataFile string `envconfig:"DRONE_GOOGLE_USERDATA_FILE"`
Zone string `envconfig:"DRONE_GOOGLE_ZONE"`
MachineType string `envconfig:"DRONE_GOOGLE_MACHINE_TYPE"`
MachineImage string `envconfig:"DRONE_GOOGLE_MACHINE_IMAGE"`
Network string `envconfig:"DRONE_GOOGLE_NETWORK"`
Subnetwork string `envconfig:"DRONE_GOOGLE_SUBNETWORK"`
Labels map[string]string `envconfig:"DRONE_GOOGLE_LABELS"`
Scopes []string `envconfig:"DRONE_GOOGLE_SCOPES"`
ServiceAccountEmail string `envconfig:"DRONE_GOOGLE_SERVICE_ACCOUNT_EMAIL"`
DiskSize int64 `envconfig:"DRONE_GOOGLE_DISK_SIZE"`
DiskType string `envconfig:"DRONE_GOOGLE_DISK_TYPE"`
Project string `envconfig:"DRONE_GOOGLE_PROJECT"`
PrivateIP bool `split_words:"true"`
Tags []string `envconfig:"DRONE_GOOGLE_TAGS"`
UserData string `envconfig:"DRONE_GOOGLE_USERDATA"`
UserDataFile string `envconfig:"DRONE_GOOGLE_USERDATA_FILE"`
Zone string `envconfig:"DRONE_GOOGLE_ZONE"`
}

HetznerCloud struct {
Expand Down
17 changes: 9 additions & 8 deletions drivers/google/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (p *provider) Create(ctx context.Context, opts autoscaler.InstanceCreateOpt
ServiceAccounts: []*compute.ServiceAccount{
{
Scopes: p.scopes,
Email: "default",
Email: p.serviceAccountEmail,
},
},
}
Expand Down Expand Up @@ -128,13 +128,14 @@ func (p *provider) Create(ctx context.Context, opts autoscaler.InstanceCreateOpt
}

instance := &autoscaler.Instance{
Provider: autoscaler.ProviderGoogle,
ID: name,
Name: opts.Name,
Image: p.image,
Region: p.zone,
Size: p.size,
Address: resp.NetworkInterfaces[0].AccessConfigs[0].NatIP,
Provider: autoscaler.ProviderGoogle,
ID: name,
Name: opts.Name,
Image: p.image,
Region: p.zone,
Size: p.size,
Address: resp.NetworkInterfaces[0].AccessConfigs[0].NatIP,
ServiceAccountEmail: p.serviceAccountEmail,
ademariag marked this conversation as resolved.
Show resolved Hide resolved
}

logger.
Expand Down
3 changes: 3 additions & 0 deletions drivers/google/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func TestCreate(t *testing.T) {
if want, got := instance.Size, "n1-standard-1"; got != want {
t.Errorf("Want instance Size %q, got %q", want, got)
}
if want, got := instance.ServiceAccountEmail, "default"; got != want {
t.Errorf("Want service account email %q, got %q", want, got)
}
}

var insertInstanceMock = &compute.Instance{
Expand Down
7 changes: 7 additions & 0 deletions drivers/google/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,10 @@ func WithScopes(scopes ...string) Option {
p.scopes = scopes
}
}

// WithServiceAccountEmail returns an option to set the ServiceAccountEmail.
func WithServiceAccountEmail(email string) Option {
return func(p *provider) {
p.serviceAccountEmail = email
}
}
4 changes: 4 additions & 0 deletions drivers/google/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestOptions(t *testing.T) {
WithMachineType("c3.large"),
WithNetwork("global/defaults/foo"),
WithPrivateIP(false),
WithServiceAccountEmail("default"),
WithProject("my-project"),
WithTags("drone", "agent"),
WithZone("us-central1-f"),
Expand Down Expand Up @@ -59,4 +60,7 @@ func TestOptions(t *testing.T) {
if got, want := len(p.scopes), 2; got != want {
t.Errorf("Want %d scopes, got %d", want, got)
}
if got, want := p.serviceAccountEmail, "default"; got != want {
t.Errorf("Want service account name %q, got %q", want, got)
}
}
30 changes: 17 additions & 13 deletions drivers/google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,20 @@ var (
type provider struct {
init sync.Once

diskSize int64
diskType string
image string
labels map[string]string
network string
subnetwork string
privateIP bool
project string
scopes []string
size string
tags []string
zone string
userdata *template.Template
diskSize int64
diskType string
image string
labels map[string]string
network string
subnetwork string
project string
privateIP bool
scopes []string
serviceAccountEmail string
size string
tags []string
zone string
userdata *template.Template

service *compute.Service
}
Expand Down Expand Up @@ -87,6 +88,9 @@ func New(opts ...Option) (autoscaler.Provider, error) {
if len(p.scopes) == 0 {
p.scopes = defaultScopes
}
if p.serviceAccountEmail == "" {
p.serviceAccountEmail = "default"
}
if p.service == nil {
client, err := google.DefaultClient(oauth2.NoContext, compute.ComputeScope)
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ type Provider interface {
// (e.g Digital Ocean Droplet).
type Instance struct {
Provider ProviderType
ID string
Name string
Address string
Region string
Image string
Size string
ID string
Name string
Address string
Region string
Image string
Size string
ServiceAccountEmail string
}

// InstanceCreateOpts define soptional instructions for
Expand Down