@@ -21,16 +21,16 @@ import (
21
21
"fmt"
22
22
"io/fs"
23
23
"os"
24
- "strings "
24
+ "reflect "
25
25
"time"
26
26
27
- "dario.cat/mergo"
28
27
"github.com/go-logr/logr"
29
- yaml "github.com/goccy/go-yaml "
28
+ "github.com/mitchellh/mapstructure "
30
29
"github.com/prometheus/common/model"
31
30
promconfig "github.com/prometheus/prometheus/config"
32
31
_ "github.com/prometheus/prometheus/discovery/install"
33
32
"github.com/spf13/pflag"
33
+ "gopkg.in/yaml.v2"
34
34
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
35
35
"k8s.io/client-go/rest"
36
36
"k8s.io/client-go/tools/clientcmd"
@@ -41,7 +41,7 @@ import (
41
41
42
42
const (
43
43
DefaultResyncTime = 5 * time .Minute
44
- DefaultConfigFilePath string = "/conf/targetallocator.yaml"
44
+ DefaultConfigFilePath string = "../.. /conf/targetallocator.yaml"
45
45
DefaultCRScrapeInterval model.Duration = model .Duration (time .Second * 30 )
46
46
DefaultAllocationStrategy = "consistent-hashing"
47
47
DefaultFilterStrategy = "relabel-config"
@@ -77,11 +77,6 @@ type HTTPSServerConfig struct {
77
77
TLSKeyFilePath string `yaml:"tls_key_file_path,omitempty"`
78
78
}
79
79
80
- type LabeLSelectorPaths struct {
81
- MatchLabelsPath string
82
- MatchExpressionsPath string
83
- }
84
-
85
80
func LoadFromFile (file string , target * Config ) error {
86
81
return unmarshal (target , file )
87
82
}
@@ -155,92 +150,55 @@ func LoadFromCLI(target *Config, flagSet *pflag.FlagSet) error {
155
150
return nil
156
151
}
157
152
158
- // readPath extracts value from specific YAMLPath.
159
- func readPath (p string , yamlFile []byte , f interface {}) error {
160
- path , err := yaml .PathString (p )
161
- if err != nil {
162
- return err
163
- }
164
- if err := path .Read (strings .NewReader (string (yamlFile )), f ); err != nil {
165
- return err
166
- }
167
- return nil
168
- }
169
-
170
- func readLabelSelectorPaths (ps LabeLSelectorPaths , yamlFile []byte , f * metav1.LabelSelector ) (bool , error ) {
171
- founderrs := 0
172
-
173
- if err := readPath (ps .MatchLabelsPath , yamlFile , & f .MatchLabels ); err != nil {
174
- if ! errors .Is (err , yaml .ErrNotFoundNode ) {
175
- return false , err
153
+ func StringToModelDurationHookFunc () mapstructure.DecodeHookFunc {
154
+ return func (
155
+ f reflect.Type ,
156
+ t reflect.Type ,
157
+ data interface {},
158
+ ) (interface {}, error ) {
159
+ if f .Kind () != reflect .String {
160
+ return data , nil
176
161
}
177
- founderrs += 1
178
- }
179
- if err := readPath (ps .MatchExpressionsPath , yamlFile , & f .MatchExpressions ); err != nil {
180
- if ! errors .Is (err , yaml .ErrNotFoundNode ) {
181
- return false , err
182
- }
183
- founderrs += 1
184
- }
185
-
186
- return founderrs < 2 , nil
187
- }
188
-
189
- func flexibleLabelSelector (yamlFile []byte , fieldPathsMap map [* metav1.LabelSelector ]LabeLSelectorPaths ) error {
190
- for f , ps := range fieldPathsMap {
191
- tmpls := metav1.LabelSelector {}
192
-
193
- found , err := readLabelSelectorPaths (ps , yamlFile , & tmpls )
194
- if err != nil {
195
- return err
162
+ if t != reflect .TypeOf (model .Duration (5 )) {
163
+ return data , nil
196
164
}
197
165
198
- if found {
199
- if err := mergo .Merge (f , tmpls , mergo .WithOverride ); err != nil {
200
- return err
201
- }
202
- }
166
+ // Convert it by parsing
167
+ return time .ParseDuration (data .(string ))
203
168
}
204
- return nil
205
169
}
206
170
207
- func flexibleCollectorSelector (yamlFile []byte , cfg * Config ) error {
208
- collectorSelectorFieldPathsMap := map [* metav1.LabelSelector ]LabeLSelectorPaths {
209
- cfg .CollectorSelector : {
210
- MatchLabelsPath : "$.collector_selector.matchlabels" ,
211
- MatchExpressionsPath : "$.collector_selector.matchexpressions" ,
212
- },
171
+ func decodeSubConfig (t interface {}, dc mapstructure.DecoderConfig ) error {
172
+ dec , decError := mapstructure .NewDecoder (& dc )
173
+ if decError != nil {
174
+ return decError
213
175
}
214
-
215
- if err := flexibleLabelSelector (yamlFile , collectorSelectorFieldPathsMap ); err != nil {
176
+ if err := dec .Decode (t ); err != nil {
216
177
return err
217
178
}
218
179
return nil
219
180
}
220
181
221
- func flexiblePrometheusCR (yamlFile []byte , cfg * Config ) error {
222
- prometheusCRFieldPathsMap := map [* metav1.LabelSelector ]LabeLSelectorPaths {
223
- cfg .PrometheusCR .PodMonitorSelector : {
224
- MatchLabelsPath : "$.prometheus_cr.pod_monitor_selector.matchlabels" ,
225
- MatchExpressionsPath : "$.prometheus_cr.pod_monitor_selector.matchexpressions" ,
226
- },
227
- cfg .PrometheusCR .ServiceMonitorSelector : {
228
- MatchLabelsPath : "$.prometheus_cr.service_monitor_selector.matchlabels" ,
229
- MatchExpressionsPath : "$.prometheus_cr.service_monitor_selector.matchexpressions" ,
230
- },
231
- cfg .PrometheusCR .ServiceMonitorNamespaceSelector : {
232
- MatchLabelsPath : "$.prometheus_cr.service_monitor_namespace_selector.matchlabels" ,
233
- MatchExpressionsPath : "$.prometheus_cr.service_monitor_namespace_selector.matchexpressions" ,
234
- },
235
- cfg .PrometheusCR .PodMonitorNamespaceSelector : {
236
- MatchLabelsPath : "$.prometheus_cr.pod_monitor_namespace_selector.matchlabels" ,
237
- MatchExpressionsPath : "$.prometheus_cr.pod_monitor_namespace_selector.matchexpressions" ,
238
- },
182
+ func flexibleUnmarshal (yamlFile []byte , cfg * Config ) error {
183
+ t := make (map [string ]interface {})
184
+ if err := yaml .Unmarshal (yamlFile , & t ); err != nil {
185
+ return fmt .Errorf ("error unmarshaling YAML: %w" , err )
239
186
}
240
187
241
- if err := flexibleLabelSelector (yamlFile , prometheusCRFieldPathsMap ); err != nil {
242
- return err
188
+ if t ["collector_selector" ] != nil {
189
+ dc := mapstructure.DecoderConfig {TagName : "yaml" , Result : cfg .CollectorSelector }
190
+ if err := decodeSubConfig (t ["collector_selector" ], dc ); err != nil {
191
+ return err
192
+ }
193
+ }
194
+
195
+ if t ["prometheus_cr" ] != nil {
196
+ dc := mapstructure.DecoderConfig {TagName : "yaml" , Result : & cfg .PrometheusCR , DecodeHook : StringToModelDurationHookFunc ()}
197
+ if err := decodeSubConfig (t ["prometheus_cr" ], dc ); err != nil {
198
+ return err
199
+ }
243
200
}
201
+
244
202
return nil
245
203
}
246
204
@@ -249,16 +207,11 @@ func unmarshal(cfg *Config, configFile string) error {
249
207
if err != nil {
250
208
return err
251
209
}
252
-
253
210
if err = yaml .Unmarshal (yamlFile , cfg ); err != nil {
254
211
return fmt .Errorf ("error unmarshaling YAML: %w" , err )
255
212
}
256
213
257
- if err := flexibleCollectorSelector (yamlFile , cfg ); err != nil {
258
- return err
259
- }
260
-
261
- if err := flexiblePrometheusCR (yamlFile , cfg ); err != nil {
214
+ if err := flexibleUnmarshal (yamlFile , cfg ); err != nil {
262
215
return err
263
216
}
264
217
0 commit comments