@@ -21,14 +21,16 @@ import (
21
21
"fmt"
22
22
"io/fs"
23
23
"os"
24
+ "strings"
24
25
"time"
25
26
27
+ "dario.cat/mergo"
26
28
"github.com/go-logr/logr"
29
+ yaml "github.com/goccy/go-yaml"
27
30
"github.com/prometheus/common/model"
28
31
promconfig "github.com/prometheus/prometheus/config"
29
32
_ "github.com/prometheus/prometheus/discovery/install"
30
33
"github.com/spf13/pflag"
31
- "gopkg.in/yaml.v2"
32
34
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
35
"k8s.io/client-go/rest"
34
36
"k8s.io/client-go/tools/clientcmd"
@@ -75,6 +77,11 @@ type HTTPSServerConfig struct {
75
77
TLSKeyFilePath string `yaml:"tls_key_file_path,omitempty"`
76
78
}
77
79
80
+ type LabeLSelectorPaths struct {
81
+ MatchLabelsPath string
82
+ MatchExpressionsPath string
83
+ }
84
+
78
85
func LoadFromFile (file string , target * Config ) error {
79
86
return unmarshal (target , file )
80
87
}
@@ -148,14 +155,113 @@ func LoadFromCLI(target *Config, flagSet *pflag.FlagSet) error {
148
155
return nil
149
156
}
150
157
158
+ // Extract 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
176
+ }
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
196
+ }
197
+
198
+ if found {
199
+ if err := mergo .Merge (f , tmpls , mergo .WithOverride ); err != nil {
200
+ return err
201
+ }
202
+ }
203
+ }
204
+ return nil
205
+ }
206
+
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
+ },
213
+ }
214
+
215
+ if err := flexibleLabelSelector (yamlFile , collectorSelectorFieldPathsMap ); err != nil {
216
+ return err
217
+ }
218
+ return nil
219
+ }
220
+
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
+ },
239
+ }
240
+
241
+ if err := flexibleLabelSelector (yamlFile , prometheusCRFieldPathsMap ); err != nil {
242
+ return err
243
+ }
244
+ return nil
245
+ }
246
+
151
247
func unmarshal (cfg * Config , configFile string ) error {
152
248
yamlFile , err := os .ReadFile (configFile )
153
249
if err != nil {
154
250
return err
155
251
}
252
+
156
253
if err = yaml .Unmarshal (yamlFile , cfg ); err != nil {
157
254
return fmt .Errorf ("error unmarshaling YAML: %w" , err )
158
255
}
256
+
257
+ if err := flexibleCollectorSelector (yamlFile , cfg ); err != nil {
258
+ return err
259
+ }
260
+
261
+ if err := flexiblePrometheusCR (yamlFile , cfg ); err != nil {
262
+ return err
263
+ }
264
+
159
265
return nil
160
266
}
161
267
0 commit comments