From 3c1e2c3095213deab69fe78fba9d885f1538b591 Mon Sep 17 00:00:00 2001 From: nkinkade Date: Tue, 14 Feb 2023 12:44:44 -0700 Subject: [PATCH] Allow for a 4 char suffix on the end of hostnames (#156) * Allow for a 4 char suffix on the end of hostnames As we migrate cloud deployments to use a load balancer backed by managed instance groups, we can no longer count on machine names to be predetermined. MIG instances have a "-<4 char>" suffix, and are managed by the instance group, not M-Lab. In order to support this use case, the changes in this PR allow for a suffix to be part of the hostname without failing Parse(). Callers can use the suffix, if found, or not. * Adds example of MIG instance name with rand suffix --- host/host.go | 7 +++++-- host/host_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/host/host.go b/host/host.go index aee8c65..815a809 100644 --- a/host/host.go +++ b/host/host.go @@ -15,6 +15,7 @@ type Name struct { Site string Project string Domain string + Suffix string Version string } @@ -24,7 +25,7 @@ func Parse(name string) (Name, error) { var parts Name reV1 := regexp.MustCompile(`(?:[a-z-.]+)?(mlab[1-4]d?)[-.]([a-z]{3}[0-9tc]{2})\.(measurement-lab.org)$`) - reV2 := regexp.MustCompile(`(?:[a-z-.]+)?(mlab[1-4]d?)-([a-z]{3}[0-9tc]{2})\.(.*?)\.(measurement-lab.org)$`) + reV2 := regexp.MustCompile(`(?:[a-z-.]+)?(mlab[1-4]d?)-([a-z]{3}[0-9tc]{2})\.(.*?)\.(measurement-lab.org)-?([a-z0-9]{4})?$`) // Example hostnames with field counts when split by '.': // v1 @@ -33,6 +34,7 @@ func Parse(name string) (Name, error) { // ndt.iupui.mlab1.lga01.measurement-lab.org - 6 // v2 // mlab1-lga01.mlab-oti.measurement-lab.org - 4 + // mlab1-lga01.mlab-oti.measurement-lab.org-d9h6 - 4 (A MIG instance with a random suffix) // ndt-iupui-mlab1-lga01.mlab-oti.measurement-lab.org - 4 // ndt-mlab1-lga01.mlab-oti.measurement-lab.org - 4 @@ -45,7 +47,7 @@ func Parse(name string) (Name, error) { // be longer than a machine name e.g. "mlab1". if len(fields) == 4 && len(fields[0]) > 6 { mV2 := reV2.FindAllStringSubmatch(name, -1) - if len(mV2) != 1 || len(mV2[0]) != 5 { + if len(mV2) != 1 || len(mV2[0]) != 6 { return parts, fmt.Errorf("Invalid v2 hostname: %s", name) } parts = Name{ @@ -53,6 +55,7 @@ func Parse(name string) (Name, error) { Site: mV2[0][2], Project: mV2[0][3], Domain: mV2[0][4], + Suffix: mV2[0][5], Version: "v2", } } else { diff --git a/host/host_test.go b/host/host_test.go index 385c31c..27eca38 100644 --- a/host/host_test.go +++ b/host/host_test.go @@ -36,6 +36,18 @@ func TestName(t *testing.T) { Version: "v2", }, }, + { + name: "valid-v2-with-suffix", + hostname: "mlab1-lol01.mlab-oti.measurement-lab.org-a9b8", + want: Name{ + Machine: "mlab1", + Site: "lol01", + Project: "mlab-oti", + Domain: "measurement-lab.org", + Suffix: "a9b8", + Version: "v2", + }, + }, { name: "valid-v1-bmc", hostname: "mlab1d.lol01.measurement-lab.org", @@ -99,6 +111,18 @@ func TestName(t *testing.T) { Version: "v2", }, }, + { + name: "valid-v2-with-ndt-short-with-suffix", + hostname: "ndt-mlab1-lol01.mlab-oti.measurement-lab.org-q44c", + want: Name{ + Machine: "mlab1", + Site: "lol01", + Project: "mlab-oti", + Domain: "measurement-lab.org", + Suffix: "q44c", + Version: "v2", + }, + }, { name: "invalid-too-few-separators", hostname: "mlab1-lol01-measurement-lab.org",