Skip to content

Commit

Permalink
basic allocate and release of shared ips
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegn committed Dec 7, 2022
1 parent 74443a0 commit b078cd2
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 18 deletions.
49 changes: 45 additions & 4 deletions api/resource_ip_addresses.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package api

import "context"
import (
"context"
"net"
"time"
)

func (c *Client) GetIPAddresses(ctx context.Context, appName string) ([]IPAddress, error) {
query := `
Expand All @@ -15,6 +19,7 @@ func (c *Client) GetIPAddresses(ctx context.Context, appName string) ([]IPAddres
createdAt
}
}
sharedIpAddress
}
}
`
Expand All @@ -27,7 +32,20 @@ func (c *Client) GetIPAddresses(ctx context.Context, appName string) ([]IPAddres
return nil, err
}

return data.App.IPAddresses.Nodes, nil
ips := data.App.IPAddresses.Nodes

// ugly hack
if data.App.SharedIPAddress != "" {
ips = append(ips, IPAddress{
ID: "",
Address: data.App.SharedIPAddress,
Type: "shared_v4",
Region: "",
CreatedAt: time.Time{},
})
}

return ips, nil
}

func (c *Client) FindIPAddress(ctx context.Context, appName string, address string) (*IPAddress, error) {
Expand Down Expand Up @@ -85,7 +103,30 @@ func (c *Client) AllocateIPAddress(ctx context.Context, appName string, addrType
return &data.AllocateIPAddress.IPAddress, nil
}

func (c *Client) ReleaseIPAddress(ctx context.Context, id string) error {
func (c *Client) AllocateSharedIPAddress(ctx context.Context, appName string) (net.IP, error) {
query := `
mutation($input: AllocateIPAddressInput!) {
allocateIpAddress(input: $input) {
app {
sharedIpAddress
}
}
}
`

req := c.NewRequest(query)

req.Var("input", AllocateIPAddressInput{AppID: appName, Type: "shared_v4"})

data, err := c.RunWithContext(ctx, req)
if err != nil {
return nil, err
}

return net.ParseIP(data.AllocateIPAddress.App.SharedIPAddress), nil
}

func (c *Client) ReleaseIPAddress(ctx context.Context, appName string, ip string) error {
query := `
mutation($input: ReleaseIPAddressInput!) {
releaseIpAddress(input: $input) {
Expand All @@ -96,7 +137,7 @@ func (c *Client) ReleaseIPAddress(ctx context.Context, id string) error {

req := c.NewRequest(query)

req.Var("input", ReleaseIPAddressInput{IPAddressID: id})
req.Var("input", ReleaseIPAddressInput{AppID: &appName, IP: &ip})

_, err := c.RunWithContext(ctx, req)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,9 @@ type App struct {
IPAddresses struct {
Nodes []IPAddress
}
IPAddress *IPAddress
Builds struct {
SharedIPAddress string
IPAddress *IPAddress
Builds struct {
Nodes []Build
}
SourceBuilds struct {
Expand Down Expand Up @@ -885,7 +886,9 @@ type AllocateIPAddressInput struct {
}

type ReleaseIPAddressInput struct {
IPAddressID string `json:"ipAddressId"`
AppID *string `json:"appId"`
IPAddressID *string `json:"ipAddressId"`
IP *string `json:"ip"`
}

type ScaleAppInput struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func reportNextStepCert(cmdCtx *cmdctx.CmdContext, hostname string, cert *api.Ap

// Extract the v4 and v6 addresses we have allocated
for _, x := range ips {
if x.Type == "v4" {
if x.Type == "v4" || x.Type == "shared_v4" {
ipV4 = x
} else if x.Type == "v6" {
ipV6 = x
Expand Down
23 changes: 22 additions & 1 deletion internal/command/ips/allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func newAllocatev4() *cobra.Command {
)

flag.Add(cmd,
flag.Bool{
Name: "shared",
Description: "Allocates a shared IPv4",
Default: false,
},
flag.App(),
flag.AppConfig(),
flag.Region(),
Expand Down Expand Up @@ -55,7 +60,11 @@ func newAllocatev6() *cobra.Command {
}

func runAllocateIPAddressV4(ctx context.Context) error {
return runAllocateIPAddress(ctx, "v4")
addrType := "v4"
if flag.GetBool(ctx, "shared") {
addrType = "shared_v4"
}
return runAllocateIPAddress(ctx, addrType)
}

func runAllocateIPAddressV6(ctx context.Context) error {
Expand All @@ -70,6 +79,18 @@ func runAllocateIPAddress(ctx context.Context, addrType string) error {
client := client.FromContext(ctx).API()

appName := app.NameFromContext(ctx)

if addrType == "shared_v4" {
ip, err := client.AllocateSharedIPAddress(ctx, appName)
if err != nil {
return err
}

renderSharedTable(ctx, ip)

return nil
}

region := flag.GetRegion(ctx)

ipAddress, err := client.AllocateIPAddress(ctx, appName, addrType, region)
Expand Down
9 changes: 2 additions & 7 deletions internal/command/ips/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,11 @@ func runReleaseIPAddress(ctx context.Context) error {
return fmt.Errorf("Invalid IP address: '%s'", address)
}

ipAddress, err := client.FindIPAddress(ctx, appName, address)
if err != nil {
if err := client.ReleaseIPAddress(ctx, appName, address); err != nil {
return err
}

if err := client.ReleaseIPAddress(ctx, ipAddress.ID); err != nil {
return err
}

fmt.Printf("Released %s from %s\n", ipAddress.Address, appName)
fmt.Printf("Released %s from %s\n", address, appName)

return nil
}
16 changes: 15 additions & 1 deletion internal/command/ips/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ips

import (
"context"
"net"
"strings"

"github.com/superfly/flyctl/api"
Expand All @@ -21,7 +22,11 @@ func renderListTable(ctx context.Context, ipAddresses []api.IPAddress) {
ipType = "public"
}

rows = append(rows, []string{ipAddr.Type, ipAddr.Address, ipType, ipAddr.Region, presenters.FormatRelativeTime(ipAddr.CreatedAt)})
if ipAddr.Type == "shared_v4" {
rows = append(rows, []string{"v4", ipAddr.Address, "public (shared)", ipAddr.Region, ""})
} else {
rows = append(rows, []string{ipAddr.Type, ipAddr.Address, ipType, ipAddr.Region, presenters.FormatRelativeTime(ipAddr.CreatedAt)})
}
}

out := iostreams.FromContext(ctx).Out
Expand Down Expand Up @@ -49,3 +54,12 @@ func renderPrivateTable(ctx context.Context, allocations []*api.AllocationStatus
out := iostreams.FromContext(ctx).Out
render.Table(out, "", rows, "ID", "Region", "IP")
}

func renderSharedTable(ctx context.Context, ip net.IP) {
rows := make([][]string, 0, 1)

rows = append(rows, []string{"v4", ip.String(), "shared", "global"})

out := iostreams.FromContext(ctx).Out
render.Table(out, "", rows, "Version", "IP", "Type", "Region")
}
2 changes: 1 addition & 1 deletion internal/command/machine/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func setupHttpService(ctx context.Context, appConfig *app.Config, srcInfo *scann
if err != nil {
return
}
_, err = client.AllocateIPAddress(ctx, appConfig.AppName, "v4", "")
_, err = client.AllocateIPAddress(ctx, appConfig.AppName, "shared_v4", "")

if err != nil {
return err
Expand Down

0 comments on commit b078cd2

Please sign in to comment.