Skip to content

Commit 77c6143

Browse files
committed
add regex for filter
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent bc88e52 commit 77c6143

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

config/config.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"fmt"
5+
"regexp"
56
"strconv"
67
"strings"
78
"time"
@@ -62,6 +63,9 @@ type (
6263
MetricFilterConfig struct {
6364
Path string `yaml:"jsonPath" json:"jsonPath"`
6465
_path *jsonpath.JSONPath
66+
67+
Regex string `yaml:"regex"`
68+
_regex *regexp.Regexp
6569
}
6670

6771
MetricJsonPath string
@@ -128,11 +132,22 @@ func (m *ConfigMetrics) Compile() error {
128132
return fmt.Errorf(`jsonPath must be set for filters`)
129133
}
130134

135+
// compile jsonPath
131136
if path, err := compileJsonPath(filterConfig.Path); err == nil {
132137
filterConfig._path = path
133138
} else {
134139
return err
135140
}
141+
142+
// compile regex
143+
if filterConfig.Regex != "" {
144+
filterRegex, err := regexp.Compile(filterConfig.Regex)
145+
if err != nil {
146+
return err
147+
}
148+
149+
filterConfig._regex = filterRegex
150+
}
136151
}
137152

138153
// selector
@@ -280,6 +295,13 @@ func (m *ConfigMetrics) IsValidObject(object unstructured.Unstructured) bool {
280295
if value == "" {
281296
return false
282297
}
298+
299+
// check regexp
300+
if filterConfig._regex != nil {
301+
if !filterConfig._regex.MatchString(value) {
302+
return false
303+
}
304+
}
283305
} else {
284306
return false
285307
}

example.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ metrics:
5151
# optional filters, must return a value, otherwise the resource is filtered
5252
filters:
5353
- jsonPath: .metadata.annotations.expiry
54+
# filter value by regex, optional
55+
regex: ^([0-9]{4}-[0-9]{2}-[0-9]{2}.*|[0-9]+)$

0 commit comments

Comments
 (0)