Skip to content

Commit e9ab42b

Browse files
committed
unmarshal camelcase matchLabels and matchExpressions in target allocator config
1 parent 11c1fa9 commit e9ab42b

8 files changed

+468
-7
lines changed

.chloggen/3350-ta-matchlabels.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: target allocator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: "Support camelcase matchLabels and matchExpressions in target allocator config"
9+
10+
# One or more tracking issues related to the change
11+
issues: [3350]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

cmd/otel-allocator/config/config.go

+107-1
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ import (
2121
"fmt"
2222
"io/fs"
2323
"os"
24+
"strings"
2425
"time"
2526

27+
"dario.cat/mergo"
2628
"github.com/go-logr/logr"
29+
yaml "github.com/goccy/go-yaml"
2730
"github.com/prometheus/common/model"
2831
promconfig "github.com/prometheus/prometheus/config"
2932
_ "github.com/prometheus/prometheus/discovery/install"
3033
"github.com/spf13/pflag"
31-
"gopkg.in/yaml.v2"
3234
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3335
"k8s.io/client-go/rest"
3436
"k8s.io/client-go/tools/clientcmd"
@@ -75,6 +77,11 @@ type HTTPSServerConfig struct {
7577
TLSKeyFilePath string `yaml:"tls_key_file_path,omitempty"`
7678
}
7779

80+
type LabeLSelectorPaths struct {
81+
MatchLabelsPath string
82+
MatchExpressionsPath string
83+
}
84+
7885
func LoadFromFile(file string, target *Config) error {
7986
return unmarshal(target, file)
8087
}
@@ -148,14 +155,113 @@ func LoadFromCLI(target *Config, flagSet *pflag.FlagSet) error {
148155
return nil
149156
}
150157

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+
151247
func unmarshal(cfg *Config, configFile string) error {
152248
yamlFile, err := os.ReadFile(configFile)
153249
if err != nil {
154250
return err
155251
}
252+
156253
if err = yaml.Unmarshal(yamlFile, cfg); err != nil {
157254
return fmt.Errorf("error unmarshaling YAML: %w", err)
158255
}
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+
159265
return nil
160266
}
161267

0 commit comments

Comments
 (0)