Skip to content

Commit 195617d

Browse files
committed
replace yaml package
Signed-off-by: Markus Blaschke <[email protected]>
1 parent 5227398 commit 195617d

File tree

3 files changed

+53
-51
lines changed

3 files changed

+53
-51
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ require (
1919
github.com/robfig/cron v1.2.0
2020
go.uber.org/zap v1.27.0
2121
golang.org/x/text v0.24.0
22-
gopkg.in/yaml.v3 v3.0.1
2322
k8s.io/apimachinery v0.33.0
2423
k8s.io/client-go v0.33.0
24+
sigs.k8s.io/yaml v1.4.0
2525
)
2626

2727
require (
@@ -81,12 +81,12 @@ require (
8181
google.golang.org/protobuf v1.36.6 // indirect
8282
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
8383
gopkg.in/inf.v0 v0.9.1 // indirect
84+
gopkg.in/yaml.v3 v3.0.1 // indirect
8485
k8s.io/api v0.33.0 // indirect
8586
k8s.io/klog/v2 v2.130.1 // indirect
8687
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
8788
k8s.io/utils v0.0.0-20250321185631-1f6e0b77f77e // indirect
8889
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
8990
sigs.k8s.io/randfill v1.0.0 // indirect
9091
sigs.k8s.io/structured-merge-diff/v4 v4.7.0 // indirect
91-
sigs.k8s.io/yaml v1.4.0 // indirect
9292
)

prometheus/kusto/config.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package kusto
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"os"
78
"regexp"
89
"strings"
910

10-
yaml "gopkg.in/yaml.v3"
11+
"sigs.k8s.io/yaml"
1112
)
1213

1314
const (
@@ -28,50 +29,50 @@ const (
2829

2930
type (
3031
Config struct {
31-
Queries []ConfigQuery `yaml:"queries"`
32+
Queries []ConfigQuery `json:"queries"`
3233
}
3334

3435
ConfigQuery struct {
35-
MetricConfig ConfigQueryMetric `yaml:",inline"`
36-
QueryMode string `yaml:"queryMode"`
37-
Workspaces *[]string `yaml:"workspaces"`
38-
Metric string `yaml:"metric"`
39-
Module string `yaml:"module"`
40-
Query string `yaml:"query"`
41-
Timespan *string `yaml:"timespan"`
42-
Subscriptions *[]string `yaml:"subscriptions"`
36+
*ConfigQueryMetric
37+
QueryMode string `json:"queryMode"`
38+
Workspaces *[]string `json:"workspaces"`
39+
Metric string `json:"metric"`
40+
Module string `json:"module"`
41+
Query string `json:"query"`
42+
Timespan *string `json:"timespan"`
43+
Subscriptions *[]string `json:"subscriptions"`
4344
}
4445

4546
ConfigQueryMetric struct {
46-
Value *float64 `yaml:"value"`
47-
Fields []ConfigQueryMetricField `yaml:"fields"`
48-
Labels map[string]string `yaml:"labels"`
49-
DefaultField ConfigQueryMetricField `yaml:"defaultField"`
50-
Publish *bool `yaml:"publish"`
47+
Value *float64 `json:"value"`
48+
Fields []ConfigQueryMetricField `json:"fields"`
49+
Labels map[string]string `json:"labels"`
50+
DefaultField ConfigQueryMetricField `json:"defaultField"`
51+
Publish *bool `json:"publish"`
5152
}
5253

5354
ConfigQueryMetricField struct {
54-
Name string `yaml:"name"`
55-
Metric string `yaml:"metric"`
56-
Source string `yaml:"source"`
57-
Target string `yaml:"target"`
58-
Type string `yaml:"type"`
59-
Labels map[string]string `yaml:"labels"`
60-
Filters []ConfigQueryMetricFieldFilter `yaml:"filters"`
61-
Expand *ConfigQueryMetric `yaml:"expand"`
55+
Name string `json:"name"`
56+
Metric string `json:"metric"`
57+
Source string `json:"source"`
58+
Target string `json:"target"`
59+
Type string `json:"type"`
60+
Labels map[string]string `json:"labels"`
61+
Filters []ConfigQueryMetricFieldFilter `json:"filters"`
62+
Expand *ConfigQueryMetric `json:"expand"`
6263
}
6364

6465
ConfigQueryMetricFieldFilter struct {
65-
Type string `yaml:"type"`
66-
RegExp string `yaml:"regexp"`
67-
Replacement string `yaml:"replacement"`
66+
Type string `json:"type"`
67+
RegExp string `json:"regexp"`
68+
Replacement string `json:"replacement"`
6869
parsedRegexp *regexp.Regexp
6970
}
7071

7172
ConfigQueryMetricFieldFilterParser struct {
72-
Type string `yaml:"type"`
73-
RegExp string `yaml:"regexp"`
74-
Replacement string `yaml:"replacement"`
73+
Type string `json:"type"`
74+
RegExp string `json:"regexp"`
75+
Replacement string `json:"replacement"`
7576
}
7677
)
7778

@@ -90,7 +91,7 @@ func (c *Config) Validate() error {
9091
}
9192

9293
func (c *ConfigQuery) Validate() error {
93-
if err := c.MetricConfig.Validate(); err != nil {
94+
if err := c.ConfigQueryMetric.Validate(); err != nil {
9495
return err
9596
}
9697

@@ -297,12 +298,12 @@ func (f *ConfigQueryMetricField) TransformBool(value bool) (ret string) {
297298
return
298299
}
299300

300-
func (f *ConfigQueryMetricFieldFilter) UnmarshalYAML(unmarshal func(interface{}) error) error {
301+
func (f *ConfigQueryMetricFieldFilter) UnmarshalJSON(c []byte) error {
301302
var multi ConfigQueryMetricFieldFilterParser
302-
err := unmarshal(&multi)
303+
err := json.Unmarshal(c, &multi)
303304
if err != nil {
304305
var single string
305-
err := unmarshal(&single)
306+
err := json.Unmarshal(c, &single)
306307
if err != nil {
307308
return err
308309
}

prometheus/kusto/parse_test.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package kusto
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"testing"
67

78
"github.com/prometheus/client_golang/prometheus"
8-
"gopkg.in/yaml.v3"
9+
"sigs.k8s.io/yaml"
910
)
1011

1112
type (
@@ -64,7 +65,7 @@ defaultField:
6465
type: ignore
6566
`)
6667

67-
metricList := BuildPrometheusMetricList(queryConfig.Metric, queryConfig.MetricConfig, resultRow)
68+
metricList := BuildPrometheusMetricList(queryConfig.Metric, *queryConfig.ConfigQueryMetric, resultRow)
6869

6970
metricTestSuite := testingMetricResult{t: t, list: metricList}
7071
metricTestSuite.assertMetricNames(2)
@@ -128,7 +129,7 @@ defaultField:
128129
type: ignore
129130
`)
130131

131-
metricList := BuildPrometheusMetricList(queryConfig.Metric, queryConfig.MetricConfig, resultRow)
132+
metricList := BuildPrometheusMetricList(queryConfig.Metric, *queryConfig.ConfigQueryMetric, resultRow)
132133

133134
metricTestSuite := testingMetricResult{t: t, list: metricList}
134135
metricTestSuite.assertMetricNames(1)
@@ -183,7 +184,7 @@ defaultField:
183184
type: ignore
184185
`)
185186

186-
metricList := BuildPrometheusMetricList(queryConfig.Metric, queryConfig.MetricConfig, resultRow)
187+
metricList := BuildPrometheusMetricList(queryConfig.Metric, *queryConfig.ConfigQueryMetric, resultRow)
187188

188189
metricTestSuite := testingMetricResult{t: t, list: metricList}
189190
metricTestSuite.assertMetricNames(2)
@@ -274,12 +275,6 @@ func TestResourceGraphArmResourceParsing(t *testing.T) {
274275
}`)
275276

276277
queryConfig := parseMetricConfig(t, `
277-
tagFields: &tagFields
278-
- name: owner
279-
- name: domain
280-
tagDefaultField: &defaultTagField
281-
type: ignore
282-
283278
metric: azurerm_managedclusters_aks_info
284279
query: |-
285280
Resources
@@ -312,8 +307,11 @@ fields:
312307
metric: azurerm_managedclusters_tags
313308
expand:
314309
value: 1
315-
fields: *tagFields
316-
defaultField: *defaultTagField
310+
fields: &tagFields
311+
- name: owner
312+
- name: domain
313+
defaultField: &defaultTagField
314+
type: ignore
317315
-
318316
name: agentPoolProfiles
319317
metric: azurerm_managedclusters_aks_pool
@@ -363,7 +361,10 @@ defaultField:
363361
type: ignore
364362
`)
365363

366-
metricList := BuildPrometheusMetricList(queryConfig.Metric, queryConfig.MetricConfig, resultRow)
364+
foo, _ := json.Marshal(queryConfig)
365+
fmt.Println(string(foo))
366+
367+
metricList := BuildPrometheusMetricList(queryConfig.Metric, *queryConfig.ConfigQueryMetric, resultRow)
367368

368369
metricTestSuite := testingMetricResult{t: t, list: metricList}
369370
metricTestSuite.assertMetricNames(8)
@@ -471,7 +472,7 @@ defaultField:
471472
type: ignore
472473
`)
473474

474-
metricList := BuildPrometheusMetricList(queryConfig.Metric, queryConfig.MetricConfig, resultRow)
475+
metricList := BuildPrometheusMetricList(queryConfig.Metric, *queryConfig.ConfigQueryMetric, resultRow)
475476

476477
metricTestSuite := testingMetricResult{t: t, list: metricList}
477478
metricTestSuite.assertMetricNames(1)
@@ -531,7 +532,7 @@ defaultField:
531532
type: ignore
532533
`)
533534

534-
metricList := BuildPrometheusMetricList(queryConfig.Metric, queryConfig.MetricConfig, resultRow)
535+
metricList := BuildPrometheusMetricList(queryConfig.Metric, *queryConfig.ConfigQueryMetric, resultRow)
535536

536537
metricTestSuite := testingMetricResult{t: t, list: metricList}
537538
metricTestSuite.assertMetricNames(2)
@@ -573,7 +574,7 @@ func parseResourceGraphJsonToResultRow(t *testing.T, data string) map[string]int
573574
func parseMetricConfig(t *testing.T, data string) ConfigQuery {
574575
t.Helper()
575576
ret := ConfigQuery{}
576-
if err := yaml.Unmarshal([]byte(data), &ret); err != nil {
577+
if err := yaml.UnmarshalStrict([]byte(data), &ret); err != nil {
577578
t.Fatalf(`unable to unmarshal query configuration yaml: %v`, err)
578579
}
579580
return ret

0 commit comments

Comments
 (0)