Skip to content

Commit 988efd6

Browse files
committed
replace yaml package
Signed-off-by: Markus Blaschke <[email protected]>
1 parent 2d58e7d commit 988efd6

File tree

10 files changed

+83
-69
lines changed

10 files changed

+83
-69
lines changed

config/config.go

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,47 @@ package config
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"time"
67
)
78

89
type (
910
Config struct {
10-
Azure Azure `yaml:"azure"`
11+
Azure Azure `json:"azure"`
1112
Collectors struct {
12-
General CollectorBase `yaml:"general"`
13-
Resource CollectorBase `yaml:"resource"`
14-
Quota CollectorBase `yaml:"quota"`
15-
Defender CollectorBase `yaml:"defender"`
16-
ResourceHealth CollectorResourceHealth `yaml:"resourceHealth"`
17-
Iam CollectorBase `yaml:"iam"`
18-
Graph CollectorGraph `yaml:"graph"`
19-
Costs CollectorCosts `yaml:"costs"`
20-
Budgets CollectorBudgets `yaml:"budgets"`
21-
Reservation CollectorReservation `yaml:"reservation"`
22-
Portscan CollectorPortscan `yaml:"portscan"`
23-
} `yaml:"collectors"`
13+
General CollectorBase `json:"general"`
14+
Resource CollectorBase `json:"resource"`
15+
Quota CollectorBase `json:"quota"`
16+
Defender CollectorBase `json:"defender"`
17+
ResourceHealth CollectorResourceHealth `json:"resourceHealth"`
18+
Iam CollectorBase `json:"iam"`
19+
Graph CollectorGraph `json:"graph"`
20+
Costs CollectorCosts `json:"costs"`
21+
Budgets CollectorBudgets `json:"budgets"`
22+
Reservation CollectorReservation `json:"reservation"`
23+
Portscan CollectorPortscan `json:"portscan"`
24+
} `json:"collectors"`
2425
}
2526

2627
Azure struct {
27-
Subscriptions []string `yaml:"subscriptions"`
28-
Locations []string `yaml:"locations"`
28+
Subscriptions []string `json:"subscriptions"`
29+
Locations []string `json:"locations"`
2930

30-
ResourceTags []string `yaml:"resourceTags"`
31-
ResourceGroupTags []string `yaml:"resourceGroupTags"`
31+
ResourceTags []string `json:"resourceTags"`
32+
ResourceGroupTags []string `json:"resourceGroupTags"`
3233
}
3334

3435
CollectorBase struct {
35-
ScrapeTime *time.Duration `yaml:"scrapeTime"`
36+
ScrapeTime *time.Duration `json:"scrapeTime"`
3637
// Cron *string
3738
}
3839
)
3940

4041
func (c *CollectorBase) IsEnabled() bool {
42+
if c == nil {
43+
return false
44+
}
45+
4146
return c.ScrapeTime != nil && c.ScrapeTime.Seconds() > 0
4247
}
4348

@@ -48,3 +53,22 @@ func (c *Config) GetJson() []byte {
4853
}
4954
return jsonBytes
5055
}
56+
57+
func (c *CollectorBase) UnmarshalJSON(b []byte) error {
58+
aux := &struct {
59+
ScrapeTime *string `json:"scrapeTime"`
60+
}{}
61+
if err := json.Unmarshal(b, &aux); err != nil {
62+
return err
63+
}
64+
65+
if aux.ScrapeTime != nil {
66+
scrapeTime, err := time.ParseDuration(*aux.ScrapeTime)
67+
if err != nil {
68+
return fmt.Errorf(`unable to parse "%s" as time.Duration: %w`, *aux.ScrapeTime, err)
69+
}
70+
c.ScrapeTime = &scrapeTime
71+
}
72+
73+
return nil
74+
}

config/config_budget.go

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

33
type (
44
CollectorBudgets struct {
5-
CollectorBase `yaml:",inline"`
5+
*CollectorBase
66

7-
Scopes []string `yaml:"scopes"`
7+
Scopes []string `json:"scopes"`
88
}
99
)

config/config_cost.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,33 @@ import (
88

99
type (
1010
CollectorCosts struct {
11-
CollectorBase `yaml:",inline"`
11+
*CollectorBase
1212

13-
RequestDelay time.Duration `yaml:"requestDelay"`
13+
RequestDelay time.Duration `json:"requestDelay"`
1414

15-
Queries []CollectorCostsQuery `yaml:"queries"`
15+
Queries []CollectorCostsQuery `json:"queries"`
1616
}
1717

1818
CollectorCostsQuery struct {
19-
Name string `yaml:"name"`
20-
Help *string `yaml:"help"`
21-
Scopes *[]string `yaml:"scopes"`
22-
Subscriptions *[]string `yaml:"subscriptions"`
23-
TimeFrames []string `yaml:"timeFrames"`
24-
Dimensions []string `yaml:"dimensions"`
25-
ExportType string `yaml:"exportType"`
26-
Granularity string `yaml:"granularity"`
27-
ValueField string `yaml:"valueField"`
28-
Labels map[string]string `yaml:"labels"`
29-
TimePeriod *CollectorCostsQueryTimePeriod `yaml:"timePeriod"`
19+
Name string `json:"name"`
20+
Help *string `json:"help"`
21+
Scopes *[]string `json:"scopes"`
22+
Subscriptions *[]string `json:"subscriptions"`
23+
TimeFrames []string `json:"timeFrames"`
24+
Dimensions []string `json:"dimensions"`
25+
ExportType string `json:"exportType"`
26+
Granularity string `json:"granularity"`
27+
ValueField string `json:"valueField"`
28+
Labels map[string]string `json:"labels"`
29+
TimePeriod *CollectorCostsQueryTimePeriod `json:"timePeriod"`
3030

3131
config *configCollectorCostsQueryConfig
3232
}
3333
CollectorCostsQueryTimePeriod struct {
34-
From *time.Time `yaml:"from"`
35-
FromDuration *time.Duration `yaml:"fromDuration"`
36-
To *time.Time `yaml:"to"`
37-
ToDuration *time.Duration `yaml:"toDuration"`
34+
From *time.Time `json:"from"`
35+
FromDuration *time.Duration `json:"fromDuration"`
36+
To *time.Time `json:"to"`
37+
ToDuration *time.Duration `json:"toDuration"`
3838
}
3939

4040
configCollectorCostsQueryConfig struct {

config/config_graph.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package config
22

33
type (
44
CollectorGraph struct {
5-
CollectorBase `yaml:",inline"`
5+
*CollectorBase
66

77
Filter struct {
8-
Application *string `yaml:"application"`
9-
ServicePrincipal *string `yaml:"servicePrincipal"`
10-
} `yaml:"filter"`
8+
Application *string `json:"application"`
9+
ServicePrincipal *string `json:"servicePrincipal"`
10+
} `json:"filter"`
1111
}
1212
)

config/config_portscan.go

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

33
type (
44
CollectorPortscan struct {
5-
CollectorBase `yaml:",inline"`
5+
*CollectorBase
66

77
Scanner struct {
8-
Parallel int `yaml:"parallel"`
9-
Threads int `yaml:"threads"`
10-
Timeout int `yaml:"timeout"`
8+
Parallel int `json:"parallel"`
9+
Threads int `json:"threads"`
10+
Timeout int `json:"timeout"`
1111

12-
Ports []string `yaml:"ports"`
13-
} `yaml:"scanner"`
12+
Ports []string `json:"ports"`
13+
} `json:"scanner"`
1414
}
1515
)

config/config_reservation.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package config
22

33
type (
44
CollectorReservation struct {
5-
CollectorBase `yaml:",inline"`
5+
*CollectorBase
66

7-
Scopes []string `yaml:"scopes"`
8-
Granularity string `yaml:"granularity"`
9-
FromDays int `yaml:"fromDays"`
7+
Scopes []string `json:"scopes"`
8+
Granularity string `json:"granularity"`
9+
FromDays int `json:"fromDays"`
1010
}
1111
)

config/config_resourcehealth.go

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

33
type (
44
CollectorResourceHealth struct {
5-
CollectorBase `yaml:",inline"`
5+
*CollectorBase
66

7-
SummaryMaxLength int `yaml:"summaryMaxLength"`
7+
SummaryMaxLength int `json:"summaryMaxLength"`
88
}
99
)

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,9 @@ require (
9191
k8s.io/utils v0.0.0-20250321185631-1f6e0b77f77e // indirect
9292
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
9393
sigs.k8s.io/structured-merge-diff/v4 v4.7.0 // indirect
94-
sigs.k8s.io/yaml v1.4.0 // indirect
94+
sigs.k8s.io/yaml v1.4.0
9595
)
9696

97-
require gopkg.in/yaml.v2 v2.4.0
98-
9997
require (
10098
github.com/KimMachineGun/automemlimit v0.7.1 // indirect
10199
github.com/dustin/go-humanize v1.0.1 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ gopkg.in/evanphx/json-patch.v4 v4.12.0 h1:n6jtcsulIzXPJaxegRbvFNNrZDjbij7ny3gmSP
256256
gopkg.in/evanphx/json-patch.v4 v4.12.0/go.mod h1:p8EYWUEYMpynmqDbY58zCKCFZw8pRWMG4EsWvDvM72M=
257257
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
258258
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
259-
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
260-
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
261259
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
262260
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
263261
k8s.io/api v0.33.0 h1:yTgZVn1XEe6opVpP1FylmNrIFWuDqe2H0V8CT5gxfIU=

main.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

33
import (
4-
"bufio"
5-
"bytes"
64
_ "embed"
75
"errors"
86
"fmt"
@@ -12,7 +10,7 @@ import (
1210
"runtime"
1311
"time"
1412

15-
"gopkg.in/yaml.v2"
13+
"sigs.k8s.io/yaml"
1614

1715
"github.com/webdevops/azure-resourcemanager-exporter/config"
1816

@@ -103,23 +101,19 @@ func initArgparser() {
103101
func initConfig() {
104102
var err error
105103

106-
decoder := yaml.NewDecoder(bytes.NewReader(defaultConfig))
107-
decoder.SetStrict(true)
108-
err = decoder.Decode(&Config)
104+
err = yaml.UnmarshalStrict(defaultConfig, &Config)
109105
if err != nil {
110106
logger.Fatal(err.Error())
111107
}
112108

113109
logger.Infof(`reading config from "%v"`, Opts.Config)
114110
/* #nosec */
115-
file, err := os.Open(Opts.Config)
111+
content, err := os.ReadFile(Opts.Config)
116112
if err != nil {
117113
logger.Fatal(err.Error())
118114
}
119115

120-
decoder = yaml.NewDecoder(bufio.NewReader(file))
121-
decoder.SetStrict(true)
122-
err = decoder.Decode(&Config)
116+
err = yaml.UnmarshalStrict(content, &Config)
123117
if err != nil {
124118
logger.Fatal(err.Error())
125119
}

0 commit comments

Comments
 (0)