-
Notifications
You must be signed in to change notification settings - Fork 85
/
combined.go
72 lines (60 loc) · 2.37 KB
/
combined.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package nodepool
import (
"fmt"
"strings"
"github.com/hashicorp/nomad/api"
)
const (
// CombinedClusterPoolIdentifierAnd requires all identifiers to return true.
CombinedClusterPoolIdentifierAnd CombinedClusterPoolIdentifierMode = "and"
// CombinedClusterPoolIdentifierOr requires at least one identifier to
// return true.
CombinedClusterPoolIdentifierOr CombinedClusterPoolIdentifierMode = "or"
)
// CombinedClusterPoolIdentifierMode defines how different
// ClusterNodePoolIdentifiers are combined.
type CombinedClusterPoolIdentifierMode string
// combinedClusterPoolIdentifier is an implementation of the
// ClusterNodePoolIdentifier interface that filters Nomad nodes by combining
// multiple filters.
type combinedClusterPoolIdentifier struct {
poolIdentifiers []ClusterNodePoolIdentifier
mode CombinedClusterPoolIdentifierMode
}
// NewCombinedClusterPoolIdentifier returns a new combinedClusterPoolIdentifier.
func NewCombinedClusterPoolIdentifier(poolIdentifiers []ClusterNodePoolIdentifier, mode CombinedClusterPoolIdentifierMode) ClusterNodePoolIdentifier {
return &combinedClusterPoolIdentifier{
poolIdentifiers: poolIdentifiers,
mode: mode,
}
}
// IsPoolMember satisfies the IsPoolMember function on the
// ClusterNodePoolIdentifier interface.
func (c *combinedClusterPoolIdentifier) IsPoolMember(n *api.NodeListStub) bool {
// If mode is 'and' we assume the node is a member unless told otherwise.
isMember := c.mode == CombinedClusterPoolIdentifierAnd
for _, identifier := range c.poolIdentifiers {
switch c.mode {
case CombinedClusterPoolIdentifierAnd:
isMember = isMember && identifier.IsPoolMember(n)
case CombinedClusterPoolIdentifierOr:
isMember = isMember || identifier.IsPoolMember(n)
}
}
return isMember
}
// Key satisfies the Key function on the ClusterNodePoolIdentifier interface.
func (c *combinedClusterPoolIdentifier) Key() string {
return "combined_identifier"
}
// Value satisfies the Value function on the ClusterNodePoolIdentifier
// interface.
func (c *combinedClusterPoolIdentifier) Value() string {
values := make([]string, 0, len(c.poolIdentifiers))
for _, identifier := range c.poolIdentifiers {
values = append(values, fmt.Sprintf("%s:%s", identifier.Key(), identifier.Value()))
}
return strings.Join(values, fmt.Sprintf(" %s ", c.mode))
}