Skip to content

Commit

Permalink
Expose list of ports nanovms#705 (nanovms#709)
Browse files Browse the repository at this point in the history
* Issue nanovms#705

-Added support to expose list of ports given by user.
-Removed http and https ports as default ports.
-In case user doesn't provide flavour, added default flavour "g1-small"

* -Added separate function to convert int array to string.
-Changed firewall rule naming convention
  • Loading branch information
chaitanya-baraskar authored Oct 16, 2020
1 parent dee4897 commit bcb6260
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
29 changes: 25 additions & 4 deletions lepton/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (p *GCloud) CreateInstance(ctx *Context) error {
}

if c.CloudConfig.Flavor == "" {
return fmt.Errorf("Flavor not provided in config.CloudConfig")
c.CloudConfig.Flavor = "g1-small"
}

client, err := google.DefaultClient(context, compute.CloudPlatformScope)
Expand Down Expand Up @@ -408,9 +408,6 @@ func (p *GCloud) CreateInstance(ctx *Context) error {
},
},
},
Tags: &compute.Tags{
Items: []string{"http-server", "https-server"},
},
}
op, err := computeService.Instances.Insert(c.CloudConfig.ProjectID, c.CloudConfig.Zone, rb).Context(context).Do()
if err != nil {
Expand All @@ -422,6 +419,30 @@ func (p *GCloud) CreateInstance(ctx *Context) error {
return err
}
fmt.Printf("Instance creation succeeded %s.\n", instanceName)

var ports []string
for _, i := range ctx.config.RunConfig.Ports {
ports = append(ports, strconv.Itoa(i))
}

rule := &compute.Firewall{
Name: "ops-rule-" + instanceName,
Description: fmt.Sprintf("Allow %s from anywhere", arrayToString(ctx.config.RunConfig.Ports, "[]")),
Allowed: []*compute.FirewallAllowed{
{
IPProtocol: "tcp",
Ports: ports,
},
},
SourceRanges: []string{"0.0.0.0/0"},
}

_, err = computeService.Firewalls.Insert(c.CloudConfig.ProjectID, rule).Context(context).Do()

if err != nil {
exitWithError("Failed to add Firewall rule")
}

return nil
}

Expand Down
5 changes: 5 additions & 0 deletions lepton/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package lepton
import (
"fmt"
"os"
"strings"
)

func exitWithError(errs string) {
fmt.Println(fmt.Sprintf(ErrorColor, errs))
os.Exit(1)
}

func arrayToString(a []int, delim string) string {
return strings.Trim(strings.Replace(fmt.Sprint(a), " ", delim, -1), "[]")
}

0 comments on commit bcb6260

Please sign in to comment.