-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: smarter fly presets for clusters
- Loading branch information
Showing
9 changed files
with
339 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Utilities to interact with Fly.io platform | ||
package fly | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"time" | ||
) | ||
|
||
type VMInfo struct { | ||
ID string | ||
Region string | ||
} | ||
|
||
// ClusterInfo contains information about the Fly.io cluster | ||
// obtained from the DNS records, such as the number of machines and regions | ||
type ClusterInfo struct { | ||
regions []string | ||
vms []*VMInfo | ||
} | ||
|
||
func (c *ClusterInfo) NumRegions() int { | ||
return len(c.regions) | ||
} | ||
|
||
func (c *ClusterInfo) Regions() []string { | ||
return c.regions | ||
} | ||
|
||
func (c *ClusterInfo) VMs() []string { | ||
ids := make([]string, len(c.vms)) | ||
for i, vm := range c.vms { | ||
ids[i] = vm.ID | ||
} | ||
return ids | ||
} | ||
|
||
func (c *ClusterInfo) NumVMs() int { | ||
return len(c.vms) | ||
} | ||
|
||
func Cluster(appName string) (*ClusterInfo, error) { | ||
addr := fmt.Sprintf("vms.%s.internal.", appName) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
|
||
textRecords, err := net.DefaultResolver.LookupTXT(ctx, addr) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
vms := make([]*VMInfo, len(textRecords)) | ||
regionsMap := make(map[string]struct{}) | ||
regions := make([]string, 0) | ||
|
||
for i, txt := range textRecords { | ||
vm := &VMInfo{} | ||
fmt.Sscanf(txt, "%s %s", &vm.ID, &vm.Region) | ||
vms[i] = vm | ||
|
||
if _, ok := regionsMap[vm.Region]; !ok { | ||
regionsMap[vm.Region] = struct{}{} | ||
regions = append(regions, vm.Region) | ||
} | ||
} | ||
|
||
return &ClusterInfo{regions: regions, vms: vms}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package fly | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/anycable/anycable-go/mocks" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCluster_multiple_nodes_and_regions(t *testing.T) { | ||
teardownDNS := mocks.MockDNSServer("vms.my-fly-app.internal.", []string{"xyz ewr", "abc sea", "def ewr"}) | ||
defer teardownDNS() | ||
|
||
cluster, err := Cluster("my-fly-app") | ||
|
||
require.NoError(t, err) | ||
|
||
assert.Equal(t, 2, cluster.NumRegions()) | ||
assert.Equal(t, 3, cluster.NumVMs()) | ||
assert.EqualValues(t, []string{"ewr", "sea"}, cluster.Regions()) | ||
assert.EqualValues(t, []string{"xyz", "abc", "def"}, cluster.VMs()) | ||
} | ||
|
||
func TestCluster_when_dns_error(t *testing.T) { | ||
cluster, err := Cluster("my-fly-app") | ||
|
||
require.Error(t, err) | ||
assert.Nil(t, cluster) | ||
} |
Oops, something went wrong.