diff --git a/cmd/main.go b/cmd/main.go index ea42da3..0f6fb6b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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) diff --git a/internal/galactic/galactic.go b/internal/galactic/galactic.go index 4e82cf9..7c9e5b4 100644 --- a/internal/galactic/galactic.go +++ b/internal/galactic/galactic.go @@ -25,6 +25,8 @@ import ( "encoding/json" "fmt" "strings" + + "go.datum.net/cloud/internal/identifier" ) const ( @@ -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 diff --git a/internal/galactic/galactic_test.go b/internal/galactic/galactic_test.go index d1ac56a..d718ff2 100644 --- a/internal/galactic/galactic_test.go +++ b/internal/galactic/galactic_test.go @@ -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) { @@ -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.