summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/agent/discovery/sd/pipeline/selector.go
blob: 8bb5fb061c92b50b8b50ee6fbd29ec27b0c3d23d (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: GPL-3.0-or-later

package pipeline

import (
	"errors"
	"fmt"
	"strings"

	"github.com/netdata/netdata/go/go.d.plugin/agent/discovery/sd/model"
)

type selector interface {
	matches(model.Tags) bool
}

type (
	exactSelector string
	trueSelector  struct{}
	negSelector   struct{ selector }
	orSelector    struct{ lhs, rhs selector }
	andSelector   struct{ lhs, rhs selector }
)

func (s exactSelector) matches(tags model.Tags) bool { _, ok := tags[string(s)]; return ok }
func (s trueSelector) matches(model.Tags) bool       { return true }
func (s negSelector) matches(tags model.Tags) bool   { return !s.selector.matches(tags) }
func (s orSelector) matches(tags model.Tags) bool    { return s.lhs.matches(tags) || s.rhs.matches(tags) }
func (s andSelector) matches(tags model.Tags) bool   { return s.lhs.matches(tags) && s.rhs.matches(tags) }

func (s exactSelector) String() string { return "{" + string(s) + "}" }
func (s negSelector) String() string   { return "{!" + stringify(s.selector) + "}" }
func (s trueSelector) String() string  { return "{*}" }
func (s orSelector) String() string    { return "{" + stringify(s.lhs) + "|" + stringify(s.rhs) + "}" }
func (s andSelector) String() string   { return "{" + stringify(s.lhs) + ", " + stringify(s.rhs) + "}" }
func stringify(sr selector) string     { return strings.Trim(fmt.Sprintf("%s", sr), "{}") }

func parseSelector(line string) (sr selector, err error) {
	words := strings.Fields(line)
	if len(words) == 0 {
		return trueSelector{}, nil
	}

	var srs []selector
	for _, word := range words {
		if idx := strings.IndexByte(word, '|'); idx > 0 {
			sr, err = parseOrSelectorWord(word)
		} else {
			sr, err = parseSingleSelectorWord(word)
		}
		if err != nil {
			return nil, fmt.Errorf("selector '%s' contains selector '%s' with forbidden symbol", line, word)
		}
		srs = append(srs, sr)
	}

	switch len(srs) {
	case 0:
		return trueSelector{}, nil
	case 1:
		return srs[0], nil
	default:
		return newAndSelector(srs[0], srs[1], srs[2:]...), nil
	}
}

func parseOrSelectorWord(orWord string) (sr selector, err error) {
	var srs []selector
	for _, word := range strings.Split(orWord, "|") {
		if sr, err = parseSingleSelectorWord(word); err != nil {
			return nil, err
		}
		srs = append(srs, sr)
	}
	switch len(srs) {
	case 0:
		return trueSelector{}, nil
	case 1:
		return srs[0], nil
	default:
		return newOrSelector(srs[0], srs[1], srs[2:]...), nil
	}
}

func parseSingleSelectorWord(word string) (selector, error) {
	if len(word) == 0 {
		return nil, errors.New("empty word")
	}
	neg := word[0] == '!'
	if neg {
		word = word[1:]
	}
	if len(word) == 0 {
		return nil, errors.New("empty word")
	}
	if word != "*" && !isSelectorWordValid(word) {
		return nil, errors.New("forbidden symbol")
	}

	var sr selector
	switch word {
	case "*":
		sr = trueSelector{}
	default:
		sr = exactSelector(word)
	}
	if neg {
		return negSelector{sr}, nil
	}
	return sr, nil
}

func newAndSelector(lhs, rhs selector, others ...selector) selector {
	m := andSelector{lhs: lhs, rhs: rhs}
	switch len(others) {
	case 0:
		return m
	default:
		return newAndSelector(m, others[0], others[1:]...)
	}
}

func newOrSelector(lhs, rhs selector, others ...selector) selector {
	m := orSelector{lhs: lhs, rhs: rhs}
	switch len(others) {
	case 0:
		return m
	default:
		return newOrSelector(m, others[0], others[1:]...)
	}
}

func isSelectorWordValid(word string) bool {
	// valid:
	// *
	// ^[a-zA-Z][a-zA-Z0-9=_.]*$
	if len(word) == 0 {
		return false
	}
	if word == "*" {
		return true
	}
	for i, b := range word {
		switch {
		case b >= 'a' && b <= 'z':
		case b >= 'A' && b <= 'Z':
		case b >= '0' && b <= '9' && i > 0:
		case (b == '=' || b == '_' || b == '.') && i > 0:
		default:
			return false
		}
	}
	return true
}