Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ func main() {
if !ok || attachment.Status.VPC == "" || attachment.Status.VPCAttachment == "" {
return nil
}
return []string{galactic.AdvertisementName(attachment.Status.VPC, attachment.Status.VPCAttachment)}
identity := galactic.AdvertisementName(attachment.Status.VPC, attachment.Status.VPCAttachment)
if identity == "" {
return nil
}
return []string{identity}
}); err != nil {
setupLog.Error(err, "unable to index VPC attachments by identity")
os.Exit(1)
Expand Down
32 changes: 25 additions & 7 deletions internal/galactic/galactic.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"encoding/json"
"fmt"
"strings"

"go.datum.net/cloud/internal/identifier"
)

const (
Expand Down Expand Up @@ -166,17 +168,33 @@ func VRFInterfaceName(vpc string) string {
return fmt.Sprintf("G%09sV", vpc)
}

// AdvertisementName returns the BGPAdvertisement name galactic derives from an
// attachment.
// AdvertisementName returns the hex-encoded identity prefix shared by every
// node-qualified BGPAdvertisement galactic derives from an attachment.
func AdvertisementName(vpc, vpcAttachment string) string {
return fmt.Sprintf("%s-%s", vpc, vpcAttachment)
vpcHex, err := identifier.Base62ToHex(vpc)
if err != nil {
return ""
}
attachmentHex, err := identifier.Base62ToHex(vpcAttachment)
if err != nil {
return ""
}
return fmt.Sprintf("%s-%s", vpcHex, attachmentHex)
}

// SplitAdvertisementName recovers the (vpc, attachment) pair from an
// advertisement name.
// SplitAdvertisementName recovers the base62 (vpc, attachment) pair from a
// current node-qualified advertisement name.
func SplitAdvertisementName(name string) (vpc, vpcAttachment string, ok bool) {
vpc, vpcAttachment, ok = strings.Cut(name, "-")
if !ok || vpc == "" || vpcAttachment == "" {
parts := strings.SplitN(name, "-", 3)
if len(parts) != 3 || parts[0] == "" || parts[1] == "" || parts[2] == "" {
return "", "", false
}
vpc, err := identifier.HexToBase62(parts[0])
if err != nil || vpc == "" {
return "", "", false
}
vpcAttachment, err = identifier.HexToBase62(parts[1])
if err != nil || vpcAttachment == "" {
return "", "", false
}
return vpc, vpcAttachment, true
Expand Down
17 changes: 14 additions & 3 deletions internal/galactic/galactic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ func TestSplitAdvertisementName(t *testing.T) {
vpcAttachment string
ok bool
}{
{"0000000jU-01a", "0000000jU", "01a", true},
{"0000000jU", "", "", false},
{"-01a", "", "", false},
{"e008-d54-compute-c21d3c-us-east-1-staging-lab", "eV2", "T2", true},
{"e007-d53-edge-567e29-us-east-1", "eV1", "T1", true},
{"e008-d54", "", "", false},
{"-d54-worker", "", "", false},
{"e008--worker", "", "", false},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
Expand All @@ -127,6 +129,15 @@ func TestSplitAdvertisementName(t *testing.T) {
}
}

func TestAdvertisementNameEncodesBase62IdentityAsHex(t *testing.T) {
if got, want := AdvertisementName("eV2", "T2"), "e008-d54"; got != want {
t.Errorf("AdvertisementName() = %q, want %q", got, want)
}
if got := AdvertisementName("not-valid!", "T2"); got != "" {
t.Errorf("AdvertisementName() with invalid VPC = %q, want empty", got)
}
}

// The tap plugin reads this one field to decide whether it describes the device
// to the hypervisor. Every attachment rendered until now leaves it out, so its
// absence has to stay the default.
Expand Down
Loading