Skip to content

Commit

Permalink
Allow for a 4 char suffix on the end of hostnames (#156)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nkinkade authored Feb 14, 2023
1 parent 479b589 commit 3c1e2c3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Name struct {
Site string
Project string
Domain string
Suffix string
Version string
}

Expand All @@ -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
Expand All @@ -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

Expand All @@ -45,14 +47,15 @@ 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{
Machine: mV2[0][1],
Site: mV2[0][2],
Project: mV2[0][3],
Domain: mV2[0][4],
Suffix: mV2[0][5],
Version: "v2",
}
} else {
Expand Down
24 changes: 24 additions & 0 deletions host/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 3c1e2c3

Please sign in to comment.