Skip to content

Commit

Permalink
Add IPv6 support for Google Cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
booya committed Jan 13, 2023
1 parent 3cbdcd0 commit 78e53fb
Show file tree
Hide file tree
Showing 10 changed files with 1,070 additions and 34 deletions.
1 change: 1 addition & 0 deletions cmd/drone-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ func setupProvider(c config.Config) (autoscaler.Provider, error) {
google.WithLabels(c.Google.Labels),
google.WithNetwork(c.Google.Network),
google.WithSubnetwork(c.Google.Subnetwork),
google.WithStackType(c.Google.StackType),
google.WithPrivateIP(c.Google.PrivateIP),
google.WithServiceAccountEmail(c.Google.ServiceAccountEmail),
google.WithProject(c.Google.Project),
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ type (
MachineImage string `envconfig:"DRONE_GOOGLE_MACHINE_IMAGE"`
Network string `envconfig:"DRONE_GOOGLE_NETWORK"`
Subnetwork string `envconfig:"DRONE_GOOGLE_SUBNETWORK"`
StackType string `envconfig:"DRONE_GOOGLE_STACK_TYPE"`
Labels map[string]string `envconfig:"DRONE_GOOGLE_LABELS"`
Scopes []string `envconfig:"DRONE_GOOGLE_SCOPES"`
ServiceAccountEmail string `envconfig:"DRONE_GOOGLE_SERVICE_ACCOUNT_EMAIL"`
Expand Down
14 changes: 13 additions & 1 deletion drivers/google/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (p *provider) Create(ctx context.Context, opts autoscaler.InstanceCreateOpt
logger.Debugln("instance insert")

networkConfig := []*compute.AccessConfig{}

if !p.privateIP {
networkConfig = []*compute.AccessConfig{
{
Expand Down Expand Up @@ -88,6 +87,7 @@ func (p *provider) Create(ctx context.Context, opts autoscaler.InstanceCreateOpt
{
Network: p.network,
Subnetwork: p.subnetwork,
StackType: p.stackType,
AccessConfigs: networkConfig,
},
},
Expand All @@ -106,6 +106,18 @@ func (p *provider) Create(ctx context.Context, opts autoscaler.InstanceCreateOpt
},
}

// Cannot add this in the same way as v4 access configs since the instance creation
// fails if any v6 access configs are specified for an instance with IPV4_ONLY stack type
if p.stackType == "IPV4_IPV6" {
in.NetworkInterfaces[0].Ipv6AccessConfigs = []*compute.AccessConfig{
{
Name: "external-ipv6",
Type: "DIRECT_IPV6",
NetworkTier: "PREMIUM",
},
}
}

op, err := p.service.Instances.Insert(p.project, zone, in).Do()
if err != nil {
logger.WithError(err).
Expand Down
18 changes: 10 additions & 8 deletions drivers/google/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import (
func TestCreate(t *testing.T) {
defer gock.Off()

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Post("/compute/v1/projects/my-project/zones/us-central1-a/instances").
JSON(insertInstanceMock).
Reply(200).
BodyString(`{ "name": "operation-name" }`)

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Get("/compute/v1/projects/my-project/zones/us-central1-a/instances/agent-807jvfwj").
Reply(200).
BodyString(`{ "networkInterfaces": [ { "accessConfigs": [ { "natIP": "1.2.3.4" } ] } ] }`)

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Get("/compute/v1/projects/my-project/zones/us-central1-a/operations/operation-name").
Reply(200).
BodyString(`{ "status": "DONE" }`)
Expand Down Expand Up @@ -82,18 +82,18 @@ func TestCreate(t *testing.T) {
func TestCreateWithMultiZones(t *testing.T) {
defer gock.Off()

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Post("/compute/v1/projects/my-project/zones/us-central1-b/instances").
JSON(insertInstanceMockB).
Reply(200).
BodyString(`{ "name": "operation-name" }`)

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Get("/compute/v1/projects/my-project/zones/us-central1-b/instances/agent-807jvfwj").
Reply(200).
BodyString(`{ "networkInterfaces": [ { "accessConfigs": [ { "natIP": "1.2.3.4" } ] } ] }`)

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Get("/compute/v1/projects/my-project/zones/us-central1-b/operations/operation-name").
Reply(200).
BodyString(`{ "status": "DONE" }`)
Expand Down Expand Up @@ -154,7 +154,8 @@ var insertInstanceMock = &compute.Instance{
CanIpForward: false,
NetworkInterfaces: []*compute.NetworkInterface{
{
Network: "global/networks/default",
Network: "global/networks/default",
StackType: "IPV4_ONLY",
AccessConfigs: []*compute.AccessConfig{
{
Name: "External NAT",
Expand Down Expand Up @@ -216,7 +217,8 @@ var insertInstanceMockB = &compute.Instance{
CanIpForward: false,
NetworkInterfaces: []*compute.NetworkInterface{
{
Network: "global/networks/default",
Network: "global/networks/default",
StackType: "IPV4_ONLY",
AccessConfigs: []*compute.AccessConfig{
{
Name: "External NAT",
Expand Down
6 changes: 3 additions & 3 deletions drivers/google/destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
func TestDestroy(t *testing.T) {
defer gock.Off()

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Delete("/compute/v1/projects/my-project/zones/us-central1-a/instances/my-instance").
Reply(200).
BodyString(`{ "name": "operation-name" }`)

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Get("/compute/v1/projects/my-project/zones/us-central1-a/operations/operation-name").
Reply(200).
BodyString(`{ "status": "DONE" }`)
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestDestroy(t *testing.T) {
func TestDestroy_Error(t *testing.T) {
defer gock.Off()

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Delete("/compute/v1/projects/my-project/zones/us-central1-a/instances/my-instance").
Reply(404)

Expand Down
7 changes: 7 additions & 0 deletions drivers/google/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ func WithSubnetwork(subnetwork string) Option {
}
}

// WithStackType returns an option to set the stack type for the instance.
func WithStackType(stackType string) Option {
return func(p *provider) {
p.stackType = stackType
}
}

// WithPrivateIP returns an option to set the private IP address.
func WithPrivateIP(private bool) Option {
return func(p *provider) {
Expand Down
4 changes: 4 additions & 0 deletions drivers/google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type provider struct {
labels map[string]string
network string
subnetwork string
stackType string
project string
privateIP bool
scopes []string
Expand Down Expand Up @@ -83,6 +84,9 @@ func New(opts ...Option) (autoscaler.Provider, error) {
if p.network == "" {
p.network = "global/networks/default"
}
if p.stackType == "" {
p.stackType = "IPV4_ONLY"
}
if p.userdata == nil {
p.userdata = userdata.T
}
Expand Down
8 changes: 4 additions & 4 deletions drivers/google/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import (
func TestSetupFirewall(t *testing.T) {
defer gock.Off()

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Get("/compute/v1/projects/my-project/global/firewalls/default-allow-docker").
Reply(404)

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Post("/compute/v1/projects/my-project/global/firewalls").
JSON(createFirewallMock).
Reply(200).
BodyString(`{ "name": "operation-name" }`)

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Get("/compute/v1/projects/my-project/global/operations/operation-name").
Reply(200).
BodyString(`{ "status": "DONE" }`)
Expand All @@ -50,7 +50,7 @@ func TestSetupFirewall(t *testing.T) {
func TestSetupFirewall_Exists(t *testing.T) {
defer gock.Off()

gock.New("https://www.googleapis.com").
gock.New("https://compute.googleapis.com").
Get("/compute/v1/projects/my-project/global/firewalls/default-allow-docker").
Reply(200).
BodyString(findFirewallRes)
Expand Down
29 changes: 11 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ go 1.12
replace github.com/docker/docker => github.com/docker/engine v17.12.0-ce-rc1.0.20200309214505-aa6a9891b09c+incompatible

require (
cloud.google.com/go v0.28.0 // indirect
github.com/99designs/basicauth-go v0.0.0-20160802081356-2a93ba0f464d
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/Microsoft/go-winio v0.4.7 // indirect
github.com/avast/retry-go v3.0.0+incompatible // indirect
github.com/avast/retry-go v3.0.0+incompatible
github.com/aws/aws-sdk-go v1.13.5
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a // indirect
github.com/bluele/slack v0.0.0-20171128075526-307046097ee9
Expand All @@ -21,7 +20,7 @@ require (
github.com/digitalocean/godo v1.1.1
github.com/docker/distribution v0.0.0-20170726174610-edc3ab29cdff // indirect
github.com/docker/docker v0.0.0-00010101000000-000000000000
github.com/docker/go-connections v0.3.0 // indirect
github.com/docker/go-connections v0.3.0
github.com/docker/go-units v0.4.0 // indirect
github.com/drone/drone-go v1.0.5-0.20190504210458-4d6116b897ba
github.com/drone/envconfig v1.4.1
Expand All @@ -32,21 +31,20 @@ require (
github.com/go-ini/ini v1.32.0 // indirect
github.com/go-sql-driver/mysql v1.3.0
github.com/gogo/protobuf v0.0.0-20170307180453-100ba4e88506 // indirect
github.com/golang/mock v1.3.1
github.com/google/go-cmp v0.4.0
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.9
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gophercloud/gophercloud v0.0.0-20181014043407-c8947f7d1c51
github.com/gorilla/mux v1.7.4 // indirect
github.com/h2non/gock v1.0.7
github.com/h2non/gock v1.2.0
github.com/hetznercloud/hcloud-go v1.4.0
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 // indirect
github.com/jmoiron/sqlx v0.0.0-20180228184624-cf35089a1979
github.com/joho/godotenv v1.2.0
github.com/kr/pretty v0.0.0-20160823170715-cfb55aafdaf3
github.com/kr/text v0.0.0-20160504234017-7cafcd837844 // indirect
github.com/kr/pretty v0.1.0
github.com/lib/pq v1.10.4
github.com/mattn/go-sqlite3 v1.6.0
github.com/matttproud/golang_protobuf_extensions v1.0.0 // indirect
Expand All @@ -64,18 +62,13 @@ require (
github.com/sirupsen/logrus v1.4.2
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/tent/http-link-go v0.0.0-20130702225549-ac974c61c2f9 // indirect
golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 // indirect
google.golang.org/api v0.0.0-20180921000521-920bb1beccf7
google.golang.org/appengine v1.4.0 // indirect
google.golang.org/grpc v1.30.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783
golang.org/x/sync v0.1.0
golang.org/x/time v0.1.0
google.golang.org/api v0.105.0
gopkg.in/ini.v1 v1.51.0 // indirect
gotest.tools v2.2.0+incompatible // indirect
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc // indirect
)

replace github.com/drone/funcmap => github.com/iainlane/funcmap v0.0.0-20211116113722-13f662008062
Loading

0 comments on commit 78e53fb

Please sign in to comment.