google: configure firewall if swarm master

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
Evan Hazlett 2015-01-27 23:04:19 -05:00
parent 4e95c2760f
commit 730f0dc68c
2 changed files with 35 additions and 7 deletions

View File

@ -3,6 +3,8 @@ package google
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/url"
"strings"
"time" "time"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
@ -20,6 +22,8 @@ type ComputeUtil struct {
zoneURL string zoneURL string
globalURL string globalURL string
ipAddress string ipAddress string
swarmMaster bool
swarmHost string
} }
const ( const (
@ -49,6 +53,8 @@ func newComputeUtil(driver *Driver) (*ComputeUtil, error) {
service: service, service: service,
zoneURL: apiURL + driver.Project + "/zones/" + driver.Zone, zoneURL: apiURL + driver.Project + "/zones/" + driver.Zone,
globalURL: apiURL + driver.Project + "/global", globalURL: apiURL + driver.Project + "/global",
swarmMaster: driver.swarmMaster,
swarmHost: driver.swarmHost,
} }
return &c, nil return &c, nil
} }
@ -79,15 +85,33 @@ func (c *ComputeUtil) firewallRule() (*raw.Firewall, error) {
func (c *ComputeUtil) createFirewallRule() error { func (c *ComputeUtil) createFirewallRule() error {
log.Infof("Creating firewall rule.") log.Infof("Creating firewall rule.")
rule := &raw.Firewall{ allowed := []*raw.FirewallAllowed{
Allowed: []*raw.FirewallAllowed{
{ {
IPProtocol: "tcp", IPProtocol: "tcp",
Ports: []string{ Ports: []string{
port, port,
},
}, },
}, },
}
if c.swarmMaster {
u, err := url.Parse(c.swarmHost)
if err != nil {
return fmt.Errorf("error authorizing port for swarm: %s", err)
}
parts := strings.Split(u.Host, ":")
swarmPort := parts[1]
allowed = append(allowed, &raw.FirewallAllowed{
IPProtocol: "tcp",
Ports: []string{
swarmPort,
},
})
}
rule := &raw.Firewall{
Allowed: allowed,
SourceRanges: []string{ SourceRanges: []string{
"0.0.0.0/0", "0.0.0.0/0",
}, },

View File

@ -31,6 +31,8 @@ type Driver struct {
PrivateKeyPath string PrivateKeyPath string
sshKeyPath string sshKeyPath string
publicSSHKeyPath string publicSSHKeyPath string
swarmMaster bool
swarmHost string
} }
// CreateFlags are the command line flags used to create a driver. // CreateFlags are the command line flags used to create a driver.
@ -109,6 +111,8 @@ func (driver *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
driver.DiskSize = flags.Int("google-disk-size") driver.DiskSize = flags.Int("google-disk-size")
driver.UserName = flags.String("google-username") driver.UserName = flags.String("google-username")
driver.Project = flags.String("google-project") driver.Project = flags.String("google-project")
driver.swarmMaster = flags.Bool("swarm-master")
driver.swarmHost = flags.String("swarm-host")
if driver.Project == "" { if driver.Project == "" {
return fmt.Errorf("Please specify the Google Cloud Project name using the option --google-project.") return fmt.Errorf("Please specify the Google Cloud Project name using the option --google-project.")
} }