Skip to content

Commit 961a80c

Browse files
committed
optimize config and refactoring
Signed-off-by: Markus Blaschke <[email protected]>
1 parent ef264a1 commit 961a80c

File tree

4 files changed

+216
-187
lines changed

4 files changed

+216
-187
lines changed

config/config.convert.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const (
1515
CONVERT_TRIM = "trim"
1616
)
1717

18-
func (m *MetricPathConfig) DoConvertValue(v string) (ret *float64) {
18+
func (m *ConfigMetricJsonPath) DoConvertValue(v string) (ret *float64) {
1919
if val, err := strconv.ParseFloat(v, 64); err == nil {
2020
ret = &val
2121
}
@@ -50,7 +50,7 @@ convertLoop:
5050
return
5151
}
5252

53-
func (m *MetricPathConfig) DoConvertLabel(val string) (ret string) {
53+
func (m *ConfigMetricJsonPath) DoConvertLabel(val string) (ret string) {
5454
ret = val
5555

5656
convertLoop:

config/config.go

+65-41
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,44 @@ const (
2020

2121
type (
2222
Config struct {
23-
Metrics []*ConfigMetrics `yaml:"metrics"`
23+
Resources []*ConfigResource `yaml:"resources"`
2424
}
2525

26-
ConfigMetrics struct {
27-
Metric *MetricConfig `yaml:"metric"`
28-
Resource schema.GroupVersionResource `yaml:"resource"`
26+
ConfigResource struct {
27+
*schema.GroupVersionResource
2928

3029
Selector *metav1.LabelSelector `yaml:"selector"`
3130
_selector string
3231

33-
Filters []*MetricFilterConfig `yaml:"filters"`
32+
Metrics []*ConfigMetric `yaml:"metrics"`
3433
}
3534

36-
MetricConfig struct {
35+
ConfigMetric struct {
3736
Name string `yaml:"name"`
3837
Help string `yaml:"help"`
39-
Value *MetricValueConfig `yaml:"value"`
40-
Labels map[string]*MetricLabelConfig `yaml:"labels"`
38+
Value *ConfigMetricValue `yaml:"value"`
39+
Labels map[string]*ConfigMetricLabel `yaml:"labels"`
40+
41+
Filters []*ConfigMetricFilter `yaml:"filters"`
4142
}
4243

43-
MetricValueConfig struct {
44-
*MetricPathConfig `yaml:",inline"`
45-
Value *float64 `yaml:"value"`
44+
ConfigMetricValue struct {
45+
*ConfigMetricJsonPath `yaml:",inline"`
46+
Value *float64 `yaml:"value"`
4647
}
4748

48-
MetricLabelConfig struct {
49-
*MetricPathConfig `yaml:",inline"`
50-
Value string `yaml:"value"`
49+
ConfigMetricLabel struct {
50+
*ConfigMetricJsonPath `yaml:",inline"`
51+
Value string `yaml:"value"`
5152
}
5253

53-
MetricPathConfig struct {
54+
ConfigMetricJsonPath struct {
5455
Path string `yaml:"jsonPath" json:"jsonPath"`
5556
_path *jsonpath.JSONPath
5657
Convert []*string `yaml:"convert"`
5758
}
5859

59-
MetricFilterConfig struct {
60+
ConfigMetricFilter struct {
6061
Path string `yaml:"jsonPath" json:"jsonPath"`
6162
_path *jsonpath.JSONPath
6263

@@ -91,8 +92,8 @@ var (
9192
)
9293

9394
func (m *Config) Compile() error {
94-
for _, metric := range m.Metrics {
95-
err := metric.Compile()
95+
for _, row := range m.Resources {
96+
err := row.Compile()
9697
if err != nil {
9798
return err
9899
}
@@ -101,19 +102,54 @@ func (m *Config) Compile() error {
101102
return nil
102103
}
103104

104-
func (m *ConfigMetrics) Compile() error {
105+
func (m *ConfigResource) Compile() error {
106+
if m.Version == "" {
107+
return fmt.Errorf("version is required")
108+
}
109+
110+
if m.Resource == "" {
111+
return fmt.Errorf("resource is required")
112+
}
113+
114+
// selector
115+
if m.Selector != nil {
116+
selector := metav1.FormatLabelSelector(m.Selector)
117+
if strings.EqualFold(selector, KUBE_SELECTOR_ERROR) {
118+
return fmt.Errorf(`unable to compile Kubernetes selector for resource "%s/%s/%s"`, m.Group, m.Version, m.Resource)
119+
}
120+
121+
if !strings.EqualFold(selector, KUBE_SELECTOR_NONE) {
122+
m._selector = selector
123+
}
124+
}
125+
126+
for _, row := range m.Metrics {
127+
err := row.Compile()
128+
if err != nil {
129+
return err
130+
}
131+
}
132+
133+
return nil
134+
}
135+
136+
func (m *ConfigMetric) Compile() error {
137+
if m.Name == "" {
138+
return fmt.Errorf("name is required")
139+
}
140+
105141
// value path
106-
if m.Metric.Value.MetricPathConfig != nil && m.Metric.Value.Path != "" {
107-
if path, err := compileJsonPath(m.Metric.Value.Path); err == nil {
108-
m.Metric.Value._path = path
142+
if m.Value.ConfigMetricJsonPath != nil && m.Value.Path != "" {
143+
if path, err := compileJsonPath(m.Value.Path); err == nil {
144+
m.Value._path = path
109145
} else {
110146
return err
111147
}
112148
}
113149

114150
// labels path
115-
for _, labelConfig := range m.Metric.Labels {
116-
if labelConfig.MetricPathConfig != nil && labelConfig.Path != "" {
151+
for _, labelConfig := range m.Labels {
152+
if labelConfig.ConfigMetricJsonPath != nil && labelConfig.Path != "" {
117153
if path, err := compileJsonPath(labelConfig.Path); err == nil {
118154
labelConfig._path = path
119155
} else {
@@ -146,22 +182,10 @@ func (m *ConfigMetrics) Compile() error {
146182
}
147183
}
148184

149-
// selector
150-
if m.Selector != nil {
151-
selector := metav1.FormatLabelSelector(m.Selector)
152-
if strings.EqualFold(selector, KUBE_SELECTOR_ERROR) {
153-
return fmt.Errorf(`unable to compile Kubernetes selector for metric "%s"`, m.Metric.Name)
154-
}
155-
156-
if !strings.EqualFold(selector, KUBE_SELECTOR_NONE) {
157-
m._selector = selector
158-
}
159-
}
160-
161185
return nil
162186
}
163187

164-
func (m *ConfigMetrics) KubeMetaListOptions() metav1.ListOptions {
188+
func (m *ConfigResource) KubeMetaListOptions() metav1.ListOptions {
165189
opts := metav1.ListOptions{}
166190
if m._selector != "" {
167191
opts.LabelSelector = m._selector
@@ -170,15 +194,15 @@ func (m *ConfigMetrics) KubeMetaListOptions() metav1.ListOptions {
170194
return opts
171195
}
172196

173-
func (m *MetricPathConfig) JsonPath() *jsonpath.JSONPath {
197+
func (m *ConfigMetricJsonPath) JsonPath() *jsonpath.JSONPath {
174198
if m == nil {
175199
return nil
176200
}
177201

178202
return m._path
179203
}
180204

181-
func (m *MetricPathConfig) ParseLabel(val interface{}) (ret string) {
205+
func (m *ConfigMetricJsonPath) ParseLabel(val interface{}) (ret string) {
182206
// convert type
183207
switch v := val.(type) {
184208
case float64:
@@ -198,7 +222,7 @@ func (m *MetricPathConfig) ParseLabel(val interface{}) (ret string) {
198222
return m.DoConvertLabel(ret)
199223
}
200224

201-
func (m *MetricPathConfig) ParseValue(val interface{}) (ret *float64) {
225+
func (m *ConfigMetricJsonPath) ParseValue(val interface{}) (ret *float64) {
202226
valueString := ""
203227
switch v := val.(type) {
204228
case float64:
@@ -218,7 +242,7 @@ func (m *MetricPathConfig) ParseValue(val interface{}) (ret *float64) {
218242
return m.DoConvertValue(valueString)
219243
}
220244

221-
func (m *ConfigMetrics) IsValidObject(object unstructured.Unstructured) bool {
245+
func (m *ConfigMetric) IsValidObject(object unstructured.Unstructured) bool {
222246
// no filters = is valid
223247
if len(m.Filters) == 0 {
224248
return true

example.yaml

+52-53
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,64 @@
1-
metrics:
2-
- metric:
3-
# metric name
4-
name: kube_secret_expiry
5-
# metric help description
6-
help: Secret Expiry
1+
resources:
2+
-
3+
group: ""
4+
version: v1
5+
resource: secrets
76

8-
# metric value config
9-
# if no value is found, metric will not be exported
10-
value:
11-
# default or static value, optional
12-
value: 1
7+
# optional selector, if empty all resources will be processed
8+
selector: {}
139

14-
# jsonPath for value extraction, must return only one value!
15-
jsonPath: .metadata.annotations.expiry
10+
metrics:
11+
# metric name
12+
- name: kube_secret_expiry
13+
# metric help description
14+
help: Secret Expiry
1615

17-
# value conversion:
18-
# timestamp: try to parse value as datetime and convert to unix timestamp
19-
convert: [toTimestamp]
16+
# metric value config
17+
# if no value is found, metric will not be exported
18+
value:
19+
# default or static value, optional
20+
value: 1
2021

21-
# metric labels
22-
labels:
22+
# jsonPath for value extraction, must return only one value!
23+
jsonPath: .metadata.annotations.expiry
2324

24-
# static label foo="bar"
25-
foo:
26-
value: bar
25+
# value conversion:
26+
# timestamp: try to parse value as datetime and convert to unix timestamp
27+
convert: [toTimestamp]
2728

28-
# plain value using jsonPath
29-
resourceVersion:
30-
jsonPath: .metadata.resourceVersion
29+
# metric labels
30+
labels:
3131

32-
# plain value with timestamp conversion (value will be a unix timestamp as string)
33-
resourceVersionTS:
34-
jsonPath: .metadata.resourceVersion
35-
# value conversions
36-
# toTimestamp: try to parse value as datetime and convert to unix timestamp
37-
# toDateTime: try to parse value as datetime and convert to RFC3399 date
38-
# toLower: lowercase value
39-
# toUpper: uppercase value
40-
# trim: trim whitespaces
41-
convert: [toTimestamp]
32+
# static label foo="bar"
33+
foo:
34+
value: bar
4235

43-
# plain value with timestamp conversion (value will be a RFC3399 timestamp as string)
44-
resourceVersionDT:
45-
jsonPath: .metadata.resourceVersion
46-
convert: [toDateTime]
36+
# plain value using jsonPath
37+
resourceVersion:
38+
jsonPath: .metadata.resourceVersion
4739

48-
managedBy:
49-
jsonPath: .metadata.labels.app\.kubernetes\.io\/managed-by
50-
convert: [ toLower ]
40+
# plain value with timestamp conversion (value will be a unix timestamp as string)
41+
resourceVersionTS:
42+
jsonPath: .metadata.resourceVersion
43+
# value conversions
44+
# toTimestamp: try to parse value as datetime and convert to unix timestamp
45+
# toDateTime: try to parse value as datetime and convert to RFC3399 date
46+
# toLower: lowercase value
47+
# toUpper: uppercase value
48+
# trim: trim whitespaces
49+
convert: [toTimestamp]
5150

52-
# Kubernetes resource as GroupVersionResource
53-
resource:
54-
group: ""
55-
version: v1
56-
resource: secrets
51+
# plain value with timestamp conversion (value will be a RFC3399 timestamp as string)
52+
resourceVersionDT:
53+
jsonPath: .metadata.resourceVersion
54+
convert: [toDateTime]
5755

58-
# optional selector, if empty all resources will be processed
59-
selector: {}
56+
managedBy:
57+
jsonPath: .metadata.labels.app\.kubernetes\.io\/managed-by
58+
convert: [ toLower ]
6059

61-
# optional filters, must return a value, otherwise the resource is filtered
62-
filters:
63-
- jsonPath: .metadata.annotations.expiry
64-
# filter value by regex, optional
65-
regex: ^([0-9]{4}-[0-9]{2}-[0-9]{2}.*|[0-9]+)$
60+
# optional filters, must return a value, otherwise the resource is filtered
61+
filters:
62+
- jsonPath: .metadata.annotations.expiry
63+
# filter value by regex, optional
64+
regex: ^([0-9]{4}-[0-9]{2}-[0-9]{2}.*|[0-9]+)$

0 commit comments

Comments
 (0)