2016-01-21 01:30:02 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mitchellh/hashstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-01-23 01:21:49 +00:00
|
|
|
// NodeUniqueNamespace is a prefix that can be appended to node meta or
|
2016-01-21 19:25:14 +00:00
|
|
|
// attribute keys to mark them for exclusion in computed node class.
|
2016-01-23 01:21:49 +00:00
|
|
|
NodeUniqueNamespace = "unique."
|
2016-01-21 01:30:02 +00:00
|
|
|
)
|
|
|
|
|
2016-01-23 02:12:16 +00:00
|
|
|
// UniqueNamespace takes a key and returns the key marked under the unique
|
|
|
|
// namespace.
|
|
|
|
func UniqueNamespace(key string) string {
|
|
|
|
return fmt.Sprintf("%s%s", NodeUniqueNamespace, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsUniqueNamespace returns whether the key is under the unique namespace.
|
|
|
|
func IsUniqueNamespace(key string) bool {
|
|
|
|
return strings.HasPrefix(key, NodeUniqueNamespace)
|
|
|
|
}
|
|
|
|
|
2016-01-21 01:30:02 +00:00
|
|
|
// ComputeClass computes a derived class for the node based on its attributes.
|
|
|
|
// ComputedClass is a unique id that identifies nodes with a common set of
|
|
|
|
// attributes and capabilities. Thus, when calculating a node's computed class
|
2018-03-11 18:15:05 +00:00
|
|
|
// we avoid including any uniquely identifying fields.
|
2016-01-21 01:30:02 +00:00
|
|
|
func (n *Node) ComputeClass() error {
|
|
|
|
hash, err := hashstructure.Hash(n, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-30 01:46:44 +00:00
|
|
|
n.ComputedClass = fmt.Sprintf("v1:%d", hash)
|
2016-01-21 01:30:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-12 12:47:05 +00:00
|
|
|
// HashInclude is used to denylist uniquely identifying node fields from being
|
2016-01-21 01:30:02 +00:00
|
|
|
// included in the computed node class.
|
|
|
|
func (n Node) HashInclude(field string, v interface{}) (bool, error) {
|
|
|
|
switch field {
|
2018-11-16 23:29:59 +00:00
|
|
|
case "Datacenter", "Attributes", "Meta", "NodeClass", "NodeResources":
|
2016-01-21 01:30:02 +00:00
|
|
|
return true, nil
|
2016-01-26 22:55:38 +00:00
|
|
|
default:
|
|
|
|
return false, nil
|
2016-01-21 01:30:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 12:47:05 +00:00
|
|
|
// HashIncludeMap is used to denylist uniquely identifying node map keys from being
|
2016-01-21 01:30:02 +00:00
|
|
|
// included in the computed node class.
|
|
|
|
func (n Node) HashIncludeMap(field string, k, v interface{}) (bool, error) {
|
|
|
|
key, ok := k.(string)
|
|
|
|
if !ok {
|
2016-01-27 17:23:57 +00:00
|
|
|
return false, fmt.Errorf("map key %v not a string", k)
|
2016-01-21 01:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch field {
|
2016-01-23 01:21:49 +00:00
|
|
|
case "Meta", "Attributes":
|
2016-01-23 02:12:16 +00:00
|
|
|
return !IsUniqueNamespace(key), nil
|
2016-01-21 01:30:02 +00:00
|
|
|
default:
|
|
|
|
return false, fmt.Errorf("unexpected map field: %v", field)
|
|
|
|
}
|
|
|
|
}
|
2016-01-25 19:45:48 +00:00
|
|
|
|
2020-10-12 12:47:05 +00:00
|
|
|
// HashInclude is used to denylist uniquely identifying node fields from being
|
2018-11-16 23:29:59 +00:00
|
|
|
// included in the computed node class.
|
|
|
|
func (n NodeResources) HashInclude(field string, v interface{}) (bool, error) {
|
|
|
|
switch field {
|
|
|
|
case "Devices":
|
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 12:47:05 +00:00
|
|
|
// HashInclude is used to denylist uniquely identifying node fields from being
|
2018-11-16 23:29:59 +00:00
|
|
|
// included in the computed node class.
|
|
|
|
func (n NodeDeviceResource) HashInclude(field string, v interface{}) (bool, error) {
|
|
|
|
switch field {
|
|
|
|
case "Vendor", "Type", "Name", "Attributes":
|
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 12:47:05 +00:00
|
|
|
// HashIncludeMap is used to denylist uniquely identifying node map keys from being
|
2018-11-16 23:29:59 +00:00
|
|
|
// included in the computed node class.
|
|
|
|
func (n NodeDeviceResource) HashIncludeMap(field string, k, v interface{}) (bool, error) {
|
|
|
|
key, ok := k.(string)
|
|
|
|
if !ok {
|
|
|
|
return false, fmt.Errorf("map key %v not a string", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch field {
|
|
|
|
case "Attributes":
|
|
|
|
return !IsUniqueNamespace(key), nil
|
|
|
|
default:
|
|
|
|
return false, fmt.Errorf("unexpected map field: %v", field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 19:45:48 +00:00
|
|
|
// EscapedConstraints takes a set of constraints and returns the set that
|
|
|
|
// escapes computed node classes.
|
|
|
|
func EscapedConstraints(constraints []*Constraint) []*Constraint {
|
|
|
|
var escaped []*Constraint
|
|
|
|
for _, c := range constraints {
|
|
|
|
if constraintTargetEscapes(c.LTarget) || constraintTargetEscapes(c.RTarget) {
|
|
|
|
escaped = append(escaped, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return escaped
|
|
|
|
}
|
|
|
|
|
|
|
|
// constraintTargetEscapes returns whether the target of a constraint escapes
|
|
|
|
// computed node class optimization.
|
|
|
|
func constraintTargetEscapes(target string) bool {
|
|
|
|
switch {
|
2016-02-05 00:50:20 +00:00
|
|
|
case strings.HasPrefix(target, "${node.unique."):
|
2016-01-25 19:45:48 +00:00
|
|
|
return true
|
2016-02-05 00:50:20 +00:00
|
|
|
case strings.HasPrefix(target, "${attr.unique."):
|
2016-01-27 00:43:42 +00:00
|
|
|
return true
|
2016-02-05 00:50:20 +00:00
|
|
|
case strings.HasPrefix(target, "${meta.unique."):
|
2016-01-27 00:43:42 +00:00
|
|
|
return true
|
2016-01-25 19:45:48 +00:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|