Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

Fix support for GCP Shared VPC #4774

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 30 additions & 6 deletions drivers/google/compute_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ComputeUtil struct {
project string
diskTypeURL string
address string
networkProject string
network string
subnetwork string
preemptible bool
Expand Down Expand Up @@ -60,13 +61,27 @@ func newComputeUtil(driver *Driver) (*ComputeUtil, error) {
return nil, err
}

var networkProject string
if strings.Contains(driver.Network, "/projects/") {
var splittedElements = strings.Split(driver.Network, "/")
for i, element := range splittedElements {
if element == "projects" {
networkProject = splittedElements[i+1]
break
}
}
} else {
networkProject = driver.Project
}

return &ComputeUtil{
zone: driver.Zone,
instanceName: driver.MachineName,
userName: driver.SSHUser,
project: driver.Project,
diskTypeURL: driver.DiskType,
address: driver.Address,
networkProject: networkProject,
network: driver.Network,
subnetwork: driver.Subnetwork,
preemptible: driver.Preemptible,
Expand Down Expand Up @@ -137,7 +152,7 @@ func (c *ComputeUtil) region() string {
}

func (c *ComputeUtil) firewallRule() (*raw.Firewall, error) {
return c.service.Firewalls.Get(c.project, firewallRule).Do()
return c.service.Firewalls.Get(c.networkProject, firewallRule).Do()
}

func missingOpenedPorts(rule *raw.Firewall, ports []string) map[string][]string {
Expand Down Expand Up @@ -185,15 +200,24 @@ func (c *ComputeUtil) openFirewallPorts(d *Driver) error {
log.Infof("Opening firewall ports")

create := false
rule, _ := c.firewallRule()
rule, err := c.firewallRule()
if err != nil {
return err
}
if rule == nil {
create = true
var net string
if strings.Contains(d.Network, "/networks/") {
net = d.Network
} else {
net = c.globalURL + "/networks/" + d.Network
}
rule = &raw.Firewall{
Name: firewallRule,
Allowed: []*raw.FirewallAllowed{},
SourceRanges: []string{"0.0.0.0/0"},
TargetTags: []string{firewallTargetTag},
Network: c.globalURL + "/networks/" + d.Network,
Network: net,
}
}

Expand All @@ -215,9 +239,9 @@ func (c *ComputeUtil) openFirewallPorts(d *Driver) error {

var op *raw.Operation
if create {
op, err = c.service.Firewalls.Insert(c.project, rule).Do()
op, err = c.service.Firewalls.Insert(c.networkProject, rule).Do()
} else {
op, err = c.service.Firewalls.Update(c.project, firewallRule, rule).Do()
op, err = c.service.Firewalls.Update(c.networkProject, firewallRule, rule).Do()
}

if err != nil {
Expand Down Expand Up @@ -277,7 +301,7 @@ func (c *ComputeUtil) createInstance(d *Driver) error {
if strings.Contains(c.subnetwork, "/subnetworks/") {
instance.NetworkInterfaces[0].Subnetwork = c.subnetwork
} else if c.subnetwork != "" {
instance.NetworkInterfaces[0].Subnetwork = "projects/" + c.project + "/regions/" + c.region() + "/subnetworks/" + c.subnetwork
instance.NetworkInterfaces[0].Subnetwork = "projects/" + c.networkProject + "/regions/" + c.region() + "/subnetworks/" + c.subnetwork
}

if !c.useInternalIPOnly {
Expand Down