From cd64d501aa5a0f1cb08006469579ce455919669c Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Fri, 22 Jan 2016 17:21:49 -0800 Subject: [PATCH] Update to use just unique namespace prefix --- nomad/structs/node_class.go | 45 ++++---------------------------- nomad/structs/node_class_test.go | 16 ++---------- 2 files changed, 7 insertions(+), 54 deletions(-) diff --git a/nomad/structs/node_class.go b/nomad/structs/node_class.go index 704810af9..aaa1f4e3a 100644 --- a/nomad/structs/node_class.go +++ b/nomad/structs/node_class.go @@ -4,14 +4,13 @@ import ( "fmt" "strings" - "github.com/gobwas/glob" "github.com/mitchellh/hashstructure" ) const ( - // NodeUniqueSuffix is a suffix that can be appended to node meta or + // NodeUniqueNamespace is a prefix that can be appended to node meta or // attribute keys to mark them for exclusion in computed node class. - NodeUniqueSuffix = "_unique" + NodeUniqueNamespace = "unique." ) // ComputeClass computes a derived class for the node based on its attributes. @@ -47,38 +46,6 @@ func (n Node) HashInclude(field string, v interface{}) (bool, error) { } } -var ( - // GlobalUniqueAttrs is a set of attributes that uniquely identify all - // nodes. It is stored once by the server, rather than by each node to - // reduce storage costs. - GlobalUniqueAttrs = []glob.Glob{ - glob.MustCompile("consul.name"), - glob.MustCompile("platform.gce.hostname"), - glob.MustCompile("platform.gce.id"), - glob.MustCompile("platform.gce.network.*.ip"), - glob.MustCompile("platform.gce.network.*.external-ip"), - glob.MustCompile("platform.aws.ami-id"), - glob.MustCompile("platform.aws.hostname"), - glob.MustCompile("platform.aws.instance-id"), - glob.MustCompile("platform.aws.local*"), - glob.MustCompile("platform.aws.public*"), - glob.MustCompile("network.ip-address"), - glob.MustCompile("storage.*"), // Ignore all storage - } -) - -// excludeAttr returns whether the key should be excluded when calculating -// computed node class. -func excludeAttr(key string) bool { - for _, g := range GlobalUniqueAttrs { - if g.Match(key) { - return true - } - } - - return false -} - // HashIncludeMap is used to blacklist uniquely identifying node map keys from being // included in the computed node class. func (n Node) HashIncludeMap(field string, k, v interface{}) (bool, error) { @@ -87,13 +54,11 @@ func (n Node) HashIncludeMap(field string, k, v interface{}) (bool, error) { return false, fmt.Errorf("map key %v not a string") } - // Check if the user marked the key as unique. - isUnique := strings.HasSuffix(key, NodeUniqueSuffix) + // Check if the key is unique. + isUnique := strings.HasPrefix(key, NodeUniqueNamespace) switch field { - case "Attributes": - return !excludeAttr(key) && !isUnique, nil - case "Meta": + case "Meta", "Attributes": return !isUnique, nil default: return false, fmt.Errorf("unexpected map field: %v", field) diff --git a/nomad/structs/node_class_test.go b/nomad/structs/node_class_test.go index da15ca455..07f4c9b6f 100644 --- a/nomad/structs/node_class_test.go +++ b/nomad/structs/node_class_test.go @@ -110,7 +110,7 @@ func TestNode_ComputedClass_Attr(t *testing.T) { old := n.ComputedClass // Add a unique addr and compute the class again - n.Attributes[fmt.Sprintf("%s%s", "foo", NodeUniqueSuffix)] = "bar" + n.Attributes[fmt.Sprintf("%s%s", NodeUniqueNamespace, "foo")] = "bar" if err := n.ComputeClass(); err != nil { t.Fatalf("ComputeClass() failed: %v", err) } @@ -130,18 +130,6 @@ func TestNode_ComputedClass_Attr(t *testing.T) { t.Fatal("ComputeClass() ignored attribute change") } old = n.ComputedClass - - // Add an ignored attribute and compute the class again. - n.Attributes["storage.bytes-foo"] = "hello world" - if err := n.ComputeClass(); err != nil { - t.Fatalf("ComputeClass() failed: %v", err) - } - if n.ComputedClass == 0 { - t.Fatal("ComputeClass() didn't set computed class") - } - if old != n.ComputedClass { - t.Fatal("ComputeClass() didn't ignore unique attribute") - } } func TestNode_ComputedClass_Meta(t *testing.T) { @@ -169,7 +157,7 @@ func TestNode_ComputedClass_Meta(t *testing.T) { old = n.ComputedClass // Add a unique meta key and compute the class again. - key := "test_unique" + key := fmt.Sprintf("%s%s", NodeUniqueNamespace, "foo") n.Meta[key] = "ignore" if err := n.ComputeClass(); err != nil { t.Fatalf("ComputeClass() failed: %v", err)