Skip to content

Commit

Permalink
Merge pull request #9 from ChorusOne/support-service-account
Browse files Browse the repository at this point in the history
Add support for specifying service accounts
  • Loading branch information
pascalbreuninger authored Jan 15, 2024
2 parents a94e4eb + ce4731d commit 71d4bf6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ This provides has the seguent options
| NETWORK | false | The network id to use. | |
| SUBNETWORK | false | The subnetwork id to use. | |
| TAG | false | A tag to attach to the instance. | devpod |
| SERVICE_ACCOUNT| false | A service account to attach to instance

Options can either be set in `env` or using for example:

Expand Down
26 changes: 20 additions & 6 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package cmd

import (
"cloud.google.com/go/compute/apiv1/computepb"
"context"
"encoding/base64"
"fmt"
"regexp"
"strconv"
"strings"

"cloud.google.com/go/compute/apiv1/computepb"
"github.com/loft-sh/devpod-provider-gcloud/pkg/gcloud"
"github.com/loft-sh/devpod-provider-gcloud/pkg/options"
"github.com/loft-sh/devpod-provider-gcloud/pkg/ptr"
"github.com/loft-sh/devpod/pkg/log"
"github.com/loft-sh/devpod/pkg/ssh"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"regexp"
"strconv"
"strings"
)

// CreateCmd holds the cmd flags
Expand Down Expand Up @@ -71,6 +72,18 @@ func buildInstance(options *options.Options) (*computepb.Instance, error) {
if err != nil {
return nil, err
}
serviceAccounts := []*computepb.ServiceAccount{}
if options.ServiceAccount != "" {

serviceAccounts = []*computepb.ServiceAccount{
{
Email: &options.ServiceAccount,
Scopes: []string{
"https://www.googleapis.com/auth/cloud-platform",
},
},
}
}

// generate instance object
instance := &computepb.Instance{
Expand Down Expand Up @@ -108,8 +121,9 @@ func buildInstance(options *options.Options) (*computepb.Instance, error) {
},
},
},
Zone: ptr.Ptr(fmt.Sprintf("projects/%s/zones/%s", options.Project, options.Zone)),
Name: ptr.Ptr(options.MachineID),
Zone: ptr.Ptr(fmt.Sprintf("projects/%s/zones/%s", options.Project, options.Zone)),
Name: ptr.Ptr(options.MachineID),
ServiceAccounts: serviceAccounts,
}

return instance, nil
Expand Down
3 changes: 3 additions & 0 deletions hack/provider/provider.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ options:
DISK_IMAGE:
description: The disk image to use.
default: projects/cos-cloud/global/images/cos-101-17162-127-5
SERVICE_ACCOUNT:
description: A service account to attach
default: ""
MACHINE_TYPE:
description: The machine type to use.
default: c2-standard-4
Expand Down
18 changes: 10 additions & 8 deletions pkg/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ type Options struct {
MachineID string
MachineFolder string

Project string
Zone string
Network string
Subnetwork string
Tag string
DiskSize string
DiskImage string
MachineType string
Project string
Zone string
Network string
Subnetwork string
Tag string
DiskSize string
DiskImage string
MachineType string
ServiceAccount string
}

func FromEnv(withMachine bool) (*Options, error) {
Expand Down Expand Up @@ -58,6 +59,7 @@ func FromEnv(withMachine bool) (*Options, error) {
return nil, err
}

retOptions.ServiceAccount = os.Getenv("SERVICE_ACCOUNT")
retOptions.Network = os.Getenv("NETWORK")
retOptions.Subnetwork = os.Getenv("SUBNETWORK")
retOptions.Tag = os.Getenv("TAG")
Expand Down

0 comments on commit 71d4bf6

Please sign in to comment.