Skip to content

Commit

Permalink
https://github.com/docker/machine/pull/4774
Browse files Browse the repository at this point in the history
  • Loading branch information
rodolfo3 committed Aug 28, 2020
1 parent b170508 commit 7effee9
Showing 1 changed file with 30 additions and 6 deletions.
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

0 comments on commit 7effee9

Please sign in to comment.