From d5f56b697e28fd661d93e527ddf78304960388a1 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Sun, 9 Feb 2020 09:41:32 +0000 Subject: [PATCH] Rename some fields in GCE provision package Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- go.mod | 1 + pkg/provision/gce.go | 44 +++++++++++++++++++++++++------------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index dbbdf7ce..681dc150 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/spf13/cobra v0.0.5 github.com/spf13/pflag v1.0.5 go.opencensus.io v0.22.2 // indirect + golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 // indirect golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 // indirect golang.org/x/oauth2 v0.0.0-20191122200657-5d9234df094c golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 // indirect diff --git a/pkg/provision/gce.go b/pkg/provision/gce.go index b3ddc05c..cd4e57f5 100644 --- a/pkg/provision/gce.go +++ b/pkg/provision/gce.go @@ -62,7 +62,10 @@ func (p *GCEProvisioner) Provision(host BasicHost) (*ProvisionedHost, error) { "inlets": "exit-node", }, Tags: &compute.Tags{ - Items: []string{"http-server", "https-server", "inlets"}, + Items: []string{ + "http-server", + "https-server", + "inlets"}, }, Scheduling: &compute.Scheduling{ AutomaticRestart: &autoRestart, @@ -90,7 +93,7 @@ func (p *GCEProvisioner) Provision(host BasicHost) (*ProvisionedHost, error) { }, } - exists, _ := p.checkInletsFirewallRuleExists(host.Additional["projectid"], host.Additional["firewall-name"], host.Additional["firewall-port"]) + exists, _ := p.gceFirewallExists(host.Additional["projectid"], host.Additional["firewall-name"], host.Additional["firewall-port"]) if !exists { err := p.createInletsFirewallRule(host.Additional["projectid"], host.Additional["firewall-name"], host.Additional["firewall-port"]) @@ -113,15 +116,15 @@ func (p *GCEProvisioner) Provision(host BasicHost) (*ProvisionedHost, error) { if op.Status == gceHostRunning { status = ActiveStatus } + return &ProvisionedHost{ - ID: constructCustomGCEID(host.Name, host.Additional["zone"], host.Additional["projectid"]), + ID: toGCEID(host.Name, host.Additional["zone"], host.Additional["projectid"]), Status: status, }, nil - } -// checkInletsFirewallRuleExists checks if the inlets firewall rule exists or not -func (p *GCEProvisioner) checkInletsFirewallRuleExists(projectID string, firewallRuleName string, inletsPort string) (bool, error) { +// gceFirewallExists checks if the inlets firewall rule exists or not +func (p *GCEProvisioner) gceFirewallExists(projectID string, firewallRuleName string, controlPort string) (bool, error) { op, err := p.gceProvisioner.Firewalls.Get(projectID, firewallRuleName).Do() if err != nil { return false, fmt.Errorf("could not get inlets firewall rule: %v", err) @@ -129,7 +132,7 @@ func (p *GCEProvisioner) checkInletsFirewallRuleExists(projectID string, firewal if op.Name == firewallRuleName { for _, firewallRule := range op.Allowed { for _, port := range firewallRule.Ports { - if port == inletsPort { + if port == controlPort { return true, nil } } @@ -139,7 +142,7 @@ func (p *GCEProvisioner) checkInletsFirewallRuleExists(projectID string, firewal } // createInletsFirewallRule creates a firewall rule opening up the control port for inlets -func (p *GCEProvisioner) createInletsFirewallRule(projectID string, firewallRuleName string, inletsPort string) error { +func (p *GCEProvisioner) createInletsFirewallRule(projectID string, firewallRuleName string, controlPort string) error { firewallRule := &compute.Firewall{ Name: firewallRuleName, Description: "Firewall rule created by inlets-operator", @@ -147,7 +150,7 @@ func (p *GCEProvisioner) createInletsFirewallRule(projectID string, firewallRule Allowed: []*compute.FirewallAllowed{ { IPProtocol: "tcp", - Ports: []string{inletsPort}, + Ports: []string{controlPort}, }, }, SourceRanges: []string{"0.0.0.0/0"}, @@ -159,6 +162,7 @@ func (p *GCEProvisioner) createInletsFirewallRule(projectID string, firewallRule if err != nil { return fmt.Errorf("could not create firewall rule: %v", err) } + return nil } @@ -212,7 +216,7 @@ func (p *GCEProvisioner) List(filter ListFilter) ([]*ProvisionedHost, error) { } host := &ProvisionedHost{ IP: instance.NetworkInterfaces[0].AccessConfigs[0].NatIP, - ID: constructCustomGCEID(instance.Name, filter.Zone, filter.ProjectID), + ID: toGCEID(instance.Name, filter.Zone, filter.ProjectID), Status: status, } inlets = append(inlets, host) @@ -225,7 +229,7 @@ func (p *GCEProvisioner) List(filter ListFilter) ([]*ProvisionedHost, error) { } func (p *GCEProvisioner) lookupID(request HostDeleteRequest) (string, error) { - inlets, err := p.List(ListFilter{ + inletHosts, err := p.List(ListFilter{ Filter: "labels.inlets=exit-node", ProjectID: request.ProjectID, Zone: request.Zone, @@ -234,12 +238,13 @@ func (p *GCEProvisioner) lookupID(request HostDeleteRequest) (string, error) { return "", err } - for _, inlet := range inlets { - if inlet.IP == request.IP { - return inlet.ID, nil + for _, host := range inletHosts { + if host.IP == request.IP { + return host.ID, nil } } - return "", fmt.Errorf("no host with ip: %s", request.IP) + + return "", fmt.Errorf("no host found with IP: %s", request.IP) } // Status checks the status of the provisioning GCE exit node @@ -256,19 +261,20 @@ func (p *GCEProvisioner) Status(id string) (*ProvisionedHost, error) { status := "" - if op.Status == "RUNNING" { + if op.Status == gceHostRunning { status = ActiveStatus } return &ProvisionedHost{ IP: op.NetworkInterfaces[0].AccessConfigs[0].NatIP, - ID: constructCustomGCEID(instanceName, zone, projectID), + ID: toGCEID(instanceName, zone, projectID), Status: status, }, nil } -// construct custom GCE instance ID from fields -func constructCustomGCEID(instanceName, zone, projectID string) (id string) { +// toGCEID creates an ID for GCE based upon the instance ID, +// zone, and projectID fields +func toGCEID(instanceName, zone, projectID string) (id string) { return fmt.Sprintf("%s|%s|%s", instanceName, zone, projectID) }