forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
184 lines (168 loc) · 5.05 KB
/
config.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package docs
import (
"fmt"
"regexp"
"sort"
"strings"
"github.com/Jeffail/gabs/v2"
)
const labelExpression = `^[a-z0-9_]+$`
var (
labelRe = regexp.MustCompile(labelExpression)
// ErrBadLabel is returned when creating a component with a bad label.
ErrBadLabel = fmt.Errorf("should match the regular expression /%v/ and must not start with an underscore", labelExpression)
)
// ValidateLabel attempts to validate the contents of a component label.
func ValidateLabel(label string) error {
if strings.HasPrefix(label, "_") {
return ErrBadLabel
}
if !labelRe.MatchString(label) {
return ErrBadLabel
}
return nil
}
var labelField = FieldString(
"label", "An optional label to use as an identifier for observability data such as metrics and logging.",
).OmitWhen(func(field, parent any) (string, bool) {
gObj := gabs.Wrap(parent)
if typeStr, exists := gObj.S("type").Data().(string); exists && typeStr == "resource" {
return "label field should be omitted when pointing to a resource", true
}
if resourceStr, exists := gObj.S("resource").Data().(string); exists && resourceStr != "" {
return "label field should be omitted when pointing to a resource", true
}
return "", false
}).AtVersion("3.44.0").LinterFunc(func(ctx LintContext, line, col int, v any) []Lint {
l, _ := v.(string)
if l == "" {
return nil
}
if err := ValidateLabel(l); err != nil {
return []Lint{
NewLintError(line, LintBadLabel, fmt.Sprintf("Invalid label '%v': %v", l, err)),
}
}
prevLine, exists := ctx.LabelsToLine[l]
if exists {
return []Lint{
NewLintError(line, LintDuplicateLabel, fmt.Sprintf("Label '%v' collides with a previously defined label at line %v", l, prevLine)),
}
}
ctx.LabelsToLine[l] = line
return nil
}).HasDefault("")
// ReservedFieldsByType returns a map of fields for a specific type.
func ReservedFieldsByType(t Type) map[string]FieldSpec {
m := map[string]FieldSpec{
"type": FieldString("type", ""),
"plugin": FieldObject("plugin", ""),
}
if t == TypeInput || t == TypeOutput {
m["processors"] = FieldProcessor("processors", "").Array().OmitWhen(func(field, _ any) (string, bool) {
if arr, ok := field.([]any); ok && len(arr) == 0 {
return "field processors is empty and can be removed", true
}
return "", false
})
}
if t == TypeMetrics {
m["mapping"] = MetricsMappingFieldSpec("mapping")
}
if _, isLabelType := map[Type]struct{}{
TypeInput: {},
TypeProcessor: {},
TypeOutput: {},
TypeCache: {},
TypeRateLimit: {},
}[t]; isLabelType {
m["label"] = labelField
}
return m
}
func defaultTypeByType(docProvider Provider, t Type) string {
switch t {
case TypeBuffer:
return "none"
case TypeInput:
return "stdin"
case TypeMetrics:
// If prometheus isn't imported then fall back to none
if _, exists := docProvider.GetDocs("prometheus", TypeMetrics); exists {
return "prometheus"
}
return "none"
case TypeOutput:
return "stdout"
case TypeTracer:
return "none"
// No defaults for the following
case TypeCache:
return ""
case TypeProcessor:
return ""
case TypeRateLimit:
return ""
}
return ""
}
// DefaultTypeOf returns the standard default implementation of a given
// component type, which is the implementation used in a stream when no config
// for the component is present. Only some component types have a default, for
// those that do not an empty string is returned.
func DefaultTypeOf(t Type) string {
return defaultTypeByType(DeprecatedProvider, t)
}
func getInferenceCandidateFromList(docProvider Provider, t Type, l []string) (string, ComponentSpec, error) {
ignore := ReservedFieldsByType(t)
var candidates []string
var inferred string
var inferredSpec ComponentSpec
for _, k := range l {
if _, exists := ignore[k]; exists {
continue
}
candidates = append(candidates, k)
if spec, exists := docProvider.GetDocs(k, t); exists {
if len(inferred) > 0 {
candidates = []string{inferred, k}
sort.Strings(candidates)
return "", ComponentSpec{}, fmt.Errorf(
"unable to infer %v type, multiple candidates '%v' and '%v'", string(t), candidates[0], candidates[1],
)
}
inferred = k
inferredSpec = spec
}
}
if len(candidates) == 0 {
defaultType := defaultTypeByType(docProvider, t)
if spec, exists := docProvider.GetDocs(defaultType, t); exists {
return defaultType, spec, nil
}
if inferred == "" {
return "", ComponentSpec{}, fmt.Errorf("an explicit %v type must be specified", string(t))
}
}
if inferred == "" {
sort.Strings(candidates)
return "", ComponentSpec{}, fmt.Errorf("unable to infer %v type from candidates: %v", string(t), candidates)
}
return inferred, inferredSpec, nil
}
// SanitiseConfig contains fields describing the desired behaviour of the config
// sanitiser such as removing certain fields.
type SanitiseConfig struct {
RemoveTypeField bool
RemoveDeprecated bool
ScrubSecrets bool
ForExample bool
Filter FieldFilter
DocsProvider Provider
}
// NewSanitiseConfig creates a new sanitise config.
func NewSanitiseConfig() SanitiseConfig {
return SanitiseConfig{
DocsProvider: DeprecatedProvider,
}
}