2019-06-21 07:24:04 +00:00
|
|
|
package constraints
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/vulcand/predicate"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MarathonConstraintPrefix is the prefix for each label's key created from a Marathon application constraint.
|
|
|
|
// It is used in order to create a specific and unique pattern for these labels.
|
|
|
|
const MarathonConstraintPrefix = "Traefik-Marathon-505F9E15-BDC7-45E7-828D-C06C7BAB8091"
|
|
|
|
|
2019-11-29 16:16:05 +00:00
|
|
|
type constraintLabelFunc func(map[string]string) bool
|
2019-06-21 07:24:04 +00:00
|
|
|
|
2019-11-29 16:16:05 +00:00
|
|
|
// MatchLabels reports whether the expression matches with the given labels.
|
2019-06-21 07:24:04 +00:00
|
|
|
// The expression must match any logical boolean combination of:
|
|
|
|
// - `Label(labelName, labelValue)`
|
|
|
|
// - `LabelRegex(labelName, regexValue)`
|
2020-05-11 10:06:07 +00:00
|
|
|
// - `MarathonConstraint(field:operator:value)`.
|
2019-11-29 16:16:05 +00:00
|
|
|
func MatchLabels(labels map[string]string, expr string) (bool, error) {
|
2019-06-21 07:24:04 +00:00
|
|
|
if expr == "" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := predicate.NewParser(predicate.Def{
|
|
|
|
Operators: predicate.Operators{
|
2019-11-29 16:16:05 +00:00
|
|
|
AND: andLabelFunc,
|
|
|
|
NOT: notLabelFunc,
|
|
|
|
OR: orLabelFunc,
|
2019-06-21 07:24:04 +00:00
|
|
|
},
|
|
|
|
Functions: map[string]interface{}{
|
|
|
|
"Label": labelFn,
|
|
|
|
"LabelRegex": labelRegexFn,
|
|
|
|
"MarathonConstraint": marathonFn,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parse, err := p.Parse(expr)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2019-11-29 16:16:05 +00:00
|
|
|
fn, ok := parse.(constraintLabelFunc)
|
2019-06-21 07:24:04 +00:00
|
|
|
if !ok {
|
2019-11-29 16:16:05 +00:00
|
|
|
return false, errors.New("not a constraintLabelFunc")
|
2019-06-21 07:24:04 +00:00
|
|
|
}
|
|
|
|
return fn(labels), nil
|
|
|
|
}
|
|
|
|
|
2019-11-29 16:16:05 +00:00
|
|
|
func labelFn(name, value string) constraintLabelFunc {
|
2019-06-21 07:24:04 +00:00
|
|
|
return func(labels map[string]string) bool {
|
|
|
|
return labels[name] == value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 16:16:05 +00:00
|
|
|
func labelRegexFn(name, expr string) constraintLabelFunc {
|
2019-06-21 07:24:04 +00:00
|
|
|
return func(labels map[string]string) bool {
|
|
|
|
matched, err := regexp.MatchString(expr, labels[name])
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return matched
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 16:16:05 +00:00
|
|
|
func marathonFn(value string) constraintLabelFunc {
|
2019-06-21 07:24:04 +00:00
|
|
|
return func(labels map[string]string) bool {
|
|
|
|
for k, v := range labels {
|
|
|
|
if strings.HasPrefix(k, MarathonConstraintPrefix) {
|
|
|
|
if v == value {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 16:16:05 +00:00
|
|
|
func andLabelFunc(a, b constraintLabelFunc) constraintLabelFunc {
|
2019-06-21 07:24:04 +00:00
|
|
|
return func(labels map[string]string) bool {
|
|
|
|
return a(labels) && b(labels)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 16:16:05 +00:00
|
|
|
func orLabelFunc(a, b constraintLabelFunc) constraintLabelFunc {
|
2019-06-21 07:24:04 +00:00
|
|
|
return func(labels map[string]string) bool {
|
|
|
|
return a(labels) || b(labels)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 16:16:05 +00:00
|
|
|
func notLabelFunc(a constraintLabelFunc) constraintLabelFunc {
|
2019-06-21 07:24:04 +00:00
|
|
|
return func(labels map[string]string) bool {
|
|
|
|
return !a(labels)
|
|
|
|
}
|
|
|
|
}
|