Skip to content

Commit 3c1e2c3

Browse files
authored
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
1 parent 479b589 commit 3c1e2c3

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

host/host.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Name struct {
1515
Site string
1616
Project string
1717
Domain string
18+
Suffix string
1819
Version string
1920
}
2021

@@ -24,7 +25,7 @@ func Parse(name string) (Name, error) {
2425
var parts Name
2526

2627
reV1 := regexp.MustCompile(`(?:[a-z-.]+)?(mlab[1-4]d?)[-.]([a-z]{3}[0-9tc]{2})\.(measurement-lab.org)$`)
27-
reV2 := regexp.MustCompile(`(?:[a-z-.]+)?(mlab[1-4]d?)-([a-z]{3}[0-9tc]{2})\.(.*?)\.(measurement-lab.org)$`)
28+
reV2 := regexp.MustCompile(`(?:[a-z-.]+)?(mlab[1-4]d?)-([a-z]{3}[0-9tc]{2})\.(.*?)\.(measurement-lab.org)-?([a-z0-9]{4})?$`)
2829

2930
// Example hostnames with field counts when split by '.':
3031
// v1
@@ -33,6 +34,7 @@ func Parse(name string) (Name, error) {
3334
// ndt.iupui.mlab1.lga01.measurement-lab.org - 6
3435
// v2
3536
// mlab1-lga01.mlab-oti.measurement-lab.org - 4
37+
// mlab1-lga01.mlab-oti.measurement-lab.org-d9h6 - 4 (A MIG instance with a random suffix)
3638
// ndt-iupui-mlab1-lga01.mlab-oti.measurement-lab.org - 4
3739
// ndt-mlab1-lga01.mlab-oti.measurement-lab.org - 4
3840

@@ -45,14 +47,15 @@ func Parse(name string) (Name, error) {
4547
// be longer than a machine name e.g. "mlab1".
4648
if len(fields) == 4 && len(fields[0]) > 6 {
4749
mV2 := reV2.FindAllStringSubmatch(name, -1)
48-
if len(mV2) != 1 || len(mV2[0]) != 5 {
50+
if len(mV2) != 1 || len(mV2[0]) != 6 {
4951
return parts, fmt.Errorf("Invalid v2 hostname: %s", name)
5052
}
5153
parts = Name{
5254
Machine: mV2[0][1],
5355
Site: mV2[0][2],
5456
Project: mV2[0][3],
5557
Domain: mV2[0][4],
58+
Suffix: mV2[0][5],
5659
Version: "v2",
5760
}
5861
} else {

host/host_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ func TestName(t *testing.T) {
3636
Version: "v2",
3737
},
3838
},
39+
{
40+
name: "valid-v2-with-suffix",
41+
hostname: "mlab1-lol01.mlab-oti.measurement-lab.org-a9b8",
42+
want: Name{
43+
Machine: "mlab1",
44+
Site: "lol01",
45+
Project: "mlab-oti",
46+
Domain: "measurement-lab.org",
47+
Suffix: "a9b8",
48+
Version: "v2",
49+
},
50+
},
3951
{
4052
name: "valid-v1-bmc",
4153
hostname: "mlab1d.lol01.measurement-lab.org",
@@ -99,6 +111,18 @@ func TestName(t *testing.T) {
99111
Version: "v2",
100112
},
101113
},
114+
{
115+
name: "valid-v2-with-ndt-short-with-suffix",
116+
hostname: "ndt-mlab1-lol01.mlab-oti.measurement-lab.org-q44c",
117+
want: Name{
118+
Machine: "mlab1",
119+
Site: "lol01",
120+
Project: "mlab-oti",
121+
Domain: "measurement-lab.org",
122+
Suffix: "q44c",
123+
Version: "v2",
124+
},
125+
},
102126
{
103127
name: "invalid-too-few-separators",
104128
hostname: "mlab1-lol01-measurement-lab.org",

0 commit comments

Comments
 (0)